Skip to content

Commit

Permalink
Added a new PXColorArray "class" plus unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrajca committed Feb 19, 2012
1 parent a0de450 commit 48350ab
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 14 deletions.
31 changes: 31 additions & 0 deletions Pixel Art Core/Palette/PXColorArray.h
@@ -0,0 +1,31 @@
//
// PXColorArray.h
// Pixen
//
// Copyright 2012 Pixen Project. All rights reserved.
//

#import "PXColor.h"

typedef struct PXColorArray *PXColorArrayRef;

typedef void (^PXColorArrayEnumerationBlock)(PXColor);

PXColorArrayRef PXColorArrayCreate(void);

void PXColorArrayRetain(PXColorArrayRef self);
void PXColorArrayRelease(PXColorArrayRef self);

NSUInteger PXColorArrayCount(PXColorArrayRef self);

NSUInteger PXColorArrayIndexOfColor(PXColorArrayRef self, PXColor color);
PXColor PXColorArrayColorAtIndex(PXColorArrayRef self, NSUInteger index);

void PXColorArrayEnumerateWithBlock(PXColorArrayRef self, PXColorArrayEnumerationBlock block);

void PXColorArrayAppendColor(PXColorArrayRef self, PXColor color);
void PXColorArrayInsertColorAtIndex(PXColorArrayRef self, NSUInteger index, PXColor color);

void PXColorArrayRemoveColorAtIndex(PXColorArrayRef self, NSUInteger index);

void PXColorArrayMoveColor(PXColorArrayRef self, NSUInteger sourceIndex, NSUInteger targetIndex);
151 changes: 151 additions & 0 deletions Pixel Art Core/Palette/PXColorArray.m
@@ -0,0 +1,151 @@
//
// PXColorArray.m
// Pixen
//
// Copyright 2012 Pixen Project. All rights reserved.
//

#import "PXColorArray.h"

#define COLOR_BATCH_SIZE 64

struct PXColorArray {
PXColor *_colors;
NSUInteger _count;
NSUInteger _allocatedCount;

NSUInteger _retainCount;
};

static void PXColorArrayReallocByOne(PXColorArrayRef self);

PXColorArrayRef PXColorArrayCreate(void)
{
PXColorArrayRef array = malloc(sizeof(struct PXColorArray));
array->_colors = NULL;
array->_count = array->_allocatedCount = 0;
array->_retainCount = 1;

return array;
}

static void PXColorArrayReallocByOne(PXColorArrayRef self)
{
if (!self->_colors) {
self->_colors = malloc(sizeof(PXColor) * COLOR_BATCH_SIZE);
self->_allocatedCount = COLOR_BATCH_SIZE;
}

if (self->_count+1 > self->_allocatedCount) {
NSUInteger nac = self->_allocatedCount + COLOR_BATCH_SIZE;

self->_colors = realloc(self->_colors, sizeof(PXColor) * nac);
self->_allocatedCount = nac;
}
}

void PXColorArrayRetain(PXColorArrayRef self)
{
if (self == NULL)
return;

self->_retainCount++;
}

void PXColorArrayRelease(PXColorArrayRef self)
{
if (self == NULL)
return;

self->_retainCount--;

if (!self->_retainCount) {
if (self->_colors)
free(self->_colors);

free(self);
}
}

NSUInteger PXColorArrayCount(PXColorArrayRef self)
{
return self->_count;
}

NSUInteger PXColorArrayIndexOfColor(PXColorArrayRef self, PXColor color)
{
for (NSUInteger i = 0; i < self->_count; i++) {
if (PXColorEqualsColor(self->_colors[i], color))
return i;
}

return NSNotFound;
}

PXColor PXColorArrayColorAtIndex(PXColorArrayRef self, NSUInteger index)
{
NSCAssert(index < self->_count, @"Out-of-bounds index");

return self->_colors[index];
}

void PXColorArrayEnumerateWithBlock(PXColorArrayRef self, PXColorArrayEnumerationBlock block)
{
for (NSUInteger i = 0; i < self->_count; i++) {
block(self->_colors[i]);
}
}

void PXColorArrayAppendColor(PXColorArrayRef self, PXColor color)
{
PXColorArrayReallocByOne(self);

self->_colors[self->_count] = color;
self->_count++;
}

void PXColorArrayInsertColorAtIndex(PXColorArrayRef self, NSUInteger index, PXColor color)
{
NSCAssert(index <= self->_count, @"Out-of-bounds index");

PXColorArrayReallocByOne(self);

if (self->_count) {
for (NSUInteger n = self->_count; n > index; n--) {
self->_colors[n] = self->_colors[n-1];
}
}

self->_colors[index] = color;
self->_count++;
}

void PXColorArrayRemoveColorAtIndex(PXColorArrayRef self, NSUInteger index)
{
NSCAssert(index < self->_count, @"Out-of-bounds index");

for (NSUInteger n = index; n < self->_count-1; n++) {
self->_colors[n] = self->_colors[n+1];
}

self->_count--;

bzero(self->_colors+self->_count, sizeof(PXColor));
}

void PXColorArrayMoveColor(PXColorArrayRef self, NSUInteger sourceIndex, NSUInteger targetIndex)
{
NSCAssert(sourceIndex < self->_count, @"Out-of-bounds source index");

if (targetIndex != sourceIndex) {
PXColor sourceColor = PXColorArrayColorAtIndex(self, sourceIndex);
PXColorArrayRemoveColorAtIndex(self, sourceIndex);

if (targetIndex >= self->_count) {
PXColorArrayAppendColor(self, sourceColor);
}
else {
PXColorArrayInsertColorAtIndex(self, targetIndex, sourceColor);
}
}
}
12 changes: 12 additions & 0 deletions Pixen.xcodeproj/project.pbxproj
Expand Up @@ -78,6 +78,10 @@
C9B1FA5E13D732FC00BB7CF7 /* PXManagePresetsWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = C9B1FA5C13D732FC00BB7CF7 /* PXManagePresetsWindow.xib */; }; C9B1FA5E13D732FC00BB7CF7 /* PXManagePresetsWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = C9B1FA5C13D732FC00BB7CF7 /* PXManagePresetsWindow.xib */; };
C9D58D8B14F0316B00A62C10 /* PXColor.h in Headers */ = {isa = PBXBuildFile; fileRef = C9D58D8914F0316B00A62C10 /* PXColor.h */; }; C9D58D8B14F0316B00A62C10 /* PXColor.h in Headers */ = {isa = PBXBuildFile; fileRef = C9D58D8914F0316B00A62C10 /* PXColor.h */; };
C9D58D8C14F0316B00A62C10 /* PXColor.m in Sources */ = {isa = PBXBuildFile; fileRef = C9D58D8A14F0316B00A62C10 /* PXColor.m */; }; C9D58D8C14F0316B00A62C10 /* PXColor.m in Sources */ = {isa = PBXBuildFile; fileRef = C9D58D8A14F0316B00A62C10 /* PXColor.m */; };
C9D58D8F14F031EB00A62C10 /* PXColorArray.h in Headers */ = {isa = PBXBuildFile; fileRef = C9D58D8D14F031EB00A62C10 /* PXColorArray.h */; };
C9D58D9014F031EB00A62C10 /* PXColorArray.m in Sources */ = {isa = PBXBuildFile; fileRef = C9D58D8E14F031EB00A62C10 /* PXColorArray.m */; };
C9D58D9114F031FC00A62C10 /* PXColorArray.m in Sources */ = {isa = PBXBuildFile; fileRef = C9D58D8E14F031EB00A62C10 /* PXColorArray.m */; };
C9D58D9214F031FD00A62C10 /* PXColor.m in Sources */ = {isa = PBXBuildFile; fileRef = C9D58D8A14F0316B00A62C10 /* PXColor.m */; };
C9DC4D9B13D0E6D70065C1A6 /* PXImageSizePrompt.xib in Resources */ = {isa = PBXBuildFile; fileRef = C9DC4D9913D0E6D70065C1A6 /* PXImageSizePrompt.xib */; }; C9DC4D9B13D0E6D70065C1A6 /* PXImageSizePrompt.xib in Resources */ = {isa = PBXBuildFile; fileRef = C9DC4D9913D0E6D70065C1A6 /* PXImageSizePrompt.xib */; };
C9DC4D9E13D0E91B0065C1A6 /* PXPresetsManager.h in Headers */ = {isa = PBXBuildFile; fileRef = C9DC4D9C13D0E91B0065C1A6 /* PXPresetsManager.h */; }; C9DC4D9E13D0E91B0065C1A6 /* PXPresetsManager.h in Headers */ = {isa = PBXBuildFile; fileRef = C9DC4D9C13D0E91B0065C1A6 /* PXPresetsManager.h */; };
C9DC4D9F13D0E91B0065C1A6 /* PXPresetsManager.m in Sources */ = {isa = PBXBuildFile; fileRef = C9DC4D9D13D0E91B0065C1A6 /* PXPresetsManager.m */; }; C9DC4D9F13D0E91B0065C1A6 /* PXPresetsManager.m in Sources */ = {isa = PBXBuildFile; fileRef = C9DC4D9D13D0E91B0065C1A6 /* PXPresetsManager.m */; };
Expand Down Expand Up @@ -626,6 +630,8 @@
C9B1FA5D13D732FC00BB7CF7 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/PXManagePresetsWindow.xib; sourceTree = "<group>"; }; C9B1FA5D13D732FC00BB7CF7 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/PXManagePresetsWindow.xib; sourceTree = "<group>"; };
C9D58D8914F0316B00A62C10 /* PXColor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PXColor.h; sourceTree = "<group>"; }; C9D58D8914F0316B00A62C10 /* PXColor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PXColor.h; sourceTree = "<group>"; };
C9D58D8A14F0316B00A62C10 /* PXColor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PXColor.m; sourceTree = "<group>"; }; C9D58D8A14F0316B00A62C10 /* PXColor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PXColor.m; sourceTree = "<group>"; };
C9D58D8D14F031EB00A62C10 /* PXColorArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PXColorArray.h; sourceTree = "<group>"; };
C9D58D8E14F031EB00A62C10 /* PXColorArray.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PXColorArray.m; sourceTree = "<group>"; };
C9DC4D9A13D0E6D70065C1A6 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/PXImageSizePrompt.xib; sourceTree = "<group>"; }; C9DC4D9A13D0E6D70065C1A6 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/PXImageSizePrompt.xib; sourceTree = "<group>"; };
C9DC4D9C13D0E91B0065C1A6 /* PXPresetsManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PXPresetsManager.h; sourceTree = "<group>"; }; C9DC4D9C13D0E91B0065C1A6 /* PXPresetsManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PXPresetsManager.h; sourceTree = "<group>"; };
C9DC4D9D13D0E91B0065C1A6 /* PXPresetsManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PXPresetsManager.m; sourceTree = "<group>"; }; C9DC4D9D13D0E91B0065C1A6 /* PXPresetsManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PXPresetsManager.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1564,6 +1570,8 @@
children = ( children = (
C9D58D8914F0316B00A62C10 /* PXColor.h */, C9D58D8914F0316B00A62C10 /* PXColor.h */,
C9D58D8A14F0316B00A62C10 /* PXColor.m */, C9D58D8A14F0316B00A62C10 /* PXColor.m */,
C9D58D8D14F031EB00A62C10 /* PXColorArray.h */,
C9D58D8E14F031EB00A62C10 /* PXColorArray.m */,
EA31FB5C0CE7D44B00EC481B /* PXPalette.h */, EA31FB5C0CE7D44B00EC481B /* PXPalette.h */,
EA31FB5D0CE7D44B00EC481B /* PXPalette.m */, EA31FB5D0CE7D44B00EC481B /* PXPalette.m */,
); );
Expand Down Expand Up @@ -2248,6 +2256,7 @@
C968A8DE14A5213E00905E3D /* PXAboutWindowController.h in Headers */, C968A8DE14A5213E00905E3D /* PXAboutWindowController.h in Headers */,
C9104A5B14E38AAC00025103 /* NSWindowController+Additions.h in Headers */, C9104A5B14E38AAC00025103 /* NSWindowController+Additions.h in Headers */,
C9D58D8B14F0316B00A62C10 /* PXColor.h in Headers */, C9D58D8B14F0316B00A62C10 /* PXColor.h in Headers */,
C9D58D8F14F031EB00A62C10 /* PXColorArray.h in Headers */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
Expand Down Expand Up @@ -2509,6 +2518,8 @@
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
C98182D014F0212C0052616F /* PaletteTests.m in Sources */, C98182D014F0212C0052616F /* PaletteTests.m in Sources */,
C9D58D9214F031FD00A62C10 /* PXColor.m in Sources */,
C9D58D9114F031FC00A62C10 /* PXColorArray.m in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
Expand Down Expand Up @@ -2685,6 +2696,7 @@
C968A8DF14A5213E00905E3D /* PXAboutWindowController.m in Sources */, C968A8DF14A5213E00905E3D /* PXAboutWindowController.m in Sources */,
C9104A5C14E38AAC00025103 /* NSWindowController+Additions.m in Sources */, C9104A5C14E38AAC00025103 /* NSWindowController+Additions.m in Sources */,
C9D58D8C14F0316B00A62C10 /* PXColor.m in Sources */, C9D58D8C14F0316B00A62C10 /* PXColor.m in Sources */,
C9D58D9014F031EB00A62C10 /* PXColorArray.m in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
Expand Down
104 changes: 90 additions & 14 deletions Tests/PaletteTests.m
Expand Up @@ -7,25 +7,101 @@


#import "PaletteTests.h" #import "PaletteTests.h"


#import "PXColorArray.h"

@implementation PaletteTests @implementation PaletteTests


- (void)setUp - (void)testColorArray
{ {
[super setUp]; PXColorArrayRef array = PXColorArrayCreate();


// Set-up code here. STAssertTrue(array != NULL, @"The color array should be non-NULL");
} STAssertTrue(PXColorArrayCount(array) == 0, @"The color array should be empty");

- (void)tearDown
{
// Tear-down code here.


[super tearDown]; PXColor firstColor = PXGetBlackColor();
} PXColor secondColor = PXColorMake(20, 100, 80, 20);

PXColor thirdColor = PXColorMake(50, 20, 140, 180);
- (void)testExample PXColor fourthColor = PXGetClearColor();
{
// STFail(@"Unit tests are not implemented yet in Tests"); PXColorArrayAppendColor(array, firstColor);

STAssertTrue(PXColorArrayCount(array) == 1, @"The count of the color array should be 1");
STAssertTrue(PXColorEqualsColor(PXColorArrayColorAtIndex(array, 0), firstColor),
@"A wrong color value was appended");

PXColorArrayAppendColor(array, secondColor);

STAssertTrue(PXColorArrayCount(array) == 2, @"The count of the color array should be 2");
STAssertTrue(PXColorEqualsColor(PXColorArrayColorAtIndex(array, 1), secondColor),
@"A wrong color value was appended");

for (NSUInteger n = 0; n < 100; n++) {
PXColorArrayAppendColor(array, thirdColor);
}

STAssertTrue(PXColorArrayCount(array) == 102, @"The count of the color array should be 102");

for (NSUInteger n = 2; n < 102; n++) {
STAssertTrue(PXColorEqualsColor(PXColorArrayColorAtIndex(array, n), thirdColor),
@"Wrong color values were appended");
}

STAssertTrue(PXColorArrayIndexOfColor(array, firstColor) == 0,
@"The reported color index is wrong");

STAssertTrue(PXColorArrayIndexOfColor(array, secondColor) == 1,
@"The reported color index is wrong");

STAssertTrue(PXColorArrayIndexOfColor(array, thirdColor) == 2,
@"The reported color index is wrong");

PXColorArrayRemoveColorAtIndex(array, 1);

STAssertTrue(PXColorArrayCount(array) == 101, @"The count of the color array should be 101");
STAssertTrue(PXColorEqualsColor(PXColorArrayColorAtIndex(array, 1), thirdColor),
@"The indicated color was not removed");

PXColorArrayRemoveColorAtIndex(array, 0);

STAssertTrue(PXColorArrayCount(array) == 100, @"The count of the color array should be 100");
STAssertTrue(PXColorEqualsColor(PXColorArrayColorAtIndex(array, 0), thirdColor),
@"The indicated color was not removed");

PXColorArrayRemoveColorAtIndex(array, 99);

STAssertTrue(PXColorArrayCount(array) == 99, @"The count of the color array should be 99");

PXColorArrayEnumerateWithBlock(array, ^(PXColor color) {
STAssertTrue(PXColorEqualsColor(color, thirdColor), @"Wrong color values were enumerated");
});

PXColorArrayInsertColorAtIndex(array, 99, fourthColor);

STAssertTrue(PXColorArrayCount(array) == 100, @"The count of the color array should be 100");
STAssertTrue(PXColorEqualsColor(PXColorArrayColorAtIndex(array, 99), fourthColor),
@"The given color was not inserted");

PXColorArrayInsertColorAtIndex(array, 0, fourthColor);

STAssertTrue(PXColorArrayCount(array) == 101, @"The count of the color array should be 101");
STAssertTrue(PXColorEqualsColor(PXColorArrayColorAtIndex(array, 0), fourthColor),
@"The given color was not inserted");
STAssertTrue(PXColorEqualsColor(PXColorArrayColorAtIndex(array, 100), fourthColor),
@"The colors did not shift correctly after the insertion");

PXColorArrayMoveColor(array, 0, 4);

STAssertTrue(PXColorArrayCount(array) == 101, @"The count of the color array should be 101");
STAssertTrue(PXColorEqualsColor(PXColorArrayColorAtIndex(array, 0), thirdColor), @"The move failed");
STAssertTrue(PXColorEqualsColor(PXColorArrayColorAtIndex(array, 4), fourthColor), @"The move failed");

PXColorArrayMoveColor(array, 4, 102);

STAssertTrue(PXColorArrayCount(array) == 101, @"The count of the color array should be 101");
STAssertTrue(PXColorEqualsColor(PXColorArrayColorAtIndex(array, 4), thirdColor), @"The move failed");
STAssertTrue(PXColorEqualsColor(PXColorArrayColorAtIndex(array, 100), fourthColor), @"The move failed");

PXColorArrayRelease(array);
} }


@end @end

0 comments on commit 48350ab

Please sign in to comment.