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
|
require 'test/unit'
class TestNumeric < Test::Unit::TestCase
class NumericWithSomeMethods < Numeric
def initialize(value = 0)
@value = value
end
def expect(arg)
@expected_arg = arg
end
def check(arg)
raise "Expected #{@expected_arg.inspect}, but received #{arg.inspect}" unless @expected_arg == arg
end
def /(arg) check(arg); 543.21 end
def %(arg) check(arg); 89 end
def to_f() @value.to_f end
def <=>(other) @value <=> other end
def <(other) @value < other end
def >(other) @value > other end
def eql?(other) @value.eql?(other) end
def to_i() @value.to_i end
end
def setup
@a = Numeric.new
@b = Numeric.new
end
def test_unary_plus
assert_same @a, +@a
end
def test_unary_minus_should_raise_if_self_cannot_be_coerced_to_float
assert_raises(TypeError) { -@a }
end
def test_unary_minus_should_coerce_to_float_and_negate_result
assert_equal 123.45, NumericWithSomeMethods.new(123.45).to_f
assert_equal -123.45, -NumericWithSomeMethods.new(123.45)
end
def test_spaceship_should_return_zero_when_comparting_with_self_and_nil_otherwise
assert_equal 0, @a <=> @a
assert_nil @a <=> @b
assert_nil @a <=> 1
assert_nil @a <=> ""
assert_nil @a <=> nil
end
def test_abs_should_return_self_if_bigger_than_zero_and_result_of_unitary_minus_on_self_otherwise
assert_raises(ArgumentError) { @a.abs }
positive = NumericWithSomeMethods.new(1).abs
assert positive > 0
assert_same positive, positive.abs
negative = NumericWithSomeMethods.new(-2)
assert negative < 0
# unitary minus operator effectively returns -(self.to_f)
assert_equal 2.0, negative.abs
assert_equal Float, negative.abs.class
end
# ceil
def test_ceil_should_delegate_to_self_to_f
assert_equal 124, NumericWithSomeMethods.new(123.49).ceil
assert_equal -123, NumericWithSomeMethods.new(-123.51).ceil
end
def test_ceil_should_raise_if_self_doesnt_implement_to_f
assert_raises(TypeError) { @a.ceil }
end
def test_coerce_should_copy_argument_and_self_if_both_have_the_same_type
assert_equal [@b, @a], @a.coerce(@b)
assert_equal [0.9, 0.1], 0.1.coerce(0.9)
assert_equal [0, 1], 1.coerce(0)
end
def test_coerce_should_call_float_on_self_and_arg_if_they_are_of_different_type
bignum = 100000000000000000000000
assert bignum.is_a?(Bignum)
assert_equal [Float(bignum), 0.0], 0.0.coerce(bignum)
assert 0.0.coerce(bignum).first.is_a?(Float)
assert 0.0.coerce(bignum).last.is_a?(Float)
n = NumericWithSomeMethods.new(123.45)
assert_equal [1.0, 123.45], n.coerce(1.0)
end
def test_coerce_should_blow_up_if_self_or_arg_cannot_be_converted_to_float_by_kernel_Float
assert_raises(TypeError) { @a.coerce(1.0) }
assert_raises(ArgumentError) { 1.coerce("test") }
assert_raises(TypeError) { 1.coerce(nil) }
assert_raises(TypeError) { 1.coerce(false) }
end
def test_div_should_raise_if_self_doesnt_implement_divison_operator
assert_raises(NoMethodError) { @a.div(@b) }
end
def test_div_should_call_division_method_and_floor_it
n = NumericWithSomeMethods.new
# n / anything returns 543.21
n.expect(:foo)
assert_equal 543.21, n / :foo
n.expect(@a)
assert_equal 543, n.div(@a)
end
def test_divmod_should_return_result_of_div_and_mod_as_array
n = NumericWithSomeMethods.new
n.expect(:foo)
# n.div(anything) returns 543, n % anything returns 89
assert_equal [543, 89], n.divmod(:foo)
end
def test_divmod_should_calculate_div_correctly
dividends = [-0.58, 0.58, -0.59, 0.59, -0.63, 0.63, -0.66, 0.66, -0.67, 0.67]
divisor = 1 / 12.0
expected_divs = [-7, 6, -8, 7, -8, 7, -8, 7, -9, 8]
dividends.each_with_index { |dividend, idx|
assert_equal(expected_divs[idx], dividend.divmod(divisor)[0])
}
end
def test_divmod_should_raise_when_self_doesnt_implement_div_or_mod
assert_raises(NoMethodError) { @a.divmod(@b) }
end
def test_eql
assert_equal true, @a.eql?(@a)
assert_equal false, @a.eql?(@b)
end
def test_floor_should_delegate_to_self_to_f
assert_equal 123, NumericWithSomeMethods.new(123.51).floor
assert_equal -124, NumericWithSomeMethods.new(-123.49).floor
end
def test_floor_should_raise_if_self_doesnt_implement_to_f
assert_raises(TypeError) { @a.floor }
end
def test_initialize_copy_should_raise
assert_raises(TypeError) { @a.instance_eval("initialize_copy(:foo)") }
end
def test_initialize_copy_should_be_private
assert @a.private_methods.include?("initialize_copy")
assert_equal false, @a.methods.include?("initialize_copy")
end
def test_integer
assert_equal false, @a.integer?
end
def test_modulo_should_raise_if_self_doesnt_have_percent_operator
assert_raises(NoMethodError) { @a.modulo(@b) }
end
def test_modulo_should_call_percent_operator
n = NumericWithSomeMethods.new
n.expect(:foo)
# n % anything returns 89
assert_equal 89, n.modulo(:foo)
end
def test_nonzero_should_check_equality_and_returns_self_or_nil
assert_same @a, @a.nonzero?
assert_nil NumericWithSomeMethods.new(0).nonzero?
one = NumericWithSomeMethods.new(1)
minus_one = NumericWithSomeMethods.new(1)
assert_same one, one.nonzero?
assert_same minus_one, minus_one.nonzero?
end
def test_quo
assert_raises(NoMethodError) { @a.quo @b }
end
def test_remainder_should_raise_if_self_doesnt_implement_modulo
assert_raises(NoMethodError) { @a.remainder(@b) }
end
def test_remainder_should_return_modulo_if_self_and_arg_have_the_same_sign_and_modulo_minus_arg_otherwise_except_when_modulo_is_zero
# Float doesn't override Numeric#remainder, so that's what we are testng here
assert_equal 2.0, 5.0.remainder(3)
assert_equal 2.0, 5.0.remainder(-3)
assert_equal -2.0, -5.0.remainder(-3)
assert_equal -2.0, -5.0.remainder(3)
# special case, was a bug
assert_equal 0.0, 4.0.remainder(2)
assert_equal 0.0, 4.0.remainder(-2)
assert_equal 0.0, -4.0.remainder(2)
assert_equal 0.0, -4.0.remainder(-2)
end
def test_round_should_delegate_to_self_to_f
assert_equal 123, NumericWithSomeMethods.new(123.49).round
assert_equal 124, NumericWithSomeMethods.new(123.51).round
assert_equal -123, NumericWithSomeMethods.new(-123.49).round
assert_equal -124, NumericWithSomeMethods.new(-123.51).round
end
def test_round_should_raise_if_self_doesnt_implement_to_f
assert_raises(TypeError) { @a.round }
end
def test_step
# Fixnum doesn't override Numeric#step()
# ends exactly at :to
a = []
1.step(5, 2) { |x| a << x }
assert_equal [1, 3, 5], a
# ends before :to
a = []
1.step(4, 2) { |x| a << x }
assert_equal [1, 3], a
# step is too big
a = []
1.step(4, 10) { |x| a << x }
assert_equal [1], a
# step is zero
assert_raises(ArgumentError) { 1.step(1, 0) }
# same to and from
a = []
1.step(1, 1) { |x| a << x }
assert_equal [1], a
# from less than to, positive step value
a = []
1.step(0, 1) { |x| a << x }
assert_equal [], a
# from less than to, negative step value
a = []
1.step(0, 1) { |x| a << x }
assert_equal [], a
# default step value of 1
a = []
1.step(3) { |x| a << x }
assert_equal [1, 2, 3], a
end
def test_step_should_raise_if_step_is_zero
end
def test_to_int_should_call_to_i_or_raise_if_to_i_is_not_implemented
assert_equal 123, NumericWithSomeMethods.new(123).to_int
assert_raises(NoMethodError) { @a.to_int }
end
def test_truncate_should_delegate_to_self_to_f
assert_equal 123, NumericWithSomeMethods.new(123.49).truncate
assert_equal 123, NumericWithSomeMethods.new(123.51).truncate
assert_equal -123, NumericWithSomeMethods.new(-123.49).truncate
assert_equal -123, NumericWithSomeMethods.new(-123.51).truncate
end
def test_truncate_should_raise_if_self_doesnt_implement_to_f
assert_raises(TypeError) { @a.truncate }
end
def test_zero_should_check_equality
assert_equal false, @a.zero?
assert_equal true, NumericWithSomeMethods.new(0).zero?
assert_equal false, NumericWithSomeMethods.new(1).zero?
assert_equal false, NumericWithSomeMethods.new(-1).zero?
end
end
require 'test/unit'
class TestNumeric < Test::Unit::TestCase
class NumericWithSomeMethods < Numeric
def initialize(value = 0)
@value = value
end
def expect(arg)
@expected_arg = arg
end
def check(arg)
raise "Expected #{@expected_arg.inspect}, but received #{arg.inspect}" unless @expected_arg == arg
end
def /(arg) check(arg); 543.21 end
def %(arg) check(arg); 89 end
def to_f() @value.to_f end
def <=>(other) @value <=> other end
def <(other) @value < other end
def >(other) @value > other end
def eql?(other) @value.eql?(other) end
def to_i() @value.to_i end
end
def setup
@a = Numeric.new
@b = Numeric.new
end
def test_unary_plus
assert_same @a, +@a
end
def test_unary_minus_should_raise_if_self_cannot_be_coerced_to_float
assert_raises(TypeError) { -@a }
end
def test_unary_minus_should_coerce_to_float_and_negate_result
assert_equal 123.45, NumericWithSomeMethods.new(123.45).to_f
assert_equal -123.45, -NumericWithSomeMethods.new(123.45)
end
def test_spaceship_should_return_zero_when_comparting_with_self_and_nil_otherwise
assert_equal 0, @a <=> @a
assert_nil @a <=> @b
assert_nil @a <=> 1
assert_nil @a <=> ""
assert_nil @a <=> nil
end
def test_abs_should_return_self_if_bigger_than_zero_and_result_of_unitary_minus_on_self_otherwise
assert_raises(ArgumentError) { @a.abs }
positive = NumericWithSomeMethods.new(1).abs
assert positive > 0
assert_same positive, positive.abs
negative = NumericWithSomeMethods.new(-2)
assert negative < 0
# unitary minus operator effectively returns -(self.to_f)
assert_equal 2.0, negative.abs
assert_equal Float, negative.abs.class
end
# ceil
def test_ceil_should_delegate_to_self_to_f
assert_equal 124, NumericWithSomeMethods.new(123.49).ceil
assert_equal -123, NumericWithSomeMethods.new(-123.51).ceil
end
def test_ceil_should_raise_if_self_doesnt_implement_to_f
assert_raises(TypeError) { @a.ceil }
end
def test_coerce_should_copy_argument_and_self_if_both_have_the_same_type
assert_equal [@b, @a], @a.coerce(@b)
assert_equal [0.9, 0.1], 0.1.coerce(0.9)
assert_equal [0, 1], 1.coerce(0)
end
def test_coerce_should_call_float_on_self_and_arg_if_they_are_of_different_type
bignum = 100000000000000000000000
assert bignum.is_a?(Bignum)
assert_equal [Float(bignum), 0.0], 0.0.coerce(bignum)
assert 0.0.coerce(bignum).first.is_a?(Float)
assert 0.0.coerce(bignum).last.is_a?(Float)
n = NumericWithSomeMethods.new(123.45)
assert_equal [1.0, 123.45], n.coerce(1.0)
end
def test_coerce_should_blow_up_if_self_or_arg_cannot_be_converted_to_float_by_kernel_Float
assert_raises(TypeError) { @a.coerce(1.0) }
assert_raises(ArgumentError) { 1.coerce("test") }
assert_raises(TypeError) { 1.coerce(nil) }
assert_raises(TypeError) { 1.coerce(false) }
end
def test_div_should_raise_if_self_doesnt_implement_divison_operator
assert_raises(NoMethodError) { @a.div(@b) }
end
def test_div_should_call_division_method_and_floor_it
n = NumericWithSomeMethods.new
# n / anything returns 543.21
n.expect(:foo)
assert_equal 543.21, n / :foo
n.expect(@a)
assert_equal 543, n.div(@a)
end
def test_divmod_should_return_result_of_div_and_mod_as_array
n = NumericWithSomeMethods.new
n.expect(:foo)
# n.div(anything) returns 543, n % anything returns 89
assert_equal [543, 89], n.divmod(:foo)
end
def test_divmod_should_raise_when_self_doesnt_implement_div_or_mod
assert_raises(NoMethodError) { @a.divmod(@b) }
end
def test_eql
assert_equal true, @a.eql?(@a)
assert_equal false, @a.eql?(@b)
end
def test_floor_should_delegate_to_self_to_f
assert_equal 123, NumericWithSomeMethods.new(123.51).floor
assert_equal -124, NumericWithSomeMethods.new(-123.49).floor
end
def test_floor_should_raise_if_self_doesnt_implement_to_f
assert_raises(TypeError) { @a.floor }
end
def test_initialize_copy_should_raise
assert_raises(TypeError) { @a.instance_eval("initialize_copy(:foo)") }
end
def test_initialize_copy_should_be_private
assert @a.private_methods.include?("initialize_copy")
assert_equal false, @a.methods.include?("initialize_copy")
end
def test_integer
assert_equal false, @a.integer?
end
def test_modulo_should_raise_if_self_doesnt_have_percent_operator
assert_raises(NoMethodError) { @a.modulo(@b) }
end
def test_modulo_should_call_percent_operator
n = NumericWithSomeMethods.new
n.expect(:foo)
# n % anything returns 89
assert_equal 89, n.modulo(:foo)
end
def test_nonzero_should_check_equality_and_returns_self_or_nil
assert_same @a, @a.nonzero?
assert_nil NumericWithSomeMethods.new(0).nonzero?
one = NumericWithSomeMethods.new(1)
minus_one = NumericWithSomeMethods.new(1)
assert_same one, one.nonzero?
assert_same minus_one, minus_one.nonzero?
end
def test_quo
assert_raises(NoMethodError) { @a.quo @b }
end
def test_remainder_should_raise_if_self_doesnt_implement_modulo
assert_raises(NoMethodError) { @a.remainder(@b) }
end
def test_remainder_should_return_modulo_if_self_and_arg_have_the_same_sign_and_modulo_minus_arg_otherwise_except_when_modulo_is_zero
# Float doesn't override Numeric#remainder, so that's what we are testng here
assert_equal 2.0, 5.0.remainder(3)
assert_equal 2.0, 5.0.remainder(-3)
assert_equal -2.0, -5.0.remainder(-3)
assert_equal -2.0, -5.0.remainder(3)
# special case, was a bug
assert_equal 0.0, 4.0.remainder(2)
assert_equal 0.0, 4.0.remainder(-2)
assert_equal 0.0, -4.0.remainder(2)
assert_equal 0.0, -4.0.remainder(-2)
end
def test_round_should_delegate_to_self_to_f
assert_equal 123, NumericWithSomeMethods.new(123.49).round
assert_equal 124, NumericWithSomeMethods.new(123.51).round
assert_equal -123, NumericWithSomeMethods.new(-123.49).round
assert_equal -124, NumericWithSomeMethods.new(-123.51).round
end
def test_round_should_raise_if_self_doesnt_implement_to_f
assert_raises(TypeError) { @a.round }
end
def test_step
# Fixnum doesn't override Numeric#step()
# ends exactly at :to
a = []
1.step(5, 2) { |x| a << x }
assert_equal [1, 3, 5], a
# ends before :to
a = []
1.step(4, 2) { |x| a << x }
assert_equal [1, 3], a
# step is too big
a = []
1.step(4, 10) { |x| a << x }
assert_equal [1], a
# step is zero
assert_raises(ArgumentError) { 1.step(1, 0) }
# same to and from
a = []
1.step(1, 1) { |x| a << x }
assert_equal [1], a
# from less than to, positive step value
a = []
1.step(0, 1) { |x| a << x }
assert_equal [], a
# from less than to, negative step value
a = []
1.step(0, 1) { |x| a << x }
assert_equal [], a
# default step value of 1
a = []
1.step(3) { |x| a << x }
assert_equal [1, 2, 3], a
end
def test_step_should_raise_if_step_is_zero
end
def test_to_int_should_call_to_i_or_raise_if_to_i_is_not_implemented
assert_equal 123, NumericWithSomeMethods.new(123).to_int
assert_raises(NoMethodError) { @a.to_int }
end
def test_truncate_should_delegate_to_self_to_f
assert_equal 123, NumericWithSomeMethods.new(123.49).truncate
assert_equal 123, NumericWithSomeMethods.new(123.51).truncate
assert_equal -123, NumericWithSomeMethods.new(-123.49).truncate
assert_equal -123, NumericWithSomeMethods.new(-123.51).truncate
end
def test_truncate_should_raise_if_self_doesnt_implement_to_f
assert_raises(TypeError) { @a.truncate }
end
def test_zero_should_check_equality
assert_equal false, @a.zero?
assert_equal true, NumericWithSomeMethods.new(0).zero?
assert_equal false, NumericWithSomeMethods.new(1).zero?
assert_equal false, NumericWithSomeMethods.new(-1).zero?
end
end
|