-
-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathnumeric_spec.rb
495 lines (395 loc) · 13.5 KB
/
numeric_spec.rb
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
require_relative 'spec_helper'
load_extension("numeric")
describe "CApiNumericSpecs" do
before :each do
@s = CApiNumericSpecs.new
end
describe "NUM2INT" do
it "raises a TypeError if passed nil" do
-> { @s.NUM2INT(nil) }.should raise_error(TypeError)
end
it "converts a Float" do
@s.NUM2INT(4.2).should == 4
end
it "converts a Bignum" do
@s.NUM2INT(0x7fff_ffff).should == 0x7fff_ffff
end
it "converts a Fixnum" do
@s.NUM2INT(5).should == 5
end
it "converts -1 to an signed number" do
@s.NUM2INT(-1).should == -1
end
it "converts a negative Bignum into an signed number" do
@s.NUM2INT(-2147442171).should == -2147442171
end
it "raises a RangeError if the value is more than 32bits" do
-> { @s.NUM2INT(0xffff_ffff+1) }.should raise_error(RangeError)
end
it "calls #to_int to coerce the value" do
obj = mock("number")
obj.should_receive(:to_int).and_return(2)
@s.NUM2INT(obj).should == 2
end
end
describe "NUM2UINT" do
it "raises a TypeError if passed nil" do
-> { @s.NUM2UINT(nil) }.should raise_error(TypeError)
end
it "converts a Float" do
@s.NUM2UINT(4.2).should == 4
end
it "converts a Bignum" do
@s.NUM2UINT(0xffff_ffff).should == 0xffff_ffff
end
it "converts a Fixnum" do
@s.NUM2UINT(5).should == 5
end
it "converts a negative number to the complement" do
@s.NUM2UINT(-1).should == 4294967295
end
it "converts a signed int value to the complement" do
@s.NUM2UINT(-0x8000_0000).should == 2147483648
end
it "raises a RangeError if the value is more than 32bits" do
-> { @s.NUM2UINT(0xffff_ffff+1) }.should raise_error(RangeError)
end
it "raises a RangeError if the value is less than 32bits negative" do
-> { @s.NUM2UINT(-0x8000_0000-1) }.should raise_error(RangeError)
end
it "raises a RangeError if the value is more than 64bits" do
-> do
@s.NUM2UINT(0xffff_ffff_ffff_ffff+1)
end.should raise_error(RangeError)
end
it "calls #to_int to coerce the value" do
obj = mock("number")
obj.should_receive(:to_int).and_return(2)
@s.NUM2UINT(obj).should == 2
end
end
describe "NUM2LONG" do
it "raises a TypeError if passed nil" do
-> { @s.NUM2LONG(nil) }.should raise_error(TypeError)
end
it "converts a Float" do
@s.NUM2LONG(4.2).should == 4
end
it "converts a Bignum" do
@s.NUM2LONG(0x7fff_ffff).should == 0x7fff_ffff
end
it "converts a Fixnum" do
@s.NUM2LONG(5).should == 5
end
platform_is c_long_size: 32 do
it "converts -1 to an signed number" do
@s.NUM2LONG(-1).should == -1
end
it "converts a negative Bignum into an signed number" do
@s.NUM2LONG(-2147442171).should == -2147442171
end
it "raises a RangeError if the value is more than 32bits" do
-> { @s.NUM2LONG(0xffff_ffff+1) }.should raise_error(RangeError)
end
end
platform_is c_long_size: 64 do
it "converts -1 to an signed number" do
@s.NUM2LONG(-1).should == -1
end
it "converts a negative Bignum into an signed number" do
@s.NUM2LONG(-9223372036854734331).should == -9223372036854734331
end
it "raises a RangeError if the value is more than 64bits" do
-> do
@s.NUM2LONG(0xffff_ffff_ffff_ffff+1)
end.should raise_error(RangeError)
end
end
it "calls #to_int to coerce the value" do
obj = mock("number")
obj.should_receive(:to_int).and_return(2)
@s.NUM2LONG(obj).should == 2
end
end
describe "NUM2SHORT" do
it "raises a TypeError if passed nil" do
-> { @s.NUM2SHORT(nil) }.should raise_error(TypeError)
end
it "converts a Float" do
@s.NUM2SHORT(4.2).should == 4
end
it "converts a Fixnum" do
@s.NUM2SHORT(5).should == 5
end
it "converts -1 to an signed number" do
@s.NUM2SHORT(-1).should == -1
end
it "raises a RangeError if the value is more than 32bits" do
-> { @s.NUM2SHORT(0xffff_ffff+1) }.should raise_error(RangeError)
end
it "calls #to_int to coerce the value" do
obj = mock("number")
obj.should_receive(:to_int).and_return(2)
@s.NUM2SHORT(obj).should == 2
end
end
describe "INT2NUM" do
it "raises a TypeError if passed nil" do
-> { @s.INT2NUM(nil) }.should raise_error(TypeError)
end
it "converts a Float" do
@s.INT2NUM(4.2).should == 4
end
it "raises a RangeError when passed a Bignum" do
-> { @s.INT2NUM(bignum_value) }.should raise_error(RangeError)
end
it "converts a Fixnum" do
@s.INT2NUM(5).should == 5
end
it "converts a negative Fixnum" do
@s.INT2NUM(-11).should == -11
end
end
describe "NUM2ULONG" do
it "raises a TypeError if passed nil" do
-> { @s.NUM2ULONG(nil) }.should raise_error(TypeError)
end
it "converts a Float" do
@s.NUM2ULONG(4.2).should == 4
end
it "converts a Bignum" do
@s.NUM2ULONG(0xffff_ffff).should == 0xffff_ffff
end
it "converts a Fixnum" do
@s.NUM2ULONG(5).should == 5
end
platform_is c_long_size: 32 do
it "converts -1 to an unsigned number" do
@s.NUM2ULONG(-1).should == 4294967295
end
it "converts a negative Bignum into an unsigned number" do
@s.NUM2ULONG(-2147442171).should == 2147525125
end
it "converts positive Bignums if the values is less than 64bits" do
@s.NUM2ULONG(0xffff_ffff).should == 0xffff_ffff
@s.NUM2ULONG(2**30).should == 2**30
@s.NUM2ULONG(fixnum_max+1).should == fixnum_max+1
@s.NUM2ULONG(fixnum_max).should == fixnum_max
end
it "raises a RangeError if the value is more than 32bits" do
-> { @s.NUM2ULONG(0xffff_ffff+1) }.should raise_error(RangeError)
end
end
platform_is c_long_size: 64 do
it "converts -1 to an unsigned number" do
@s.NUM2ULONG(-1).should == 18446744073709551615
end
it "converts a negative Bignum into an unsigned number" do
@s.NUM2ULONG(-9223372036854734331).should == 9223372036854817285
end
it "converts positive Bignums if the values is less than 64bits" do
@s.NUM2ULONG(0xffff_ffff_ffff_ffff).should == 0xffff_ffff_ffff_ffff
@s.NUM2ULONG(2**62).should == 2**62
@s.NUM2ULONG(fixnum_max+1).should == fixnum_max+1
@s.NUM2ULONG(fixnum_max).should == fixnum_max
end
it "raises a RangeError if the value is more than 64bits" do
-> do
@s.NUM2ULONG(0xffff_ffff_ffff_ffff+1)
end.should raise_error(RangeError)
end
end
it "calls #to_int to coerce the value" do
obj = mock("number")
obj.should_receive(:to_int).and_return(2)
@s.NUM2ULONG(obj).should == 2
end
end
describe "rb_Integer" do
it "creates an Integer from a String" do
i = @s.rb_Integer("8675309")
i.should == 8675309
end
end
describe "rb_ll2inum" do
it "creates a Fixnum from a small signed long long" do
i = @s.rb_ll2inum_14()
i.should == 14
end
end
describe "rb_ull2inum" do
it "creates a Fixnum from a small unsigned long long" do
i = @s.rb_ull2inum_14()
i.should == 14
end
it "creates a positive Bignum from a negative long long" do
i = @s.rb_ull2inum_n14()
i.should == (2 ** (@s.size_of_long_long * 8) - 14)
end
end
describe "rb_int2inum" do
it "creates a Fixnum from a long" do
i = @s.rb_int2inum_14()
i.should == 14
end
end
describe "rb_uint2inum" do
it "creates a Fixnum from a long" do
i = @s.rb_uint2inum_14()
i.should == 14
end
it "creates a positive Bignum from a negative long" do
i = @s.rb_uint2inum_n14()
i.should == (2 ** (@s.size_of_VALUE * 8) - 14)
end
end
describe "NUM2DBL" do
it "raises a TypeError if passed nil" do
-> { @s.NUM2DBL(nil) }.should raise_error(TypeError)
end
it "raises a TypeError if passed a String" do
-> { @s.NUM2DBL("1.2") }.should raise_error(TypeError)
end
it "converts a Float" do
@s.NUM2DBL(4.2).should == 4.2
end
it "converts a Bignum" do
@s.NUM2DBL(2**70).should == (2**70).to_f
end
it "converts a Fixnum" do
@s.NUM2DBL(5).should == 5.0
end
it "calls #to_f to coerce the value" do
obj = mock("number")
obj.should_receive(:to_f).and_return(2.0)
@s.NUM2DBL(obj).should == 2.0
end
end
describe "NUM2CHR" do
it "returns the first character of a String" do
@s.NUM2CHR("Abc").should == 65
end
it "returns the least significant byte of an Integer" do
@s.NUM2CHR(0xa7c).should == 0x07c
end
it "returns the least significant byte of a Float converted to an Integer" do
@s.NUM2CHR(0xa7c.to_f).should == 0x07c
end
it "raises a TypeError when passed an empty String" do
-> { @s.NUM2CHR("") }.should raise_error(TypeError)
end
end
describe "rb_num_zerodiv" do
it "raises a RuntimeError" do
-> { @s.rb_num_zerodiv() }.should raise_error(ZeroDivisionError, 'divided by 0')
end
end
describe "rb_cmpint" do
it "returns a Fixnum if passed one" do
@s.rb_cmpint(1, 2).should == 1
end
it "uses > to check if the value is greater than 1" do
m = mock("number")
m.should_receive(:>).and_return(true)
@s.rb_cmpint(m, 4).should == 1
end
it "uses < to check if the value is less than 1" do
m = mock("number")
m.should_receive(:>).and_return(false)
m.should_receive(:<).and_return(true)
@s.rb_cmpint(m, 4).should == -1
end
it "returns 0 if < and > are false" do
m = mock("number")
m.should_receive(:>).and_return(false)
m.should_receive(:<).and_return(false)
@s.rb_cmpint(m, 4).should == 0
end
it "raises an ArgumentError when passed nil" do
-> {
@s.rb_cmpint(nil, 4)
}.should raise_error(ArgumentError)
end
end
describe "rb_num_coerce_bin" do
it "calls #coerce on the first argument" do
obj = mock("rb_num_coerce_bin")
obj.should_receive(:coerce).with(2).and_return([1, 2])
@s.rb_num_coerce_bin(2, obj, :+).should == 3
end
it "calls the specified method on the first argument returned by #coerce" do
obj = mock("rb_num_coerce_bin")
obj.should_receive(:coerce).with(2).and_return([obj, 2])
obj.should_receive(:+).with(2).and_return(3)
@s.rb_num_coerce_bin(2, obj, :+).should == 3
end
it "raises a TypeError if #coerce does not return an Array" do
obj = mock("rb_num_coerce_bin")
obj.should_receive(:coerce).with(2).and_return(nil)
-> { @s.rb_num_coerce_bin(2, obj, :+) }.should raise_error(TypeError)
end
end
describe "rb_num_coerce_cmp" do
it "calls #coerce on the first argument" do
obj = mock("rb_num_coerce_cmp")
obj.should_receive(:coerce).with(2).and_return([1, 2])
@s.rb_num_coerce_cmp(2, obj, :<=>).should == -1
end
it "calls the specified method on the first argument returned by #coerce" do
obj = mock("rb_num_coerce_cmp")
obj.should_receive(:coerce).with(2).and_return([obj, 2])
obj.should_receive(:<=>).with(2).and_return(-1)
@s.rb_num_coerce_cmp(2, obj, :<=>).should == -1
end
it "lets the exception go through if #coerce raises an exception" do
obj = mock("rb_num_coerce_cmp")
obj.should_receive(:coerce).with(2).and_raise(RuntimeError.new("my error"))
-> {
@s.rb_num_coerce_cmp(2, obj, :<=>)
}.should raise_error(RuntimeError, "my error")
end
it "returns nil if #coerce does not return an Array" do
obj = mock("rb_num_coerce_cmp")
obj.should_receive(:coerce).with(2).and_return(nil)
@s.rb_num_coerce_cmp(2, obj, :<=>).should be_nil
end
end
describe "rb_num_coerce_relop" do
it "calls #coerce on the first argument" do
obj = mock("rb_num_coerce_relop")
obj.should_receive(:coerce).with(2).and_return([1, 2])
@s.rb_num_coerce_relop(2, obj, :<).should be_true
end
it "calls the specified method on the first argument returned by #coerce" do
obj = mock("rb_num_coerce_relop")
obj.should_receive(:coerce).with(2).and_return([obj, 2])
obj.should_receive(:<).with(2).and_return(false)
@s.rb_num_coerce_relop(2, obj, :<).should be_false
end
it "raises an ArgumentError if #<op> returns nil" do
obj = mock("rb_num_coerce_relop")
obj.should_receive(:coerce).with(2).and_return([obj, 2])
obj.should_receive(:<).with(2).and_return(nil)
-> { @s.rb_num_coerce_relop(2, obj, :<) }.should raise_error(ArgumentError)
end
it "raises an ArgumentError if #coerce does not return an Array" do
obj = mock("rb_num_coerce_relop")
obj.should_receive(:coerce).with(2).and_return(nil)
-> { @s.rb_num_coerce_relop(2, obj, :<) }.should raise_error(ArgumentError)
end
end
describe "rb_absint_singlebit_p" do
it "returns 1 if absolute value fits into a bit" do
@s.rb_absint_singlebit_p(1).should == 1
@s.rb_absint_singlebit_p(2).should == 1
@s.rb_absint_singlebit_p(3).should == 0
@s.rb_absint_singlebit_p(-1).should == 1
@s.rb_absint_singlebit_p(-2).should == 1
@s.rb_absint_singlebit_p(-3).should == 0
@s.rb_absint_singlebit_p(bignum_value).should == 1
@s.rb_absint_singlebit_p(bignum_value(1)).should == 0
@s.rb_absint_singlebit_p(-bignum_value).should == 1
@s.rb_absint_singlebit_p(-bignum_value(1)).should == 0
end
end
end