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
|
##
# Integer ISO Test
assert('Integer', '15.2.8') do
assert_equal Class, Integer.class
end
assert('Integer#+', '15.2.8.3.1') do
a = 1+1
b = 1+1.0
assert_equal 2, a
assert_equal 2.0, b
assert_raise(TypeError){ 0+nil }
assert_raise(TypeError){ 1+nil }
c = Mrbtest::FIXNUM_MAX + 1
d = Mrbtest::FIXNUM_MAX.__send__(:+, 1)
e = Mrbtest::FIXNUM_MAX + 1.0
assert_equal Float, c.class
assert_equal Float, d.class
assert_float e, c
assert_float e, d
end
assert('Integer#-', '15.2.8.3.2') do
a = 2-1
b = 2-1.0
assert_equal 1, a
assert_equal 1.0, b
c = Mrbtest::FIXNUM_MIN - 1
d = Mrbtest::FIXNUM_MIN.__send__(:-, 1)
e = Mrbtest::FIXNUM_MIN - 1.0
assert_equal Float, c.class
assert_equal Float, d.class
assert_float e, c
assert_float e, d
end
assert('Integer#*', '15.2.8.3.3') do
a = 1*1
b = 1*1.0
assert_equal 1, a
assert_equal 1.0, b
assert_raise(TypeError){ 0*nil }
assert_raise(TypeError){ 1*nil }
c = Mrbtest::FIXNUM_MAX * 2
d = Mrbtest::FIXNUM_MAX.__send__(:*, 2)
e = Mrbtest::FIXNUM_MAX * 2.0
assert_equal Float, c.class
assert_equal Float, d.class
assert_float e, c
assert_float e, d
end
assert('Integer#/', '15.2.8.3.4') do
a = 2/1
b = 2/1.0
assert_equal 2, a
assert_equal 2.0, b
end
assert('Integer#%', '15.2.8.3.5') do
a = 1%1
b = 1%1.0
c = 2%4
d = 2%5
e = 2%-5
f = -2%5
g = -2%-5
h = 2%-2
i = -2%2
j = -2%-2
assert_equal 0, a
assert_equal 0.0, b
assert_equal 2, c
assert_equal 2, d
assert_equal(-3, e)
assert_equal 3, f
assert_equal(-2, g)
assert_equal 0, h
assert_equal 0, i
assert_equal 0, j
end
assert('Integer#<=>', '15.2.9.3.6') do
a = 1<=>0
b = 1<=>1
c = 1<=>2
assert_equal 1, a
assert_equal 0, b
assert_equal(-1, c)
end
assert('Integer#==', '15.2.8.3.7') do
a = 1==0
b = 1==1
assert_false a
assert_true b
end
assert('Integer#~', '15.2.8.3.8') do
# Complement
assert_equal(-1, ~0)
assert_equal(-3, ~2)
end
assert('Integer#&', '15.2.8.3.9') do
# Bitwise AND
# 0101 (5)
# & 0011 (3)
# = 0001 (1)
assert_equal 1, 5 & 3
end
assert('Integer#|', '15.2.8.3.10') do
# Bitwise OR
# 0101 (5)
# | 0011 (3)
# = 0111 (7)
assert_equal 7, 5 | 3
end
assert('Integer#^', '15.2.8.3.11') do
# Bitwise XOR
# 0101 (5)
# ^ 0011 (3)
# = 0110 (6)
assert_equal 6, 5 ^ 3
end
assert('Integer#<<', '15.2.8.3.12') do
# Left Shift by one
# 00010111 (23)
# = 00101110 (46)
assert_equal 46, 23 << 1
# Left Shift by a negative is Right Shift
assert_equal 23, 46 << -1
# Left Shift by 31 is bitShift overflow to SignedInt
assert_equal 2147483648, 1 << 31
# -3 Left Shift by 30 is bitShift overflow to SignedInt
assert_equal(-3221225472, -3 << 30)
end
assert('Integer#>>', '15.2.8.3.13') do
# Right Shift by one
# 00101110 (46)
# = 00010111 (23)
assert_equal 23, 46 >> 1
# Right Shift by a negative is Left Shift
assert_equal 46, 23 >> -1
# Don't raise on large Right Shift
assert_equal 0, 23 >> 128
end
assert('Integer#ceil', '15.2.8.3.14') do
assert_equal 10, 10.ceil
end
assert('Integer#downto', '15.2.8.3.15') do
a = 0
3.downto(1) do |i|
a += i
end
assert_equal 6, a
end
assert('Integer#eql?', '15.2.8.3.16') do
a = 1.eql?(1)
b = 1.eql?(2)
c = 1.eql?(nil)
assert_true a
assert_false b
assert_false c
end
assert('Integer#floor', '15.2.8.3.17') do
a = 1.floor
assert_equal 1, a
end
assert('Integer#next', '15.2.8.3.19') do
assert_equal 2, 1.next
end
assert('Integer#round', '15.2.8.3.20') do
assert_equal 1, 1.round
end
assert('Integer#succ', '15.2.8.3.21') do
assert_equal 2, 1.succ
end
assert('Integer#times', '15.2.8.3.22') do
a = 0
3.times do
a += 1
end
assert_equal 3, a
end
assert('Integer#to_f', '15.2.8.3.23') do
assert_equal 1.0, 1.to_f
end
assert('Integer#to_i', '15.2.8.3.24') do
assert_equal 1, 1.to_i
end
assert('Integer#to_s', '15.2.8.3.25') do
assert_equal '1', 1.to_s
assert_equal("-1", -1.to_s)
end
assert('Integer#truncate', '15.2.8.3.26') do
assert_equal 1, 1.truncate
end
assert('Integer#upto', '15.2.8.3.27') do
a = 0
1.upto(3) do |i|
a += i
end
assert_equal 6, a
end
assert('Integer#divmod', '15.2.8.3.30') do
assert_equal [ 0, 0], 0.divmod(1)
assert_equal [ 0, 1], 1.divmod(3)
assert_equal [ 3, 0], 3.divmod(1)
assert_equal [ 2, 6], 20.divmod(7)
assert_equal [-1, 2], -3.divmod(5)
assert_equal [-2, -1], 25.divmod(-13)
assert_equal [ 1, -6], -13.divmod(-7)
end
# Not ISO specified
assert('Integer#step') do
a = []
b = []
1.step(3) do |i|
a << i
end
1.step(6, 2) do |i|
b << i
end
assert_equal [1, 2, 3], a
assert_equal [1, 3, 5], b
end
|