PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/test_numeric.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 577 lines | 421 code | 124 blank | 32 comment | 0 complexity | 5d9c8f09e88025a51e55e9ddb8f9f16b MD5 | raw file
  1. require 'test/unit'
  2. class TestNumeric < Test::Unit::TestCase
  3. class NumericWithSomeMethods < Numeric
  4. def initialize(value = 0)
  5. @value = value
  6. end
  7. def expect(arg)
  8. @expected_arg = arg
  9. end
  10. def check(arg)
  11. raise "Expected #{@expected_arg.inspect}, but received #{arg.inspect}" unless @expected_arg == arg
  12. end
  13. def /(arg) check(arg); 543.21 end
  14. def %(arg) check(arg); 89 end
  15. def to_f() @value.to_f end
  16. def <=>(other) @value <=> other end
  17. def <(other) @value < other end
  18. def >(other) @value > other end
  19. def eql?(other) @value.eql?(other) end
  20. def to_i() @value.to_i end
  21. end
  22. def setup
  23. @a = Numeric.new
  24. @b = Numeric.new
  25. end
  26. def test_unary_plus
  27. assert_same @a, +@a
  28. end
  29. def test_unary_minus_should_raise_if_self_cannot_be_coerced_to_float
  30. assert_raises(TypeError) { -@a }
  31. end
  32. def test_unary_minus_should_coerce_to_float_and_negate_result
  33. assert_equal 123.45, NumericWithSomeMethods.new(123.45).to_f
  34. assert_equal -123.45, -NumericWithSomeMethods.new(123.45)
  35. end
  36. def test_spaceship_should_return_zero_when_comparting_with_self_and_nil_otherwise
  37. assert_equal 0, @a <=> @a
  38. assert_nil @a <=> @b
  39. assert_nil @a <=> 1
  40. assert_nil @a <=> ""
  41. assert_nil @a <=> nil
  42. end
  43. def test_abs_should_return_self_if_bigger_than_zero_and_result_of_unitary_minus_on_self_otherwise
  44. assert_raises(ArgumentError) { @a.abs }
  45. positive = NumericWithSomeMethods.new(1).abs
  46. assert positive > 0
  47. assert_same positive, positive.abs
  48. negative = NumericWithSomeMethods.new(-2)
  49. assert negative < 0
  50. # unitary minus operator effectively returns -(self.to_f)
  51. assert_equal 2.0, negative.abs
  52. assert_equal Float, negative.abs.class
  53. end
  54. # ceil
  55. def test_ceil_should_delegate_to_self_to_f
  56. assert_equal 124, NumericWithSomeMethods.new(123.49).ceil
  57. assert_equal -123, NumericWithSomeMethods.new(-123.51).ceil
  58. end
  59. def test_ceil_should_raise_if_self_doesnt_implement_to_f
  60. assert_raises(TypeError) { @a.ceil }
  61. end
  62. def test_coerce_should_copy_argument_and_self_if_both_have_the_same_type
  63. assert_equal [@b, @a], @a.coerce(@b)
  64. assert_equal [0.9, 0.1], 0.1.coerce(0.9)
  65. assert_equal [0, 1], 1.coerce(0)
  66. end
  67. def test_coerce_should_call_float_on_self_and_arg_if_they_are_of_different_type
  68. bignum = 100000000000000000000000
  69. assert bignum.is_a?(Bignum)
  70. assert_equal [Float(bignum), 0.0], 0.0.coerce(bignum)
  71. assert 0.0.coerce(bignum).first.is_a?(Float)
  72. assert 0.0.coerce(bignum).last.is_a?(Float)
  73. n = NumericWithSomeMethods.new(123.45)
  74. assert_equal [1.0, 123.45], n.coerce(1.0)
  75. end
  76. def test_coerce_should_blow_up_if_self_or_arg_cannot_be_converted_to_float_by_kernel_Float
  77. assert_raises(TypeError) { @a.coerce(1.0) }
  78. assert_raises(ArgumentError) { 1.coerce("test") }
  79. assert_raises(TypeError) { 1.coerce(nil) }
  80. assert_raises(TypeError) { 1.coerce(false) }
  81. end
  82. def test_div_should_raise_if_self_doesnt_implement_divison_operator
  83. assert_raises(NoMethodError) { @a.div(@b) }
  84. end
  85. def test_div_should_call_division_method_and_floor_it
  86. n = NumericWithSomeMethods.new
  87. # n / anything returns 543.21
  88. n.expect(:foo)
  89. assert_equal 543.21, n / :foo
  90. n.expect(@a)
  91. assert_equal 543, n.div(@a)
  92. end
  93. def test_divmod_should_return_result_of_div_and_mod_as_array
  94. n = NumericWithSomeMethods.new
  95. n.expect(:foo)
  96. # n.div(anything) returns 543, n % anything returns 89
  97. assert_equal [543, 89], n.divmod(:foo)
  98. end
  99. def test_divmod_should_calculate_div_correctly
  100. dividends = [-0.58, 0.58, -0.59, 0.59, -0.63, 0.63, -0.66, 0.66, -0.67, 0.67]
  101. divisor = 1 / 12.0
  102. expected_divs = [-7, 6, -8, 7, -8, 7, -8, 7, -9, 8]
  103. dividends.each_with_index { |dividend, idx|
  104. assert_equal(expected_divs[idx], dividend.divmod(divisor)[0])
  105. }
  106. end
  107. def test_divmod_should_raise_when_self_doesnt_implement_div_or_mod
  108. assert_raises(NoMethodError) { @a.divmod(@b) }
  109. end
  110. def test_eql
  111. assert_equal true, @a.eql?(@a)
  112. assert_equal false, @a.eql?(@b)
  113. end
  114. def test_floor_should_delegate_to_self_to_f
  115. assert_equal 123, NumericWithSomeMethods.new(123.51).floor
  116. assert_equal -124, NumericWithSomeMethods.new(-123.49).floor
  117. end
  118. def test_floor_should_raise_if_self_doesnt_implement_to_f
  119. assert_raises(TypeError) { @a.floor }
  120. end
  121. def test_initialize_copy_should_raise
  122. assert_raises(TypeError) { @a.instance_eval("initialize_copy(:foo)") }
  123. end
  124. def test_initialize_copy_should_be_private
  125. assert @a.private_methods.include?("initialize_copy")
  126. assert_equal false, @a.methods.include?("initialize_copy")
  127. end
  128. def test_integer
  129. assert_equal false, @a.integer?
  130. end
  131. def test_modulo_should_raise_if_self_doesnt_have_percent_operator
  132. assert_raises(NoMethodError) { @a.modulo(@b) }
  133. end
  134. def test_modulo_should_call_percent_operator
  135. n = NumericWithSomeMethods.new
  136. n.expect(:foo)
  137. # n % anything returns 89
  138. assert_equal 89, n.modulo(:foo)
  139. end
  140. def test_nonzero_should_check_equality_and_returns_self_or_nil
  141. assert_same @a, @a.nonzero?
  142. assert_nil NumericWithSomeMethods.new(0).nonzero?
  143. one = NumericWithSomeMethods.new(1)
  144. minus_one = NumericWithSomeMethods.new(1)
  145. assert_same one, one.nonzero?
  146. assert_same minus_one, minus_one.nonzero?
  147. end
  148. def test_quo
  149. assert_raises(NoMethodError) { @a.quo @b }
  150. end
  151. def test_fdiv
  152. assert_equal 0.5, 1.fdiv(2)
  153. end
  154. def test_remainder_should_raise_if_self_doesnt_implement_modulo
  155. assert_raises(NoMethodError) { @a.remainder(@b) }
  156. end
  157. def test_remainder_should_return_modulo_if_self_and_arg_have_the_same_sign_and_modulo_minus_arg_otherwise_except_when_modulo_is_zero
  158. # Float doesn't override Numeric#remainder, so that's what we are testng here
  159. assert_equal 2.0, 5.0.remainder(3)
  160. assert_equal 2.0, 5.0.remainder(-3)
  161. assert_equal -2.0, -5.0.remainder(-3)
  162. assert_equal -2.0, -5.0.remainder(3)
  163. # special case, was a bug
  164. assert_equal 0.0, 4.0.remainder(2)
  165. assert_equal 0.0, 4.0.remainder(-2)
  166. assert_equal 0.0, -4.0.remainder(2)
  167. assert_equal 0.0, -4.0.remainder(-2)
  168. end
  169. def test_round_should_delegate_to_self_to_f
  170. assert_equal 123, NumericWithSomeMethods.new(123.49).round
  171. assert_equal 124, NumericWithSomeMethods.new(123.51).round
  172. assert_equal -123, NumericWithSomeMethods.new(-123.49).round
  173. assert_equal -124, NumericWithSomeMethods.new(-123.51).round
  174. end
  175. def test_round_should_raise_if_self_doesnt_implement_to_f
  176. assert_raises(TypeError) { @a.round }
  177. end
  178. def test_step
  179. # Fixnum doesn't override Numeric#step()
  180. # ends exactly at :to
  181. a = []
  182. 1.step(5, 2) { |x| a << x }
  183. assert_equal [1, 3, 5], a
  184. # ends before :to
  185. a = []
  186. 1.step(4, 2) { |x| a << x }
  187. assert_equal [1, 3], a
  188. # step is too big
  189. a = []
  190. 1.step(4, 10) { |x| a << x }
  191. assert_equal [1], a
  192. # step is zero
  193. assert_raises(ArgumentError) { 1.step(1, 0) }
  194. # same to and from
  195. a = []
  196. 1.step(1, 1) { |x| a << x }
  197. assert_equal [1], a
  198. # from less than to, positive step value
  199. a = []
  200. 1.step(0, 1) { |x| a << x }
  201. assert_equal [], a
  202. # from less than to, negative step value
  203. a = []
  204. 1.step(0, 1) { |x| a << x }
  205. assert_equal [], a
  206. # default step value of 1
  207. a = []
  208. 1.step(3) { |x| a << x }
  209. assert_equal [1, 2, 3], a
  210. end
  211. def test_step_should_raise_if_step_is_zero
  212. end
  213. def test_to_int_should_call_to_i_or_raise_if_to_i_is_not_implemented
  214. assert_equal 123, NumericWithSomeMethods.new(123).to_int
  215. assert_raises(NoMethodError) { @a.to_int }
  216. end
  217. def test_truncate_should_delegate_to_self_to_f
  218. assert_equal 123, NumericWithSomeMethods.new(123.49).truncate
  219. assert_equal 123, NumericWithSomeMethods.new(123.51).truncate
  220. assert_equal -123, NumericWithSomeMethods.new(-123.49).truncate
  221. assert_equal -123, NumericWithSomeMethods.new(-123.51).truncate
  222. end
  223. def test_truncate_should_raise_if_self_doesnt_implement_to_f
  224. assert_raises(TypeError) { @a.truncate }
  225. end
  226. def test_zero_should_check_equality
  227. assert_equal false, @a.zero?
  228. assert_equal true, NumericWithSomeMethods.new(0).zero?
  229. assert_equal false, NumericWithSomeMethods.new(1).zero?
  230. assert_equal false, NumericWithSomeMethods.new(-1).zero?
  231. end
  232. end
  233. require 'test/unit'
  234. class TestNumeric < Test::Unit::TestCase
  235. class NumericWithSomeMethods < Numeric
  236. def initialize(value = 0)
  237. @value = value
  238. end
  239. def expect(arg)
  240. @expected_arg = arg
  241. end
  242. def check(arg)
  243. raise "Expected #{@expected_arg.inspect}, but received #{arg.inspect}" unless @expected_arg == arg
  244. end
  245. def /(arg) check(arg); 543.21 end
  246. def %(arg) check(arg); 89 end
  247. def to_f() @value.to_f end
  248. def <=>(other) @value <=> other end
  249. def <(other) @value < other end
  250. def >(other) @value > other end
  251. def eql?(other) @value.eql?(other) end
  252. def to_i() @value.to_i end
  253. end
  254. def setup
  255. @a = Numeric.new
  256. @b = Numeric.new
  257. end
  258. def test_unary_plus
  259. assert_same @a, +@a
  260. end
  261. def test_unary_minus_should_raise_if_self_cannot_be_coerced_to_float
  262. assert_raises(TypeError) { -@a }
  263. end
  264. def test_unary_minus_should_coerce_to_float_and_negate_result
  265. assert_equal 123.45, NumericWithSomeMethods.new(123.45).to_f
  266. assert_equal -123.45, -NumericWithSomeMethods.new(123.45)
  267. end
  268. def test_spaceship_should_return_zero_when_comparting_with_self_and_nil_otherwise
  269. assert_equal 0, @a <=> @a
  270. assert_nil @a <=> @b
  271. assert_nil @a <=> 1
  272. assert_nil @a <=> ""
  273. assert_nil @a <=> nil
  274. end
  275. def test_abs_should_return_self_if_bigger_than_zero_and_result_of_unitary_minus_on_self_otherwise
  276. assert_raises(ArgumentError) { @a.abs }
  277. positive = NumericWithSomeMethods.new(1).abs
  278. assert positive > 0
  279. assert_same positive, positive.abs
  280. negative = NumericWithSomeMethods.new(-2)
  281. assert negative < 0
  282. # unitary minus operator effectively returns -(self.to_f)
  283. assert_equal 2.0, negative.abs
  284. assert_equal Float, negative.abs.class
  285. end
  286. # ceil
  287. def test_ceil_should_delegate_to_self_to_f
  288. assert_equal 124, NumericWithSomeMethods.new(123.49).ceil
  289. assert_equal -123, NumericWithSomeMethods.new(-123.51).ceil
  290. end
  291. def test_ceil_should_raise_if_self_doesnt_implement_to_f
  292. assert_raises(TypeError) { @a.ceil }
  293. end
  294. def test_coerce_should_copy_argument_and_self_if_both_have_the_same_type
  295. assert_equal [@b, @a], @a.coerce(@b)
  296. assert_equal [0.9, 0.1], 0.1.coerce(0.9)
  297. assert_equal [0, 1], 1.coerce(0)
  298. end
  299. def test_coerce_should_call_float_on_self_and_arg_if_they_are_of_different_type
  300. bignum = 100000000000000000000000
  301. assert bignum.is_a?(Bignum)
  302. assert_equal [Float(bignum), 0.0], 0.0.coerce(bignum)
  303. assert 0.0.coerce(bignum).first.is_a?(Float)
  304. assert 0.0.coerce(bignum).last.is_a?(Float)
  305. n = NumericWithSomeMethods.new(123.45)
  306. assert_equal [1.0, 123.45], n.coerce(1.0)
  307. end
  308. def test_coerce_should_blow_up_if_self_or_arg_cannot_be_converted_to_float_by_kernel_Float
  309. assert_raises(TypeError) { @a.coerce(1.0) }
  310. assert_raises(ArgumentError) { 1.coerce("test") }
  311. assert_raises(TypeError) { 1.coerce(nil) }
  312. assert_raises(TypeError) { 1.coerce(false) }
  313. end
  314. def test_div_should_raise_if_self_doesnt_implement_divison_operator
  315. assert_raises(NoMethodError) { @a.div(@b) }
  316. end
  317. def test_div_should_call_division_method_and_floor_it
  318. n = NumericWithSomeMethods.new
  319. # n / anything returns 543.21
  320. n.expect(:foo)
  321. assert_equal 543.21, n / :foo
  322. n.expect(@a)
  323. assert_equal 543, n.div(@a)
  324. end
  325. def test_divmod_should_return_result_of_div_and_mod_as_array
  326. n = NumericWithSomeMethods.new
  327. n.expect(:foo)
  328. # n.div(anything) returns 543, n % anything returns 89
  329. assert_equal [543, 89], n.divmod(:foo)
  330. end
  331. def test_divmod_should_raise_when_self_doesnt_implement_div_or_mod
  332. assert_raises(NoMethodError) { @a.divmod(@b) }
  333. end
  334. def test_eql
  335. assert_equal true, @a.eql?(@a)
  336. assert_equal false, @a.eql?(@b)
  337. end
  338. def test_floor_should_delegate_to_self_to_f
  339. assert_equal 123, NumericWithSomeMethods.new(123.51).floor
  340. assert_equal -124, NumericWithSomeMethods.new(-123.49).floor
  341. end
  342. def test_floor_should_raise_if_self_doesnt_implement_to_f
  343. assert_raises(TypeError) { @a.floor }
  344. end
  345. def test_initialize_copy_should_raise
  346. assert_raises(TypeError) { @a.instance_eval("initialize_copy(:foo)") }
  347. end
  348. def test_initialize_copy_should_be_private
  349. assert @a.private_methods.include?("initialize_copy")
  350. assert_equal false, @a.methods.include?("initialize_copy")
  351. end
  352. def test_integer
  353. assert_equal false, @a.integer?
  354. end
  355. def test_modulo_should_raise_if_self_doesnt_have_percent_operator
  356. assert_raises(NoMethodError) { @a.modulo(@b) }
  357. end
  358. def test_modulo_should_call_percent_operator
  359. n = NumericWithSomeMethods.new
  360. n.expect(:foo)
  361. # n % anything returns 89
  362. assert_equal 89, n.modulo(:foo)
  363. end
  364. def test_nonzero_should_check_equality_and_returns_self_or_nil
  365. assert_same @a, @a.nonzero?
  366. assert_nil NumericWithSomeMethods.new(0).nonzero?
  367. one = NumericWithSomeMethods.new(1)
  368. minus_one = NumericWithSomeMethods.new(1)
  369. assert_same one, one.nonzero?
  370. assert_same minus_one, minus_one.nonzero?
  371. end
  372. def test_quo
  373. assert_raises(NoMethodError) { @a.quo @b }
  374. end
  375. def test_remainder_should_raise_if_self_doesnt_implement_modulo
  376. assert_raises(NoMethodError) { @a.remainder(@b) }
  377. end
  378. def test_remainder_should_return_modulo_if_self_and_arg_have_the_same_sign_and_modulo_minus_arg_otherwise_except_when_modulo_is_zero
  379. # Float doesn't override Numeric#remainder, so that's what we are testng here
  380. assert_equal 2.0, 5.0.remainder(3)
  381. assert_equal 2.0, 5.0.remainder(-3)
  382. assert_equal -2.0, -5.0.remainder(-3)
  383. assert_equal -2.0, -5.0.remainder(3)
  384. # special case, was a bug
  385. assert_equal 0.0, 4.0.remainder(2)
  386. assert_equal 0.0, 4.0.remainder(-2)
  387. assert_equal 0.0, -4.0.remainder(2)
  388. assert_equal 0.0, -4.0.remainder(-2)
  389. end
  390. def test_round_should_delegate_to_self_to_f
  391. assert_equal 123, NumericWithSomeMethods.new(123.49).round
  392. assert_equal 124, NumericWithSomeMethods.new(123.51).round
  393. assert_equal -123, NumericWithSomeMethods.new(-123.49).round
  394. assert_equal -124, NumericWithSomeMethods.new(-123.51).round
  395. end
  396. def test_round_should_raise_if_self_doesnt_implement_to_f
  397. assert_raises(TypeError) { @a.round }
  398. end
  399. def test_step
  400. # Fixnum doesn't override Numeric#step()
  401. # ends exactly at :to
  402. a = []
  403. 1.step(5, 2) { |x| a << x }
  404. assert_equal [1, 3, 5], a
  405. # ends before :to
  406. a = []
  407. 1.step(4, 2) { |x| a << x }
  408. assert_equal [1, 3], a
  409. # step is too big
  410. a = []
  411. 1.step(4, 10) { |x| a << x }
  412. assert_equal [1], a
  413. # step is zero
  414. assert_raises(ArgumentError) { 1.step(1, 0) }
  415. # same to and from
  416. a = []
  417. 1.step(1, 1) { |x| a << x }
  418. assert_equal [1], a
  419. # from less than to, positive step value
  420. a = []
  421. 1.step(0, 1) { |x| a << x }
  422. assert_equal [], a
  423. # from less than to, negative step value
  424. a = []
  425. 1.step(0, 1) { |x| a << x }
  426. assert_equal [], a
  427. # default step value of 1
  428. a = []
  429. 1.step(3) { |x| a << x }
  430. assert_equal [1, 2, 3], a
  431. end
  432. def test_step_should_raise_if_step_is_zero
  433. end
  434. def test_to_int_should_call_to_i_or_raise_if_to_i_is_not_implemented
  435. assert_equal 123, NumericWithSomeMethods.new(123).to_int
  436. assert_raises(NoMethodError) { @a.to_int }
  437. end
  438. def test_truncate_should_delegate_to_self_to_f
  439. assert_equal 123, NumericWithSomeMethods.new(123.49).truncate
  440. assert_equal 123, NumericWithSomeMethods.new(123.51).truncate
  441. assert_equal -123, NumericWithSomeMethods.new(-123.49).truncate
  442. assert_equal -123, NumericWithSomeMethods.new(-123.51).truncate
  443. end
  444. def test_truncate_should_raise_if_self_doesnt_implement_to_f
  445. assert_raises(TypeError) { @a.truncate }
  446. end
  447. def test_zero_should_check_equality
  448. assert_equal false, @a.zero?
  449. assert_equal true, NumericWithSomeMethods.new(0).zero?
  450. assert_equal false, NumericWithSomeMethods.new(1).zero?
  451. assert_equal false, NumericWithSomeMethods.new(-1).zero?
  452. end
  453. end