Skip to content

Commit dcdcf9b

Browse files
committed
refactor assertMono
1 parent 9a878ba commit dcdcf9b

File tree

1 file changed

+14
-39
lines changed

1 file changed

+14
-39
lines changed

Example/Tests/Tests.m

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ @interface Tests : XCTestCase
2929

3030
{
3131
@private
32+
SDImageAVIFCoder* coder;
3233
NSMutableArray<NSDictionary*>* blackList;
3334
NSMutableArray<NSDictionary*>* whiteList;
3435
NSMutableArray<NSDictionary*>* grayList;
@@ -44,7 +45,7 @@ @implementation Tests
4445
- (void)setUp
4546
{
4647
[super setUp];
47-
48+
self->coder = [[SDImageAVIFCoder alloc] init];
4849
self->blackList = [[NSMutableArray alloc] init];
4950
self->whiteList = [[NSMutableArray alloc] init];
5051
self->grayList = [[NSMutableArray alloc] init];
@@ -217,26 +218,11 @@ -(void)assertMono8: (NSString*)filename img:(CGImageRef)img expectedNumComponent
217218
size_t const stride = CGImageGetBytesPerRow(img);
218219
size_t const bitsPerPixel = CGImageGetBitsPerPixel(img);
219220
size_t const bytesPerPixel = bitsPerPixel/8;
221+
XCTAssertEqual(1, bytesPerPixel);
220222
for(size_t y = 0; y < height; ++y) {
221223
for(size_t x = 0; x < width; ++x) {
222-
UInt16* pix = (UInt16*)(buf + (stride * y) + (bytesPerPixel * x));
223-
UInt16 color = 0;
224-
for(size_t c = 0; c < expectedNumComponents; ++c) {
225-
bool ok = false;
226-
if(c == 0) {
227-
color = pix[c];
228-
}else if(pix[c] != color) {
229-
NSMutableString* colorStr = [[NSMutableString alloc] initWithString: @"["];
230-
for (size_t d = 0; d < expectedNumComponents; ++d) {
231-
[colorStr appendFormat: @"%d, ", pix[d]];
232-
}
233-
[colorStr appendString: @"]"];
234-
XCTAssertTrue(ok = (pix[c] == color), "(x: %ld, y: %ld, c:%ld): color=%@ (%@)", x, y, c, colorStr, filename);
235-
}
236-
if(!ok) {
237-
goto end;
238-
}
239-
}
224+
UInt8* pix = (buf + (stride * y) + (bytesPerPixel * x));
225+
XCTAssertEqual(buf[0], *pix);
240226
}
241227
}
242228
end:
@@ -253,26 +239,11 @@ -(void)assertMono16: (NSString*)filename img:(CGImageRef)img expectedNumComponen
253239
size_t const stride = CGImageGetBytesPerRow(img);
254240
size_t const bitsPerPixel = CGImageGetBitsPerPixel(img);
255241
size_t const bytesPerPixel = bitsPerPixel/8;
242+
XCTAssertEqual(2, bytesPerPixel);
256243
for(size_t y = 0; y < height; ++y) {
257244
for(size_t x = 0; x < width; ++x) {
258245
UInt16* pix = (UInt16*)(buf + (stride * y) + (bytesPerPixel * x));
259-
UInt16 color = 0;
260-
for(size_t c = 0; c < expectedNumComponents; ++c) {
261-
bool ok = false;
262-
if(c == 0) {
263-
color = pix[c];
264-
}else if(pix[c] != color) {
265-
NSMutableString* colorStr = [[NSMutableString alloc] initWithString: @"["];
266-
for (size_t d = 0; d < expectedNumComponents; ++d) {
267-
[colorStr appendFormat: @"%d, ", pix[d]];
268-
}
269-
[colorStr appendString: @"]"];
270-
XCTAssertTrue(ok = (pix[c] == color), "(x: %ld, y: %ld, c:%ld): color=%@ (%@)", x, y, c, colorStr, filename);
271-
}
272-
if(!ok) {
273-
goto end;
274-
}
275-
}
246+
XCTAssertEqual(((UInt16*)buf)[0], *pix);
276247
}
277248
}
278249
end:
@@ -282,7 +253,6 @@ -(void)assertMono16: (NSString*)filename img:(CGImageRef)img expectedNumComponen
282253
- (void)assertImages: (NSMutableArray*) list colorName:(NSString*)colorName expectedColor8:(UInt8*)expectedColor8 expectedNumComponents8:(size_t)expectedNumComponents8
283254
expectedColor16:(UInt16*)expectedColor16 expectedNumComponents16:(size_t)expectedNumComponents16
284255
{
285-
SDImageAVIFCoder* const coder = [SDImageAVIFCoder sharedCoder];
286256
NSLog(@"Testing %lu [%@] images", list.count, colorName);
287257
[list enumerateObjectsUsingBlock:^(NSDictionary* item, NSUInteger idx, BOOL *stop) {
288258
NSString* convertedFilename = [item objectForKey:@"converted"];
@@ -300,7 +270,13 @@ - (void)assertImages: (NSMutableArray*) list colorName:(NSString*)colorName expe
300270
NSString* imgBundle = [[NSBundle mainBundle] pathForResource: convertedFilename ofType: @""];
301271
NSData* imgData = [[NSData alloc] initWithContentsOfFile: imgBundle];
302272

303-
UIImage* img = [coder decodedImageWithData: imgData options:nil];
273+
UIImage* img = [self->coder decodedImageWithData: imgData options:nil];
274+
[self->coder decodedImageWithData: imgData options:nil];
275+
[self->coder decodedImageWithData: imgData options:nil];
276+
[self->coder decodedImageWithData: imgData options:nil];
277+
[self->coder decodedImageWithData: imgData options:nil];
278+
[self->coder decodedImageWithData: imgData options:nil];
279+
304280
bool hdr = CGImageGetBitsPerComponent(img.CGImage) != 8;
305281
// FIXME(ledyba-z): add images with alpha.
306282
if([color isEqual: @"mono"]){
@@ -314,7 +290,6 @@ - (void)assertImages: (NSMutableArray*) list colorName:(NSString*)colorName expe
314290
} else {
315291
[self assertColor16: convertedFilename img:img.CGImage expectedColor:expectedColor16 expectedNumComponents: expectedNumComponents16];
316292
}
317-
318293
}];
319294
}
320295

0 commit comments

Comments
 (0)