Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented unit tests for pre-iOS5 -[NSIndexSet enumerateRangesUsing…

…Block:] implementation.
  • Loading branch information...
commit 578e54ad4a464442aab7e0ebec6ebf46fed359ef 1 parent 3e3d58c
@AlanQuatermain authored
View
5 AQAppStateMachine/AQRangeMethods.m
@@ -105,7 +105,7 @@ - (void)aq_enumerateRangesInRange:(NSRange)range options:(NSEnumerationOptions)o
range.length = (currentIndex - rangeStartIndex) + 1;
}
- if ( (opts & NSEnumerationConcurrent) == NSEnumerationConcurrent )
+ if ( group != NULL )
{
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ block(range, stop); });
}
@@ -117,6 +117,9 @@ - (void)aq_enumerateRangesInRange:(NSRange)range options:(NSEnumerationOptions)o
currentIndex = idx;
rangeStartIndex = idx;
}];
+
+ if ( group != NULL )
+ dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
}
- (NSUInteger) numberOfRanges
View
38 AQAppStateMachineTests/AQRangeMethodsTest.h
@@ -5,23 +5,41 @@
// Created by Jim Dovey on 11-06-23.
// Copyright 2011 Jim Dovey. All rights reserved.
//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions
+// are met:
+//
+// Redistributions of source code must retain the above copyright notice,
+// this list of conditions and the following disclaimer.
+//
+// Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+//
+// Neither the name of the project's author nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
// See Also: http://developer.apple.com/iphone/library/documentation/Xcode/Conceptual/iphone_development/135-Unit_Testing_Applications/unit_testing_applications.html
// Application unit tests contain unit test code that must be injected into an application to run correctly.
// Define USE_APPLICATION_UNIT_TEST to 0 if the unit test code is designed to be linked into an independent test executable.
-#define USE_APPLICATION_UNIT_TEST 1
-
#import <SenTestingKit/SenTestingKit.h>
#import <UIKit/UIKit.h>
-//#import "application_headers" as required
@interface AQRangeMethodsTest : SenTestCase
-
-#if USE_APPLICATION_UNIT_TEST
-- (void)testAppDelegate; // simple test on application
-#else
-- (void)testMath; // simple standalone test
-#endif
-
+@property (nonatomic, copy) NSMutableIndexSet * indexSet;
@end
View
90 AQAppStateMachineTests/AQRangeMethodsTest.m
@@ -5,28 +5,96 @@
// Created by Jim Dovey on 11-06-23.
// Copyright 2011 Jim Dovey. All rights reserved.
//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions
+// are met:
+//
+// Redistributions of source code must retain the above copyright notice,
+// this list of conditions and the following disclaimer.
+//
+// Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+//
+// Neither the name of the project's author nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
#import "AQRangeMethodsTest.h"
+#import "AQRangeMethods.h"
+#import "AQRange.h"
@implementation AQRangeMethodsTest
-#if USE_APPLICATION_UNIT_TEST // all code under test is in the iPhone Application
+@synthesize indexSet;
-- (void)testAppDelegate {
-
- id yourApplicationDelegate = [[UIApplication sharedApplication] delegate];
- STAssertNotNil(yourApplicationDelegate, @"UIApplication failed to find the AppDelegate");
-
+- (void) setUp
+{
+ NSMutableIndexSet * set = [NSMutableIndexSet indexSet];
+ [set addIndexesInRange: NSMakeRange(0, 5)];
+ [set addIndexesInRange: NSMakeRange(10, 5)];
+ [set addIndexesInRange: NSMakeRange(20, 10)];
+ [set addIndexesInRange: NSMakeRange(30, 5)]; // should merge with previous range
+ self.indexSet = set;
}
-#else // all code under test must be linked into the Unit Test bundle
+// these tests work by comparing against the official 10.7/5.0 version of the API we're emulating for 10.6/4.0
+#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0
-- (void)testMath {
-
- STAssertTrue((1+1)==2, @"Compiler isn't feeling well today :-(" );
-
+- (void) testEnumerateRanges
+{
+ NSMutableArray * appleResults = [NSMutableArray array];
+ NSMutableArray * aqResults = [NSMutableArray array];
+
+ [self.indexSet enumerateRangesUsingBlock: ^(NSRange range, BOOL *stop) {
+ AQRange * r = [[AQRange alloc] initWithRange: range];
+ [appleResults addObject: r];
+ }];
+
+ [self.indexSet aq_enumerateRangesUsingBlock: ^(NSRange range, BOOL *stop) {
+ AQRange * r = [[AQRange alloc] initWithRange: range];
+ [aqResults addObject: r];
+ }];
+
+ STAssertEqualObjects(appleResults, aqResults, @"Expected indexSet %@ to contain ranges %@, but got %@", self.indexSet, appleResults, aqResults);
+}
+
+- (void) testReverseEnumerateRanges
+{
+ NSMutableArray * appleResults = [NSMutableArray array];
+ NSMutableArray * aqResults = [NSMutableArray array];
+
+ [self.indexSet enumerateRangesWithOptions: NSEnumerationReverse usingBlock: ^(NSRange range, BOOL *stop) {
+ AQRange * r = [[AQRange alloc] initWithRange: range];
+ [appleResults addObject: r];
+ }];
+
+ [self.indexSet aq_enumerateRangesWithOptions: NSEnumerationReverse usingBlock: ^(NSRange range, BOOL *stop) {
+ AQRange * r = [[AQRange alloc] initWithRange: range];
+ [aqResults addObject: r];
+ }];
+
+ STAssertEqualObjects(appleResults, aqResults, @"Expected indexSet %@ to contain reversed ranges %@, but got %@", self.indexSet, appleResults, aqResults);
}
#endif
+- (void) testNumberOfRanges
+{
+ STAssertTrue([self.indexSet numberOfRanges] == 3, @"Expected %@ to contain 3 ranges, instead has %lu", self.indexSet, (unsigned long)[self.indexSet numberOfRanges]);
+}
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.