1 require 'rubicon_testcase'
3 class TestRange < RubiconTestCase
5 ############################################################
9 # Tests where the methods == and eql? behave the same.
10 # This is true when the class of the endpoints of
11 # the Range have == and eql? methods that are
12 # aliases. This in turn is true for most classes,
13 # for example for the class used here: Fixnum.
15 # TODO: write tests using an endpoint class where
16 # the methods == and eql? differ.
18 def util_test_equals(method)
25 rx25 = Range.new(2, 5, true)
26 rx34 = Range.new(3, 4, true)
27 rx35 = Range.new(3, 5, true)
28 rx36 = Range.new(3, 6, true)
29 rx45 = Range.new(4, 5, true)
32 assert_equal(false, r35.send(method, r34))
33 assert_equal(true, r35.send(method, r35))
34 assert_equal(false, r35.send(method, r36))
36 assert_equal(false, r35.send(method, r25))
37 assert_equal(false, r35.send(method, r45))
40 assert_equal(false, rx35.send(method, rx34))
41 assert_equal(true, rx35.send(method, rx35))
42 assert_equal(false, rx35.send(method, rx36))
44 assert_equal(false, rx35.send(method, rx25))
45 assert_equal(false, rx35.send(method, rx45))
47 # half-open / closed interval: never equal
48 assert_equal(false, rx35.send(method, r34))
49 assert_equal(false, rx35.send(method, r35))
50 assert_equal(false, rx35.send(method, r36))
52 assert_equal(false, rx35.send(method, r25))
53 assert_equal(false, rx35.send(method, r45))
55 # closed / half-open interval: never equal
56 assert_equal(false, r35.send(method, rx34))
57 assert_equal(false, r35.send(method, rx35))
58 assert_equal(false, r35.send(method, rx36))
60 assert_equal(false, r35.send(method, rx25))
61 assert_equal(false, r35.send(method, rx45))
64 assert_equal(false, r35.send(method, Object.new))
65 assert_equal(false, rx35.send(method, Object.new))
69 def util_test_end(msg)
70 assert_equal(10, Range.new(1, 10).send(msg))
71 assert_equal(11, Range.new(1, 11, true).send(msg))
72 assert_equal("z", Range.new("a", "z").send(msg))
73 assert_equal("A", Range.new("a", "A", true).send(msg))
76 def util_member(method)
77 is = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
78 xs = is.map {|x| 0.5 + x}
79 iss = ["aj", "ak", "al", "am", "an" ,"ao" "ap"]
80 xss = ["ajjj", "akkk", "alll", "ammm", "annn" ,"aooo" "appp"]
83 rx47 = Range.new(4, 7, true)
84 r_akam = Range.new("ak", "am")
85 rx_akam = Range.new("ak", "am", true)
88 assert_equal([4, 5, 6, 7 ], is.select {|i| r47.send(method, i) })
89 assert_equal([4, 5, 6], is.select {|i| rx47.send(method, i) })
91 assert_equal(["ak","al","am"], iss.select {|i| r_akam.send(method, i) })
92 assert_equal(["ak","al"], iss.select {|i| rx_akam.send(method, i) })
95 assert_equal([4.5, 5.5, 6.5], xs.select {|x| r47.send(method, x) })
96 assert_equal([4.5, 5.5, 6.5], xs.select {|x| rx47.send(method, x) })
98 assert_equal(["akkk","alll"], xss.select {|i| r_akam.send(method, i) })
99 assert_equal(["akkk","alll"], xss.select {|i| rx_akam.send(method, i) })
101 # non-comparable argument
102 assert_equal(false, Range.new(5, 10) === Object.new)
103 assert_equal(false, Range.new(5, 10, true) === Object.new)
106 def util_step_tester(acc_facit, range, *step_args)
108 res = range.step(*step_args) {|x| acc << x }
109 assert_equal(acc_facit, acc)
110 assert_same(range, res)
113 def util_each(first, last, exclude, expected)
116 Range.new(first, last, exclude).each do |x|
117 assert_equal(index, x)
121 assert_equal(expected, count)
124 ############################################################
127 # TODO: need to test new/initialize, esp w/ bad values
130 assert_equal(1, Range.new(1, 10).begin)
131 assert_equal("a", Range.new("a", "z").begin)
132 assert_equal(1, Range.new(1, 10, true).begin)
133 assert_equal("a", Range.new("a", "z", true).begin)
137 util_each(1, 10, false, 10)
138 util_each(1, 10, true, 9)
140 util_each("A", "J", false, 10)
141 util_each("A", "J", true, 9)
143 # TODO: test something that has a .succ, but is neither int nor string
146 util_each(t1, t10, false, 10)
147 util_each(t1, t10, true, 9)
149 # test something that has no .succ:
151 # util_each(Object.new, Object.new, false, 10)
152 # util_each(Object.new, Object.new, true, 9)
160 util_test_equals(:eql?)
163 def test_equals2 # '=='
164 util_test_equals(:==)
167 def test_equals3 # '==='
173 when Range.new(0, 49)
174 fail("Shouldn't have matched")
175 when Range.new(50, 75)
178 fail("Shouldn't have matched")
180 assert_equal(true,gotit)
184 when Range.new(0, 49)
185 fail("Shouldn't have matched")
186 when Range.new(50, 75)
189 fail("Shouldn't have matched")
191 assert_equal(true,gotit)
195 when Range.new(0, 49)
196 fail("Shouldn't have matched")
197 when Range.new(50, 75)
200 fail("Shouldn't have matched")
202 assert_equal(true,gotit)
205 def test_exclude_end_eh
206 assert_equal(true, Range.new(1, 10, true).exclude_end?)
207 assert_equal(false,Range.new(1, 10).exclude_end?)
208 assert_equal(true, Range.new("A", "Z", true).exclude_end?)
209 assert_equal(false,Range.new("A", "Z").exclude_end?)
213 assert_equal(1, Range.new(1, 10).first)
214 assert_equal("a", Range.new("a", "z").first)
215 assert_equal(1, Range.new(1, 10, true).first)
216 assert_equal("a", Range.new("a", "z", true).first)
220 assert_equal(Range.new(5, 9).hash, Range.new(5, 9).hash)
221 assert_equal(Range.new("A", "Z").hash, Range.new("A", "Z").hash)
223 assert_equal(Range.new(5, 9, true).hash, Range.new(5, 9, true).hash)
224 assert_equal(Range.new("A", "Z", true).hash, Range.new("A", "Z", true).hash)
226 assert_not_equal(Range.new(5, 9).hash, Range.new(5, 9, true).hash)
227 assert_not_equal(Range.new("A", "Z").hash, Range.new("A", "Z", true).hash)
229 assert_not_equal(Range.new(5, 9).hash, Range.new(5, 8).hash)
230 assert_not_equal(Range.new("A", "Z").hash, Range.new("a", "Z", true).hash)
234 util_member(:include?)
238 assert_equal('1..10', Range.new(1, 10).inspect)
239 assert_equal('1...10', Range.new(1, 10, true).inspect)
240 assert_equal('"a".."z"', Range.new('a', 'z').inspect)
241 assert_equal('"a"..."z"', Range.new('a', 'z', true).inspect)
249 util_member(:member?)
253 # n=1 default in step(n)
254 util_step_tester([5,6,7,8,9], Range.new(5, 9))
255 util_step_tester([5,6,7,8], Range.new(5, 9, true))
258 util_step_tester([5,6,7,8,9], Range.new(5, 9), 1)
259 util_step_tester([5,6,7,8], Range.new(5, 9, true), 1)
262 util_step_tester([5,7,9], Range.new(5, 9), 2)
263 util_step_tester([5,7], Range.new(5, 9, true), 2)
266 util_step_tester([5,8], Range.new(5, 9), 3)
267 util_step_tester([5,8], Range.new(5, 9, true), 3)
270 util_step_tester([5,9], Range.new(5, 9), 4)
271 util_step_tester([5], Range.new(5, 9, true), 4)
275 assert_equal('1..10', Range.new(1, 10).to_s)
276 assert_equal('1...10', Range.new(1, 10, true).to_s)
277 assert_equal('a..z', Range.new('a', 'z').to_s)
278 assert_equal('a...z', Range.new('a', 'z', true).to_s)