|
4 | 4 | ;; Copyright (c) 2007 Tim Burks, Radtastical Inc.
|
5 | 5 |
|
6 | 6 | (class TestArray is NuTestCase
|
7 |
| - |
8 |
| - (- testCreate is |
9 |
| - (set a (NSMutableArray arrayWithList:'(1 2))) |
10 |
| - (a << "three") |
11 |
| - (assert_equal 3 (a count)) |
12 |
| - (assert_equal 2 (a 1)) |
13 |
| - (assert_equal "three" (a 2))) |
14 |
| - |
15 |
| - (- testEach is |
16 |
| - (set i 0) |
17 |
| - (set a (array 0 1 2)) |
18 |
| - (a each: |
19 |
| - (do (x) |
20 |
| - (assert_equal i x) |
21 |
| - (set i (+ i 1)))) |
22 |
| - ;; iteration with break |
23 |
| - (set a (array 0 1 2 3 4 5 6)) |
24 |
| - (set sum 0) |
25 |
| - (a each: |
26 |
| - (do (x) |
27 |
| - (if (eq x 4) (break)) |
28 |
| - (set sum (+ sum x)))) |
29 |
| - (assert_equal 6 sum) |
30 |
| - ;; iteration with continue |
31 |
| - (set a (array 0 1 2 3 4 5 6)) |
32 |
| - (set sum 0) |
33 |
| - (a each: |
34 |
| - (do (x) |
35 |
| - (if (eq x 4) (continue)) |
36 |
| - (set sum (+ sum x)))) |
37 |
| - (assert_equal 17 sum)) |
38 |
| - |
39 |
| - (- testEachInReverse is |
40 |
| - (set i 0) |
41 |
| - (set a (array 2 1 0)) |
42 |
| - (a eachInReverse: |
43 |
| - (do (x) |
44 |
| - (assert_equal i x) |
45 |
| - (set i (+ i 1)))) |
46 |
| - ;; iteration with break |
47 |
| - (set a (array 6 5 4 3 2 1 0)) |
48 |
| - (set sum 0) |
49 |
| - (a eachInReverse: |
50 |
| - (do (x) |
51 |
| - (if (eq x 4) (break)) |
52 |
| - (set sum (+ sum x)))) |
53 |
| - (assert_equal 6 sum) |
54 |
| - ;; iteration with continue |
55 |
| - (set a (array 6 5 4 3 2 1 0)) |
56 |
| - (set sum 0) |
57 |
| - (a eachInReverse: |
58 |
| - (do (x) |
59 |
| - (if (eq x 4) (continue)) |
60 |
| - (set sum (+ sum x)))) |
61 |
| - (assert_equal 17 sum)) |
62 |
| - |
63 |
| - (- testEachWithIndex is |
64 |
| - (set i 0) |
65 |
| - (set a (array "zero" "one" "two")) |
66 |
| - (a eachWithIndex: |
67 |
| - (do (value index) |
68 |
| - (assert_equal i index) |
69 |
| - (set i (+ i 1)))) |
70 |
| - ;; iteration with break |
71 |
| - (set a (array "zero" "one" "two" "three" "four" "five" "six")) |
72 |
| - (set sum 0) |
73 |
| - (a eachWithIndex: |
74 |
| - (do (value index) |
75 |
| - (if (eq index 4) (break)) |
76 |
| - (set sum (+ sum index)))) |
77 |
| - (assert_equal 6 sum) |
78 |
| - ;; iteration with continue |
79 |
| - (set a (array "zero" "one" "two" "three" "four" "five" "six")) |
80 |
| - (set sum 0) |
81 |
| - (a eachWithIndex: |
82 |
| - (do (value index) |
83 |
| - (if (eq index 4) (continue)) |
84 |
| - (set sum (+ sum index)))) |
85 |
| - (assert_equal 17 sum)) |
86 |
| - |
87 |
| - (- testSortedArrayUsingBlock is |
88 |
| - (set array (NSArray arrayWithList:(list 9 42 37 1 17 30 11 28))) |
89 |
| - (set sorted (array sortedArrayUsingBlock:(do (a b) (b compare:a)))) |
90 |
| - (assert_equal '(42 37 30 28 17 11 9 1) (sorted list)) |
91 |
| - (set array (NSArray arrayWithList:(list "mary" "christopher" "ed" "brian" "tim" "jennifer"))) |
92 |
| - (set sorted (array sortedArrayUsingBlock:(do (a b) ((a length) compare:(b length))))) |
93 |
| - (assert_equal '("ed" "tim" "mary" "brian" "jennifer" "christopher") (sorted list))) |
94 |
| - |
95 |
| - (- testSortedArrayUsingSelector is |
96 |
| - ;; I don't like this, but want to be sure we bridge the right |
97 |
| - ;; return type for the comparison method. On Snow Leopard (at |
98 |
| - ;; least), NSInteger is either int or long depending on the |
99 |
| - ;; build architecture. |
100 |
| - (case ((NSNumber instanceMethodWithName:"compare:") returnType) |
101 |
| - ("i" (class NSObject (- (int) reverseCompare:(id) other is (* -1 (self compare:other))))) |
102 |
| - ("q" (class NSObject (- (long) reverseCompare:(id) other is (* -1 (self compare:other)))))) |
103 |
| - (set a (array 1 9 2 8 3 7 4 6 5)) |
104 |
| - (set sorted (a sortedArrayUsingSelector:"reverseCompare:")) |
105 |
| - (assert_equal (array 9 8 7 6 5 4 3 2 1) sorted) |
106 |
| - ;; this is better. We directly support NSComparisonResult in declarations. |
107 |
| - (class NSObject (- (NSComparisonResult) reverseCompare2:(id) other is (* -1 (self compare:other)))) |
108 |
| - (set sorted2 (a sortedArrayUsingSelector:"reverseCompare2:")) |
109 |
| - (assert_equal (array 9 8 7 6 5 4 3 2 1) sorted2)) |
110 |
| - |
111 |
| - (- testIndexing is |
112 |
| - (set a (array 1 2 3)) |
113 |
| - (assert_equal 1 (a 0)) |
114 |
| - (assert_equal 2 (a 1)) |
115 |
| - (assert_equal 3 (a (+ 1 1))) |
116 |
| - (assert_equal 3 (a -1)) |
117 |
| - (assert_equal 2 (a -2)) |
118 |
| - (assert_equal 1 (a -3)) |
119 |
| - (assert_equal nil (a 3)) |
120 |
| - (assert_equal nil (a -4)))) |
| 7 | + |
| 8 | + (- testCreate is |
| 9 | + (set a (NSMutableArray arrayWithList:'(1 2))) |
| 10 | + (a << "three") |
| 11 | + (assert_equal 3 (a count)) |
| 12 | + (assert_equal 2 (a 1)) |
| 13 | + (assert_equal "three" (a 2))) |
| 14 | + |
| 15 | + (- testEach is |
| 16 | + (set i 0) |
| 17 | + (set a (array 0 1 2)) |
| 18 | + (a each: |
| 19 | + (do (x) |
| 20 | + (assert_equal i x) |
| 21 | + (set i (+ i 1)))) |
| 22 | + ;; iteration with break |
| 23 | + (set a (array 0 1 2 3 4 5 6)) |
| 24 | + (set sum 0) |
| 25 | + (a each: |
| 26 | + (do (x) |
| 27 | + (if (eq x 4) (break)) |
| 28 | + (set sum (+ sum x)))) |
| 29 | + (assert_equal 6 sum) |
| 30 | + ;; iteration with continue |
| 31 | + (set a (array 0 1 2 3 4 5 6)) |
| 32 | + (set sum 0) |
| 33 | + (a each: |
| 34 | + (do (x) |
| 35 | + (if (eq x 4) (continue)) |
| 36 | + (set sum (+ sum x)))) |
| 37 | + (assert_equal 17 sum)) |
| 38 | + |
| 39 | + (- testEachInReverse is |
| 40 | + (set i 0) |
| 41 | + (set a (array 2 1 0)) |
| 42 | + (a eachInReverse: |
| 43 | + (do (x) |
| 44 | + (assert_equal i x) |
| 45 | + (set i (+ i 1)))) |
| 46 | + ;; iteration with break |
| 47 | + (set a (array 6 5 4 3 2 1 0)) |
| 48 | + (set sum 0) |
| 49 | + (a eachInReverse: |
| 50 | + (do (x) |
| 51 | + (if (eq x 4) (break)) |
| 52 | + (set sum (+ sum x)))) |
| 53 | + (assert_equal 6 sum) |
| 54 | + ;; iteration with continue |
| 55 | + (set a (array 6 5 4 3 2 1 0)) |
| 56 | + (set sum 0) |
| 57 | + (a eachInReverse: |
| 58 | + (do (x) |
| 59 | + (if (eq x 4) (continue)) |
| 60 | + (set sum (+ sum x)))) |
| 61 | + (assert_equal 17 sum)) |
| 62 | + |
| 63 | + (- testEachWithIndex is |
| 64 | + (set i 0) |
| 65 | + (set a (array "zero" "one" "two")) |
| 66 | + (a eachWithIndex: |
| 67 | + (do (value index) |
| 68 | + (assert_equal i index) |
| 69 | + (set i (+ i 1)))) |
| 70 | + ;; iteration with break |
| 71 | + (set a (array "zero" "one" "two" "three" "four" "five" "six")) |
| 72 | + (set sum 0) |
| 73 | + (a eachWithIndex: |
| 74 | + (do (value index) |
| 75 | + (if (eq index 4) (break)) |
| 76 | + (set sum (+ sum index)))) |
| 77 | + (assert_equal 6 sum) |
| 78 | + ;; iteration with continue |
| 79 | + (set a (array "zero" "one" "two" "three" "four" "five" "six")) |
| 80 | + (set sum 0) |
| 81 | + (a eachWithIndex: |
| 82 | + (do (value index) |
| 83 | + (if (eq index 4) (continue)) |
| 84 | + (set sum (+ sum index)))) |
| 85 | + (assert_equal 17 sum)) |
| 86 | + |
| 87 | + (- testSortedArrayUsingBlock is |
| 88 | + (set array (NSArray arrayWithList:(list 9 42 37 1 17 30 11 28))) |
| 89 | + (set sorted (array sortedArrayUsingBlock:(do (a b) (b compare:a)))) |
| 90 | + (assert_equal '(42 37 30 28 17 11 9 1) (sorted list)) |
| 91 | + (set array (NSArray arrayWithList:(list "mary" "christopher" "ed" "brian" "tim" "jennifer"))) |
| 92 | + (set sorted (array sortedArrayUsingBlock:(do (a b) ((a length) compare:(b length))))) |
| 93 | + (assert_equal '("ed" "tim" "mary" "brian" "jennifer" "christopher") (sorted list))) |
| 94 | + |
| 95 | + (- testSortUsingBlock is |
| 96 | + (set array1 (NSArray arrayWithList:(list 9 42 37 1 17 30 11 28))) |
| 97 | + (array1 sortUsingBlock:(do (a b) (b compare:a))) |
| 98 | + (assert_equal '(42 37 30 28 17 11 9 1) (array1 list)) |
| 99 | + (set array2 (NSArray arrayWithList:(list "mary" "christopher" "ed" "brian" "tim" "jennifer"))) |
| 100 | + (array2 sortUsingBlock:(do (a b) ((a length) compare:(b length)))) |
| 101 | + (assert_equal '("ed" "tim" "mary" "brian" "jennifer" "christopher") (array2 list))) |
| 102 | + |
| 103 | + (- testSortedArrayUsingSelector is |
| 104 | + ;; I don't like this, but want to be sure we bridge the right |
| 105 | + ;; return type for the comparison method. On Snow Leopard (at |
| 106 | + ;; least), NSInteger is either int or long depending on the |
| 107 | + ;; build architecture. |
| 108 | + (case ((NSNumber instanceMethodWithName:"compare:") returnType) |
| 109 | + ("i" (class NSObject (- (int) reverseCompare:(id) other is (* -1 (self compare:other))))) |
| 110 | + ("q" (class NSObject (- (long) reverseCompare:(id) other is (* -1 (self compare:other)))))) |
| 111 | + (set a (array 1 9 2 8 3 7 4 6 5)) |
| 112 | + (set sorted (a sortedArrayUsingSelector:"reverseCompare:")) |
| 113 | + (assert_equal (array 9 8 7 6 5 4 3 2 1) sorted) |
| 114 | + ;; this is better. We directly support NSComparisonResult in declarations. |
| 115 | + (class NSObject (- (NSComparisonResult) reverseCompare2:(id) other is (* -1 (self compare:other)))) |
| 116 | + (set sorted2 (a sortedArrayUsingSelector:"reverseCompare2:")) |
| 117 | + (assert_equal (array 9 8 7 6 5 4 3 2 1) sorted2)) |
| 118 | + |
| 119 | + (- testIndexing is |
| 120 | + (set a (array 1 2 3)) |
| 121 | + (assert_equal 1 (a 0)) |
| 122 | + (assert_equal 2 (a 1)) |
| 123 | + (assert_equal 3 (a (+ 1 1))) |
| 124 | + (assert_equal 3 (a -1)) |
| 125 | + (assert_equal 2 (a -2)) |
| 126 | + (assert_equal 1 (a -3)) |
| 127 | + (assert_equal nil (a 3)) |
| 128 | + (assert_equal nil (a -4)))) |
0 commit comments