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
|
require 'test/unit'
class TestFixnum < Test::Unit::TestCase
def setup
@verbose = $VERBOSE
$VERBOSE = nil
end
def teardown
$VERBOSE = @verbose
end
def test_pow
[1, 2, 2**64, 2**63*3, 2**64*3].each do |y|
[-1, 0, 1].each do |x|
z1 = x**y
z2 = (-x)**y
if y % 2 == 1
assert_equal(z2, -z1)
else
assert_equal(z2, z1)
end
end
end
end
def test_succ
assert_equal(0x40000000, 0x3fffffff.succ, "[ruby-dev:31189]")
assert_equal(0x4000000000000000, 0x3fffffffffffffff.succ, "[ruby-dev:31190]")
end
def test_pred
assert_equal(-0x40000001, (-0x40000000).pred)
assert_equal(-0x4000000000000001, (-0x4000000000000000).pred)
end
def test_plus
assert_equal(0x40000000, 0x3fffffff+1)
assert_equal(0x4000000000000000, 0x3fffffffffffffff+1)
assert_equal(-0x40000001, (-0x40000000)+(-1))
assert_equal(-0x4000000000000001, (-0x4000000000000000)+(-1))
assert_equal(-0x80000000, (-0x40000000)+(-0x40000000))
end
def test_sub
assert_equal(0x40000000, 0x3fffffff-(-1))
assert_equal(0x4000000000000000, 0x3fffffffffffffff-(-1))
assert_equal(-0x40000001, (-0x40000000)-1)
assert_equal(-0x4000000000000001, (-0x4000000000000000)-1)
assert_equal(-0x80000000, (-0x40000000)-0x40000000)
end
def test_mult
assert_equal(0x40000000, 0x20000000*2)
assert_equal(0x4000000000000000, 0x2000000000000000*2)
assert_equal(-0x40000001, 33025*(-32513))
assert_equal(-0x4000000000000001, 1380655685*(-3340214413))
assert_equal(0x40000000, (-0x40000000)*(-1))
end
def test_div
assert_equal(2, 5/2)
assert_equal(0, 1/2)
assert_equal(-1, -1/2)
assert_equal(0, -(1/2))
assert_equal(-1, (-1)/2)
assert_equal(0, (-1)/(-2))
assert_equal(-1, 1/(-2))
assert_equal(1, -(1/(-2)))
assert_equal(0x3fffffff, 0xbffffffd/3)
assert_equal(0x40000000, 0xc0000000/3)
assert_equal(0x4000000000000000, 0xc000000000000000/3)
assert_equal(-0x40000001, 0xc0000003/(-3))
assert_equal(-0x4000000000000001, 0xc000000000000003/(-3))
assert_equal(0x40000000, (-0x40000000)/(-1), "[ruby-dev:31210]")
assert_equal(0x4000000000000000, (-0x4000000000000000)/(-1))
end
def test_mod
assert_equal(2, (-0x40000000) % 3)
assert_equal(0, (-0x40000000) % (-1))
end
def test_divmod
(-5).upto(5) {|a|
(-5).upto(5) {|b|
next if b == 0
q, r = a.divmod(b)
assert_equal(a, b*q+r)
assert(r.abs < b.abs)
assert(0 < b ? (0 <= r && r < b) : (b < r && r <= 0))
assert_equal(q, a/b)
assert_equal(q, a.div(b))
assert_equal(r, a%b)
assert_equal(r, a.modulo(b))
}
}
end
def test_not
assert_equal(-0x40000000, ~0x3fffffff)
assert_equal(0x3fffffff, ~-0x40000000)
end
def test_lshift
assert_equal(0x40000000, 0x20000000 << 1)
assert_equal(-0x40000000, (-0x20000000) << 1)
assert_equal(-0x80000000, (-0x40000000) << 1)
end
def test_rshift
assert_equal(0x20000000, 0x40000000 >> 1)
assert_equal(-0x20000000, (-0x40000000) >> 1)
assert_equal(-0x40000000, (-0x80000000) >> 1)
end
def test_abs
assert_equal(0x40000000, (-0x40000000).abs)
assert_equal(0x4000000000000000, (-0x4000000000000000).abs)
end
def test_to_s
assert_equal("1010", 10.to_s(2))
assert_equal("a", 10.to_s(36))
assert_raise(ArgumentError) { 10.to_s(1) }
end
def test_plus2
assert_equal(2, 1 + 1)
assert_equal(4294967297, 1 + 2**32)
assert_equal(2.0, 1 + 1.0)
assert_raise(TypeError) { 1 + nil }
end
def test_minus
assert_equal(0, 1 - 1)
assert_equal(-4294967295, 1 - 2**32)
assert_equal(0.0, 1 - 1.0)
assert_raise(TypeError) { 1 - nil }
end
def test_mul
assert_equal(6, 2.send(:*, 3))
a = 2**30-1
assert_equal(1152921502459363329, a.send(:*, a))
assert_equal(6.0, 2 * 3.0)
assert_raise(TypeError) { 2 * nil }
end
def test_divide
assert_equal(2.0, 4.quo(2))
assert_equal(2.0, 4 / 2)
assert_equal(2.0, 4.div(2))
assert_equal(0.5**32, 1.quo(2**32))
assert_equal(0, 1 / (2**32))
assert_equal(0, 1.div(2**32))
assert_equal(0.5, 1.quo(2.0))
assert_equal(0.5, 1 / 2.0)
assert_equal(0, 1.div(2.0))
### rational changes the behavior of Fixnum#quo
#assert_raise(TypeError) { 2.quo(nil) }
assert_raise(TypeError, NoMethodError) { 2.quo(nil) }
assert_raise(TypeError) { 2 / nil }
assert_raise(TypeError) { 2.div(nil) }
assert_equal(0, 4.modulo(2))
assert_equal(1, 1.modulo(2**32))
assert_equal(1, 1.modulo(2.0))
assert_raise(TypeError) { 2.modulo(nil) }
assert_equal([2, 0], 4.divmod(2))
assert_equal([0, 1], 1.divmod(2**32))
assert_equal([0, 1], 1.divmod(2.0))
assert_raise(TypeError) { 2.divmod(nil) }
end
def test_pow2
assert_equal(65536, 2**16)
assert_equal(4294967296, 2**32)
assert_equal(0.5**16, 2**-16)
assert_equal(1, (-1)**4294967296)
assert_equal(-1, (-1)**4294967295)
assert_equal(4, 2**((2**32).coerce(2).first))
assert_equal(2, 4**0.5)
assert_equal(0, 0**0.5)
assert_equal(1, (0**-1.0).infinite?)
### rational changes the behavior of Fixnum#**
#assert_raise(TypeError) { 1 ** nil }
assert_raise(TypeError, NoMethodError) { 1 ** nil }
end
def test_cmp
assert(1 != nil)
assert_equal(0, 1 <=> 1)
assert_equal(-1, 1 <=> 4294967296)
assert_equal(0, 1 <=> 1.0)
assert_nil(1 <=> nil)
assert(1.send(:>, 0))
assert(!(1.send(:>, 1)))
assert(!(1.send(:>, 2)))
assert(!(1.send(:>, 4294967296)))
assert(1.send(:>, 0.0))
assert_raise(ArgumentError) { 1.send(:>, nil) }
assert(1.send(:>=, 0))
assert(1.send(:>=, 1))
assert(!(1.send(:>=, 2)))
assert(!(1.send(:>=, 4294967296)))
assert(1.send(:>=, 0.0))
assert_raise(ArgumentError) { 1.send(:>=, nil) }
assert(!(1.send(:<, 0)))
assert(!(1.send(:<, 1)))
assert(1.send(:<, 2))
assert(1.send(:<, 4294967296))
assert(!(1.send(:<, 0.0)))
assert_raise(ArgumentError) { 1.send(:<, nil) }
assert(!(1.send(:<=, 0)))
assert(1.send(:<=, 1))
assert(1.send(:<=, 2))
assert(1.send(:<=, 4294967296))
assert(!(1.send(:<=, 0.0)))
assert_raise(ArgumentError) { 1.send(:<=, nil) }
end
end
|