# Exhaustive test for bignum.c

**URL:** <https://www.ruby-forum.com/t/exhaustive-test-for-bignum-c/126703>\
**Category:** Development\
**Created:** [January 21, 2008, 2:57pm UTC](https://www.ruby-forum.com/t/exhaustive-test-for-bignum-c/126703 "2008-01-21T14:57:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yusuke\_ENDOH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yusuke\_ENDOH](https://www.ruby-forum.com/u/Yusuke_ENDOH)\
**Post date:** [January 21, 2008, 2:57pm UTC](https://www.ruby-forum.com/t/exhaustive-test-for-bignum-c/126703/1 "2008-01-21T14:57:24Z")

</div>

e$B1sF#$H?=$7$^$9!#e(B

test\_bignum.rb e$B$K%F%9%H$rDI2C$7$FLVMe@-$r\>e$2$F$\_$^$7$?!#e(B  
gcov e$B$G$Ne(B bignum.c e$B$N%+%P%l%C%8$,e(B 50% e$B$+$ie(B 90%  
e$B$K$J$j$^$9!#e(B  
e$B$I$&$G$7$g$&$+!#e(B

$ ./ruby test/ruby/test\_bignum.rb  
Loaded suite test/ruby/test\_bignum  
Started  
…test/ruby/test\_bignum.rb:251: warning: in  
a**b, b may be too big  
test/ruby/test\_bignum.rb:252: warning: in a**b, b may be too big  
…/home/mame/work/ruby19/gcov/local/lib/ruby/1.9.0/test/unit/assertions.rb:83:  
warning: Bignum out of Float range  
/home/mame/work/ruby19/gcov/local/lib/ruby/1.9.0/test/unit/assertions.rb:83:  
warning: Bignum out of Float range  
/home/mame/work/ruby19/gcov/local/lib/ruby/1.9.0/test/unit/assertions.rb:83:  
warning: Bignum out of Float range  
…test/ruby/test\_bignum.rb:162: warning: Bignum out of Float range  
…  
Finished in 2.3600356 seconds.

37 tests, 299 assertions, 0 failures, 0 errors

$ gcov bignum.c  
File ‘/usr/include/sys/sysmacros.h’  
Lines executed:0.00% of 6  
/usr/include/sys/sysmacros.h:creating ‘sysmacros.h.gcov’

File ‘./include/ruby/ruby.h’  
Lines executed:45.45% of 22  
./include/ruby/ruby.h:creating ‘ruby.h.gcov’

File ‘bignum.c’  
Lines executed:93.01% of 1187  
bignum.c:creating ‘bignum.c.gcov’

# Index: test/ruby/test\_bignum.rb

— test/ruby/test\_bignum.rb (revision 15159)  
+++ test/ruby/test\_bignum.rb (working copy)  
@@ -95,4 +95,257 @@  
assert\_equal(“1777777777777777777777”  
,18446744073709551615.to\_s(8))  
assert\_equal("-1777777777777777777777"  
,-18446744073709551615.to\_s(8))  
end  
+  
+

- T\_ZERO = (2\*\*32).coerce(0).first
- T\_ONE = (2\*\*32).coerce(1).first
- T\_MONE = (2\*\*32).coerce(-1).first
- T31 = 2\*\*31 # 2147483648
- T31P = T31 - 1 # 2147483647
- T32 = 2\*\*32 # 4294967296
- T32P = T32 - 1 # 4294967295
- T64 = 2\*\*64 # 18446744073709551616
- T64P = T64 - 1 # 18446744073709551615
- 
- def test\_big\_2comp
- assert\_equal("-4294967296", (~T32P).to\_s)
- assert\_equal("…f00000000", “%x” % -T32)
- end
- 
- def test\_int2inum
- assert\_equal([T31P], [T31P].pack(“I”).unpack(“I”))
- assert\_equal([T31P], [T31P].pack(“i”).unpack(“i”))
- end
- 
- def test\_quad\_pack
- assert\_equal([1], [1].pack(“q”).unpack(“q”))
- assert\_equal([- 1], [- 1].pack(“q”).unpack(“q”))
- assert\_equal([T31P], [T31P].pack(“q”).unpack(“q”))
- assert\_equal([-T31P], [-T31P].pack(“q”).unpack(“q”))
- assert\_equal([T64P], [T64P].pack(“Q”).unpack(“Q”))
- assert\_equal([0], [T64].pack(“Q”).unpack(“Q”))
- end
- 
- def test\_str\_to\_inum
- assert\_equal(1, " +1".to\_i)
- assert\_equal(-1, " -1".to\_i)
- assert\_equal(0, “++1”.to\_i)
- assert\_equal(73, “111”.oct)
- assert\_equal(273, “0x111”.oct)
- assert\_equal(7, “0b111”.oct)
- assert\_equal(73, “0o111”.oct)
- assert\_equal(111, “0d111”.oct)
- assert\_equal(73, “0111”.oct)
- assert\_equal(111, Integer(“111”))
- assert\_equal(13, “111”.to\_i(3))
- assert\_raise(ArgumentError) { “111”.to\_i(37) }
- assert\_equal(1333, “111”.to\_i(36))
- assert\_equal(1057, “111”.to\_i(32))
- assert\_equal(0, “00a”.to\_i)
- assert\_equal(1, Integer("1 "))
- assert\_raise(ArgumentError) { Integer(“1\_”) }
- assert\_raise(ArgumentError) { Integer(“1\_\_”) }
- assert\_raise(ArgumentError) { Integer(“1\_0 x”) }
- assert\_equal(T31P, “1111111111111111111111111111111”.to\_i(2))
- end
- 
- def test\_to\_s2
- assert\_raise(ArgumentError) { T31P.to\_s(37) }
- assert\_equal(32768, (10\*\*32768-1).to\_s.size)
- assert\_raise(RangeError) { Process.wait(1, T64P) }
- assert\_equal(“0”, T\_ZERO.to\_s)
- assert\_equal(“1”, T\_ONE.to\_s)
- end
- 
- def test\_to\_f
- assert\_nothing\_raised { T31P.to\_f.to\_i }
- assert\_raise(FloatDomainError) { (1024\*\*1024).to\_f.to\_i }
- end
- 
- def test\_cmp
- assert(T31P \> 1)
- assert(T31P \< 2147483648.0)
- assert(T31P \< T64P)
- assert(T64P \> T31P)
- assert\_raise(ArgumentError) { T31P \< “foo” }
- end
- 
- def test\_eq
- assert(T31P != 1)
- assert(T31P == 2147483647.0)
- assert(T31P != “foo”)
- end
- 
- def test\_eql
- assert(T31P.eql?(T31P))
- end
- 
- def test\_convert
- assert\_equal([255], [T\_MONE].pack(“C”).unpack(“C”))
- assert\_equal([0], [T32].pack(“C”).unpack(“C”))
- assert\_raise(RangeError) { 0.to\_s(T32) }
- assert\_raise(Errno::EINVAL) { Process.wait(0, T32P) }
- assert\_raise(RangeError) { Process.wait(0, T32) }
- assert\_raise(RangeError) { Process.wait(0, -T32P) }
- end
- 
- def test\_sub
- assert\_equal(-T31, T32 - (T32 + T31))
- end
- 
- def test\_plus
- assert\_equal(T64 + T32, T32 + T64)
- assert\_equal(T32.to\_f, T32P + 1.0)
- assert\_raise(TypeError) { T32 + “foo” }
- end
- 
- def test\_minus
- assert\_equal(T32P.to\_f, T32 - 1.0)
- assert\_raise(TypeError) { T32 - “foo” }
- end
- 
- def test\_mul
- assert\_equal(T32.to\_f, T32 \* 1.0)
- assert\_raise(TypeError) { T32 \* “foo” }
- end
- 
- def test\_divrem
- assert\_equal(0, T32 / T64)
- end
- 
- def test\_div
- assert\_equal(T32.to\_f, T32 / 1.0)
- assert\_raise(TypeError) { T32 / “foo” }
- end
- 
- def test\_modulo
- assert\_raise(TypeError) { T32 % “foo” }
- end
- 
- def test\_remainder
- assert\_equal(0, T32.remainder(1))
- assert\_raise(TypeError) { T32.remainder(“foo”) }
- end
- 
- def test\_divmod
- assert\_equal([T32, 0], T32.divmod(1))
- assert\_equal([2, 0], T32.divmod(T31))
- assert\_raise(TypeError) { T32.divmod(“foo”) }
- end
- 
- def test\_quo
- assert\_equal(T32.to\_f, T32.quo(1))
- assert\_equal(T32.to\_f, T32.quo(1.0))
- assert\_equal(T32.to\_f, T32.quo(T\_ONE))
- assert\_raise(TypeError) { T32.quo(“foo”) }
- assert\_equal(1024**1024, (1024**1024).quo(1))
- assert\_equal(1024**1024, (1024**1024).quo(1.0))
- assert\_equal(1024**1024\*2, (1024**1024\*2).quo(1))
- inf = 1 / 0.0; nan = inf / inf
- assert\_raise(FloatDomainError) { (1024\*\*1024\*2).quo(nan) }
- end
- 
- def test\_pow
- assert\_equal(1.0, T32 \*\* 0.0)
- assert\_equal(1.0 / T32, T32 \*\* -1)
- assert((T32 \*\* T32).infinite?)
- assert((T32 \*\* (2\*\*30-1)).infinite?)
- assert\_raise(TypeError) { T32\*\*“foo” }
- end
- 
- def test\_and
- assert\_equal(0, T32 & 1)
- assert\_equal(-T32, (-T32) & (-T31))
- assert\_equal(0, T32 & T64)
- end
- 
- def test\_or
- assert\_equal(T32 + 1, T32 | 1)
- assert\_equal(T32 + T31, T32 | T31)
- assert\_equal(-T31, (-T32) | (-T31))
- assert\_equal(T64 + T32, T32 | T64)
- end
- 
- def test\_xor
- assert\_equal(T32 + 1, T32 ^ 1)
- assert\_equal(T32 + T31, T32 ^ T31)
- assert\_equal(T31, (-T32) ^ (-T31))
- assert\_equal(T64 + T32, T32 ^ T64)
- end
- 
- def test\_shift2
- assert\_equal(2**33, (2**32) \<\< 1)
- assert\_equal(2**31, (2**32) \<\< -1)
- assert\_equal(2**33, (2**32) \<\< 1.0)
- assert\_equal(2**31, (2**32) \<\< -1.0)
- assert\_equal(2**33, (2**32) \<\< T\_ONE)
- assert\_equal(2**31, (2**32) \<\< T\_MONE)
- assert\_equal(2**31, (2**32) \>\> 1)
- assert\_equal(2**33, (2**32) \>\> -1)
- assert\_equal(2**31, (2**32) \>\> 1.0)
- assert\_equal(2**33, (2**32) \>\> -1.0)
- assert\_equal(2**31, (2**32) \>\> T\_ONE)
- assert\_equal(2**33, (2**32) \>\> T\_MONE)
- assert\_equal( 0, (2**32) \>\> (2**32))
- assert\_equal(-1, -(2**32) \>\> (2**32))
- assert\_equal( 0, (2\*\*32) \>\> 128)
- assert\_equal(-1, -(2\*\*32) \>\> 128)
- assert\_equal( 0, (2\*\*31) \>\> 32)
- assert\_equal(-1, -(2\*\*31) \>\> 32)
- end
- 
- def test\_aref
- assert\_equal(0, (2\*\*32)[0])
- assert\_equal(0, (2**32)[2**32])
- assert\_equal(0, (2**32)[-(2**32)])
- assert\_equal(0, (2\*\*32)[T\_ZERO])
- assert\_equal(0, (-(2\*\*64))[0])
- assert\_equal(1, (-2\*\*256)[256])
- end
- 
- def test\_hash
- assert\_nothing\_raised { T31P.hash }
- end
- 
- def test\_coerce
- assert\_equal([T64P, T31P], T31P.coerce(T64P))
- assert\_raise(TypeError) { T31P.coerce(nil) }
- end
- 
- def test\_abs
- assert\_equal(T31P, (-T31P).abs)
- end
- 
- def test\_size
- assert(T31P.size.is\_a?(Integer))
- end
- 
- def test\_odd
- assert\_equal(true, (2\*\*32+1).odd?)
- assert\_equal(false, (2\*\*32).odd?)
- end
- 
- def test\_even
- assert\_equal(false, (2\*\*32+1).even?)
- assert\_equal(true, (2\*\*32).even?)
- end
- 
- def interrupt
- time = Time.now
- start\_flag = false
- end\_flag = false
- thread = Thread.new do
- 

```
 start_flag = true

```

- 

```
 yield

```

- 

```
 end_flag = true

```

- end
- sleep 1
- thread.raise
- thread.join rescue nil
- start\_flag && !end\_flag && Time.now - time \< 10
- end
- 
- def test\_interrupt
- assert(interrupt { (65536 \*\* 65536).to\_s })
- end  
end

---

<div class="post-metadata">

**Author:** ![Yusuke\_ENDOH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yusuke\_ENDOH](https://www.ruby-forum.com/u/Yusuke_ENDOH)\
**Post date:** [January 21, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/exhaustive-test-for-bignum-c/126703/2 "2008-01-21T15:28:08Z")

</div>

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:33258] exhaustive test for bignum.c”  
on Mon, 21 Jan 2008 22:56:26 +0900, “Yusuke ENDOH” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|test\_bignum.rb e$B$K%F%9%H$rDI2C$7$FLVMe@-$r\>e$2$F$\_$^$7$?!#e(B  
|gcov e$B$G$Ne(B bignum.c e$B$N%+%P%l%C%8$,e(B 50% e$B$+$ie(B 90% e$B$K$J$j$^$9!#e(B  
|e$B$I$&$G$7$g$&$+!#e(B

e$B%3%\_%C%H$7$F$/$@$5$$!#e(B

---

<div class="post-metadata">

**Author:** ![Yusuke\_ENDOH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yusuke\_ENDOH](https://www.ruby-forum.com/u/Yusuke_ENDOH)\
**Post date:** [January 21, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/exhaustive-test-for-bignum-c/126703/3 "2008-01-21T20:35:31Z")

</div>

In article  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
“Yusuke ENDOH” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> test\_bignum.rb e$B$K%F%9%H$rDI2C$7$FLVMe@-$r\>e$2$F$\_$^$7$?!#e(B  
> gcov e$B$G$Ne(B bignum.c e$B$N%+%P%l%C%8$,e(B 50% e$B$+$ie(B 90% e$B$K$J$j$^$9!#e(B  
> e$B$I$&$G$7$g$&$+!#e(B

rational e$B$re(B require  
e$B$7$?\>uBV$@$H%F%9%H$,$U$?$D\<:GT$9$k$h$&$G!"e(B  
test-all e$B$G$N\<:GT$,A}$($F$$$^$9!#e(B  
[http://www.rubyist.net/~akr/chkbuild/debian-sarge/ruby-trunk/log/20080122T030900.diff.txt.gz](http://www.rubyist.net/~akr/chkbuild/debian-sarge/ruby-trunk/log/20080122T030900.diff.txt.gz)

e$B$^$!!"e(Brational e$B$,0-$$$h$&$J5$$,$7$^$9$,!#e(B

% ./ruby -rrational test/ruby/test\_bignum.rb  
Loaded suite test/ruby/test\_bignum  
Started  
…/home/akr/ruby/yarvo0/lib/ruby/1.9.0/rational.rb:517:  
warning: in a**b, b may be too big  
/home/akr/ruby/yarvo0/lib/ruby/1.9.0/rational.rb:517: warning: in a**b,  
b may be too big  
F.F…test/ruby/test\_bignum.rb:162: warning: Bignum out of Float  
range  
…  
Finished in 2.752199378 seconds.

1. Failure:  
test\_pow(TestBignum) [test/ruby/test\_bignum.rb:252]:  
 exception expected but was  
Class:   
Message: \<“comparison of String with 0 failed”\>  
—Backtrace—  
/home/akr/ruby/yarvo0/lib/ruby/1.9.0/rational.rb:516:in `>=' /home/akr/ruby/yarvo0/lib/ruby/1.9.0/rational.rb:516:in `rpower’  
test/ruby/test\_bignum.rb:252:in `block in test_pow' test/ruby/test_bignum.rb:252:in `test\_pow’

* * *

1. Failure:  
test\_quo(TestBignum) [test/ruby/test\_bignum.rb:239]:  
 exception expected but was  
Class:   
Message: \<"undefined method `coerce' for \"foo\":String"> ---Backtrace--- /home/akr/ruby/yarvo0/lib/ruby/1.9.0/rational.rb:204:in `/’  
/home/akr/ruby/yarvo0/lib/ruby/1.9.0/rational.rb:510:in `quo' test/ruby/test_bignum.rb:239:in `block in test\_quo’  
test/ruby/test\_bignum.rb:239:in `test\_quo’

* * *

37 tests, 294 assertions, 2 failures, 0 errors

---

<div class="post-metadata">

**Author:** ![Yusuke\_ENDOH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yusuke\_ENDOH](https://www.ruby-forum.com/u/Yusuke_ENDOH)\
**Post date:** [January 22, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/exhaustive-test-for-bignum-c/126703/4 "2008-01-22T14:36:59Z")

</div>

e$B1sF#$G$9!#e(B

08/01/22 e$B$Ke(B Tanaka A.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) e$B$5$s$O=q$-$^$7$?e(B:

> rational e$B$re(B require e$B$7$?\>uBV$@$H%F%9%H$,$U$?$D\<:GT$9$k$h$&$G!"e(B  
> test-all e$B$G$N\<:GT$,A}$($F$$$^$9!#e(B  
> [http://www.rubyist.net/~akr/chkbuild/debian-sarge/ruby-trunk/log/20080122T030900.diff.txt.gz](http://www.rubyist.net/~akr/chkbuild/debian-sarge/ruby-trunk/log/20080122T030900.diff.txt.gz)

make test-all e$B$G3NG’$7$F$^$;$s$G$7$?!#$9$\_$^$;$s!#e(B  
e$B1~5^=hCV$G$9$,!"LdBj$H$J$C$F$$$k%F%9%H$@$14K$a$F$\*$-$^$7$?!#e(B
