source: EcnlProtoTool/trunk/mruby-1.2.0/test/t/integer.rb@ 270

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

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby
File size: 4.4 KB
Line 
1##
2# Integer ISO Test
3
4assert('Integer', '15.2.8') do
5 assert_equal Class, Integer.class
6end
7
8assert('Integer#+', '15.2.8.3.1') do
9 a = 1+1
10 b = 1+1.0
11
12 assert_equal 2, a
13 assert_equal 2.0, b
14
15 assert_raise(TypeError){ 0+nil }
16 assert_raise(TypeError){ 1+nil }
17
18 c = Mrbtest::FIXNUM_MAX + 1
19 d = Mrbtest::FIXNUM_MAX.__send__(:+, 1)
20 e = Mrbtest::FIXNUM_MAX + 1.0
21 assert_equal Float, c.class
22 assert_equal Float, d.class
23 assert_float e, c
24 assert_float e, d
25end
26
27assert('Integer#-', '15.2.8.3.2') do
28 a = 2-1
29 b = 2-1.0
30
31 assert_equal 1, a
32 assert_equal 1.0, b
33
34 c = Mrbtest::FIXNUM_MIN - 1
35 d = Mrbtest::FIXNUM_MIN.__send__(:-, 1)
36 e = Mrbtest::FIXNUM_MIN - 1.0
37 assert_equal Float, c.class
38 assert_equal Float, d.class
39 assert_float e, c
40 assert_float e, d
41end
42
43assert('Integer#*', '15.2.8.3.3') do
44 a = 1*1
45 b = 1*1.0
46
47 assert_equal 1, a
48 assert_equal 1.0, b
49
50 assert_raise(TypeError){ 0*nil }
51 assert_raise(TypeError){ 1*nil }
52
53 c = Mrbtest::FIXNUM_MAX * 2
54 d = Mrbtest::FIXNUM_MAX.__send__(:*, 2)
55 e = Mrbtest::FIXNUM_MAX * 2.0
56 assert_equal Float, c.class
57 assert_equal Float, d.class
58 assert_float e, c
59 assert_float e, d
60end
61
62assert('Integer#/', '15.2.8.3.4') do
63 a = 2/1
64 b = 2/1.0
65
66 assert_equal 2, a
67 assert_equal 2.0, b
68end
69
70assert('Integer#%', '15.2.8.3.5') do
71 a = 1%1
72 b = 1%1.0
73 c = 2%4
74 d = 2%5
75 e = 2%-5
76 f = -2%5
77 g = -2%-5
78 h = 2%-2
79 i = -2%2
80 j = -2%-2
81
82 assert_equal 0, a
83 assert_equal 0.0, b
84 assert_equal 2, c
85 assert_equal 2, d
86 assert_equal(-3, e)
87 assert_equal 3, f
88 assert_equal(-2, g)
89 assert_equal 0, h
90 assert_equal 0, i
91 assert_equal 0, j
92end
93
94assert('Integer#<=>', '15.2.9.3.6') do
95 a = 1<=>0
96 b = 1<=>1
97 c = 1<=>2
98
99 assert_equal 1, a
100 assert_equal 0, b
101 assert_equal(-1, c)
102end
103
104assert('Integer#==', '15.2.8.3.7') do
105 a = 1==0
106 b = 1==1
107
108 assert_false a
109 assert_true b
110end
111
112assert('Integer#~', '15.2.8.3.8') do
113 # Complement
114 assert_equal(-1, ~0)
115 assert_equal(-3, ~2)
116end
117
118assert('Integer#&', '15.2.8.3.9') do
119 # Bitwise AND
120 # 0101 (5)
121 # & 0011 (3)
122 # = 0001 (1)
123 assert_equal 1, 5 & 3
124end
125
126assert('Integer#|', '15.2.8.3.10') do
127 # Bitwise OR
128 # 0101 (5)
129 # | 0011 (3)
130 # = 0111 (7)
131 assert_equal 7, 5 | 3
132end
133
134assert('Integer#^', '15.2.8.3.11') do
135 # Bitwise XOR
136 # 0101 (5)
137 # ^ 0011 (3)
138 # = 0110 (6)
139 assert_equal 6, 5 ^ 3
140end
141
142assert('Integer#<<', '15.2.8.3.12') do
143 # Left Shift by one
144 # 00010111 (23)
145 # = 00101110 (46)
146 assert_equal 46, 23 << 1
147
148 # Left Shift by a negative is Right Shift
149 assert_equal 23, 46 << -1
150end
151
152assert('Integer#>>', '15.2.8.3.13') do
153 # Right Shift by one
154 # 00101110 (46)
155 # = 00010111 (23)
156 assert_equal 23, 46 >> 1
157
158 # Right Shift by a negative is Left Shift
159 assert_equal 46, 23 >> -1
160
161 # Don't raise on large Right Shift
162 assert_equal 0, 23 >> 128
163end
164
165assert('Integer#ceil', '15.2.8.3.14') do
166 assert_equal 10, 10.ceil
167end
168
169assert('Integer#downto', '15.2.8.3.15') do
170 a = 0
171 3.downto(1) do |i|
172 a += i
173 end
174 assert_equal 6, a
175end
176
177assert('Integer#eql?', '15.2.8.3.16') do
178 a = 1.eql?(1)
179 b = 1.eql?(2)
180 c = 1.eql?(nil)
181
182 assert_true a
183 assert_false b
184 assert_false c
185end
186
187assert('Integer#floor', '15.2.8.3.17') do
188 a = 1.floor
189
190 assert_equal 1, a
191end
192
193assert('Integer#next', '15.2.8.3.19') do
194 assert_equal 2, 1.next
195end
196
197assert('Integer#round', '15.2.8.3.20') do
198 assert_equal 1, 1.round
199end
200
201assert('Integer#succ', '15.2.8.3.21') do
202 assert_equal 2, 1.succ
203end
204
205assert('Integer#times', '15.2.8.3.22') do
206 a = 0
207 3.times do
208 a += 1
209 end
210 assert_equal 3, a
211end
212
213assert('Integer#to_f', '15.2.8.3.23') do
214 assert_equal 1.0, 1.to_f
215end
216
217assert('Integer#to_i', '15.2.8.3.24') do
218 assert_equal 1, 1.to_i
219end
220
221assert('Integer#to_s', '15.2.8.3.25') do
222 assert_equal '1', 1.to_s
223 assert_equal("-1", -1.to_s)
224end
225
226assert('Integer#truncate', '15.2.8.3.26') do
227 assert_equal 1, 1.truncate
228end
229
230assert('Integer#upto', '15.2.8.3.27') do
231 a = 0
232 1.upto(3) do |i|
233 a += i
234 end
235 assert_equal 6, a
236end
237
238assert('Integer#divmod', '15.2.8.3.30') do
239 assert_equal [ 0, 0], 0.divmod(1)
240 assert_equal [ 0, 1], 1.divmod(3)
241 assert_equal [ 3, 0], 3.divmod(1)
242 assert_equal [ 2, 6], 20.divmod(7)
243 assert_equal [-1, 2], -3.divmod(5)
244 assert_equal [-2, -1], 25.divmod(-13)
245 assert_equal [ 1, -6], -13.divmod(-7)
246end
247
248# Not ISO specified
249
250assert('Integer#step') do
251 a = []
252 b = []
253 1.step(3) do |i|
254 a << i
255 end
256 1.step(6, 2) do |i|
257 b << i
258 end
259
260 assert_equal [1, 2, 3], a
261 assert_equal [1, 3, 5], b
262end
Note: See TracBrowser for help on using the repository browser.