1 UnitTests addPrototype: #Range derivedFrom: {TestCase}.
3 tc@(UnitTests Range traits) testInclusion1
4 "Verify that all elements of an Range are found in that Range."
6 range: (25 to: 50 by: 1).
7 tc assert: (range allSatisfy: [| :item | range includes: item]).
10 tc@(UnitTests Range traits) testInclusion2
11 "Same as testInclusion1, but for a downward Range."
13 range: (-1 to: -33 by: -1).
14 tc assert: (range allSatisfy: [| :item | range includes: item]).
17 tc@(UnitTests Range traits) testInclusion3
18 "Verify that elements that are in range2, but not in range, are detected as missing in
20 [| range range2 found notFound |
21 range: (25 to: 50 by: 1).
25 [| :idx | (range includes: idx)
26 ifTrue: [found: found + 1]
27 ifFalse: [notFound: notFound + 1]].
28 tc assert: found = range size.
29 tc assert: range2 size - range size = notFound.
32 tc@(UnitTests Range traits) testInclusion4
33 "This test additionally verifies that elements that are in range2, but not in range,
34 are detected as missing in range."
35 [| range range2 found notFound |
36 range: (-25 to: -50 by: -1).
37 range2: (0 to: -100 by: -1).
40 [| :idx | (range includes: idx)
41 ifTrue: [found: found + 1]
42 ifFalse: [notFound: notFound + 1]].
43 tc assert: found = range size.
44 tc assert: range2 size - range size = notFound.
47 tc@(UnitTests Range traits) testIndex1
49 range: (25 to: 50 by: 1).
50 tc assert: (range allSatisfy: [| :item | (range at: (range indexOf: item)) = item]).
51 "Now we do it a second time (to avoid the use of Range at:) "
52 array: (range as: {}).
53 tc assert: (range allSatisfy: [| :item | (array at: (range indexOf: item)) = item]).
56 tc@(UnitTests Range traits) testIndex2
58 range: (-25 to: -50 by: -1).
59 tc assert: (range allSatisfy: [| :item | (range at: (range indexOf: item)) = item]).
60 "Now we do it a second time (to avoid the use of Range at:) "
61 array: (range as: {}).
62 tc assert: (range allSatisfy: [| :item | (array at: (range indexOf: item)) = item]).
65 tc@(UnitTests Range traits) testIndex3
66 "For suitably chosen displacements, a shifted range is disjunct from the unshifted one."
68 range: (-25 to: -50 by: -5).
70 [| :shift | tc assert: (range + shift allSatisfy: [| :item | (range indexOf: item) = Nil])].
73 tc@(UnitTests Range traits) testInclusionFloat
74 [| start stop step candidate factor range1 range2 |
78 factor: 100000000000000.
80 range1: (start to: stop by: 1).
81 range2: (start * factor to: stop * factor by: step * factor).
82 tc assert: (range1 includes: candidate) not.
83 tc assert: (range2 includes: candidate * factor) not.
86 tc@(UnitTests Range traits) testIndexFloat
87 "Complements testInclusionFloat."
88 [| start stop step candidate missingCandidate factor range1 range2 |
92 factor: 100000000000000.
94 missingCandidate: 2.5.
95 range1: (start to: stop by: step).
96 range2: (start * factor to: stop * factor by: step * factor).
97 tc assert: (range1 indexOf: candidate) = 1.
98 tc assert: (range2 indexOf: candidate * factor) = 1.
99 tc assert: (range1 indexOf: missingCandidate) = Nil.
100 tc assert: (range2 indexOf: missingCandidate * factor) = Nil.
103 tc@(UnitTests Range traits) testInclusionNonFloat
104 "The example is similar to testInclusionFloat, but it avoids the use of a Float.
105 The elements of the Ranges are now Integers and Fractions."
106 [| start stop step candidate factor range1 range2 |
110 factor: 100000000000000.
112 range1: (start to: stop by: 1).
113 range2: (start * factor to: stop * factor by: step * factor).
114 tc assert: (range1 includes: candidate) not.
115 tc assert: (range2 includes: candidate * factor) not.
118 t@(UnitTests Range traits) suite
119 [t suiteForSelectors: {
120 #testInclusionNonFloat.