libs-base/Tests/base/NSDictionary/sort.m
Richard Frith-Macdonald 8363a7f3a1 Added check for blocks availability. Added testcase for sort by selector.
Tidied to match coding style
2019-09-26 12:44:25 +01:00

60 lines
1.6 KiB
Objective-C

#import "Testing.h"
#import "ObjectTesting.h"
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSString.h>
int main()
{
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 *keys = [NSArray arrayWithObjects:
@"shouldSortToSecond",
@"shouldSortToFirst",
@"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 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")
return 0;
}