/
CPIndexSetTest.j
668 lines (509 loc) · 23.3 KB
/
CPIndexSetTest.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
@import <Foundation/CPIndexSet.j>
function descriptionWithoutEntity(aString)
{
var descriptionWithEntity = [aString description];
return descriptionWithEntity.substr(descriptionWithEntity.indexOf('>') + 1);
}
@implementation CPIndexSetTest : OJTestCase
{
CPIndexSet _set;
}
- (void)setUp
{
_set = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(10, 10)];
}
- (void)testAddIndexes
{
var indexSet = [CPIndexSet indexSet];
// Test no indexes
[self assert:descriptionWithoutEntity(indexSet) equals:@"(no indexes)"];
// Test adding initial range
[indexSet addIndexesInRange:CPMakeRange(30,10)];
[self assert:@"[number of indexes: 10 (in 1 range), indexes: (30-39)]" equals:descriptionWithoutEntity(indexSet)];
// Test adding range after existing ranges.
[indexSet addIndexesInRange:CPMakeRange(50,10)];
[self assert:@"[number of indexes: 20 (in 2 ranges), indexes: (30-39 50-59)]" equals:descriptionWithoutEntity(indexSet)];
// Test adding range before existing ranges.
[indexSet addIndexesInRange:CPMakeRange(10,10)];
[self assert:@"[number of indexes: 30 (in 3 ranges), indexes: (10-19 30-39 50-59)]" equals:descriptionWithoutEntity(indexSet)];
// Test adding range inbetween existing ranges.
[indexSet addIndexesInRange:CPMakeRange(45,2)];
[self assert:@"[number of indexes: 32 (in 4 ranges), indexes: (10-19 30-39 45-46 50-59)]" equals:descriptionWithoutEntity(indexSet)];
// Test adding single index inbetween existing ranges.
[indexSet addIndexesInRange:CPMakeRange(23,1)];
[self assert:@"[number of indexes: 33 (in 5 ranges), indexes: (10-19 23 30-39 45-46 50-59)]" equals:descriptionWithoutEntity(indexSet)];
// Test adding range inbetween existing ranges that forces a combination
[indexSet addIndexesInRange:CPMakeRange(47,3)];
[self assert:@"[number of indexes: 36 (in 4 ranges), indexes: (10-19 23 30-39 45-59)]" equals:descriptionWithoutEntity(indexSet)];
// Test adding range across ranges forcing a combination
[indexSet addIndexesInRange:CPMakeRange(35,15)];
[self assert:@"[number of indexes: 41 (in 3 ranges), indexes: (10-19 23 30-59)]" equals:descriptionWithoutEntity(indexSet)];
// Test adding range across two empty slots forcing a combination
[indexSet addIndexesInRange:CPMakeRange(5,70)];
[self assert:@"[number of indexes: 70 (in 1 range), indexes: (5-74)]" equals:descriptionWithoutEntity(indexSet)];
// Test adding to extend the beginning of the first range
[indexSet addIndex:4];
[self assert:@"[number of indexes: 71 (in 1 range), indexes: (4-74)]" equals:descriptionWithoutEntity(indexSet)];
}
- (void)testRemoveIndexes
{
var indexSet = [CPIndexSet indexSet];
// Test no indexes
[self assert:descriptionWithoutEntity(indexSet) equals:@"(no indexes)"];
// Test adding initial range
[indexSet addIndexesInRange:CPMakeRange(0, 70)];
[self assert:@"[number of indexes: 70 (in 1 range), indexes: (0-69)]" equals:descriptionWithoutEntity(indexSet)];
// Test remove range that is subset of existing range, causing a split.
[indexSet removeIndexesInRange:CPMakeRange(30, 10)];
[self assert:@"[number of indexes: 60 (in 2 ranges), indexes: (0-29 40-69)]" equals:descriptionWithoutEntity(indexSet)];
// Test remove range that is subset of existing range, causing a split.
[indexSet removeIndexesInRange:CPMakeRange(50, 5)];
[self assert:@"[number of indexes: 55 (in 3 ranges), indexes: (0-29 40-49 55-69)]" equals:descriptionWithoutEntity(indexSet)];
// Test remove index that is subset of existing range, causing a split.
[indexSet removeIndex:57];
[self assert:@"[number of indexes: 54 (in 4 ranges), indexes: (0-29 40-49 55-56 58-69)]" equals:descriptionWithoutEntity(indexSet)];
// Test remove range that is an exactly represented in the set.
[indexSet removeIndexesInRange:CPMakeRange(40, 10)];
[self assert:@"[number of indexes: 44 (in 3 ranges), indexes: (0-29 55-56 58-69)]" equals:descriptionWithoutEntity(indexSet)];
// Test remove range that isn't in the set.
[indexSet removeIndexesInRange:CPMakeRange(35, 3)];
[self assert:@"[number of indexes: 44 (in 3 ranges), indexes: (0-29 55-56 58-69)]" equals:descriptionWithoutEntity(indexSet)];
// Test remove range that is partially in a left range.
[indexSet removeIndexesInRange:CPMakeRange(25, 7)];
[self assert:@"[number of indexes: 39 (in 3 ranges), indexes: (0-24 55-56 58-69)]" equals:descriptionWithoutEntity(indexSet)];
// Test remove range that is partially in a left range.
[indexSet removeIndexesInRange:CPMakeRange(57, 3)];
[self assert:@"[number of indexes: 37 (in 3 ranges), indexes: (0-24 55-56 60-69)]" equals:descriptionWithoutEntity(indexSet)];
// Test remove range that is partially in a left and right range.
[indexSet removeIndexesInRange:CPMakeRange(20, 36)];
[self assert:@"[number of indexes: 31 (in 3 ranges), indexes: (0-19 56 60-69)]" equals:descriptionWithoutEntity(indexSet)];
// Remove single index that represents an entire range.
[indexSet removeIndex:56];
[self assert:@"[number of indexes: 30 (in 2 ranges), indexes: (0-19 60-69)]" equals:descriptionWithoutEntity(indexSet)];
// Remove index set that is subset of existing range, causing a split.
[indexSet removeIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(5, 10)]];
[self assert:@"[number of indexes: 20 (in 3 ranges), indexes: (0-4 15-19 60-69)]" equals:descriptionWithoutEntity(indexSet)];
// Remove indexes that are partially in 2 ranges and contains intermediate range.
[indexSet removeIndexesInRange:CPMakeRange(2, 62)];
[self assert:@"[number of indexes: 8 (in 2 ranges), indexes: (0-1 64-69)]" equals:descriptionWithoutEntity(indexSet)];
// Remove indexes that fit exactly in 2 ranges.
[indexSet removeIndexesInRange:CPMakeRange(0, 70)];
[self assert:@"(no indexes)" equals:descriptionWithoutEntity(indexSet)];
indexSet = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, 30)];
// Remove indexes from left hand of single range
[indexSet removeIndexesInRange:CPMakeRange(0, 29)];
[self assert:@"[number of indexes: 1 (in 1 range), indexes: (29)]" equals:descriptionWithoutEntity(indexSet)];
indexSet = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, 30)];
// Remove indexes from right hand of single range
[indexSet removeIndexesInRange:CPMakeRange(1, 29)];
[self assert:@"[number of indexes: 1 (in 1 range), indexes: (0)]" equals:descriptionWithoutEntity(indexSet)];
}
- (void)testGetIndexes
{
var indexSet = [CPIndexSet indexSet];
// Test no indexes
[self assert:descriptionWithoutEntity(indexSet) equals:@"(no indexes)"];
// Test adding initial range
[indexSet addIndexesInRange:CPMakeRange(0, 10)];
[indexSet addIndexesInRange:CPMakeRange(15, 1)];
[indexSet addIndexesInRange:CPMakeRange(20, 10)];
[indexSet addIndexesInRange:CPMakeRange(50, 10)];
[self assert:@"[number of indexes: 31 (in 4 ranges), indexes: (0-9 15 20-29 50-59)]" equals:descriptionWithoutEntity(indexSet)];
var array = [];
[indexSet getIndexes:array maxCount:1000 inIndexRange:nil];
[self assert:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59] equals:array];
}
- (void)testIndexSet:(CPIndexSet)set containsRange:(CPRange)range
{
[self assertFalse:[set containsIndex:range.location -1]];
for (var i = range.location, max = CPMaxRange(range); i < max; i++)
[self assertTrue:[set containsIndex:i]];
[self assertFalse:[set containsIndex:i]];
}
- (void)testIndexSet
{
[self assertNotNull:[CPIndexSet indexSet]];
[self assert:[[CPIndexSet indexSet] class] equals:[CPIndexSet class]];
}
- (void)testIndexSetWithIndex
{
[self assertTrue:[[CPIndexSet indexSetWithIndex:1] containsIndex:1]];
[self assertTrue:[[CPIndexSet indexSetWithIndex:0] containsIndex:0]];
[self assertFalse:[[CPIndexSet indexSetWithIndex:0] containsIndex:1]];
}
- (void)testIndexSetWithIndexesInRange
{
var set = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, 7)];
[self assertNotNull:set];
[self testIndexSet:set containsRange:CPMakeRange(0, 7)];
}
- (void)testInit
{
var set = [[CPIndexSet alloc] init];
[self assertNotNull:set];
[self assert:[set count] equals:0];
}
- (void)testInitWithIndex
{
var set = [[CPIndexSet alloc] initWithIndex:234];
[self assertNotNull:set];
[self assertTrue:[set containsIndex:234]];
[self assertFalse:[set containsIndex:5432]];
}
- (void)testInitWithIndexesInRange
{
var set = [[CPIndexSet alloc] initWithIndexesInRange:CPMakeRange(0, 7)];
[self assertNotNull:set];
[self testIndexSet:set containsRange:CPMakeRange(0, 7)];
[self assertTrue:[set containsIndexesInRange:CPMakeRange(0, 7)]];
[self assertTrue:[set containsIndexesInRange:CPMakeRange(1, 6)]];
[self assertFalse:[set containsIndexesInRange:CPMakeRange(2, 6)]];
}
- (void)testInitWithIndexSet
{
var set1 = [[CPIndexSet alloc] initWithIndexesInRange:CPMakeRange(0, 7)],
set = [[CPIndexSet alloc] initWithIndexSet:set1];
[self assertNotNull:set];
[self testIndexSet:set containsRange:CPMakeRange(0, 7)];
}
- (void)testIsEqualToIndexSet
{
var set1 = [CPIndexSet indexSetWithIndex:7],
set2 = [CPIndexSet indexSetWithIndex:7],
set3 = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(7, 2)];
[self assertFalse:[set1 isEqualToIndexSet:nil]];
[self assertTrue:[set1 isEqualToIndexSet:set2]];
[self assertTrue:[set1 isEqualToIndexSet:set1]];
[self assertFalse:[set1 isEqualToIndexSet:set3]];
}
- (void)testIsEqual
{
var set1 = [CPIndexSet indexSetWithIndex:7],
set2 = [CPIndexSet indexSetWithIndex:7],
set3 = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(7, 2)];
[self assertFalse:[set1 isEqual:nil]];
[self assertFalse:[set1 isEqual:7]];
[self assertTrue:[set1 isEqual:set2]];
[self assertTrue:[set1 isEqual:set1]];
[self assertFalse:[set1 isEqual:set3]];
}
- (void)testCount
{
var set = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(7, 2)],
set2 = [CPIndexSet indexSetWithIndex:7];
[self assert:[set2 count] equals:1];
[self assert:[set count] equals:2];
}
- (void)testFirstIndex
{
[self assert:[_set firstIndex] equals:10];
[self assert:[[CPIndexSet indexSet] firstIndex] equals:CPNotFound];
}
- (void)testLastIndex
{
[self assert:[_set lastIndex] equals:19];
[self assert:[[CPIndexSet indexSet] lastIndex] equals:CPNotFound];
var singleIndexSet = [CPIndexSet indexSetWithIndex:3];
[self assert:3 equals:[singleIndexSet lastIndex]];
}
/*
- (void)testAddSpeed
{
var startTime = [CPDate date];
for (var i = 0; i < 1000; i++)
{
[_set addIndex:ROUND(RAND()*100000)];
}
print([startTime timeIntervalSinceNow]);
//[self assertTrue: ABS([startTime timeIntervalSinceNow]) < 2];
}
*/
- (void)testIndexGreaterThanIndex
{
[self assert:[_set indexGreaterThanIndex:5] equals:10];
[self assert:[_set indexGreaterThanIndex:11] equals:12];
[self assert:[_set indexGreaterThanIndex:19] equals:CPNotFound];
}
- (void)testIndexLessThanIndex
{
[self assert:[_set indexLessThanIndex:5] equals:CPNotFound];
[self assert:[_set indexLessThanIndex:11] equals:10];
[self assert:[_set indexLessThanIndex:20] equals:19];
[self assert:[_set indexLessThanIndex:222] equals:19];
}
- (void)testIndexGreaterThanOrEqualToIndex
{
[self assert:[_set indexGreaterThanOrEqualToIndex:5] equals:10];
[self assert:[_set indexGreaterThanOrEqualToIndex:10] equals:10];
[self assert:[_set indexGreaterThanOrEqualToIndex:19] equals:19];
[self assert:[_set indexGreaterThanOrEqualToIndex:20] equals:CPNotFound];
}
- (void)testIndexLessThanOrEqualToIndex
{
[self assert:[_set indexLessThanOrEqualToIndex:5] equals:CPNotFound];
[self assert:[_set indexLessThanOrEqualToIndex:10] equals:10];
[self assert:[_set indexLessThanOrEqualToIndex:19] equals:19];
[self assert:[_set indexLessThanOrEqualToIndex:20] equals:19];
}
- (void)testContainsIndex
{
[self assertTrue:[_set containsIndex:10]];
[self assertTrue:[_set containsIndex:11]];
[self assertTrue:[_set containsIndex:19]];
[self assertFalse:[_set containsIndex:9]];
[self assertFalse:[_set containsIndex:20]];
}
- (void)testContainsIndexesInRange
{
[self assertTrue:[_set containsIndexesInRange:CPMakeRange(10, 10)]];
[self assertFalse:[_set containsIndexesInRange:CPMakeRange(10, 0)]];
[self assertTrue:[_set containsIndexesInRange:CPMakeRange(10, 1)]];
[self assertTrue:[_set containsIndexesInRange:CPMakeRange(14, 2)]];
[self assertFalse:[_set containsIndexesInRange:CPMakeRange(10, 11)]];
[self assertFalse:[_set containsIndexesInRange:CPMakeRange(9, 2)]];
[self assertFalse:[_set containsIndexesInRange:CPMakeRange(19, 2)]];
}
- (void)testContainsIndexes
{
[self assertTrue:[_set containsIndexes:_set]];
[self assertTrue:[_set containsIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(10, 1)]]];
[self assertTrue:[_set containsIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(10, 10)]]];
[self assertTrue:[_set containsIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(19, 1)]]];
[self assertFalse:[_set containsIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(10, 11)]]];
[self assertFalse:[_set containsIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(9, 2)]]];
}
- (void)testShiftIndexesStartingAtIndex
{
var startRange = CPMakeRange(1, 5),
shiftRange = CPMakeRange(2, 5);
_set = [CPIndexSet indexSetWithIndexesInRange:startRange];
// positive delta for downward shift
[_set shiftIndexesStartingAtIndex:1 by:1];
[self assertTrue:[_set containsIndexes:[CPIndexSet indexSetWithIndexesInRange:shiftRange]]];
// negative delta for downward shift
[_set shiftIndexesStartingAtIndex:1 by:-1];
[self assertTrue:[_set containsIndexes:[CPIndexSet indexSetWithIndexesInRange:startRange]]];
// test for fix to issue #746 (last item is mistakenly shifted)
_set = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, 1)];
[self assertTrue:[_set lastIndex] === 0];
[_set shiftIndexesStartingAtIndex:[_set lastIndex] + 1 by:1];
[self assertTrue:[_set lastIndex] === 0];
// make sure shifting past the lower bound works
_set = [CPIndexSet indexSetWithIndex:0];
[_set shiftIndexesStartingAtIndex:0 by:-1];
[self assert:[_set lastIndex] equals:CPNotFound];
[self assert:[_set count] equals:0];
}
- (void)testIsEqual
{
var differentSet = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(10, 11)],
equalSet = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(10, 10)];
[self assertFalse:[_set isEqual:nil]];
[self assertFalse:[_set isEqual:differentSet]];
[self assertTrue:[_set isEqual:equalSet]];
[self assertTrue:[_set isEqual:_set]];
}
- (void)testIsEqualToIndexSet
{
var differentSet = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(10, 11)],
equalSet = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(10, 10)];
[self assertFalse:[_set isEqualToIndexSet:differentSet]];
[self assertTrue:[_set isEqualToIndexSet:equalSet]];
[self assertTrue:[_set isEqualToIndexSet:_set]];
}
- (void)testEnumerateIndexesUsingBlock_
{
var set0 = [CPIndexSet indexSet],
set1 = [CPMutableIndexSet indexSet],
set2 = [CPMutableIndexSet indexSet];
[set1 addIndexesInRange:CPMakeRange(3, 2)];
[set2 addIndexesInRange:CPMakeRange(3, 2)];
[set2 addIndexesInRange:CPMakeRange(0, 1)];
var visitedIndexes = [],
aBlock;
aBlock = function(idx)
{
visitedIndexes.push(idx);
};
[set0 enumerateIndexesUsingBlock:aBlock];
[self assert:[] equals:visitedIndexes message:"enumerate empty set"];
[set1 enumerateIndexesUsingBlock:aBlock];
[self assert:[3, 4] equals:visitedIndexes message:"enumerate " + [set1 description]];
visitedIndexes = [];
[set2 enumerateIndexesUsingBlock:aBlock];
[self assert:[0, 3, 4] equals:visitedIndexes message:"enumerate " + [set2 description]];
}
- (void)testEnumerateIndexesWithOptions_usingBlock_
{
var set0 = [CPIndexSet indexSet],
set1 = [CPMutableIndexSet indexSet],
set2 = [CPMutableIndexSet indexSet];
[set1 addIndexesInRange:CPMakeRange(3, 2)];
[set2 addIndexesInRange:CPMakeRange(3, 2)];
[set2 addIndexesInRange:CPMakeRange(0, 1)];
var visitedIndexes = [],
aBlock;
aBlock = function(idx)
{
visitedIndexes.push(idx);
};
[set0 enumerateIndexesWithOptions:CPEnumerationNormal usingBlock:aBlock];
[self assert:[] equals:visitedIndexes message:"enumerate empty set"];
[set1 enumerateIndexesWithOptions:CPEnumerationNormal usingBlock:aBlock];
[self assert:[3, 4] equals:visitedIndexes message:"enumerate " + [set1 description]];
visitedIndexes = [];
[set2 enumerateIndexesWithOptions:CPEnumerationNormal usingBlock:aBlock];
[self assert:[0, 3, 4] equals:visitedIndexes message:"enumerate " + [set2 description]];
visitedIndexes = [];
[set0 enumerateIndexesWithOptions:CPEnumerationReverse usingBlock:aBlock];
[self assert:[] equals:visitedIndexes message:"reverse enumerate empty set"];
visitedIndexes = [];
[set1 enumerateIndexesWithOptions:CPEnumerationReverse usingBlock:aBlock];
[self assert:[4, 3] equals:visitedIndexes message:"reverse enumerate " + [set1 description]];
visitedIndexes = [];
[set2 enumerateIndexesWithOptions:CPEnumerationReverse usingBlock:aBlock];
[self assert:[4, 3, 0] equals:visitedIndexes message:"reverse enumerate " + [set2 description]];
}
- (void)testEnumerateIndexesInRange_options_usingBlock_
{
var set0 = [CPIndexSet indexSet],
set1 = [CPMutableIndexSet indexSet],
set2 = [CPMutableIndexSet indexSet];
[set1 addIndexesInRange:CPMakeRange(3, 2)];
[set2 addIndexesInRange:CPMakeRange(3, 2)];
[set2 addIndexesInRange:CPMakeRange(0, 1)];
var visitedIndexes = [],
aBlock;
aBlock = function(idx)
{
visitedIndexes.push(idx);
};
visitedIndexes = [];
[set0 enumerateIndexesInRange:CPMakeRange(0, 4) options:CPEnumerationReverse usingBlock:aBlock];
[self assert:[] equals:visitedIndexes message:"reverse enumerate in range empty set"];
visitedIndexes = [];
[set1 enumerateIndexesInRange:CPMakeRange(0, 5) options:CPEnumerationReverse usingBlock:aBlock];
[self assert:[4, 3] equals:visitedIndexes message:"reverse enumerate in range " + [set1 description]];
visitedIndexes = [];
[set1 enumerateIndexesInRange:CPMakeRange(0, 4) options:CPEnumerationReverse usingBlock:aBlock];
[self assert:[3] equals:visitedIndexes message:"reverse enumerate in range " + [set1 description]];
visitedIndexes = [];
[set2 addIndexesInRange:CPMakeRange(7, 2)];
[set2 enumerateIndexesInRange:CPMakeRange(2, 4) options:CPEnumerationReverse usingBlock:aBlock];
[self assert:[4, 3] equals:visitedIndexes message:"reverse enumerate " + [set2 description]];
}
- (void)testEnumerateIndexesAndStop
{
var set0 = [CPMutableIndexSet indexSet];
[set0 addIndexesInRange:CPMakeRange(3, 2)];
[set0 addIndexesInRange:CPMakeRange(0, 1)];
var visitedIndexes = [],
aBlock;
aBlock = function(idx, /* ref */ stop)
{
visitedIndexes.push(idx);
if (visitedIndexes.length >= 2)
stop(YES); // AT_DEREF(stop, YES) - FIXME Replace with proper @ref @deref when in ObjJ.
}
[set0 enumerateIndexesUsingBlock:aBlock];
[self assert:[0, 3] equals:visitedIndexes message:"enumeration should stop after 2 results"];
}
- (void)testIndexPassingTest
{
var set0 = [CPMutableIndexSet indexSet],
index = [set0 indexPassingTest:function(anIndex)
{
return anIndex % 2 === 0;
}];
[self assertTrue:index === CPNotFound message:"must be equal to CPNotFound, was " + index];
[set0 addIndexesInRange:CPMakeRange(1, 10)];
index = [set0 indexPassingTest:function(anIndex)
{
return anIndex % 2 === 0;
}];
[self assertTrue:index === 2 message:"index must be equal to 2"];
index = [set0 indexPassingTest:function(anIndex)
{
return anIndex === 1000;
}];
[self assertTrue:index === CPNotFound message:"must be equal to CPNotFound, was " + index];
}
- (void)testIndexesPassingTest
{
var set0 = [CPMutableIndexSet indexSet],
set1 = [CPMutableIndexSet indexSet],
indexes = [set0 indexesPassingTest:function(anIndex)
{
return anIndex % 2 === 0;
}];
[self assertTrue:[indexes isEqualToIndexSet:set1] message:"must be equal to " + [set1 description] + ", was " + [indexes description]];
[set0 addIndexesInRange:CPMakeRange(1, 10)];
[set1 addIndex:2];
[set1 addIndex:4];
[set1 addIndex:6];
[set1 addIndex:8];
[set1 addIndex:10];
indexes = [set0 indexesPassingTest:function(anIndex)
{
return anIndex % 2 === 0;
}];
[self assertTrue:[indexes isEqualToIndexSet:set1] message:"must be equal to " + [set1 description] + ", was " + [indexes description]];
}
- (void)testIndexPassingTest_options
{
var set0 = [CPMutableIndexSet indexSetWithIndexesInRange:CPMakeRange(1, 10)],
index = [set0 indexWithOptions:CPEnumerationReverse
passingTest:function(anIndex)
{
return anIndex % 2 === 0;
}];
[self assertTrue:index === 10 message:"index must be equal to 10"];
}
- (void)testIndexesPassingTest_options
{
var set0 = [CPMutableIndexSet indexSetWithIndexesInRange:CPMakeRange(1, 5)],
set1 = [CPMutableIndexSet indexSet],
visitedIndexes = [],
indexes = [set0 indexesWithOptions:CPEnumerationReverse
passingTest:function(anIndex)
{
visitedIndexes.push(anIndex);
return anIndex % 2 === 0;
}];
[set1 addIndex:2];
[set1 addIndex:4];
[self assertTrue:[indexes isEqualToIndexSet:set1] message:"must be equal to " + [set1 description] + ", was " + [indexes description]];
[self assert:[5, 4, 3, 2, 1] equals:visitedIndexes message:"enumeration should be done in reverse"];
}
- (void)testIndexPassingTest_options_range
{
var set0 = [CPMutableIndexSet indexSetWithIndexesInRange:CPMakeRange(1, 10)],
index = [set0 indexInRange:CPMakeRange(3, 4)
options:CPEnumerationReverse
passingTest:function(anIndex)
{
return anIndex % 2 === 0;
}];
[self assertTrue:index === 6 message:"index must be equal to 6"];
}
- (void)testIndexesPassingTest_options_range
{
var set0 = [CPMutableIndexSet indexSetWithIndexesInRange:CPMakeRange(1, 5)],
set1 = [CPMutableIndexSet indexSet],
visitedIndexes = [],
indexes = [set0 indexesInRange:CPMakeRange(3, 4)
options:CPEnumerationReverse
passingTest:function(anIndex)
{
visitedIndexes.push(anIndex);
return anIndex % 2 === 0;
}];
[set1 addIndex:4];
[self assertTrue:[indexes isEqualToIndexSet:set1] message:"must be equal to " + [set1 description] + ", was " + [indexes description]];
[self assert:[5, 4, 3] equals:visitedIndexes message:"enumeration should be done in reverse"];
}
- (void)tearDown
{
_set = nil;
}
@end