forked from rubinius/rubinius
/
numeric_spec.rb
688 lines (585 loc) · 22.2 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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
require File.dirname(__FILE__) + '/../spec_helper'
TOLERANCE = 0.00003 unless Object.const_defined?(:TOLERANCE)
# TODO <=> angle arg conj conjugate
# im imag image polar real singleton_method_added
zero = 0
zero_float = 0.0
nonzero = 1
a_possitive_number = 100
a_negative_number = -100
a_possitive_float = 34.56
a_negative_float = -34.56
a_possitive_bignumber = 2147483648
a_negative_bignumber = -2147483648
a_num_twopb = 9223372036854775808
a_num_twonb = -9223372036854775808
a_negative_num_bignumber = -2**31
a_possitive_den_bignumber = 2**31
# numerators and denominators
a_num_int = 13
a_num_int_n = -13
a_den_int = 4
a_den_int_n = -4
a_num_flt = 13.0
a_den_flt = 4.0
a_den_flt_n = -4.0
a_num_bigint = 3**33
describe "Numeric#-@" do
it " should return the same value with opposite sign (integers)" do
0.send(:-@).should == 0
a_possitive_number.send(:-@).should == -100
a_negative_number.send(:-@).should == 100
end
it " should return the same value with opposite sign (floats)" do
a_possitive_float.send(:-@).should == -34.56
a_negative_float.send(:-@).should == 34.56
end
it " should return the same value with opposite sign (two complement)" do
a_possitive_bignumber.send(:-@).should == -2147483648
a_negative_bignumber.send(:-@).should == 2147483648
a_num_twopb.send(:-@).should == -9223372036854775808
a_num_twonb.send(:-@).should == 9223372036854775808
end
end
describe "Numeric#+@" do
it " should return the same value with opposite sign (integers)" do
0.send(:+@).should == 0
a_possitive_number.send(:+@).should == 100
a_negative_number.send(:+@).should == -100
end
it " should return the same value with opposite sign (floats)" do
a_possitive_float.send(:+@).should == 34.56
a_negative_float.send(:+@).should == -34.56
end
it " should return the same value with opposite sign (floats)" do
a_possitive_den_bignumber.send(:+@).should == 2**31
a_negative_num_bignumber.send(:+@).should == -2**31
end
it " should return the same value with opposite sign (two complement)" do
a_possitive_bignumber.send(:+@).should == 2147483648
a_negative_bignumber.send(:+@).should == -2147483648
a_num_twopb.send(:+@).should == 9223372036854775808
a_num_twonb.send(:+@).should == -9223372036854775808
end
end
# Returns the smallest Integer greater than or equal to num.
# Class Numeric achieves this by converting itself to a Float then invoking Float#ceil.
describe "Numeric#ceil " do
it "ceil to integer " do
0.ceil.should == 0
a_possitive_number.ceil.should == 100
a_negative_number.ceil.should == -100
end
it " should ceil to float " do
0.ceil.should == 0.0
a_possitive_float.ceil.should == 35
a_negative_float.ceil.should == -34
end
it "ceil twos complement" do
a_possitive_bignumber.ceil.should == 2147483648
a_negative_bignumber.ceil.should == -2147483648
a_num_twopb.ceil.should == 9223372036854775808
a_num_twonb.ceil.should == -9223372036854775808
end
end
# Returns the absolute value of num.
describe "Numeric#abs" do
it "return the abs (integers) " do
0.abs.should == 0
a_possitive_number.abs.should == 100
a_negative_number.abs.should == 100
end
it "return the abs (floats) " do
zero_float.abs.should == 0.0
a_possitive_float.abs.should == 34.56
a_negative_float.abs.should == 34.56
end
it "return the abs (two complement)" do
a_possitive_bignumber.abs.should == 2147483648
a_negative_bignumber.abs.should == 2147483648
a_num_twopb.abs.should == 9223372036854775808
a_num_twonb.abs.should == 9223372036854775808
end
end
# Returns an array containing the quotient and modulus obtained
# by dividing num by aNumeric. If q, r = x.divmod(y), then
# q = floor(float(x)/float(y))
# x = q*y + r
describe "Numeric#divmod" do
it "divmod right integers" do
a_num_int.divmod(a_den_int).should == [3,1]
a_den_int.divmod(a_num_int).should == [0,4]
end
it "divmod right integers and floats" do
a_num_int.divmod(a_den_flt).should == [3,1]
a_den_int.divmod(a_num_int).should == [0,4]
end
it "divmod right the integers and floats" do
a_num_int.divmod(a_den_flt).should == [3,1]
a_den_int.divmod(a_num_int).should == [0,4]
end
it "divmod right floats" do
a_num_flt.divmod(a_den_flt).should == [3.0,1.0]
a_den_flt.divmod(a_num_int).should == [0.0,4.0]
end
it " should divmod right with bignums and integers" do
a_num_bigint.divmod( a_possitive_number).should == [55590605665555, 23]
end
it "raise the expected exception" do
should_raise(ArgumentError){ a_num_int.divmod }
should_raise(ZeroDivisionError){ a_num_int.divmod(0) }
should_raise(TypeError){ a_num_int.divmod(nil) }
should_raise(TypeError){ a_num_int.divmod('test') }
should_raise(TypeError){ a_num_int.divmod(true) }
end
end
# Equivalent to Numeric#/, but overridden in subclasses.
describe "Numeric#quo" do
specify "quo should return the floating-point result of self divided by other" do
# the to_f is required because RSpec (I'm assuming) requires 'rational'
2.quo(2.5).to_s.should == '0.8'
5.quo(2).to_f.to_s.should == '2.5'
end
specify "quo should NOT raise an exception when other is zero" do
# 1.quo(0) should also not raise (i.e works in irb and from a file),
# but fails here.
1.quo(0.0).to_s.should == 'Infinity'
1.quo(-0.0).to_s.should == '-Infinity'
end
it "quo right integers" do
a_num_int.quo(a_den_int).should_be_close(3.25, TOLERANCE)
a_den_int.quo(a_num_int).should_be_close( 0.307692307692308, TOLERANCE)
end
it "quo right integers and floats" do
a_num_int.quo(a_den_flt).should_be_close(3.25, TOLERANCE)
a_den_int.quo(a_num_int).should_be_close(0.307692307692308, TOLERANCE)
end
it "quo right the integers and floats" do
a_num_int.quo(a_den_flt).should_be_close(3.25, TOLERANCE)
a_den_int.quo(a_num_int).should_be_close(0.307692307692308, TOLERANCE)
end
it "quo right floats" do
a_num_flt.quo(a_den_flt).should_be_close(3.25, TOLERANCE)
a_den_flt.quo(a_num_int).should_be_close(0.307692307692308, TOLERANCE)
end
# NOTE: Doesn't work when run with RSpec because it loads rational.rb
it " should quo right with bignums and integers" do
a_possitive_bignumber.quo( a_possitive_number).to_s.should == 21474836.48.to_s
end
# NOTE: Doesn't work when run with RSpec because it loads rational.rb
it "not raise a Exception when quo by 0" do
a_num_int.quo(0)
a_num_flt.quo(0)
a_num_bigint.quo(0)
end
# NOTE: Doesn't work when run with RSpec because it loads rational.rb
it "raise the expected exception" do
should_raise(ArgumentError){ a_num_int.quo }
should_raise(TypeError){ a_num_int.quo(nil) }
should_raise(TypeError){ a_num_int.quo('test') }
should_raise(TypeError){ a_num_int.quo(true) }
end
end
# Uses / to perform division, then converts the result to an integer
describe "Numeric#div" do
it "div right integers" do
a_num_int.div(a_den_int).should == 3
a_den_int.div(a_num_int).should == 0
end
it "div right integers and floats" do
a_num_int.div(a_den_flt).should == 3
a_den_int.div(a_num_int).should == 0
end
it "div right the integers and floats" do
a_num_int.div(a_den_flt).should == 3
a_den_int.div(a_num_int).should == 0
end
it "div right floats" do
a_num_flt.div(a_den_flt).should == 3
a_den_flt.div(a_num_int).should == 0
end
it " should div right with bignums and integers" do
a_num_bigint.div( a_possitive_number).should == 55590605665555
end
it "raise the expected exception" do
should_raise(ArgumentError){ a_num_int.div }
should_raise(ZeroDivisionError){ a_num_int.div(0) }
should_raise(TypeError){ a_num_int.div(nil) }
should_raise(TypeError){ a_num_int.div('test') }
should_raise(TypeError){ a_num_int.div(true) }
end
end
describe " Numeric#eql?" do
before(:each) do
@integer = 1
@float = 1.0
@bignum = 4294967296
@bigfloat = 4294967296.0
end
after(:each) do
@integer = nil
@float = nil
@bignum = nil
@bigfloat = nil
end
it "be equal (integers and floats)" do
@integer.eql?(@integer).should == true
@integer.eql?(@float).should == false
@float.eql?(@float).should == true
@float.eql?(@integer).should == false
end
it " should be equal (bignums and floats " do
@bignum.eql?(@bignum).should == true
@bignum.eql?(@bigfloat).should == false
end
it "be equal (edge cases)" do
0.eql?(0).should == true
1.0.eql?(1.00000000000000000000).should == true
0.eql?(0.0).should == false
000000.eql?(0.0).should == false
000000.eql?(000) .should == true
end
end
# Returns the largest integer less than or equal to num. Numeric implements
# this by converting anInteger to a Float and invoking Float#floor.
describe "Numeric#floor" do
it "return the largest integer less than or equal to num (integer)" do
0.floor.should == 0
a_possitive_number.floor.should == 100
a_negative_number.floor.should == -100
end
it "return the largest integer less than or equal to num (two complement)" do
a_possitive_bignumber.floor.should == 2147483648
a_negative_bignumber.floor.should == -2147483648
a_num_twopb.floor.should == 9223372036854775808
a_num_twonb.floor.should == -9223372036854775808
end
end
# Returns true if num is an Integer (including Fixnum and Bignum).
describe "Numeric#integer?" do
it "retrun true if the num is an integer?" do
0.integer?.should == true
a_possitive_number.integer?.should == true
a_negative_number.integer?.should == true
a_possitive_float.integer?.should == false
a_negative_float.integer?.should == false
a_possitive_bignumber.integer?.should == true
a_negative_bignumber.integer?.should == true
a_num_twopb.integer?.should == true
a_num_twonb.integer?.should == true
end
end
describe "Numeric#modulo" do
it " zero modulo x should be 0 (integer) " do
0.modulo(a_possitive_number).should == 0
0.modulo(a_negative_number).should == 0
end
it " zero modulo x should be 0 (float) " do
0.modulo(a_possitive_float).should == 0
0.modulo(a_negative_float).should == 0
end
it " zero modulo x should be 0 (bignum) " do
0.modulo(a_possitive_bignumber).should == 0
0.modulo(a_negative_bignumber).should == 0
end
it "y modulo x should be z (integer - integer) " do
a_possitive_number.modulo(a_possitive_number).should == 0
a_possitive_number.modulo(a_negative_number).should == 0
a_negative_number.modulo(a_possitive_number).should == 0
a_negative_number.modulo(a_negative_number).should == 0
end
it "y modulo x should be z (integer - float) " do
a_possitive_number.modulo(a_possitive_float).should_be_close(30.88,TOLERANCE)
a_possitive_number.modulo(a_negative_float).should_be_close(-3.68000000000001,TOLERANCE)
a_negative_number.modulo(a_possitive_float).should_be_close(3.68000000000001 ,TOLERANCE)
a_negative_number.modulo(a_negative_float).should_be_close(-30.88,TOLERANCE)
end
it "y modulo x should be z (integer - bignum) " do
a_possitive_number.modulo(a_possitive_bignumber).should == 100
a_possitive_number.modulo(a_negative_bignumber).should == -2147483548
a_negative_number.modulo(a_negative_bignumber).should == -100
a_negative_number.modulo(a_possitive_bignumber).should == 2147483548
end
it "modulo x should be z (floats and integers)"do
a_possitive_float.modulo(a_possitive_number).should_be_close(34.56,TOLERANCE)
a_possitive_float.modulo(a_negative_number).should_be_close(-65.44,TOLERANCE)
a_negative_float.modulo(a_negative_number).should_be_close(-34.56,TOLERANCE)
a_negative_float.modulo(a_possitive_number).should_be_close(65.44,TOLERANCE)
end
it "modulo x should be z (float - float)"do
a_possitive_float.modulo(a_possitive_float).should_be_close(0.0,TOLERANCE)
a_possitive_float.modulo(a_negative_float).should_be_close(0.0,TOLERANCE)
a_negative_float.modulo(a_negative_float).should_be_close(0.0,TOLERANCE)
a_negative_float.modulo(a_possitive_float).should_be_close(0.0,TOLERANCE)
end
it "modulo x should be z (float - bignum)"do
a_possitive_float.modulo(a_possitive_bignumber).should_be_close(34.56,TOLERANCE)
a_possitive_float.modulo(a_negative_bignumber).should_be_close(-2147483613.44 ,TOLERANCE)
a_negative_float.modulo(a_possitive_bignumber).should_be_close(2147483613.44 ,TOLERANCE)
end
it "y modulo x should be z (bignum -integer) "do
a_possitive_bignumber.modulo(a_possitive_number).should == 48
a_possitive_bignumber.modulo(a_negative_number).should == -52
a_negative_bignumber.modulo(a_possitive_number).should == 52
a_negative_bignumber.modulo(a_negative_number).should == -48
end
it "y modulo x should be z (bignum - float) "do
a_possitive_bignumber.modulo(a_possitive_float).should_be_close(1.27999985871492, TOLERANCE)
a_negative_bignumber.modulo(a_possitive_float).should_be_close(33.2800001412851 , TOLERANCE)
a_possitive_bignumber.modulo(a_negative_float).should_be_close(-33.2800001412851 , TOLERANCE)
a_negative_bignumber.modulo(a_negative_float).should_be_close(-1.27999985871492, TOLERANCE)
end
it "y modulo x should be z (bignum - bignum) "do
a_possitive_bignumber.modulo(a_possitive_bignumber).should == 0
a_negative_bignumber.modulo(a_possitive_bignumber).should == 0
a_possitive_bignumber.modulo(a_negative_bignumber).should == 0
a_negative_bignumber.modulo(a_negative_bignumber).should == 0
end
it " should NOT raise ZeroDivisionError if other is zero and is a Float" do
1.modulo(0.0).to_s.should == 'NaN'
1.modulo(0.0).to_s.should == 'NaN'
end
it " should raise an Exception when divide by 0 (non float)" do
should_raise(ZeroDivisionError){ a_num_int.modulo(0) }
end
end
# Returns num if num is not zero, nil otherwise.
# This behavior is useful when chaining comparisons:
describe "Numeric#nonzero?" do
it "return the vaule if number is different to 0" do
0.nonzero?.should == nil
1.nonzero?.should == 1
end
end
describe "Numeric#to_int" do
it "return the integer (integers)" do
0.to_int.should == 0
a_possitive_number.to_int.should == 100
a_negative_number.to_int.should == -100
end
it "return the integer part (float)" do
a_possitive_float.to_int.should == 34
a_negative_float.to_int.should == -34
end
it "return the integer part (two complement)" do
a_possitive_bignumber.to_int.should == 2147483648
a_negative_bignumber.to_int.should == -2147483648
a_num_twopb.to_int.should == 9223372036854775808
a_num_twonb.to_int.should == -9223372036854775808
end
end
# If num and numeric have different signs, returns mod-numeric;
# otherwise, returns mod. In both cases mod is the value num.modulo(numeric)
describe "Numeric#remainder" do
it "remainder the right integers" do
a_num_int.remainder(a_den_int).should == 1
a_num_int.remainder(a_den_int_n).should == 1
a_num_int_n.remainder(a_den_int).should == -1
a_num_int_n.remainder(a_den_int_n).should == -1
end
it "remainder right integers and floats" do
a_num_int.remainder(a_den_flt).should == 1.0
a_num_int.remainder(a_den_flt_n).should == 1.0
a_num_int_n.remainder(a_den_flt).should == -1.0
a_num_int_n.remainder(a_den_flt_n).should == -1.0
end
it "remainder right the integers and floats" do
11.5.remainder(a_den_flt).should == 3.5
11.5.remainder(a_den_flt_n).should == 3.5
-11.5.remainder(a_den_flt).should == -3.5
-11.5.remainder(a_den_flt_n).should == -3.5
end
it " should remainder right with bignums and integers" do
a_num_bigint.remainder( a_negative_number).should == 23
a_num_bigint.remainder( a_possitive_number).should == 23
a_negative_num_bignumber.remainder( a_possitive_den_bignumber).should == 0
a_negative_num_bignumber.remainder( a_possitive_den_bignumber).should == 0
end
it "raise the expected exception" do
should_raise(ArgumentError){ a_num_int.remainder }
should_raise(ZeroDivisionError){ a_num_int.remainder(0) }
should_raise(TypeError){ a_num_int.remainder(nil) }
should_raise(TypeError){ a_num_int.remainder('test') }
should_raise(TypeError){ a_num_int.remainder(true) }
end
end
# Rounds num to the nearest integer
describe "Numeric#round" do
it " round (down) " do
0.round.should == 0.0
a_possitive_number.round.should == 100
a_negative_number.round.should == -100
end
it " round (up) " do
a_possitive_float.round.should == 35
a_negative_float.round.should == -35
end
it " round twos complement " do
a_num_twopb.round.should == 9223372036854775808
a_num_twonb.round.should == -9223372036854775808
end
end
describe "Numeric#truncate" do
it " truncate integers " do
0.truncate.should == 0.0
a_possitive_number.truncate.should == 100
a_negative_number.truncate.should == -100
end
it " truncate floats " do
a_possitive_float.truncate.should == 34
a_negative_float.truncate.should == -34
end
it " truncate two complement " do
a_possitive_bignumber.truncate.should == 2147483648
a_negative_bignumber.truncate.should == -2147483648
a_num_twopb.truncate.should == 9223372036854775808
a_num_twonb.truncate.should == -9223372036854775808
end
end
# Invokes block with the sequence of numbers starting at num, incremented by
# step on each call. The loop finishes when the value to be passed to the block
# is greater than limit (if step is positive) or less than limit (if step is negative).
# If all the arguments are integers, the loop operates using an integer counter.
describe "Numeric#step" do
before(:each) do
@step = 2
@stepn = -2
@stepnf = -2
@stepf = 2.0
@limitf = 10.0
@limit = 10
@base = 1
@basef = 5.4
end
it "if base < limit > step then it should iterate (base-limit)/step times (integers)" do
x = []
@base.step(@limit, @step) { |i| x << i }
x.should == [1, 3, 5, 7, 9]
end
it "iterate one time if step is bigger than base-limit (integers)" do
x = []
@base.step(@limit, 11) { |i| x<< i }
x.should == [1]
end
it "not iterate if base is bigger than limit and step >0 (integers)" do
x = []
12.step(@limit, @step) { |i| x<< i }
x.should == []
end
it "iterate backward if base is bigger than limit (integers)" do
x = []
10.step(1, @stepn) { |i| x << i}
x.should == [10, 8, 6, 4, 2]
end
it "not iterate if base is minor than limit and step <0 (integers)" do
x = []
@base.step(@limit, @stepn) { |i| x<< i }
x.should == []
end
it "if base < limit > step then it should iterate (base-limit)/step times (integers)" do
x = []
@base.step(@limit, @step) { |i| x << i }
x.should == [1, 3, 5, 7, 9]
end
it "iterate one time if step is bigger than base-limit (integers)" do
x = []
@base.step(@limit, 11) { |i| x<< i }
x.should == [1]
end
it "if base < limit > step then it should iterate (base-limit)/step times (floats)" do
x = []
@basef.step(@limitf, @stepf) { |i| x << i }
x.should == [5.4, 7.4, 9.4]
end
it "iterate one time if step is bigger than base-limit (floats)" do
x = []
@basef.step(@limitf, 11) { |i| x<< i }
x.should == [5.4]
end
it "not iterate if base is bigger than limit and step >0 (floats)" do
x = []
12.0.step(@limitf, @stepf) { |i| x<< i }
x.should == []
end
it "iterate backward if base is bigger than limit (floats)" do
x = []
10.0.step(1.0, @stepnf) { |i| x << i}
x.should == [10, 8, 6, 4, 2]
end
it "not iterate if base is minor than limit and step <0 (floats)" do
x = []
@basef.step(@limitf, @stepnf) { |i| x<< i }
x.should == []
end
it "if base < limit > step then iterate (base-limit)/step times (floats)" do
x = []
@basef.step(@limitf, @stepf) { |i| x << i }
x.should == [5.4, 7.4, 9.4]
end
it "raise the expected exception" do
should_raise(ArgumentError){ @base.step }
should_raise(ArgumentError){ @base.step(100,0) }
should_raise(ArgumentError){ @base.step(nil) }
should_raise(ArgumentError){ @base.step('test') }
should_raise(ArgumentError){ @base.step(true, 123) }
should_raise(LocalJumpError){ @base.step(5, 3.4) }
should_raise(LocalJumpError){ @base.step(5.0, 2) }
should_raise(LocalJumpError){ @base.step(5.0, 1.0) }
end
end
describe "Numeric#zero?" do
it "return the vaule if number is different to 0" do
0.zero?.should == true
1.zero?.should == false
end
end
describe "Numeric#coerce" do
class String
def coerce(other)
case other
when Integer
begin
return other, Integer(self)
rescue
return Float(other), Float(self)
end
when Float
return other, Float(self)
else
super
end
end
end
before(:each) do
@integer = 1
@float = 2.5
end
it "coerce integers" do
@integer.coerce(@integer).should == [1,1]
@integer.coerce(@float).should ==[2.5,1]
end
it "coerce floats" do
@float.coerce(@float).should == [2.5,2.5]
@float.coerce(@integer).should == [1,2.5]
end
it "coerce with 0" do
@integer.coerce(0).should == [0,1]
@float.coerce(0).should == [0,2.5]
end
it "coerce with bignums " do
@integer.coerce(4294967296).should == [4294967296, 1]
@float.coerce(4294967296).should == [4294967296.0, 2.5]
end
it "coerce strings to strings" do
3.should == 1 + "2"
-1.3.should_be_close((1 - "2.3"), 0.001)
3.5.should == 1.2 + "2.3"
0.should == 1 - "1"
end
it "return the vaule if number is different to 0" do
should_raise(ArgumentError){ @integer.coerce("test") }
should_raise(TypeError){ @integer.coerce(nil) }
should_raise(TypeError){ @integer.coerce(false) }
should_raise(ArgumentError){ @integer.coerce }
end
end