Skip to content

Commit

Permalink
Fix for broken insertObjects:atIndexes: and tests and isEqual.
Browse files Browse the repository at this point in the history
[cappuccino#136 state:resolved]

Reviewed by me.
  • Loading branch information
Francisco Ryan Tolmasky I committed Nov 14, 2008
1 parent 1a8093c commit e1d57aa
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 7 deletions.
45 changes: 38 additions & 7 deletions Foundation/CPArray.j
Expand Up @@ -632,19 +632,39 @@
*/
- (BOOL)isEqualToArray:(id)anArray
{
if (self === anArray)
return YES;

if(length != anArray.length)
return NO;

var index = 0,
count = [self count];

for(; index < count; ++index)
if(!self[index] || !anArray[index] || ![self[index] isEqual:anArray[index]])
{
var lhs = self[index],
rhs = anArray[index];

// If they're not equal, and either doesn't have an isa, or they're !isEqual (not isEqual)
if (lhs !== rhs && (!lhs.isa || !rhs.isa || ![lhs isEqual:rhs]))
return NO;

}

return YES;
}

- (BOOL)isEqual:(id)anObject
{
if (self === anObject)
return YES;

if(![anObject isKindOfClass:[CPArray class]])
return NO;

return [self isEqualToArray:anObject];
}

// Deriving new arrays
/*!
Returns a copy of this array plus <code>anObject</code> inside the copy.
Expand Down Expand Up @@ -906,13 +926,24 @@
@param objects the objects to add to this array
@param anIndexSet the indices for the objects
*/
- (void)insertObjects:(CPArray)objects atIndexes:(CPIndexSet)anIndexSet
- (void)insertObjects:(CPArray)objects atIndexes:(CPIndexSet)indexes
{
var index = 0,
position = CPNotFound;
var indexesCount = [indexes count],
objectsCount = [objects count];

if(indexesCount !== objectsCount)
[CPException raise:CPRangeException reason:"the counts of the passed-in array (" + objectsCount + ") and index set (" + indexesCount + ") must be identical."];

while ((position = [indexes indexGreaterThanIndex:position]) != CPNotFound)
[self insertObject:objects[index++] atindex:position];
var lastIndex = [indexes lastIndex];

if(lastIndex >= [self count] + indexesCount)
[CPException raise:CPRangeException reason:"the last index (" + lastIndex + ") must be less than the sum of the original count (" + [self count] + ") and the insertion count (" + indexesCount + ")."];

var index = 0,
currentIndex = [indexes firstIndex];

for (; index < objectsCount; ++index, currentIndex = [indexes indexGreaterThanIndex:currentIndex])
[self insertObject:objects[index] atIndex:currentIndex];
}

/*!
Expand Down
65 changes: 65 additions & 0 deletions Tests/Foundation/CPArrayTest.j
Expand Up @@ -18,4 +18,69 @@ import <Foundation/CPNumber.j>
[self assert:[testStrings[i][0] componentsJoinedByString:testStrings[i][1]] equals:testStrings[i][2]];
}

- (void)testsInsertObjectsAtIndexes
{
var array = [CPMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil],
newAdditions = [CPArray arrayWithObjects:@"a", @"b", nil],
indexes = [CPMutableIndexSet indexSetWithIndex:1];

[indexes addIndex:3];

[array insertObjects:newAdditions atIndexes:indexes];

[self assert:array equals:[@"one", @"a", @"two", @"b", @"three", @"four"]];

var array = [CPMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil],
newAdditions = [CPArray arrayWithObjects:@"a", @"b", nil],
indexes = [CPMutableIndexSet indexSetWithIndex:5];

[indexes addIndex:4];

[array insertObjects:newAdditions atIndexes:indexes];

[self assert:array equals:[@"one", @"two", @"three", @"four", @"a", @"b"]];

var array = [CPMutableArray arrayWithObjects: @"one", @"two", @"three", @"four", nil],
newAdditions = [CPArray arrayWithObjects: @"a", @"b", @"c", nil],
indexes = [CPMutableIndexSet indexSetWithIndex:1];

[indexes addIndex:2];
[indexes addIndex:4];

[array insertObjects:newAdditions atIndexes:indexes];

[self assert:array equals:[@"one", @"a", @"b", @"two", @"c", @"three", @"four"]];


var array = [CPMutableArray arrayWithObjects: @"one", @"two", @"three", @"four", nil],
newAdditions = [CPArray arrayWithObjects: @"a", @"b", @"c", nil],
indexes = [CPMutableIndexSet indexSetWithIndex:1];

[indexes addIndex:2];
[indexes addIndex:6];

[array insertObjects:newAdditions atIndexes:indexes];

[self assert:array equals:[@"one", @"a", @"b", @"two", @"three", @"four", @"c"]];

//

var array = [CPMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil],
newAdditions = [CPArray arrayWithObjects:@"a", @"b", nil],
indexes = [CPMutableIndexSet indexSetWithIndex:5];

[indexes addIndex:6];

try
{
[array insertObjects:newAdditions atIndexes:indexes];
[self fail];
}
catch (e)
{
if ((e.isa) && [e name] == AssertionFailedError)
throw e;
}
}

@end

0 comments on commit e1d57aa

Please sign in to comment.