Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.

Commit

Permalink
added helper for padding out arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
icaruswings committed Aug 7, 2012
1 parent 37bba62 commit 69fac0e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions UsefulBits/Foundation/NSArray+Blocks.h
Expand Up @@ -51,4 +51,7 @@
- (BOOL)any:(BOOL (^)(id))block;
- (BOOL)all:(BOOL (^)(id))block;

- (NSArray *)pad:(NSInteger)size;
- (NSArray *)pad:(NSInteger)size with:(id (^)())fill;

@end
22 changes: 22 additions & 0 deletions UsefulBits/Foundation/NSArray+Blocks.m
Expand Up @@ -215,4 +215,26 @@ - (BOOL)all:(BOOL (^)(id))block;
return NSNotFound == [self indexOfFirst:^ BOOL (id item) { return !block(item); }];
}

- (NSArray *)pad:(NSInteger)size;
{
return [self pad:size with:^ id (NSInteger index) {
return [NSNull null];
}];
}

- (NSArray *)pad:(NSInteger)size with:(id (^)())fill;
{
if ([self count] >= size) return self;

NSMutableArray *result = [NSMutableArray arrayWithCapacity:size];
[result addObjectsFromArray:self];

for (NSInteger idx = [result count]; idx < size; idx++)
{
[result addObject:fill()];
}

return [[result copy] autorelease];
}

@end
12 changes: 12 additions & 0 deletions UsefulTests/UsefulTests.xcodeproj/project.pbxproj
Expand Up @@ -17,6 +17,7 @@
0AE9E47114943593006D8985 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 0AE9E46F14943593006D8985 /* InfoPlist.strings */; };
0AE9E47314943593006D8985 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 0AE9E47214943593006D8985 /* main.m */; };
0AE9E48B14943AAA006D8985 /* UsefulBits.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0AE9E48914943AAA006D8985 /* UsefulBits.framework */; };
AA6F7CDC15D0BF0700D881A4 /* NSArrayPaddingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AA6F7CDB15D0BF0700D881A4 /* NSArrayPaddingTests.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -33,6 +34,7 @@
0AE9E47214943593006D8985 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
0AE9E47414943593006D8985 /* UsefulTests-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UsefulTests-Prefix.pch"; sourceTree = "<group>"; };
0AE9E48914943AAA006D8985 /* UsefulBits.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UsefulBits.framework; path = "../build/Release-iphoneos/UsefulBits.framework"; sourceTree = "<group>"; };
AA6F7CDB15D0BF0700D881A4 /* NSArrayPaddingTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NSArrayPaddingTests.m; path = Foundation/NSArrayPaddingTests.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -102,6 +104,7 @@
0AE9E46C14943593006D8985 /* UsefulTests */ = {
isa = PBXGroup;
children = (
AA6F7CD815D0BED800D881A4 /* Foundation */,
0A06DF8E149443FE00F14344 /* UIKit */,
0AE9E46D14943593006D8985 /* Supporting Files */,
);
Expand All @@ -119,6 +122,14 @@
name = "Supporting Files";
sourceTree = "<group>";
};
AA6F7CD815D0BED800D881A4 /* Foundation */ = {
isa = PBXGroup;
children = (
AA6F7CDB15D0BF0700D881A4 /* NSArrayPaddingTests.m */,
);
name = Foundation;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -183,6 +194,7 @@
files = (
0AE9E47314943593006D8985 /* main.m in Sources */,
0A06DF911494486700F14344 /* SkinTest.m in Sources */,
AA6F7CDC15D0BF0700D881A4 /* NSArrayPaddingTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
60 changes: 60 additions & 0 deletions UsefulTests/UsefulTests/Foundation/NSArrayPaddingTests.m
@@ -0,0 +1,60 @@
//
// NSArrayFillTests.m
// UsefulTests
//
// Created by Luke Cunningham on 7/08/12.
//
//

#import <GHUnitIOS/GHUnit.h>

#import <UsefulBits/NSArray+Blocks.h>

@interface NSArrayPaddingTests : GHTestCase
@end

@implementation NSArrayPaddingTests

- (void)testPadsEmptyArrayToSize;
{
NSArray *array = [[NSArray array] pad:10];

GHAssertTrue(10 == [array count], @"array has incorrect count");
GHAssertEqualObjects([array objectAtIndex:5], [NSNull null], @"array was not filled correctly with return value of block");
}

- (void)testPadsArrayWithLessValuesToSize;
{
NSArray *array = [[NSArray arrayWithObjects:@"first", @"second", nil] pad:10];

GHAssertEqualObjects([NSNumber numberWithUnsignedInteger:[array count]], [NSNumber numberWithInt:10], @"array has incorrect count");
}

- (void)testIgnoresArrayWithMoreValues;
{
NSArray *array = [[NSArray arrayWithObjects:@"first", @"second", @"third", nil] pad:2];

GHAssertEqualObjects([NSNumber numberWithUnsignedInteger:[array count]], [NSNumber numberWithInt:3], @"array has incorrect count");
}

- (void)testAddsReturnValueOfBlockToArrayWithMoreValues;
{
NSArray *array = [[NSArray array] pad:10 with:^id{
return @"some string";
}];

GHAssertEqualObjects([array objectAtIndex:1], @"some string", @"array has incorrect value");
GHAssertEqualObjects([array objectAtIndex:8], @"some string", @"array has incorrect value");
}

- (void)testAddsReturnValueOfBlockToArrayWithLessValues;
{
NSArray *array = [[NSArray arrayWithObjects:@"first", @"second", nil] pad:10 with:^ id {
return @"some string";
}];

GHAssertEqualObjects([array objectAtIndex:1], @"second", @"array has incorrect value");
GHAssertEqualObjects([array objectAtIndex:8], @"some string", @"array has incorrect value");
}

@end

0 comments on commit 69fac0e

Please sign in to comment.