source: EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-range-ext/test/range.rb@ 439

Last change on this file since 439 was 439, checked in by coas-nagasima, 4 years ago

mrubyを2.1.1に更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 4.0 KB
Line 
1##
2# Range(Ext) Test
3
4assert('Range#cover?') do
5 assert_true ("a".."z").cover?("c")
6 assert_true !("a".."z").cover?("5")
7 assert_true ("a".."z").cover?("cc")
8end
9
10assert('Range#first') do
11 assert_equal 10, (10..20).first
12 assert_equal [10, 11, 12], (10..20).first(3)
13
14 skip unless Object.const_defined?(:Float)
15 assert_equal [0, 1, 2], (0..Float::INFINITY).first(3)
16end
17
18assert('Range#last') do
19 assert_equal 20, (10..20).last
20 assert_equal 20, (10...20).last
21 assert_equal [18, 19, 20], (10..20).last(3)
22 assert_equal [17, 18, 19], (10...20).last(3)
23end
24
25assert('Range#size') do
26 assert_equal 42, (1..42).size
27 assert_equal 41, (1...42).size
28 assert_nil ('a'..'z').size
29
30 skip unless Object.const_defined?(:Float)
31 assert_equal 6, (1...6.3).size
32 assert_equal 5, (1...6.0).size
33 assert_equal 5, (1.1...6).size
34 assert_equal 15, (1.0..15.9).size
35 assert_equal Float::INFINITY, (0..Float::INFINITY).size
36end
37
38assert('Range#max') do
39 # returns the maximum value in the range when called with no arguments
40 assert_equal 10, (1..10).max
41 assert_equal 9, (1...10).max
42 assert_equal 536870911, (0...2**29).max
43
44 # returns nil when the endpoint is less than the start point
45 assert_equal nil, (100..10).max
46
47 # returns nil when the endpoint equals the start point and the range is exclusive
48 assert_equal nil, (5...5).max
49
50 # returns the endpoint when the endpoint equals the start point and the range is inclusive
51 assert_equal 5, (5..5).max
52
53 skip unless Object.const_defined?(:Float)
54
55 # returns the maximum value in the Float range when called with no arguments
56 assert_equal 908.1111, (303.20..908.1111).max
57
58 # raises TypeError when called on an exclusive range and a non Integer value
59 assert_raise(TypeError) { (303.20...908.1111).max }
60
61 # returns nil when the endpoint is less than the start point in a Float range
62 assert_equal nil, (3003.20..908.1111).max
63end
64
65assert('Range#max given a block') do
66 # passes each pair of values in the range to the block
67 acc = []
68 (1..10).max do |a, b|
69 acc << a
70 acc << b
71 a
72 end
73 (1..10).each do |value|
74 assert_true acc.include?(value)
75 end
76
77 # passes each pair of elements to the block in reversed order
78 acc = []
79 (1..5).max do |a, b|
80 acc << [a, b]
81 a
82 end
83 assert_equal [[2, 1], [3, 2], [4, 3], [5, 4]], acc
84
85 # returns the element the block determines to be the maximum
86 assert_equal 1, ((1..3).max { |_a, _b| -3 })
87
88 # returns nil when the endpoint is less than the start point
89 assert_equal nil, ((100..10).max { |x, y| x <=> y })
90 assert_equal nil, ((5...5).max { |x, y| x <=> y })
91end
92
93assert('Range#min') do
94 # returns the minimum value in the range when called with no arguments
95 assert_equal 1, (1..10).min
96 assert_equal 1, (1...10).min
97
98 # returns nil when the start point is greater than the endpoint
99 assert_equal nil, (100..10).min
100
101 # returns nil when the endpoint equals the start point and the range is exclusive
102 assert_equal nil, (5...5).max
103
104 # returns the endpoint when the endpoint equals the start point and the range is inclusive
105 assert_equal 5, (5..5).max
106
107 skip unless Object.const_defined?(:Float)
108
109 # returns the minimum value in the Float range when called with no arguments
110 assert_equal 303.20, (303.20..908.1111).min
111
112 # returns nil when the start point is greater than the endpoint in a Float range
113 assert_equal nil, (3003.20..908.1111).max
114end
115
116assert('Range#min given a block') do
117 # passes each pair of values in the range to the block
118 acc = []
119 (1..10).min do |a, b|
120 acc << a
121 acc << b
122 a
123 end
124 (1..10).each do |value|
125 assert_true acc.include?(value)
126 end
127
128 # passes each pair of elements to the block in reversed order
129 acc = []
130 (1..5).min do |a, b|
131 acc << [a, b]
132 a
133 end
134 assert_equal [[2, 1], [3, 1], [4, 1], [5, 1]], acc
135
136 # returns the element the block determines to be the minimum
137 assert_equal 3, ((1..3).min { |_a, _b| -3 })
138
139 # returns nil when the start point is greater than the endpoint
140 assert_equal nil, ((100..10).min { |x, y| x <=> y })
141 assert_equal nil, ((5...5).min { |x, y| x <=> y })
142end
Note: See TracBrowser for help on using the repository browser.