Split macros into helpers in Objective C Tests

This commit is contained in:
Prianka Liz Kariat 2023-04-11 17:37:54 +05:30
parent d06cf68c70
commit 089361cd89

View File

@ -23,9 +23,8 @@ static NSDictionary *const kCatsAndDogsImage = @{@"name" : @"cats_and_dogs", @"t
static NSDictionary *const kCatsAndDogsRotatedImage = static NSDictionary *const kCatsAndDogsRotatedImage =
@{@"name" : @"cats_and_dogs_rotated", @"type" : @"jpg"}; @{@"name" : @"cats_and_dogs_rotated", @"type" : @"jpg"};
static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks"; static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
static const float pixelDifferenceTolerance = 5.0f;
#define PixelDifferenceTolerance 5.0f static const float scoreDifferenceTolerance = 1e-2f;
#define ScoreDifferenceTolerance 1e-2f
#define AssertEqualErrors(error, expectedError) \ #define AssertEqualErrors(error, expectedError) \
XCTAssertNotNil(error); \ XCTAssertNotNil(error); \
@ -35,59 +34,29 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
[error.localizedDescription rangeOfString:expectedError.localizedDescription].location, \ [error.localizedDescription rangeOfString:expectedError.localizedDescription].location, \
NSNotFound) NSNotFound)
#define AssertEqualCategoryArrays(categories, expectedCategories, detectionIndex) \ #define AssertEqualCategories(category, expectedCategory, detectionIndex, categoryIndex) \
XCTAssertEqual(categories.count, expectedCategories.count); \ XCTAssertEqual(category.index, expectedCategory.index, \
for (int j = 0; j < categories.count; j++) { \ @"detection Index = %d category array index j = %d", detectionIndex, \
XCTAssertEqual(categories[j].index, expectedCategories[j].index, \ categoryIndex); \
@"detection Index = %d category array index j = %d", detectionIndex, j); \ XCTAssertEqualWithAccuracy(category.score, expectedCategory.score, scoreDifferenceTolerance, \
XCTAssertEqualWithAccuracy( \ @"detection Index = %d, category array index j = %d", detectionIndex, \
categories[j].score, expectedCategories[j].score, ScoreDifferenceTolerance, \ categoryIndex); \
@"detection Index = %d, category array index j = %d", detectionIndex, j); \ XCTAssertEqualObjects(category.categoryName, expectedCategory.categoryName, \
XCTAssertEqualObjects(categories[j].categoryName, expectedCategories[j].categoryName, \ @"detection Index = %d, category array index j = %d", detectionIndex, \
@"detection Index = %d, category array index j = %d", detectionIndex, \ categoryIndex); \
j); \ XCTAssertEqualObjects(category.displayName, expectedCategory.displayName, \
XCTAssertEqualObjects(categories[j].displayName, expectedCategories[j].displayName, \ @"detection Index = %d, category array index j = %d", detectionIndex, \
@"detection Index = %d, category array index j = %d", detectionIndex, \ categoryIndex);
j); \
\
\
}
#define AssertApproximatelyEqualBoundingBoxes(boundingBox, expectedBoundingBox, idx) \ #define AssertApproximatelyEqualBoundingBoxes(boundingBox, expectedBoundingBox, idx) \
XCTAssertEqualWithAccuracy(boundingBox.origin.x, expectedBoundingBox.origin.x, \ XCTAssertEqualWithAccuracy(boundingBox.origin.x, expectedBoundingBox.origin.x, \
PixelDifferenceTolerance, @"index i = %d", idx); \ pixelDifferenceTolerance, @"index i = %d", idx); \
XCTAssertEqualWithAccuracy(boundingBox.origin.y, expectedBoundingBox.origin.y, \ XCTAssertEqualWithAccuracy(boundingBox.origin.y, expectedBoundingBox.origin.y, \
PixelDifferenceTolerance, @"index i = %d", idx); \ pixelDifferenceTolerance, @"index i = %d", idx); \
XCTAssertEqualWithAccuracy(boundingBox.size.width, expectedBoundingBox.size.width, \ XCTAssertEqualWithAccuracy(boundingBox.size.width, expectedBoundingBox.size.width, \
PixelDifferenceTolerance, @"index i = %d", idx); \ pixelDifferenceTolerance, @"index i = %d", idx); \
XCTAssertEqualWithAccuracy(boundingBox.size.height, expectedBoundingBox.size.height, \ XCTAssertEqualWithAccuracy(boundingBox.size.height, expectedBoundingBox.size.height, \
PixelDifferenceTolerance, @"index i = %d", idx); pixelDifferenceTolerance, @"index i = %d", idx);
#define AssertEqualDetections(detection, expectedDetection, idx) \
XCTAssertNotNil(detection); \
AssertEqualCategoryArrays(detection.categories, expectedDetection.categories, idx); \
AssertApproximatelyEqualBoundingBoxes(detection.boundingBox, expectedDetection.boundingBox, idx);
#define AssertEqualDetectionArrays(detections, expectedDetections) \
XCTAssertEqual(detections.count, expectedDetections.count); \
for (int i = 0; i < detections.count; i++) { \
AssertEqualDetections(detections[i], expectedDetections[i], i); \
}
#define AssertEqualObjectDetectionResults(objectDetectionResult, expectedObjectDetectionResult, \
expectedDetectionCount) \
XCTAssertNotNil(objectDetectionResult); \
NSArray<MPPDetection *> *detectionsSubsetToCompare; \
XCTAssertEqual(objectDetectionResult.detections.count, expectedDetectionCount); \
if (objectDetectionResult.detections.count > expectedObjectDetectionResult.detections.count) { \
detectionsSubsetToCompare = [objectDetectionResult.detections \
subarrayWithRange:NSMakeRange(0, expectedObjectDetectionResult.detections.count)]; \
} else { \
detectionsSubsetToCompare = objectDetectionResult.detections; \
} \
\
AssertEqualDetectionArrays(detectionsSubsetToCompare, expectedObjectDetectionResult.detections); \
XCTAssertEqual(objectDetectionResult.timestampMs, expectedObjectDetectionResult.timestampMs);
@interface MPPObjectDetectorTests : XCTestCase @interface MPPObjectDetectorTests : XCTestCase
@end @end
@ -124,6 +93,39 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
return [[MPPObjectDetectionResult alloc] initWithDetections:detections timestampMs:timestampMs]; return [[MPPObjectDetectionResult alloc] initWithDetections:detections timestampMs:timestampMs];
} }
- (void)assertDetections:(NSArray<MPPDetection *> *)detections
isEqualToExpectedDetections:(NSArray<MPPDetection *> *)expectedDetections {
for (int i = 0; i < detections.count; i++) {
MPPDetection *detection = detections[i];
XCTAssertNotNil(detection);
for (int j = 0; j < detection.categories.count; j++) {
AssertEqualCategories(detection.categories[j], expectedDetections[i].categories[j], i, j);
}
AssertApproximatelyEqualBoundingBoxes(detection.boundingBox, expectedDetections[i].boundingBox,
i);
}
}
- (void)assertObjectDetectionResult:(MPPObjectDetectionResult *)objectDetectionResult
isEqualToExpectedResult:(MPPObjectDetectionResult *)expectedObjectDetectionResult
expectedDetectionsCount:(NSInteger)expectedDetectionsCount {
XCTAssertNotNil(objectDetectionResult);
NSArray<MPPDetection *> *detectionsSubsetToCompare;
XCTAssertEqual(objectDetectionResult.detections.count, expectedDetectionsCount);
if (objectDetectionResult.detections.count > expectedObjectDetectionResult.detections.count) {
detectionsSubsetToCompare = [objectDetectionResult.detections
subarrayWithRange:NSMakeRange(0, expectedObjectDetectionResult.detections.count)];
} else {
detectionsSubsetToCompare = objectDetectionResult.detections;
}
[self assertDetections:detectionsSubsetToCompare
isEqualToExpectedDetections:expectedObjectDetectionResult.detections];
XCTAssertEqual(objectDetectionResult.timestampMs, expectedObjectDetectionResult.timestampMs);
}
#pragma mark File #pragma mark File
- (NSString *)filePathWithName:(NSString *)fileName extension:(NSString *)extension { - (NSString *)filePathWithName:(NSString *)fileName extension:(NSString *)extension {
@ -189,9 +191,11 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
equalsObjectDetectionResult:(MPPObjectDetectionResult *)expectedObjectDetectionResult { equalsObjectDetectionResult:(MPPObjectDetectionResult *)expectedObjectDetectionResult {
MPPObjectDetectionResult *objectDetectionResult = [objectDetector detectInImage:mppImage MPPObjectDetectionResult *objectDetectionResult = [objectDetector detectInImage:mppImage
error:nil]; error:nil];
AssertEqualObjectDetectionResults(
objectDetectionResult, expectedObjectDetectionResult, [self assertObjectDetectionResult:objectDetectionResult
maxResults > 0 ? maxResults : objectDetectionResult.detections.count); isEqualToExpectedResult:expectedObjectDetectionResult
expectedDetectionsCount:maxResults > 0 ? maxResults
: objectDetectionResult.detections.count];
} }
- (void)assertResultsOfDetectInImageWithFileInfo:(NSDictionary *)fileInfo - (void)assertResultsOfDetectInImageWithFileInfo:(NSDictionary *)fileInfo
@ -604,10 +608,12 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
MPPObjectDetectionResult *objectDetectionResult = [objectDetector detectInVideoFrame:image MPPObjectDetectionResult *objectDetectionResult = [objectDetector detectInVideoFrame:image
timestampMs:i timestampMs:i
error:nil]; error:nil];
AssertEqualObjectDetectionResults(
objectDetectionResult, [self
[MPPObjectDetectorTests expectedDetectionResultForCatsAndDogsImageWithTimestampMs:i], assertObjectDetectionResult:objectDetectionResult
maxResults); isEqualToExpectedResult:[MPPObjectDetectorTests
expectedDetectionResultForCatsAndDogsImageWithTimestampMs:i]
expectedDetectionsCount:maxResults];
} }
} }
@ -624,10 +630,11 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
expectation.expectedFulfillmentCount = 1; expectation.expectedFulfillmentCount = 1;
options.completion = ^(MPPObjectDetectionResult *result, NSInteger timestampMs, NSError *error) { options.completion = ^(MPPObjectDetectionResult *result, NSInteger timestampMs, NSError *error) {
AssertEqualObjectDetectionResults( [self assertObjectDetectionResult:result
result, isEqualToExpectedResult:
[MPPObjectDetectorTests expectedDetectionResultForCatsAndDogsImageWithTimestampMs:1], [MPPObjectDetectorTests
maxResults); expectedDetectionResultForCatsAndDogsImageWithTimestampMs:timestampMs]
expectedDetectionsCount:maxResults];
[expectation fulfill]; [expectation fulfill];
}; };
@ -678,11 +685,11 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
expectation.inverted = YES; expectation.inverted = YES;
options.completion = ^(MPPObjectDetectionResult *result, NSInteger timestampMs, NSError *error) { options.completion = ^(MPPObjectDetectionResult *result, NSInteger timestampMs, NSError *error) {
AssertEqualObjectDetectionResults( [self assertObjectDetectionResult:result
result, isEqualToExpectedResult:
[MPPObjectDetectorTests [MPPObjectDetectorTests
expectedDetectionResultForCatsAndDogsImageWithTimestampMs:timestampMs], expectedDetectionResultForCatsAndDogsImageWithTimestampMs:timestampMs]
maxResults); expectedDetectionsCount:maxResults];
[expectation fulfill]; [expectation fulfill];
}; };