96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
- (void)dealloc
{
[_array release];
[super dealloc];
}
- (id)objectAtIndex: (size_t)index
- (id)objectAtIndex: (size_t)idx
{
return [_array objectAtIndex: index];
return [_array objectAtIndex: idx];
}
- (size_t)count
{
return [_array count];
}
@end
@implementation SimpleMutableArray
+ (void)initialize
{
if (self == [SimpleMutableArray class])
[self inheritMethodsFromClass: [SimpleArray class]];
}
- (void)insertObject: (id)object
atIndex: (size_t)index
atIndex: (size_t)idx
{
[_array insertObject: object
atIndex: index];
atIndex: idx];
}
- (void)replaceObjectAtIndex: (size_t)index
- (void)replaceObjectAtIndex: (size_t)idx
withObject: (id)object
{
[_array replaceObjectAtIndex: index
[_array replaceObjectAtIndex: idx
withObject: object];
}
- (void)removeObjectAtIndex: (size_t)index
- (void)removeObjectAtIndex: (size_t)idx
{
[_array removeObjectAtIndex: index];
[_array removeObjectAtIndex: idx];
}
@end
@implementation TestsAppDelegate (OFArrayTests)
- (void)arrayTestsWithClass: (Class)arrayClass
mutableClass: (Class)mutableArrayClass
{
|
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
|
TEST(@"Detection of mutation during Fast Enumeration", ok)
[m[0] removeLastObject];
#ifdef OF_HAVE_BLOCKS
{
__block bool ok = true;
__block bool blockOk = true;
__block size_t count = 0;
OFArray *cmp = a[0];
OFMutableArray *a2;
m[0] = [[a[0] mutableCopy] autorelease];
[m[0] enumerateObjectsUsingBlock:
^ (id obj, size_t idx, bool *stop) {
^ (id object, size_t idx, bool *stop) {
count++;
if (![obj isEqual: [cmp objectAtIndex: idx]])
ok = false;
if (![object isEqual: [cmp objectAtIndex: idx]])
blockOk = false;
}];
if (count != [cmp count])
ok = false;
blockOk = false;
TEST(@"Enumeration using blocks", ok)
TEST(@"Enumeration using blocks", blockOk)
ok = false;
blockOk = false;
a2 = m[0];
@try {
[a2 enumerateObjectsUsingBlock:
^ (id obj, size_t idx, bool *stop) {
^ (id object, size_t idx, bool *stop) {
[a2 removeObjectAtIndex: idx];
}];
} @catch (OFEnumerationMutationException *e) {
ok = true;
blockOk = true;
} @catch (OFOutOfRangeException *e) {
/*
* Out of bounds access due to enumeration not being
* detected.
*/
}
TEST(@"Detection of mutation during enumeration using blocks",
ok)
blockOk)
}
TEST(@"-[replaceObjectsUsingBlock:]",
R([m[0] replaceObjectsUsingBlock: ^ id (id obj, size_t idx) {
R([m[0] replaceObjectsUsingBlock: ^ id (id object, size_t idx) {
switch (idx) {
case 0:
return @"foo";
case 1:
return @"bar";
}
return nil;
}]) && [[m[0] description] isEqual: @"(\n\tfoo,\n\tbar\n)"])
TEST(@"-[mappedArrayUsingBlock:]",
[[[m[0] mappedArrayUsingBlock: ^ id (id obj, size_t idx) {
[[[m[0] mappedArrayUsingBlock: ^ id (id object, size_t idx) {
switch (idx) {
case 0:
return @"foobar";
case 1:
return @"qux";
}
return nil;
}] description] isEqual: @"(\n\tfoobar,\n\tqux\n)"])
TEST(@"-[filteredArrayUsingBlock:]",
[[[m[0] filteredArrayUsingBlock: ^ bool (id obj, size_t idx) {
return [obj isEqual: @"foo"];
[[[m[0] filteredArrayUsingBlock: ^ bool (id object, size_t idx) {
return [object isEqual: @"foo"];
}] description] isEqual: @"(\n\tfoo\n)"])
TEST(@"-[foldUsingBlock:]",
[[arrayClass arrayWithObjects: [OFMutableString string], @"foo",
@"bar", @"baz", nil] foldUsingBlock: ^ id (id left, id right) {
[left appendString: right];
return left;
|