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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
# frozen_string_literal: false
require 'test/unit'
class TestInteger < Test::Unit::TestCase
BDSIZE = 0x4000000000000000.coerce(0)[0].size
def self.bdsize(x)
((x + 1) / 8 + BDSIZE) / BDSIZE * BDSIZE
end
def bdsize(x)
self.class.bdsize(x)
end
def test_aref
# assert_equal(1, (1 << 0x40000000)[0x40000000], "[ruby-dev:31271]")
# assert_equal(0, (-1 << 0x40000001)[0x40000000], "[ruby-dev:31271]")
big_zero = 0x40000000.coerce(0)[0]
assert_equal(0, (-0x40000002)[big_zero], "[ruby-dev:31271]")
assert_equal(1, 0x400000001[big_zero], "[ruby-dev:31271]")
end
def test_pow
assert_not_equal(0, begin
0**-1
rescue
nil
end, "[ruby-dev:32084] [ruby-dev:34547]")
end
def test_lshift
assert_equal(0, 1 << -0x40000000)
assert_equal(0, 1 << -0x40000001)
assert_equal(0, 1 << -0x80000000)
assert_equal(0, 1 << -0x80000001)
# assert_equal(bdsize(0x80000000), (1 << 0x80000000).size)
end
def test_rshift
# assert_equal(bdsize(0x40000001), (1 >> -0x40000001).size)
assert_predicate((1 >> 0x80000000), :zero?)
assert_predicate((1 >> 0xffffffff), :zero?)
assert_predicate((1 >> 0x100000000), :zero?)
# assert_equal((1 << 0x40000000), (1 >> -0x40000000))
# assert_equal((1 << 0x40000001), (1 >> -0x40000001))
end
def test_Integer
assert_raise(ArgumentError) {Integer("0x-1")}
assert_raise(ArgumentError) {Integer("-0x-1")}
assert_raise(ArgumentError) {Integer("0x 123")}
assert_raise(ArgumentError) {Integer("0x 123")}
assert_raise(ArgumentError) {Integer("0x0x5")}
assert_raise(ArgumentError) {Integer("0x0x000000005")}
assert_nothing_raised(ArgumentError) {
assert_equal(1540841, "0x0x5".to_i(36))
}
assert_raise(ArgumentError) { Integer("--0") }
assert_raise(ArgumentError) { Integer("-+0") }
assert_raise(ArgumentError) { Integer("++1") }
assert_raise(ArgumentError) { Integer("") }
assert_raise(ArgumentError) { Integer("10 x") }
assert_raise(ArgumentError) { Integer("1__2") }
assert_raise(ArgumentError) { Integer("1z") }
assert_raise(ArgumentError) { Integer("46116860184273__87904") }
assert_raise(ArgumentError) { Integer("4611686018427387904_") }
assert_raise(ArgumentError) { Integer("4611686018427387904 :") }
assert_equal(0x4000000000000000, Integer("46_11_686_0184273_87904"))
assert_raise(ArgumentError) { Integer("\0") }
assert_nothing_raised(ArgumentError, "[ruby-core:13873]") {
assert_equal(0, Integer("0 "))
}
assert_nothing_raised(ArgumentError, "[ruby-core:14139]") {
assert_equal(0377, Integer("0_3_7_7"))
}
assert_raise(ArgumentError, "[ruby-core:14139]") {Integer("0__3_7_7")}
assert_equal(1234, Integer(1234))
assert_equal(1, Integer(1.234))
# base argument
assert_equal(1234, Integer("1234", 10))
assert_equal(668, Integer("1234", 8))
assert_equal(4660, Integer("1234", 16))
assert_equal(49360, Integer("1234", 36))
# decimal, not octal
assert_equal(1234, Integer("01234", 10))
assert_raise(ArgumentError) { Integer("0x123", 10) }
assert_raise(ArgumentError) { Integer(1234, 10) }
assert_raise(ArgumentError) { Integer(12.34, 10) }
assert_raise(ArgumentError) { Integer(Object.new, 1) }
assert_raise(ArgumentError) { Integer(1, 1, 1) }
assert_equal(2 ** 50, Integer(2.0 ** 50))
assert_raise(TypeError) { Integer(nil) }
bug6192 = '[ruby-core:43566]'
assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-16be"))}
assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-16le"))}
assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-32be"))}
assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-32le"))}
assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("iso-2022-jp"))}
assert_raise_with_message(ArgumentError, /\u{1f4a1}/) {Integer("\u{1f4a1}")}
obj = Struct.new(:s).new(%w[42 not-an-integer])
def obj.to_str; s.shift; end
assert_equal(42, Integer(obj, 10))
assert_separately([], "#{<<-"begin;"}\n#{<<-"end;"}")
begin;
class Float
undef to_int
def to_int; raise "conversion failed"; end
end
assert_equal (1 << 100), Integer((1 << 100).to_f)
assert_equal 1, Integer(1.0)
end;
end
def test_int_p
assert_not_predicate(1.0, :integer?)
assert_predicate(1, :integer?)
end
def test_succ
assert_equal(2, 1.send(:succ))
end
def test_chr
assert_equal("a", "a".ord.chr)
assert_raise(RangeError) { (-1).chr }
assert_raise(RangeError) { 0x100.chr }
end
def test_upto
a = []
1.upto(3) {|x| a << x }
assert_equal([1, 2, 3], a)
a = []
1.upto(0) {|x| a << x }
assert_equal([], a)
y = 2**30 - 1
a = []
y.upto(y+2) {|x| a << x }
assert_equal([y, y+1, y+2], a)
end
def test_downto
a = []
-1.downto(-3) {|x| a << x }
assert_equal([-1, -2, -3], a)
a = []
1.downto(2) {|x| a << x }
assert_equal([], a)
y = -(2**30)
a = []
y.downto(y-2) {|x| a << x }
assert_equal([y, y-1, y-2], a)
end
def test_times
(2**32).times do |i|
break if i == 2
end
end
def assert_int_equal(expected, result, mesg = nil)
assert_kind_of(Integer, result, mesg)
assert_equal(expected, result, mesg)
end
def assert_float_equal(expected, result, mesg = nil)
assert_kind_of(Float, result, mesg)
assert_equal(expected, result, mesg)
end
def test_round
assert_int_equal(11111, 11111.round)
assert_int_equal(11111, 11111.round(0))
assert_float_equal(11111.0, 11111.round(1))
assert_float_equal(11111.0, 11111.round(2))
assert_int_equal(11110, 11111.round(-1))
assert_int_equal(11100, 11111.round(-2))
assert_int_equal(+200, +249.round(-2))
assert_int_equal(+200, +250.round(-2))
assert_int_equal(-200, -249.round(-2))
assert_int_equal(+200, +249.round(-2, half: :even))
assert_int_equal(+200, +250.round(-2, half: :even))
assert_int_equal(+300, +349.round(-2, half: :even))
assert_int_equal(+400, +350.round(-2, half: :even))
assert_int_equal(+200, +249.round(-2, half: :up))
assert_int_equal(+300, +250.round(-2, half: :up))
assert_int_equal(+300, +349.round(-2, half: :up))
assert_int_equal(+400, +350.round(-2, half: :up))
assert_int_equal(+200, +249.round(-2, half: :down))
assert_int_equal(+200, +250.round(-2, half: :down))
assert_int_equal(+300, +349.round(-2, half: :down))
assert_int_equal(+300, +350.round(-2, half: :down))
assert_int_equal(-200, -250.round(-2))
assert_int_equal(-200, -249.round(-2, half: :even))
assert_int_equal(-200, -250.round(-2, half: :even))
assert_int_equal(-300, -349.round(-2, half: :even))
assert_int_equal(-400, -350.round(-2, half: :even))
assert_int_equal(-200, -249.round(-2, half: :up))
assert_int_equal(-300, -250.round(-2, half: :up))
assert_int_equal(-300, -349.round(-2, half: :up))
assert_int_equal(-400, -350.round(-2, half: :up))
assert_int_equal(-200, -249.round(-2, half: :down))
assert_int_equal(-200, -250.round(-2, half: :down))
assert_int_equal(-300, -349.round(-2, half: :down))
assert_int_equal(-300, -350.round(-2, half: :down))
assert_int_equal(+20 * 10**70, (+25 * 10**70).round(-71))
assert_int_equal(-20 * 10**70, (-25 * 10**70).round(-71))
assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).round(-71))
assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).round(-71))
assert_int_equal(+40 * 10**70, (+35 * 10**70).round(-71))
assert_int_equal(-40 * 10**70, (-35 * 10**70).round(-71))
assert_int_equal(+30 * 10**70, (+35 * 10**70 - 1).round(-71))
assert_int_equal(-30 * 10**70, (-35 * 10**70 + 1).round(-71))
assert_int_equal(+20 * 10**70, (+25 * 10**70).round(-71, half: :even))
assert_int_equal(-20 * 10**70, (-25 * 10**70).round(-71, half: :even))
assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).round(-71, half: :even))
assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).round(-71, half: :even))
assert_int_equal(+40 * 10**70, (+35 * 10**70).round(-71, half: :even))
assert_int_equal(-40 * 10**70, (-35 * 10**70).round(-71, half: :even))
assert_int_equal(+30 * 10**70, (+35 * 10**70 - 1).round(-71, half: :even))
assert_int_equal(-30 * 10**70, (-35 * 10**70 + 1).round(-71, half: :even))
assert_int_equal(+30 * 10**70, (+25 * 10**70).round(-71, half: :up))
assert_int_equal(-30 * 10**70, (-25 * 10**70).round(-71, half: :up))
assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).round(-71, half: :up))
assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).round(-71, half: :up))
assert_int_equal(+40 * 10**70, (+35 * 10**70).round(-71, half: :up))
assert_int_equal(-40 * 10**70, (-35 * 10**70).round(-71, half: :up))
assert_int_equal(+30 * 10**70, (+35 * 10**70 - 1).round(-71, half: :up))
assert_int_equal(-30 * 10**70, (-35 * 10**70 + 1).round(-71, half: :up))
assert_int_equal(+20 * 10**70, (+25 * 10**70).round(-71, half: :down))
assert_int_equal(-20 * 10**70, (-25 * 10**70).round(-71, half: :down))
assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).round(-71, half: :down))
assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).round(-71, half: :down))
assert_int_equal(+30 * 10**70, (+35 * 10**70).round(-71, half: :down))
assert_int_equal(-30 * 10**70, (-35 * 10**70).round(-71, half: :down))
assert_int_equal(+30 * 10**70, (+35 * 10**70 - 1).round(-71, half: :down))
assert_int_equal(-30 * 10**70, (-35 * 10**70 + 1).round(-71, half: :down))
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.round(-1))
assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).round(-1))
end
def test_floor
assert_int_equal(11111, 11111.floor)
assert_int_equal(11111, 11111.floor(0))
assert_float_equal(11111.0, 11111.floor(1))
assert_float_equal(11111.0, 11111.floor(2))
assert_int_equal(11110, 11110.floor(-1))
assert_int_equal(11110, 11119.floor(-1))
assert_int_equal(11100, 11100.floor(-2))
assert_int_equal(11100, 11199.floor(-2))
assert_int_equal(0, 11111.floor(-5))
assert_int_equal(+200, +299.floor(-2))
assert_int_equal(+300, +300.floor(-2))
assert_int_equal(-300, -299.floor(-2))
assert_int_equal(-300, -300.floor(-2))
assert_int_equal(+20 * 10**70, (+25 * 10**70).floor(-71))
assert_int_equal(-30 * 10**70, (-25 * 10**70).floor(-71))
assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).floor(-71))
assert_int_equal(-30 * 10**70, (-25 * 10**70 + 1).floor(-71))
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.floor(-1))
assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1120, (-1111_1111_1111_1111_1111_1111_1111_1111).floor(-1))
end
def test_ceil
assert_int_equal(11111, 11111.ceil)
assert_int_equal(11111, 11111.ceil(0))
assert_float_equal(11111.0, 11111.ceil(1))
assert_float_equal(11111.0, 11111.ceil(2))
assert_int_equal(11110, 11110.ceil(-1))
assert_int_equal(11120, 11119.ceil(-1))
assert_int_equal(11200, 11101.ceil(-2))
assert_int_equal(11200, 11200.ceil(-2))
assert_int_equal(100000, 11111.ceil(-5))
assert_int_equal(300, 299.ceil(-2))
assert_int_equal(300, 300.ceil(-2))
assert_int_equal(-200, -299.ceil(-2))
assert_int_equal(-300, -300.ceil(-2))
assert_int_equal(+30 * 10**70, (+25 * 10**70).ceil(-71))
assert_int_equal(-20 * 10**70, (-25 * 10**70).ceil(-71))
assert_int_equal(+30 * 10**70, (+25 * 10**70 - 1).ceil(-71))
assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).ceil(-71))
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1120, 1111_1111_1111_1111_1111_1111_1111_1111.ceil(-1))
assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).ceil(-1))
end
def test_truncate
assert_int_equal(11111, 11111.truncate)
assert_int_equal(11111, 11111.truncate(0))
assert_float_equal(11111.0, 11111.truncate(1))
assert_float_equal(11111.0, 11111.truncate(2))
assert_int_equal(11110, 11110.truncate(-1))
assert_int_equal(11110, 11119.truncate(-1))
assert_int_equal(11100, 11100.truncate(-2))
assert_int_equal(11100, 11199.truncate(-2))
assert_int_equal(0, 11111.truncate(-5))
assert_int_equal(+200, +299.truncate(-2))
assert_int_equal(+300, +300.truncate(-2))
assert_int_equal(-200, -299.truncate(-2))
assert_int_equal(-300, -300.truncate(-2))
assert_int_equal(+20 * 10**70, (+25 * 10**70).truncate(-71))
assert_int_equal(-20 * 10**70, (-25 * 10**70).truncate(-71))
assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).truncate(-71))
assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).truncate(-71))
assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.truncate(-1))
assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).truncate(-1))
end
MimicInteger = Struct.new(:to_int)
module CoercionToInt
def coerce(other)
[other, to_int]
end
end
def test_bitwise_and_with_integer_mimic_object
obj = MimicInteger.new(10)
assert_raise(TypeError, '[ruby-core:39491]') { 3 & obj }
obj.extend(CoercionToInt)
assert_equal(3 & 10, 3 & obj)
end
def test_bitwise_or_with_integer_mimic_object
obj = MimicInteger.new(10)
assert_raise(TypeError, '[ruby-core:39491]') { 3 | obj }
obj.extend(CoercionToInt)
assert_equal(3 | 10, 3 | obj)
end
def test_bitwise_xor_with_integer_mimic_object
obj = MimicInteger.new(10)
assert_raise(TypeError, '[ruby-core:39491]') { 3 ^ obj }
obj.extend(CoercionToInt)
assert_equal(3 ^ 10, 3 ^ obj)
end
module CoercionToSelf
def coerce(other)
[self.class.new(other), self]
end
end
def test_bitwise_and_with_integer_coercion
obj = Struct.new(:value) do
include(CoercionToSelf)
def &(other)
self.value & other.value
end
end.new(10)
assert_equal(3 & 10, 3 & obj)
end
def test_bitwise_or_with_integer_coercion
obj = Struct.new(:value) do
include(CoercionToSelf)
def |(other)
self.value | other.value
end
end.new(10)
assert_equal(3 | 10, 3 | obj)
end
def test_bitwise_xor_with_integer_coercion
obj = Struct.new(:value) do
include(CoercionToSelf)
def ^(other)
self.value ^ other.value
end
end.new(10)
assert_equal(3 ^ 10, 3 ^ obj)
end
def test_bit_length
assert_equal(13, (-2**12-1).bit_length)
assert_equal(12, (-2**12).bit_length)
assert_equal(12, (-2**12+1).bit_length)
assert_equal(9, -0x101.bit_length)
assert_equal(8, -0x100.bit_length)
assert_equal(8, -0xff.bit_length)
assert_equal(1, -2.bit_length)
assert_equal(0, -1.bit_length)
assert_equal(0, 0.bit_length)
assert_equal(1, 1.bit_length)
assert_equal(8, 0xff.bit_length)
assert_equal(9, 0x100.bit_length)
assert_equal(9, 0x101.bit_length)
assert_equal(12, (2**12-1).bit_length)
assert_equal(13, (2**12).bit_length)
assert_equal(13, (2**12+1).bit_length)
assert_equal(10001, (-2**10000-1).bit_length)
assert_equal(10000, (-2**10000).bit_length)
assert_equal(10000, (-2**10000+1).bit_length)
assert_equal(10000, (2**10000-1).bit_length)
assert_equal(10001, (2**10000).bit_length)
assert_equal(10001, (2**10000+1).bit_length)
2.upto(1000) {|i|
n = 2**i
assert_equal(i+1, (-n-1).bit_length, "(#{-n-1}).bit_length")
assert_equal(i, (-n).bit_length, "(#{-n}).bit_length")
assert_equal(i, (-n+1).bit_length, "(#{-n+1}).bit_length")
assert_equal(i, (n-1).bit_length, "#{n-1}.bit_length")
assert_equal(i+1, (n).bit_length, "#{n}.bit_length")
assert_equal(i+1, (n+1).bit_length, "#{n+1}.bit_length")
}
end
def test_digits
assert_equal([0], 0.digits)
assert_equal([1], 1.digits)
assert_equal([0, 9, 8, 7, 6, 5, 4, 3, 2, 1], 1234567890.digits)
assert_equal([90, 78, 56, 34, 12], 1234567890.digits(100))
assert_equal([10, 5, 6, 8, 0, 10, 8, 6, 1], 1234567890.digits(13))
end
def test_digits_for_negative_numbers
assert_raise(Math::DomainError) { -1.digits }
assert_raise(Math::DomainError) { -1234567890.digits }
assert_raise(Math::DomainError) { -1234567890.digits(100) }
assert_raise(Math::DomainError) { -1234567890.digits(13) }
end
def test_digits_for_invalid_base_numbers
assert_raise(ArgumentError) { 10.digits(-1) }
assert_raise(ArgumentError) { 10.digits(0) }
assert_raise(ArgumentError) { 10.digits(1) }
end
def test_digits_for_non_integral_base_numbers
assert_equal([1], 1.digits(10r))
assert_equal([1], 1.digits(10.0))
assert_raise(RangeError) { 10.digits(10+1i) }
end
def test_digits_for_non_numeric_base_argument
assert_raise(TypeError) { 10.digits("10") }
assert_raise(TypeError) { 10.digits("a") }
class << (o = Object.new)
def to_int
10
end
end
assert_equal([0, 1], 10.digits(o))
end
end
|