Fix for broken insertObjects:atIndexes: and tests and isEqual.

[#136 state:resolved]

Reviewed by me.
This commit is contained in:
Francisco Ryan Tolmasky I committed 2008-11-13 16:55:33 -08:00
1 parent 1a8093c314
commit e1d57aac3b
2 files changed
+103 -7

No files matched your search

+38 -7
View File
@@ -632,6 +632,9 @@
*/
- (BOOL)isEqualToArray:(id)anArray
{
if (self === anArray)
return YES;
if(length != anArray.length)
return NO;
@@ -639,12 +642,29 @@
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.
@@ -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];
while ((position = [indexes indexGreaterThanIndex:position]) != CPNotFound)
[self insertObject:objects[index++] atindex:position];
if(indexesCount !== objectsCount)
[CPException raise:CPRangeException reason:"the counts of the passed-in array (" + objectsCount + ") and index set (" + indexesCount + ") must be identical."];
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];
}
/*!
+65
View File
@@ -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