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
|
require_relative 'spec_helper'
load_extension("bignum")
def ensure_bignum(n)
raise "Bignum#coerce returned Fixnum" if fixnum_min <= n && n <= fixnum_max
n
end
full_range_longs = (fixnum_max == 2**(0.size * 8 - 1) - 1)
describe "CApiBignumSpecs" do
before :each do
@s = CApiBignumSpecs.new
if full_range_longs
@max_long = 2**(0.size * 8 - 1) - 1
@min_long = -@max_long - 1
@max_ulong = ensure_bignum(2**(0.size * 8) - 1)
else
@max_long = ensure_bignum(2**(0.size * 8 - 1) - 1)
@min_long = ensure_bignum(-@max_long - 1)
@max_ulong = ensure_bignum(2**(0.size * 8) - 1)
end
end
describe "rb_big2long" do
unless full_range_longs
it "converts a Bignum" do
@s.rb_big2long(@max_long).should == @max_long
@s.rb_big2long(@min_long).should == @min_long
end
end
it "raises RangeError if passed Bignum overflow long" do
-> { @s.rb_big2long(ensure_bignum(@max_long + 1)) }.should raise_error(RangeError)
-> { @s.rb_big2long(ensure_bignum(@min_long - 1)) }.should raise_error(RangeError)
end
end
describe "rb_big2ll" do
unless full_range_longs
it "converts a Bignum" do
@s.rb_big2ll(@max_long).should == @max_long
@s.rb_big2ll(@min_long).should == @min_long
end
end
it "raises RangeError if passed Bignum overflow long" do
-> { @s.rb_big2ll(ensure_bignum(@max_long << 40)) }.should raise_error(RangeError)
-> { @s.rb_big2ll(ensure_bignum(@min_long << 40)) }.should raise_error(RangeError)
end
end
describe "rb_big2ulong" do
it "converts a Bignum" do
@s.rb_big2ulong(@max_ulong).should == @max_ulong
end
unless full_range_longs
it "wraps around if passed a negative bignum" do
@s.rb_big2ulong(ensure_bignum(@min_long + 1)).should == -(@min_long - 1)
@s.rb_big2ulong(ensure_bignum(@min_long)).should == -(@min_long)
end
end
it "raises RangeError if passed Bignum overflow long" do
-> { @s.rb_big2ulong(ensure_bignum(@max_ulong + 1)) }.should raise_error(RangeError)
-> { @s.rb_big2ulong(ensure_bignum(@min_long - 1)) }.should raise_error(RangeError)
end
end
describe "rb_big2dbl" do
it "converts a Bignum to a double value" do
@s.rb_big2dbl(ensure_bignum(Float::MAX.to_i)).eql?(Float::MAX).should == true
end
it "returns Infinity if the number is too big for a double" do
huge_bignum = ensure_bignum(Float::MAX.to_i * 2)
@s.rb_big2dbl(huge_bignum).should == infinity_value
end
it "returns -Infinity if the number is negative and too big for a double" do
huge_bignum = -ensure_bignum(Float::MAX.to_i * 2)
@s.rb_big2dbl(huge_bignum).should == -infinity_value
end
end
describe "rb_big2str" do
it "converts a Bignum to a string with base 10" do
@s.rb_big2str(ensure_bignum(2**70), 10).eql?("1180591620717411303424").should == true
end
it "converts a Bignum to a string with a different base" do
@s.rb_big2str(ensure_bignum(2**70), 16).eql?("400000000000000000").should == true
end
end
describe "RBIGNUM_SIGN" do
it "returns 1 for a positive Bignum" do
@s.RBIGNUM_SIGN(bignum_value(1)).should == 1
end
it "returns 0 for a negative Bignum" do
@s.RBIGNUM_SIGN(-bignum_value(1)).should == 0
end
end
describe "rb_big_cmp" do
it "compares a Bignum with a Bignum" do
@s.rb_big_cmp(bignum_value, bignum_value(1)).should == -1
end
it "compares a Bignum with a Fixnum" do
@s.rb_big_cmp(bignum_value, 5).should == 1
end
end
describe "rb_big_pack" do
it "packs a Bignum into an unsigned long" do
val = @s.rb_big_pack(@max_ulong)
val.should == @max_ulong
end
platform_is wordsize: 64 do
it "packs max_ulong into 2 ulongs to allow sign bit" do
val = @s.rb_big_pack_length(@max_ulong)
val.should == 2
val = @s.rb_big_pack_array(@max_ulong, 2)
val[0].should == @max_ulong
val[1].should == 0
end
it "packs a 72-bit positive Bignum into 2 unsigned longs" do
num = 2 ** 71
val = @s.rb_big_pack_length(num)
val.should == 2
end
it "packs a 72-bit positive Bignum into correct 2 longs" do
num = 2 ** 71 + 1
val = @s.rb_big_pack_array(num, 2)
val[0].should == 1;
val[1].should == 0x80;
end
it "packs a 72-bit negative Bignum into correct 2 longs" do
num = -(2 ** 71 + 1)
val = @s.rb_big_pack_array(num, @s.rb_big_pack_length(num))
val[0].should == @max_ulong;
val[1].should == @max_ulong - 0x80;
end
it "packs lower order bytes into least significant bytes of longs for positive bignum" do
num = 0
32.times { |i| num += i << (i * 8) }
val = @s.rb_big_pack_array(num, @s.rb_big_pack_length(num))
val.size.should == 4
32.times do |i|
a_long = val[i/8]
a_byte = (a_long >> ((i % 8) * 8)) & 0xff
a_byte.should == i
end
end
it "packs lower order bytes into least significant bytes of longs for negative bignum" do
num = 0
32.times { |i| num += i << (i * 8) }
num = -num
val = @s.rb_big_pack_array(num, @s.rb_big_pack_length(num))
val.size.should == 4
expected_bytes = [0x00, 0xff, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8,
0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9, 0xe8,
0xe7, 0xe6, 0xe5, 0xe4, 0xe3, 0xe2, 0xe1, 0xe0 ]
32.times do |i|
a_long = val[i/8]
a_byte = (a_long >> ((i % 8) * 8)) & 0xff
a_byte.should == expected_bytes[i]
end
end
end
end
describe "rb_dbl2big" do
it "returns a Fixnum for a Fixnum input value" do
val = @s.rb_dbl2big(2)
val.kind_of?(Integer).should == true
val.should == 2
end
it "returns a Fixnum for a Float input value" do
val = @s.rb_dbl2big(2.5)
val.kind_of?(Integer).should == true
val.should == 2
end
it "returns a Bignum for a large enough Float input value" do
input = 219238102380912830988.5 # chosen by fair dice roll
val = @s.rb_dbl2big(input)
val.kind_of?(Integer).should == true
# This value is based on the output of a simple C extension that uses
# rb_dbl2big() to convert the above input value to a Bignum.
val.should == 219238102380912836608
end
it "raises FloatDomainError for Infinity values" do
inf = 1.0 / 0
-> { @s.rb_dbl2big(inf) }.should raise_error(FloatDomainError)
end
it "raises FloatDomainError for NaN values" do
nan = 0.0 / 0
-> { @s.rb_dbl2big(nan) }.should raise_error(FloatDomainError)
end
end
end
|