Added check for blocks availability. Added testcase for sort by selector.

Tidied to match coding style
This commit is contained in:
Richard Frith-Macdonald 2019-09-26 12:44:25 +01:00
parent c8030ce0cb
commit 8363a7f3a1

View file

@ -7,37 +7,54 @@
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
START_SET("NSDictionary Sorting")
NSDictionary *d;
NSArray *keysOrderedByKeyedValue;
NSArray* values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:2.0],
[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:3.0],
[NSNumber numberWithFloat:4.0],
nil];
NSArray *values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat: 2.0],
[NSNumber numberWithFloat: 1.0],
[NSNumber numberWithFloat: 3.0],
[NSNumber numberWithFloat: 4.0],
nil];
NSArray* keys = [NSArray arrayWithObjects:
@"shouldSortToSecond",
@"shouldSortToFirst",
@"shouldSortToThird",
@"shouldSortToFourth",
nil];
NSArray *keys = [NSArray arrayWithObjects:
@"shouldSortToSecond",
@"shouldSortToFirst",
@"shouldSortToThird",
@"shouldSortToFourth",
nil];
NSDictionary *d = [NSDictionary dictionaryWithObjects:values forKeys:keys];
NSArray* keysOrderedByKeyedValue = [d keysSortedByValueUsingComparator:
^NSComparisonResult(id obj1, id obj2) {
return [(NSNumber*)obj1 compare:(NSNumber*)obj2];
}];
NSArray *expected = [NSArray arrayWithObjects:
@"shouldSortToFirst",
@"shouldSortToSecond",
@"shouldSortToThird",
@"shouldSortToFourth",
nil];
NSArray* expected = [NSArray arrayWithObjects:
@"shouldSortToFirst",
@"shouldSortToSecond",
@"shouldSortToThird",
@"shouldSortToFourth",
nil];
d = [NSDictionary dictionaryWithObjects: values forKeys: keys];
keysOrderedByKeyedValue = [d keysSortedByValueUsingSelector:
@selector(compare:)];
PASS([keysOrderedByKeyedValue isEqual:expected], "Can sort a dictionary's keys by its values");
PASS([keysOrderedByKeyedValue isEqual: expected],
"Can sort a dictionary's keys by its values using a selector");
# ifndef __has_feature
# define __has_feature(x) 0
# endif
# if __has_feature(blocks)
d = [NSDictionary dictionaryWithObjects: values forKeys: keys];
keysOrderedByKeyedValue = [d keysSortedByValueUsingComparator:
^NSComparisonResult(id obj1, id obj2) {
return [(NSNumber*)obj1 compare: (NSNumber*)obj2];
}];
PASS([keysOrderedByKeyedValue isEqual: expected],
"Can sort a dictionary's keys by its values using a comparator");
# else
SKIP("No Blocks support in the compiler.")
# endif
END_SET("NSDictionary Sorting")
[arp release]; arp = nil;
return 0;
}