forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestIndexPath.swift
793 lines (672 loc) · 26.1 KB
/
TestIndexPath.swift
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016, 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
class TestIndexPath: XCTestCase {
static var allTests: [(String, (TestIndexPath) -> () throws -> Void)] {
return [
("testEmpty", testEmpty),
("testSingleIndex", testSingleIndex),
("testTwoIndexes", testTwoIndexes),
("testManyIndexes", testManyIndexes),
("testCreateFromSequence", testCreateFromSequence),
("testCreateFromLiteral", testCreateFromLiteral),
("testDropLast", testDropLast),
("testDropLastFromEmpty", testDropLastFromEmpty),
("testDropLastFromSingle", testDropLastFromSingle),
("testDropLastFromPair", testDropLastFromPair),
("testDropLastFromTriple", testDropLastFromTriple),
("testStartEndIndex", testStartEndIndex),
("testIterator", testIterator),
("testIndexing", testIndexing),
("testCompare", testCompare),
("testHashing", testHashing),
("testEquality", testEquality),
("testSubscripting", testSubscripting),
("testAppending", testAppending),
("testAppendEmpty", testAppendEmpty),
("testAppendEmptyIndexPath", testAppendEmptyIndexPath),
("testAppendManyIndexPath", testAppendManyIndexPath),
("testAppendEmptyIndexPathToSingle", testAppendEmptyIndexPathToSingle),
("testAppendSingleIndexPath", testAppendSingleIndexPath),
("testAppendSingleIndexPathToSingle", testAppendSingleIndexPathToSingle),
("testAppendPairIndexPath", testAppendPairIndexPath),
("testAppendManyIndexPathToEmpty", testAppendManyIndexPathToEmpty),
("testAppendByOperator", testAppendByOperator),
("testAppendArray", testAppendArray),
("testRanges", testRanges),
("testRangeFromEmpty", testRangeFromEmpty),
("testRangeFromSingle", testRangeFromSingle),
("testRangeFromPair", testRangeFromPair),
("testRangeFromMany", testRangeFromMany),
("testRangeReplacementSingle", testRangeReplacementSingle),
("testRangeReplacementPair", testRangeReplacementPair),
("testMoreRanges", testMoreRanges),
("testIteration", testIteration),
("testDescription", testDescription),
("testBridgeToObjC", testBridgeToObjC),
("testForceBridgeFromObjC", testForceBridgeFromObjC),
("testConditionalBridgeFromObjC", testConditionalBridgeFromObjC),
("testUnconditionalBridgeFromObjC", testUnconditionalBridgeFromObjC),
("testObjcBridgeType", testObjcBridgeType),
("test_AnyHashableContainingIndexPath", test_AnyHashableContainingIndexPath),
("test_AnyHashableCreatedFromNSIndexPath", test_AnyHashableCreatedFromNSIndexPath),
("test_unconditionallyBridgeFromObjectiveC", test_unconditionallyBridgeFromObjectiveC),
("test_slice_1ary", test_slice_1ary),
("test_copy", test_copy),
]
}
func testEmpty() {
let ip = IndexPath()
XCTAssertEqual(ip.count, 0)
// Darwin allows nil if length is 0
let nsip = NSIndexPath(indexes: nil, length: 0)
XCTAssertEqual(nsip.length, 0)
let newIp = nsip.adding(1)
XCTAssertEqual(newIp.count, 1)
}
func testSingleIndex() {
let ip = IndexPath(index: 1)
XCTAssertEqual(ip.count, 1)
XCTAssertEqual(ip[0], 1)
let highValueIp = IndexPath(index: .max)
XCTAssertEqual(highValueIp.count, 1)
XCTAssertEqual(highValueIp[0], .max)
let lowValueIp = IndexPath(index: .min)
XCTAssertEqual(lowValueIp.count, 1)
XCTAssertEqual(lowValueIp[0], .min)
}
func testTwoIndexes() {
let ip = IndexPath(indexes: [0, 1])
XCTAssertEqual(ip.count, 2)
XCTAssertEqual(ip[0], 0)
XCTAssertEqual(ip[1], 1)
}
func testManyIndexes() {
let ip = IndexPath(indexes: [0, 1, 2, 3, 4])
XCTAssertEqual(ip.count, 5)
XCTAssertEqual(ip[0], 0)
XCTAssertEqual(ip[1], 1)
XCTAssertEqual(ip[2], 2)
XCTAssertEqual(ip[3], 3)
XCTAssertEqual(ip[4], 4)
}
func testCreateFromSequence() {
let seq = repeatElement(5, count: 3)
let ip = IndexPath(indexes: seq)
XCTAssertEqual(ip.count, 3)
XCTAssertEqual(ip[0], 5)
XCTAssertEqual(ip[1], 5)
XCTAssertEqual(ip[2], 5)
}
func testCreateFromLiteral() {
let ip: IndexPath = [1, 2, 3, 4]
XCTAssertEqual(ip.count, 4)
XCTAssertEqual(ip[0], 1)
XCTAssertEqual(ip[1], 2)
XCTAssertEqual(ip[2], 3)
XCTAssertEqual(ip[3], 4)
}
func testDropLast() {
let ip: IndexPath = [1, 2, 3, 4]
let ip2 = ip.dropLast()
XCTAssertEqual(ip2.count, 3)
XCTAssertEqual(ip2[0], 1)
XCTAssertEqual(ip2[1], 2)
XCTAssertEqual(ip2[2], 3)
}
func testDropLastFromEmpty() {
let ip: IndexPath = []
let ip2 = ip.dropLast()
XCTAssertEqual(ip2.count, 0)
}
func testDropLastFromSingle() {
let ip: IndexPath = [1]
let ip2 = ip.dropLast()
XCTAssertEqual(ip2.count, 0)
}
func testDropLastFromPair() {
let ip: IndexPath = [1, 2]
let ip2 = ip.dropLast()
XCTAssertEqual(ip2.count, 1)
XCTAssertEqual(ip2[0], 1)
}
func testDropLastFromTriple() {
let ip: IndexPath = [1, 2, 3]
let ip2 = ip.dropLast()
XCTAssertEqual(ip2.count, 2)
XCTAssertEqual(ip2[0], 1)
XCTAssertEqual(ip2[1], 2)
}
func testStartEndIndex() {
let ip: IndexPath = [1, 2, 3, 4]
XCTAssertEqual(ip.startIndex, 0)
XCTAssertEqual(ip.endIndex, ip.count)
}
func testIterator() {
let ip: IndexPath = [1, 2, 3, 4]
var iter = ip.makeIterator()
var sum = 0
while let index = iter.next() {
sum += index
}
XCTAssertEqual(sum, 1 + 2 + 3 + 4)
}
func testIndexing() {
let ip: IndexPath = [1, 2, 3, 4]
XCTAssertEqual(ip.index(before: 1), 0)
XCTAssertEqual(ip.index(before: 0), -1) // beyond range!
XCTAssertEqual(ip.index(after: 1), 2)
XCTAssertEqual(ip.index(after: 4), 5) // beyond range!
}
func testCompare() {
let ip1: IndexPath = [1, 2]
let ip2: IndexPath = [3, 4]
let ip3: IndexPath = [5, 1]
let ip4: IndexPath = [1, 1, 1]
let ip5: IndexPath = [1, 1, 9]
XCTAssertEqual(ip1.compare(ip1), .orderedSame)
XCTAssertEqual(ip1 < ip1, false)
XCTAssertEqual(ip1 <= ip1, true)
XCTAssertEqual(ip1 == ip1, true)
XCTAssertEqual(ip1 >= ip1, true)
XCTAssertEqual(ip1 > ip1, false)
XCTAssertEqual(ip1.compare(ip2), .orderedAscending)
XCTAssertEqual(ip1 < ip2, true)
XCTAssertEqual(ip1 <= ip2, true)
XCTAssertEqual(ip1 == ip2, false)
XCTAssertEqual(ip1 >= ip2, false)
XCTAssertEqual(ip1 > ip2, false)
XCTAssertEqual(ip1.compare(ip3), .orderedAscending)
XCTAssertEqual(ip1 < ip3, true)
XCTAssertEqual(ip1 <= ip3, true)
XCTAssertEqual(ip1 == ip3, false)
XCTAssertEqual(ip1 >= ip3, false)
XCTAssertEqual(ip1 > ip3, false)
XCTAssertEqual(ip1.compare(ip4), .orderedDescending)
XCTAssertEqual(ip1 < ip4, false)
XCTAssertEqual(ip1 <= ip4, false)
XCTAssertEqual(ip1 == ip4, false)
XCTAssertEqual(ip1 >= ip4, true)
XCTAssertEqual(ip1 > ip4, true)
XCTAssertEqual(ip1.compare(ip5), .orderedDescending)
XCTAssertEqual(ip1 < ip5, false)
XCTAssertEqual(ip1 <= ip5, false)
XCTAssertEqual(ip1 == ip5, false)
XCTAssertEqual(ip1 >= ip5, true)
XCTAssertEqual(ip1 > ip5, true)
XCTAssertEqual(ip2.compare(ip1), .orderedDescending)
XCTAssertEqual(ip2 < ip1, false)
XCTAssertEqual(ip2 <= ip1, false)
XCTAssertEqual(ip2 == ip1, false)
XCTAssertEqual(ip2 >= ip1, true)
XCTAssertEqual(ip2 > ip1, true)
XCTAssertEqual(ip2.compare(ip2), .orderedSame)
XCTAssertEqual(ip2 < ip2, false)
XCTAssertEqual(ip2 <= ip2, true)
XCTAssertEqual(ip2 == ip2, true)
XCTAssertEqual(ip2 >= ip2, true)
XCTAssertEqual(ip2 > ip2, false)
XCTAssertEqual(ip2.compare(ip3), .orderedAscending)
XCTAssertEqual(ip2 < ip3, true)
XCTAssertEqual(ip2 <= ip3, true)
XCTAssertEqual(ip2 == ip3, false)
XCTAssertEqual(ip2 >= ip3, false)
XCTAssertEqual(ip2 > ip3, false)
XCTAssertEqual(ip2.compare(ip4), .orderedDescending)
XCTAssertEqual(ip2.compare(ip5), .orderedDescending)
XCTAssertEqual(ip3.compare(ip1), .orderedDescending)
XCTAssertEqual(ip3.compare(ip2), .orderedDescending)
XCTAssertEqual(ip3.compare(ip3), .orderedSame)
XCTAssertEqual(ip3.compare(ip4), .orderedDescending)
XCTAssertEqual(ip3.compare(ip5), .orderedDescending)
XCTAssertEqual(ip4.compare(ip1), .orderedAscending)
XCTAssertEqual(ip4.compare(ip2), .orderedAscending)
XCTAssertEqual(ip4.compare(ip3), .orderedAscending)
XCTAssertEqual(ip4.compare(ip4), .orderedSame)
XCTAssertEqual(ip4.compare(ip5), .orderedAscending)
XCTAssertEqual(ip5.compare(ip1), .orderedAscending)
XCTAssertEqual(ip5.compare(ip2), .orderedAscending)
XCTAssertEqual(ip5.compare(ip3), .orderedAscending)
XCTAssertEqual(ip5.compare(ip4), .orderedDescending)
XCTAssertEqual(ip5.compare(ip5), .orderedSame)
let ip6: IndexPath = [1, 1]
XCTAssertEqual(ip6.compare(ip5), .orderedAscending)
XCTAssertEqual(ip5.compare(ip6), .orderedDescending)
}
func testHashing() {
let samples: [IndexPath] = [
[],
[1],
[2],
[Int.max],
[1, 1],
[2, 1],
[1, 2],
[1, 1, 1],
[2, 1, 1],
[1, 2, 1],
[1, 1, 2],
[Int.max, Int.max, Int.max],
]
checkHashable(samples, equalityOracle: { $0 == $1 })
// this should not cause an overflow crash
let hash: Int? = IndexPath(indexes: [Int.max >> 8, 2, Int.max >> 36]).hashValue
XCTAssertNotNil(hash)
}
func testEquality() {
let ip1: IndexPath = [1, 1]
let ip2: IndexPath = [1, 1]
let ip3: IndexPath = [1, 1, 1]
let ip4: IndexPath = []
let ip5: IndexPath = [1]
XCTAssertTrue(ip1 == ip2)
XCTAssertFalse(ip1 == ip3)
XCTAssertFalse(ip1 == ip4)
XCTAssertFalse(ip4 == ip1)
XCTAssertFalse(ip5 == ip1)
XCTAssertFalse(ip5 == ip4)
XCTAssertTrue(ip4 == ip4)
XCTAssertTrue(ip5 == ip5)
}
func testSubscripting() {
var ip1: IndexPath = [1]
var ip2: IndexPath = [1, 2]
var ip3: IndexPath = [1, 2, 3]
XCTAssertEqual(ip1[0], 1)
XCTAssertEqual(ip2[0], 1)
XCTAssertEqual(ip2[1], 2)
XCTAssertEqual(ip3[0], 1)
XCTAssertEqual(ip3[1], 2)
XCTAssertEqual(ip3[2], 3)
ip1[0] = 2
XCTAssertEqual(ip1[0], 2)
ip2[0] = 2
ip2[1] = 3
XCTAssertEqual(ip2[0], 2)
XCTAssertEqual(ip2[1], 3)
ip3[0] = 2
ip3[1] = 3
ip3[2] = 4
XCTAssertEqual(ip3[0], 2)
XCTAssertEqual(ip3[1], 3)
XCTAssertEqual(ip3[2], 4)
let ip4 = ip3[0..<2]
XCTAssertEqual(ip4.count, 2)
XCTAssertEqual(ip4[0], 2)
XCTAssertEqual(ip4[1], 3)
}
func testAppending() {
var ip : IndexPath = [1, 2, 3, 4]
let ip2 = IndexPath(indexes: [5, 6, 7])
ip.append(ip2)
XCTAssertEqual(ip.count, 7)
XCTAssertEqual(ip[0], 1)
XCTAssertEqual(ip[6], 7)
let ip3 = ip.appending(IndexPath(indexes: [8, 9]))
XCTAssertEqual(ip3.count, 9)
XCTAssertEqual(ip3[7], 8)
XCTAssertEqual(ip3[8], 9)
let ip4 = ip3.appending([10, 11])
XCTAssertEqual(ip4.count, 11)
XCTAssertEqual(ip4[9], 10)
XCTAssertEqual(ip4[10], 11)
let ip5 = ip.appending(8)
XCTAssertEqual(ip5.count, 8)
XCTAssertEqual(ip5[7], 8)
}
func testAppendEmpty() {
var ip: IndexPath = []
ip.append(1)
XCTAssertEqual(ip.count, 1)
XCTAssertEqual(ip[0], 1)
ip.append(2)
XCTAssertEqual(ip.count, 2)
XCTAssertEqual(ip[0], 1)
XCTAssertEqual(ip[1], 2)
ip.append(3)
XCTAssertEqual(ip.count, 3)
XCTAssertEqual(ip[0], 1)
XCTAssertEqual(ip[1], 2)
XCTAssertEqual(ip[2], 3)
ip.append(4)
XCTAssertEqual(ip.count, 4)
XCTAssertEqual(ip[0], 1)
XCTAssertEqual(ip[1], 2)
XCTAssertEqual(ip[2], 3)
XCTAssertEqual(ip[3], 4)
}
func testAppendEmptyIndexPath() {
var ip: IndexPath = []
ip.append(IndexPath(indexes: []))
XCTAssertEqual(ip.count, 0)
}
func testAppendManyIndexPath() {
var ip: IndexPath = []
ip.append(IndexPath(indexes: [1, 2, 3]))
XCTAssertEqual(ip.count, 3)
XCTAssertEqual(ip[0], 1)
XCTAssertEqual(ip[1], 2)
XCTAssertEqual(ip[2], 3)
}
func testAppendEmptyIndexPathToSingle() {
var ip: IndexPath = [1]
ip.append(IndexPath(indexes: []))
XCTAssertEqual(ip.count, 1)
XCTAssertEqual(ip[0], 1)
}
func testAppendSingleIndexPath() {
var ip: IndexPath = []
ip.append(IndexPath(indexes: [1]))
XCTAssertEqual(ip.count, 1)
XCTAssertEqual(ip[0], 1)
}
func testAppendSingleIndexPathToSingle() {
var ip: IndexPath = [1]
ip.append(IndexPath(indexes: [1]))
XCTAssertEqual(ip.count, 2)
XCTAssertEqual(ip[0], 1)
XCTAssertEqual(ip[1], 1)
}
func testAppendPairIndexPath() {
var ip: IndexPath = []
ip.append(IndexPath(indexes: [1, 2]))
XCTAssertEqual(ip.count, 2)
XCTAssertEqual(ip[0], 1)
XCTAssertEqual(ip[1], 2)
}
func testAppendManyIndexPathToEmpty() {
var ip: IndexPath = []
ip.append(IndexPath(indexes: [1, 2, 3]))
XCTAssertEqual(ip.count, 3)
XCTAssertEqual(ip[0], 1)
XCTAssertEqual(ip[1], 2)
XCTAssertEqual(ip[2], 3)
}
func testAppendByOperator() {
let ip1: IndexPath = []
let ip2: IndexPath = []
let ip3 = ip1 + ip2
XCTAssertEqual(ip3.count, 0)
let ip4: IndexPath = [1]
let ip5: IndexPath = [2]
let ip6 = ip4 + ip5
XCTAssertEqual(ip6.count, 2)
XCTAssertEqual(ip6[0], 1)
XCTAssertEqual(ip6[1], 2)
var ip7: IndexPath = []
ip7 += ip6
XCTAssertEqual(ip7.count, 2)
XCTAssertEqual(ip7[0], 1)
XCTAssertEqual(ip7[1], 2)
}
func testAppendArray() {
var ip: IndexPath = [1, 2, 3, 4]
let indexes = [5, 6, 7]
ip.append(indexes)
XCTAssertEqual(ip.count, 7)
XCTAssertEqual(ip[0], 1)
XCTAssertEqual(ip[6], 7)
}
func testRanges() {
let ip1 = IndexPath(indexes: [1, 2, 3])
let ip2 = IndexPath(indexes: [6, 7, 8])
// Replace the whole range
var mutateMe = ip1
mutateMe[0..<3] = ip2
XCTAssertEqual(mutateMe, ip2)
// Insert at the beginning
mutateMe = ip1
mutateMe[0..<0] = ip2
XCTAssertEqual(mutateMe, IndexPath(indexes: [6, 7, 8, 1, 2, 3]))
// Insert at the end
mutateMe = ip1
mutateMe[3..<3] = ip2
XCTAssertEqual(mutateMe, IndexPath(indexes: [1, 2, 3, 6, 7, 8]))
// Insert in middle
mutateMe = ip1
mutateMe[2..<2] = ip2
XCTAssertEqual(mutateMe, IndexPath(indexes: [1, 2, 6, 7, 8, 3]))
}
func testRangeFromEmpty() {
let ip1 = IndexPath()
let ip2 = ip1[0..<0]
XCTAssertEqual(ip2.count, 0)
}
func testRangeFromSingle() {
let ip1 = IndexPath(indexes: [1])
let ip2 = ip1[0..<0]
XCTAssertEqual(ip2.count, 0)
let ip3 = ip1[0..<1]
XCTAssertEqual(ip3.count, 1)
XCTAssertEqual(ip3[0], 1)
}
func testRangeFromPair() {
let ip1 = IndexPath(indexes: [1, 2])
let ip2 = ip1[0..<0]
XCTAssertEqual(ip2.count, 0)
let ip3 = ip1[0..<1]
XCTAssertEqual(ip3.count, 1)
XCTAssertEqual(ip3[0], 1)
let ip4 = ip1[1..<1]
XCTAssertEqual(ip4.count, 0)
let ip5 = ip1[0..<2]
XCTAssertEqual(ip5.count, 2)
XCTAssertEqual(ip5[0], 1)
XCTAssertEqual(ip5[1], 2)
let ip6 = ip1[1..<2]
XCTAssertEqual(ip6.count, 1)
XCTAssertEqual(ip6[0], 2)
let ip7 = ip1[2..<2]
XCTAssertEqual(ip7.count, 0)
}
func testRangeFromMany() {
let ip1 = IndexPath(indexes: [1, 2, 3])
let ip2 = ip1[0..<0]
XCTAssertEqual(ip2.count, 0)
let ip3 = ip1[0..<1]
XCTAssertEqual(ip3.count, 1)
let ip4 = ip1[0..<2]
XCTAssertEqual(ip4.count, 2)
let ip5 = ip1[0..<3]
XCTAssertEqual(ip5.count, 3)
}
func testRangeReplacementSingle() {
var ip1 = IndexPath(indexes: [1])
ip1[0..<1] = IndexPath(indexes: [2])
XCTAssertEqual(ip1[0], 2)
ip1[0..<1] = IndexPath(indexes: [])
XCTAssertEqual(ip1.count, 0)
}
func testRangeReplacementPair() {
var ip1 = IndexPath(indexes: [1, 2])
ip1[0..<1] = IndexPath(indexes: [2, 3])
XCTAssertEqual(ip1.count, 3)
XCTAssertEqual(ip1[0], 2)
XCTAssertEqual(ip1[1], 3)
XCTAssertEqual(ip1[2], 2)
ip1[0..<1] = IndexPath(indexes: [])
XCTAssertEqual(ip1.count, 2)
}
func testMoreRanges() {
var ip = IndexPath(indexes: [1, 2, 3])
let ip2 = IndexPath(indexes: [5, 6, 7, 8, 9, 10])
ip[1..<2] = ip2
XCTAssertEqual(ip, IndexPath(indexes: [1, 5, 6, 7, 8, 9, 10, 3]))
}
func testIteration() {
let ip = IndexPath(indexes: [1, 2, 3])
var count = 0
for _ in ip {
count += 1
}
XCTAssertEqual(3, count)
}
func testDescription() {
let ip1: IndexPath = []
let ip2: IndexPath = [1]
let ip3: IndexPath = [1, 2]
let ip4: IndexPath = [1, 2, 3]
XCTAssertEqual(ip1.description, "[]")
XCTAssertEqual(ip2.description, "[1]")
XCTAssertEqual(ip3.description, "[1, 2]")
XCTAssertEqual(ip4.description, "[1, 2, 3]")
XCTAssertEqual(ip1.debugDescription, ip1.description)
XCTAssertEqual(ip2.debugDescription, ip2.description)
XCTAssertEqual(ip3.debugDescription, ip3.description)
XCTAssertEqual(ip4.debugDescription, ip4.description)
}
func testBridgeToObjC() {
let ip1: IndexPath = []
let ip2: IndexPath = [1]
let ip3: IndexPath = [1, 2]
let ip4: IndexPath = [1, 2, 3]
let nsip1 = ip1._bridgeToObjectiveC()
let nsip2 = ip2._bridgeToObjectiveC()
let nsip3 = ip3._bridgeToObjectiveC()
let nsip4 = ip4._bridgeToObjectiveC()
XCTAssertEqual(nsip1.length, 0)
XCTAssertEqual(nsip2.length, 1)
XCTAssertEqual(nsip3.length, 2)
XCTAssertEqual(nsip4.length, 3)
}
func testForceBridgeFromObjC() {
let nsip1 = NSIndexPath()
let nsip2 = NSIndexPath(index: 1)
let nsip3 = [1, 2].withUnsafeBufferPointer { (buffer: UnsafeBufferPointer<Int>) -> NSIndexPath in
return NSIndexPath(indexes: buffer.baseAddress, length: buffer.count)
}
let nsip4 = [1, 2, 3].withUnsafeBufferPointer { (buffer: UnsafeBufferPointer<Int>) -> NSIndexPath in
return NSIndexPath(indexes: buffer.baseAddress, length: buffer.count)
}
var ip1: IndexPath?
IndexPath._forceBridgeFromObjectiveC(nsip1, result: &ip1)
XCTAssertNotNil(ip1)
XCTAssertEqual(ip1!.count, 0)
var ip2: IndexPath?
IndexPath._forceBridgeFromObjectiveC(nsip2, result: &ip2)
XCTAssertNotNil(ip2)
XCTAssertEqual(ip2!.count, 1)
XCTAssertEqual(ip2![0], 1)
var ip3: IndexPath?
IndexPath._forceBridgeFromObjectiveC(nsip3, result: &ip3)
XCTAssertNotNil(ip3)
XCTAssertEqual(ip3!.count, 2)
XCTAssertEqual(ip3![0], 1)
XCTAssertEqual(ip3![1], 2)
var ip4: IndexPath?
IndexPath._forceBridgeFromObjectiveC(nsip4, result: &ip4)
XCTAssertNotNil(ip4)
XCTAssertEqual(ip4!.count, 3)
XCTAssertEqual(ip4![0], 1)
XCTAssertEqual(ip4![1], 2)
XCTAssertEqual(ip4![2], 3)
}
func testConditionalBridgeFromObjC() {
let nsip1 = NSIndexPath()
let nsip2 = NSIndexPath(index: 1)
let nsip3 = [1, 2].withUnsafeBufferPointer { (buffer: UnsafeBufferPointer<Int>) -> NSIndexPath in
return NSIndexPath(indexes: buffer.baseAddress, length: buffer.count)
}
let nsip4 = [1, 2, 3].withUnsafeBufferPointer { (buffer: UnsafeBufferPointer<Int>) -> NSIndexPath in
return NSIndexPath(indexes: buffer.baseAddress, length: buffer.count)
}
var ip1: IndexPath?
XCTAssertTrue(IndexPath._conditionallyBridgeFromObjectiveC(nsip1, result: &ip1))
XCTAssertNotNil(ip1)
XCTAssertEqual(ip1!.count, 0)
var ip2: IndexPath?
XCTAssertTrue(IndexPath._conditionallyBridgeFromObjectiveC(nsip2, result: &ip2))
XCTAssertNotNil(ip2)
XCTAssertEqual(ip2!.count, 1)
XCTAssertEqual(ip2![0], 1)
var ip3: IndexPath?
XCTAssertTrue(IndexPath._conditionallyBridgeFromObjectiveC(nsip3, result: &ip3))
XCTAssertNotNil(ip3)
XCTAssertEqual(ip3!.count, 2)
XCTAssertEqual(ip3![0], 1)
XCTAssertEqual(ip3![1], 2)
var ip4: IndexPath?
XCTAssertTrue(IndexPath._conditionallyBridgeFromObjectiveC(nsip4, result: &ip4))
XCTAssertNotNil(ip4)
XCTAssertEqual(ip4!.count, 3)
XCTAssertEqual(ip4![0], 1)
XCTAssertEqual(ip4![1], 2)
XCTAssertEqual(ip4![2], 3)
}
func testUnconditionalBridgeFromObjC() {
let nsip1 = NSIndexPath()
let nsip2 = NSIndexPath(index: 1)
let nsip3 = [1, 2].withUnsafeBufferPointer { (buffer: UnsafeBufferPointer<Int>) -> NSIndexPath in
return NSIndexPath(indexes: buffer.baseAddress, length: buffer.count)
}
let nsip4 = [1, 2, 3].withUnsafeBufferPointer { (buffer: UnsafeBufferPointer<Int>) -> NSIndexPath in
return NSIndexPath(indexes: buffer.baseAddress, length: buffer.count)
}
let ip1: IndexPath = IndexPath._unconditionallyBridgeFromObjectiveC(nsip1)
XCTAssertEqual(ip1.count, 0)
let ip2: IndexPath = IndexPath._unconditionallyBridgeFromObjectiveC(nsip2)
XCTAssertEqual(ip2.count, 1)
XCTAssertEqual(ip2[0], 1)
let ip3: IndexPath = IndexPath._unconditionallyBridgeFromObjectiveC(nsip3)
XCTAssertEqual(ip3.count, 2)
XCTAssertEqual(ip3[0], 1)
XCTAssertEqual(ip3[1], 2)
let ip4: IndexPath = IndexPath._unconditionallyBridgeFromObjectiveC(nsip4)
XCTAssertEqual(ip4.count, 3)
XCTAssertEqual(ip4[0], 1)
XCTAssertEqual(ip4[1], 2)
XCTAssertEqual(ip4[2], 3)
}
func testObjcBridgeType() {
XCTAssertTrue(IndexPath._getObjectiveCType() == NSIndexPath.self)
}
func test_AnyHashableContainingIndexPath() {
let values: [IndexPath] = [
IndexPath(indexes: [1, 2]),
IndexPath(indexes: [1, 2, 3]),
IndexPath(indexes: [1, 2, 3]),
]
let anyHashables = values.map(AnyHashable.init)
XCTAssert(IndexPath.self == type(of: anyHashables[0].base))
XCTAssert(IndexPath.self == type(of: anyHashables[1].base))
XCTAssert(IndexPath.self == type(of: anyHashables[2].base))
XCTAssertNotEqual(anyHashables[0], anyHashables[1])
XCTAssertEqual(anyHashables[1], anyHashables[2])
}
func test_AnyHashableCreatedFromNSIndexPath() {
let values: [NSIndexPath] = [
NSIndexPath(index: 1),
NSIndexPath(index: 2),
NSIndexPath(index: 2),
]
let anyHashables = values.map(AnyHashable.init)
XCTAssert(IndexPath.self == type(of: anyHashables[0].base))
XCTAssert(IndexPath.self == type(of: anyHashables[1].base))
XCTAssert(IndexPath.self == type(of: anyHashables[2].base))
XCTAssertNotEqual(anyHashables[0], anyHashables[1])
XCTAssertEqual(anyHashables[1], anyHashables[2])
}
func test_unconditionallyBridgeFromObjectiveC() {
XCTAssertEqual(IndexPath(), IndexPath._unconditionallyBridgeFromObjectiveC(nil))
}
func test_slice_1ary() {
let indexPath: IndexPath = [0]
let res = indexPath.dropFirst()
XCTAssertEqual(0, res.count)
let slice = indexPath[1..<1]
XCTAssertEqual(0, slice.count)
}
func test_copy() {
var indexes = [1, 2, 3]
let nip1 = NSIndexPath(indexes: &indexes, length: 3)
let nip2 = nip1
XCTAssertEqual(nip1.length, 3)
XCTAssertEqual(nip2.length, 3)
XCTAssertEqual(nip1, nip2)
}
}