1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
require 'test/unit'
class TestInteger < Test::Unit::TestCase
VS = [
-0x10000000000000002,
-0x10000000000000001,
-0x10000000000000000,
-0xffffffffffffffff,
-0x4000000000000002,
-0x4000000000000001,
-0x4000000000000000,
-0x3fffffffffffffff,
-0x100000002,
-0x100000001,
-0x100000000,
-0xffffffff,
-0xc717a08d, # 0xc717a08d * 0x524b2245 = 0x4000000000000001
-0x524b2245,
-0x40000002,
-0x40000001,
-0x40000000,
-0x3fffffff,
-0x10002,
-0x10001,
-0x10000,
-0xffff,
-0x8101, # 0x8101 * 0x7f01 = 0x40000001
-0x8002,
-0x8001,
-0x8000,
-0x7fff,
-0x7f01,
-65,
-64,
-63,
-62,
-33,
-32,
-31,
-30,
-3,
-2,
-1,
0,
1,
2,
3,
30,
31,
32,
33,
62,
63,
64,
65,
0x7f01,
0x7ffe,
0x7fff,
0x8000,
0x8001,
0x8101,
0xfffe,
0xffff,
0x10000,
0x10001,
0x3ffffffe,
0x3fffffff,
0x40000000,
0x40000001,
0x524b2245,
0xc717a08d,
0xfffffffe,
0xffffffff,
0x100000000,
0x100000001,
0x3ffffffffffffffe,
0x3fffffffffffffff,
0x4000000000000000,
0x4000000000000001,
0xfffffffffffffffe,
0xffffffffffffffff,
0x10000000000000000,
0x10000000000000001,
]
#VS.map! {|v| 0x40000000.coerce(v)[0] }
def test_aref
VS.each {|a|
100.times {|i|
assert_equal((a >> i).odd? ? 1 : 0, a[i], "(#{a})[#{i}]")
}
}
end
def test_plus
VS.each {|a|
VS.each {|b|
c = a + b
assert_equal(b + a, c, "#{a} + #{b}")
assert_equal(a, c - b, "(#{a} + #{b}) - #{b}")
assert_equal(a-~b-1, c, "#{a} + #{b}") # Hacker's Delight
assert_equal((a^b)+2*(a&b), c, "#{a} + #{b}") # Hacker's Delight
assert_equal((a|b)+(a&b), c, "#{a} + #{b}") # Hacker's Delight
assert_equal(2*(a|b)-(a^b), c, "#{a} + #{b}") # Hacker's Delight
}
}
end
def test_minus
VS.each {|a|
VS.each {|b|
c = a - b
assert_equal(a, c + b, "(#{a} - #{b}) + #{b}")
assert_equal(-b, c - a, "(#{a} - #{b}) - #{a}")
assert_equal(a+~b+1, c, "#{a} - #{b}") # Hacker's Delight
assert_equal((a^b)-2*(b&~a), c, "#{a} - #{b}") # Hacker's Delight
assert_equal((a&~b)-(b&~a), c, "#{a} - #{b}") # Hacker's Delight
assert_equal(2*(a&~b)-(a^b), c, "#{a} - #{b}") # Hacker's Delight
}
}
end
def test_mult
VS.each {|a|
VS.each {|b|
c = a * b
assert_equal(b * a, c, "#{a} * #{b}")
assert_equal(b, c / a, "(#{a} * #{b}) / #{a}") if a != 0
assert_equal(a.abs * b.abs, (a * b).abs, "(#{a} * #{b}).abs")
assert_equal((a-100)*(b-100)+(a-100)*100+(b-100)*100+10000, c, "#{a} * #{b}")
assert_equal((a+100)*(b+100)-(a+100)*100-(b+100)*100+10000, c, "#{a} * #{b}")
}
}
end
def test_divmod
VS.each {|a|
VS.each {|b|
next if b == 0
q, r = a.divmod(b)
assert_equal(a, b*q+r)
assert(r.abs < b.abs)
assert(0 < b ? (0 <= r && r < b) : (b < r && r <= 0))
assert_equal(q, a/b)
assert_equal(q, a.div(b))
assert_equal(r, a%b)
assert_equal(r, a.modulo(b))
}
}
end
def test_pow
small_values = VS.find_all {|v| 0 <= v && v < 1000 }
VS.each {|a|
small_values.each {|b|
c = a ** b
d = 1
b.times { d *= a }
assert_equal(d, c, "(#{a}) ** #{b}")
if a != 0
d = c
b.times { d /= a }
assert_equal(1, d, "((#{a}) ** #{b}) ** (-#{b})")
end
}
}
end
def test_not
VS.each {|a|
b = ~a
assert_equal(-1 ^ a, b, "~#{a}")
assert_equal(-a-1, b, "~#{a}") # Hacker's Delight
assert_equal(0, a & b, "#{a} & ~#{a}")
assert_equal(-1, a | b, "#{a} | ~#{a}")
}
end
def test_or
VS.each {|a|
VS.each {|b|
c = a | b
assert_equal(b | a, c, "#{a} | #{b}")
assert_equal(a + b - (a&b), c, "#{a} | #{b}")
assert_equal((a & ~b) + b, c, "#{a} | #{b}") # Hacker's Delight
assert_equal(-1, c | ~a, "(#{a} | #{b}) | ~#{a})")
}
}
end
def test_and
VS.each {|a|
VS.each {|b|
c = a & b
assert_equal(b & a, c, "#{a} & #{b}")
assert_equal(a + b - (a|b), c, "#{a} & #{b}")
assert_equal((~a | b) - ~a, c, "#{a} & #{b}") # Hacker's Delight
assert_equal(0, c & ~a, "(#{a} & #{b}) & ~#{a}")
}
}
end
def test_xor
VS.each {|a|
VS.each {|b|
c = a ^ b
assert_equal(b ^ a, c, "#{a} ^ #{b}")
assert_equal((a|b)-(a&b), c, "#{a} ^ #{b}") # Hacker's Delight
assert_equal(b, c ^ a, "(#{a} ^ #{b}) ^ #{a}")
}
}
end
def test_lshift
small_values = VS.find_all {|v| -1000 < v && v < 1000 }
VS.each {|a|
small_values.each {|b|
c = a << b
if 0 <= b
assert_equal(a, c >> b, "(#{a} << #{b}) >> #{b}")
assert_equal(a * 2**b, c, "#{a} << #{b}")
else
assert_equal(a / 2**(-b), c, "#{a} << #{b}")
end
}
}
end
def test_rshift
small_values = VS.find_all {|v| -1000 < v && v < 1000 }
VS.each {|a|
small_values.each {|b|
c = a >> b
if 0 < b
assert_equal(a / 2**b, c, "#{a} >> #{b}")
else
assert_equal(a, c << b, "(#{a} >> #{b}) << #{b}")
assert_equal(a * 2**(-b), c, "#{a} >> #{b}")
end
}
}
end
def test_succ
VS.each {|a|
b = a.succ
assert_equal(a+1, b, "(#{a}).succ")
assert_equal(a, b.pred, "(#{a}).succ.pred")
assert_equal(a, b-1, "(#{a}).succ - 1")
}
end
def test_pred
VS.each {|a|
b = a.pred
assert_equal(a-1, b, "(#{a}).pred")
assert_equal(a, b.succ, "(#{a}).pred.succ")
assert_equal(a, b + 1, "(#{a}).pred + 1")
}
end
def test_unary_plus
VS.each {|a|
b = +a
assert_equal(a, b, "+(#{a})")
}
end
def test_unary_minus
VS.each {|a|
b = -a
assert_equal(0-a, b, "-(#{a})")
assert_equal(~a+1, b, "-(#{a})")
assert_equal(0, a+b, "#{a}+(-(#{a}))")
}
end
def test_cmp
VS.each_with_index {|a, i|
VS.each_with_index {|b, j|
assert_equal(i <=> j, a <=> b, "#{a} <=> #{b}")
assert_equal(i < j, a < b, "#{a} < #{b}")
assert_equal(i <= j, a <= b, "#{a} <= #{b}")
assert_equal(i > j, a > b, "#{a} > #{b}")
assert_equal(i >= j, a >= b, "#{a} >= #{b}")
}
}
end
def test_eq
VS.each_with_index {|a, i|
VS.each_with_index {|b, j|
c = a == b
assert_equal(b == a, c, "#{a} == #{b}")
assert_equal(i == j, c, "#{a} == #{b}")
}
}
end
def test_abs
VS.each {|a|
b = a.abs
if a < 0
assert_equal(-a, b, "(#{a}).abs")
else
assert_equal(a, b, "(#{a}).abs")
end
}
end
def test_ceil
VS.each {|a|
assert_equal(a, a.ceil, "(#{a}).ceil")
}
end
def test_floor
VS.each {|a|
assert_equal(a, a.floor, "(#{a}).floor")
}
end
def test_round
VS.each {|a|
assert_equal(a, a.round, "(#{a}).round")
}
end
def test_truncate
VS.each {|a|
assert_equal(a, a.truncate, "(#{a}).truncate")
}
end
def test_remainder
VS.each {|a|
VS.each {|b|
next if b == 0
r = a.remainder(b)
if a < 0
assert_operator(-b.abs, :<, r, "#{a}.remainder(#{b})")
assert_operator(r, :<=, 0, "#{a}.remainder(#{b})")
elsif 0 < a
assert_operator(0, :<=, r, "#{a}.remainder(#{b})")
assert_operator(r, :<, b.abs, "#{a}.remainder(#{b})")
else
assert_equal(0, r, "#{a}.remainder(#{b})")
end
}
}
end
def test_zero_nonzero
VS.each {|a|
z = a.zero?
n = a.nonzero?
if a == 0
assert_equal(true, z, "(#{a}).zero?")
assert_equal(nil, n, "(#{a}).nonzero?")
else
assert_equal(false, z, "(#{a}).zero?")
assert_equal(a, n, "(#{a}).nonzero?")
end
assert(z ^ n)
}
end
def test_even_odd
VS.each {|a|
e = a.even?
o = a.odd?
assert_equal((a % 2) == 0, e, "(#{a}).even?")
assert_equal((a % 2) == 1, o, "(#{a}).odd")
assert(e ^ o)
}
end
end
|