Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)test
- {
- NSArray *arr = @[@{@"name": @"a", @"website": @"http://a.com", @"isTrue": @(YES)},
- @{@"name": @"b", @"website": @"http://b.com", @"isTrue": @(NO)},
- @{@"name": @"c", @"website": @"http://c.com", @"isTrue": @(NO)}];
- // Both the cases should be same as we don't care what value 'isTrue' has
- NSDictionary *testCaseFound1 = @{@"name": @"c", @"website": @"http://c.com", @"isTrue": @(YES)};
- NSDictionary *testCaseFound2 = @{@"name": @"c", @"website": @"http://c.com", @"isTrue": @(NO)};
- NSDictionary *testCaseNotFound1 = @{@"name": @"d", @"website": @"http://d.com", @"isTrue": @(YES)};
- NSDictionary *testCaseNotFound2 = @{@"name": @"d", @"website": @"http://d.com", @"isTrue": @(NO)};
- /* Comment or uncomment this line to toggle between two logics */
- //#define USE_OLD_LOGIC
- #ifdef USE_OLD_LOGIC
- /* Old logic: must not work */
- NSAssert([arr indexOfObject:testCaseFound1] == 2, @"Found logic 1 broken!");
- NSAssert([arr indexOfObject:testCaseFound2] == 2, @"Found logic 2 broken!");
- NSAssert([arr indexOfObject:testCaseNotFound1] == NSNotFound, @"Not found logic 1 broken!");
- NSAssert([arr indexOfObject:testCaseNotFound2] == NSNotFound, @"Not found logic 2 broken!");
- #else
- /* New logic: must work */
- __block NSDictionary *testCase = nil; // will be set later
- BOOL (^skipIsTruePredicate)(NSDictionary *, NSUInteger idx, BOOL *stop) = ^BOOL(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
- NSAssert(testCase, @"Invalid testCase");
- NSUInteger checks = 0;
- for (NSString *key in [obj allKeys]) {
- if ([key isEqualToString:@"isTrue"]) {
- checks++;
- continue; // skip test
- }
- if ([obj objectForKey:key] == [testCase objectForKey:key]) {
- checks++;
- }
- }
- return (checks == [[obj allKeys] count]);
- };
- testCase = testCaseFound1;
- NSAssert([arr indexOfObjectPassingTest:skipIsTruePredicate] == 2, @"Found logic 1 broken!");
- testCase = testCaseFound2;
- NSAssert([arr indexOfObjectPassingTest:skipIsTruePredicate] == 2, @"Found logic 2 broken!");
- testCase = testCaseNotFound1;
- NSAssert([arr indexOfObjectPassingTest:skipIsTruePredicate] == NSNotFound, @"Not found logic 1 broken!");
- testCase = testCaseNotFound2;
- NSAssert([arr indexOfObjectPassingTest:skipIsTruePredicate] == NSNotFound, @"Not found logic 2 broken!");
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement