2013-11-04 3 views
0

나는 두 가지 기능을 정의하고있어 영향을하나 개의 단위 테스트는 다른

MKMapRect MKMapRectForCoordinateRegion(MKCoordinateRegion region) 
{ 
    CLLocationCoordinate2D center = region.center; 
    MKCoordinateSpan span = region.span; 
    CLLocationCoordinate2D topLeft = 
     CLLocationCoordinate2DMake(center.latitude - span.latitudeDelta/2.0, 
            center.longitude - span.longitudeDelta/2.0); 
    CLLocationCoordinate2D bottomRight = 
    CLLocationCoordinate2DMake(center.latitude - span.latitudeDelta/2.0, 
           center.longitude - span.longitudeDelta/2.0); 
    MKMapPoint mapPointTopLeft = MKMapPointForCoordinate(topLeft); 
    MKMapPoint mapPointBottomRight = MKMapPointForCoordinate(bottomRight); 
    double width = mapPointTopLeft.x - mapPointBottomRight.x; 
    double height = mapPointTopLeft.y - mapPointBottomRight.y; 

    return MKMapRectMake(mapPointTopLeft.x, mapPointTopLeft.y, width, height); 
} 

MKCoordinateRegion MKCoordinateRegionForSouthWestAndNorthEast(CLLocationCoordinate2D sw, CLLocationCoordinate2D ne) 
{ 
    MKCoordinateSpan span = 
     MKCoordinateSpanMake(ne.latitude - sw.latitude, ne.longitude - sw.longitude); 
    CLLocationCoordinate2D topLeft = CLLocationCoordinate2DMake(ne.latitude, sw.longitude); 
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(topLeft.latitude + span.latitudeDelta * 0.5, topLeft.longitude + span.longitudeDelta * 0.5); 
    return MKCoordinateRegionMake(center, span); 
} 

두 파일의 거의 전부입니다. XCTests 내 단위 테스트에서

:

- (void)testMKMapRectForCoordinateRegionAfterReverseConversion 
{ 
    CLLocationCoordinate2D fremont = CLLocationCoordinate2DMake(37.54827, -121.98857); 
    MKCoordinateRegion region = MKCoordinateRegionMake(fremont, MKCoordinateSpanMake(0.05, 0.05)); 
    MKMapRect mapRect = MKMapRectForCoordinateRegion(region); 
    MKCoordinateRegion derivedRegion = MKCoordinateRegionForMapRect(mapRect); 

    double accuracy = 0.0001; 

    XCTAssertEqualWithAccuracy(region.center.latitude, derivedRegion.center.latitude, 
           accuracy, @"Latitude is equal"); 
    XCTAssertEqualWithAccuracy(region.center.longitude, derivedRegion.center.longitude, 
           accuracy, @"Latitude is equal"); 
    XCTAssertEqualWithAccuracy(region.span.latitudeDelta, derivedRegion.span.latitudeDelta, 
           accuracy, @"Latitude delta is equal"); 
    XCTAssertEqualWithAccuracy(region.span.longitudeDelta, derivedRegion.span.longitudeDelta, 
           accuracy, @"Latitude delta is equal"); 
} 

- (void)testMKCoordinateRegionForSouthWestAndNorthEast 
{ 
    CLLocationCoordinate2D taipeiWanHua = CLLocationCoordinate2DMake(25.02946, 121.49652); 
    CLLocationCoordinate2D taipeiSongShan = CLLocationCoordinate2DMake(25.06640, 121.56166); 

    /* When the following line is called, the first test fails */ 
    MKCoordinateRegionForSouthWestAndNorthEast(taipeiWanHua, taipeiSongShan); 

    /* I have the rest of the lines in this test commented out */ 
} 

지금,이 테스트 클래스 빈 설치 및 분해가 있습니다. 시험을 볼 때. 두 번째 테스트의 세 번째 줄이 테스트에 있으면 첫 번째 테스트가 실패합니다. 자세히 살펴보면 그것이 실패했을 때, 그 값이 여전히 주어진 정확도 비슷하지만 떨어지는 외부 있다는 있다고 보여줍니다 enter image description here

내가 두 번째 테스트 아웃의 세 번째 줄은, 첫 번째 테스트가 통과 것이라고 논평합니다. 왜 그런가요?

+0

하는 BTW, 같은 일을 내가 첫 번째 테스트를 실행할 경우에도 발생합니다. =/ – huggie

답변

0

문제는 단위 테스트와 관련이 없습니다. 두 번째 테스트를 완전히 주석 처리하더라도 문제가 있습니다. 이는 MKMapRectForCoordinateRegion 구현과 함께합니다. 나는 올바른 구현이 생각 :

MKMapRect MKMapRectForCoordinateRegion(MKCoordinateRegion region) 
{ 
    CLLocationCoordinate2D center = region.center; 
    MKCoordinateSpan span = region.span; 
    CLLocationCoordinate2D topLeft = 
    CLLocationCoordinate2DMake(center.latitude + span.latitudeDelta/2.0, 
           center.longitude - span.longitudeDelta/2.0); 
    CLLocationCoordinate2D bottomRight = 
    CLLocationCoordinate2DMake(center.latitude - span.latitudeDelta/2.0, 
           center.longitude + span.longitudeDelta/2.0); 
    MKMapPoint mapPointTopLeft = MKMapPointForCoordinate(topLeft); 
    MKMapPoint mapPointBottomRight = MKMapPointForCoordinate(bottomRight); 
    double width = mapPointBottomRight.x - mapPointTopLeft.x; 
    double height = mapPointBottomRight.y - mapPointTopLeft.y; 

    MKMapRect ret = MKMapRectMake(mapPointTopLeft.x, mapPointTopLeft.y, width, height); 
    return ret; 
} 

확인이 아웃 : Mercator Projection

+0

두 번째 테스트의 세 번째 줄을 주석으로 처리하면 실패하지 않습니다. 모르겠지만 구현이 잘못되었다고 가정 할 때, 두 번째 테스트에 대한 주석을 달아 첫 번째 테스트의 결과에 왜 영향을 미치는지 설명하지 못합니다. – huggie

+0

첫 번째 테스트에 중단 점을 넣고 두 번째 테스트에서 해당 줄을 주석으로 처리하고 첫 번째 테스트에서 어떤 일이 발생했는지 수동으로 체크 아웃합니다. 실행됩니까? derivedRegion의 값은 괜찮습니까? 이 경우에는 모든 것이 정상임을 확인하기 만하면됩니다. – imihaly

+0

'topLeft'와'bottomRight'은 구현시 동일합니다. 이는 복사 - 붙여 넣기 오류입니다 ... – imihaly