source: EcnlProtoTool/trunk/mruby-1.3.0/test/t/integer.rb@ 331

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

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 4.6 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
150
151 # Left Shift by 31 is bitShift overflow to SignedInt
152 assert_equal 2147483648, 1 << 31
153
154 # -3 Left Shift by 30 is bitShift overflow to SignedInt
155 assert_equal(-3221225472, -3 << 30)
156end
157
158assert('Integer#>>', '15.2.8.3.13') do
159 # Right Shift by one
160 # 00101110 (46)
161 # = 00010111 (23)
162 assert_equal 23, 46 >> 1
163
164 # Right Shift by a negative is Left Shift
165 assert_equal 46, 23 >> -1
166
167 # Don't raise on large Right Shift
168 assert_equal 0, 23 >> 128
169end
170
171assert('Integer#ceil', '15.2.8.3.14') do
172 assert_equal 10, 10.ceil
173end
174
175assert('Integer#downto', '15.2.8.3.15') do
176 a = 0
177 3.downto(1) do |i|
178 a += i
179 end
180 assert_equal 6, a
181end
182
183assert('Integer#eql?', '15.2.8.3.16') do
184 a = 1.eql?(1)
185 b = 1.eql?(2)
186 c = 1.eql?(nil)
187
188 assert_true a
189 assert_false b
190 assert_false c
191end
192
193assert('Integer#floor', '15.2.8.3.17') do
194 a = 1.floor
195
196 assert_equal 1, a
197end
198
199assert('Integer#next', '15.2.8.3.19') do
200 assert_equal 2, 1.next
201end
202
203assert('Integer#round', '15.2.8.3.20') do
204 assert_equal 1, 1.round
205end
206
207assert('Integer#succ', '15.2.8.3.21') do
208 assert_equal 2, 1.succ
209end
210
211assert('Integer#times', '15.2.8.3.22') do
212 a = 0
213 3.times do
214 a += 1
215 end
216 assert_equal 3, a
217end
218
219assert('Integer#to_f', '15.2.8.3.23') do
220 assert_equal 1.0, 1.to_f
221end
222
223assert('Integer#to_i', '15.2.8.3.24') do
224 assert_equal 1, 1.to_i
225end
226
227assert('Integer#to_s', '15.2.8.3.25') do
228 assert_equal '1', 1.to_s
229 assert_equal("-1", -1.to_s)
230end
231
232assert('Integer#truncate', '15.2.8.3.26') do
233 assert_equal 1, 1.truncate
234end
235
236assert('Integer#upto', '15.2.8.3.27') do
237 a = 0
238 1.upto(3) do |i|
239 a += i
240 end
241 assert_equal 6, a
242end
243
244assert('Integer#divmod', '15.2.8.3.30') do
245 assert_equal [ 0, 0], 0.divmod(1)
246 assert_equal [ 0, 1], 1.divmod(3)
247 assert_equal [ 3, 0], 3.divmod(1)
248 assert_equal [ 2, 6], 20.divmod(7)
249 assert_equal [-1, 2], -3.divmod(5)
250 assert_equal [-2, -1], 25.divmod(-13)
251 assert_equal [ 1, -6], -13.divmod(-7)
252end
253
254# Not ISO specified
255
256assert('Integer#step') do
257 a = []
258 b = []
259 1.step(3) do |i|
260 a << i
261 end
262 1.step(6, 2) do |i|
263 b << i
264 end
265
266 assert_equal [1, 2, 3], a
267 assert_equal [1, 3, 5], b
268end
Note: See TracBrowser for help on using the repository browser.