Fix for objectAtIndex: throwing out of range exceptions.

Closes #105.

Reviewed by me.
This commit is contained in:
Francisco Ryan Tolmasky I
2009-07-08 11:52:47 -07:00
parent b2f35b61d3
commit a5a43de26e
2 changed files with 44 additions and 0 deletions
+4
View File
@@ -26,6 +26,7 @@
@import "CPSortDescriptor.j"
@import "CPException.j"
/* @ignore */
@implementation _CPArrayEnumerator : CPEnumerator
{
@@ -520,6 +521,9 @@
*/
- (id)objectAtIndex:(int)anIndex
{
if (anIndex >= length)
[CPException raise:CPRangeException reason:@"index (" + anIndex + @") beyond bounds (" + length + @")"];
return self[anIndex];
}
+40
View File
@@ -115,6 +115,46 @@
equals:2];
}
- (void)testIndexOutOfBounds
{
try
{
[[] objectAtIndex:0];
[self assert:false];
}
catch (anException)
{
[self assert:[anException name] equals:CPRangeException];
[self assert:[anException reason] equals:@"index (0) beyond bounds (0)"];
}
[[0, 1, 2] objectAtIndex:0];
[[0, 1, 2] objectAtIndex:1];
[[0, 1, 2] objectAtIndex:2];
try
{
[[0, 1, 2] objectAtIndex:3];
[self assert:false];
}
catch (anException)
{
[self assert:[anException name] equals:CPRangeException];
[self assert:[anException reason] equals:@"index (3) beyond bounds (3)"];
}
try
{
[[0, 1, 2] objectAtIndex:4];
[self assert:false];
}
catch (anException)
{
[self assert:[anException name] equals:CPRangeException];
[self assert:[anException reason] equals:@"index (4) beyond bounds (3)"];
}
}
@end
@implementation CPArray (reverse)