Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 83 lines (63 sloc) 1.599 kB
db9f1ed @clarkware Initial import
authored
1 #!/usr/bin/env ruby
2
3 require 'test/unit'
4
5 class RangeTest < Test::Unit::TestCase
6
7 def test_inclusive
8 range = 0..10
9 assert_equal(true, range.include?(0))
10 assert_equal(true, range.include?(10))
11 assert_equal(false, range.include?(11))
12 end
13
14 def test_exclusive
15 range = 0...10
16 assert_equal(true, range.include?(0))
17 assert_equal(false, range.include?(10))
18 end
19
20 def test_min_and_max
21 range = 0..10
22 assert_equal(0, range.min)
23 assert_equal(10, range.max)
24 end
25
26 def test_to_array
27 assert_equal([0, 1, 2], (0..2).to_a)
28 assert_equal([0, 1], (0...2).to_a)
29 end
30
31 def test_reject
32 range = 0..10
33 accepted = range.reject { |i| i > 2 }
34 assert_equal([0, 1, 2], accepted)
35 end
36
37 def test_each
38 range = 0..3
39 result = []
40 range.each { |i| result << i }
41 assert_equal([0, 1, 2, 3], result)
42 end
43
44 def test_interval
45 range = 0..3
46 assert_equal(true, range.member?(3))
47 assert_equal(true, range === 3)
48
49 range = 0...3
50 assert_equal(false, range === 3)
51 assert_equal(false, range.member?(3))
52 end
53
54 def test_range_of_objects
55 medium = Volume.new(4)..Volume.new(7)
56 assert_equal(false, medium.include?(Volume.new(3)))
57 assert_equal(true, medium.include?(Volume.new(5)))
58 end
59
60 end
61
62 class Volume
63
64 include Comparable
65
66 attr :volume
67
68 def initialize(volume) # 0..9
69 @volume = volume
70 end
71
72 # Support for ranges
73
74 def <=>(other)
75 self.volume <=> other.volume
76 end
77
78 def succ
79 raise(IndexError, "Volume too big") if @volume >= 9
80 Volume.new(@volume.succ)
81 end
82 end
Something went wrong with that request. Please try again.