PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/test/ruby/test_fixnum.rb

http://github.com/ruby/ruby
Ruby | 352 lines | 293 code | 54 blank | 5 comment | 5 complexity | 878c669d9393fb3736ed42480c08931f MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. class TestFixnum < Test::Unit::TestCase
  4. def setup
  5. @verbose = $VERBOSE
  6. $VERBOSE = nil
  7. end
  8. def teardown
  9. $VERBOSE = @verbose
  10. end
  11. def test_pow
  12. [1, 2, 2**64, 2**63*3, 2**64*3].each do |y|
  13. [-1, 0, 1].each do |x|
  14. z1 = x**y
  15. z2 = (-x)**y
  16. if y % 2 == 1
  17. assert_equal(z2, -z1)
  18. else
  19. assert_equal(z2, z1)
  20. end
  21. end
  22. end
  23. end
  24. def test_succ
  25. assert_equal(0x40000000, 0x3fffffff.succ, "[ruby-dev:31189]")
  26. assert_equal(0x4000000000000000, 0x3fffffffffffffff.succ, "[ruby-dev:31190]")
  27. end
  28. def test_pred
  29. assert_equal(-0x40000001, (-0x40000000).pred)
  30. assert_equal(-0x4000000000000001, (-0x4000000000000000).pred)
  31. end
  32. def test_plus
  33. assert_equal(0x40000000, 0x3fffffff+1)
  34. assert_equal(0x7ffffffe, 0x3fffffff+0x3fffffff)
  35. assert_equal(0x4000000000000000, 0x3fffffffffffffff+1)
  36. assert_equal(0x7ffffffffffffffe, 0x3fffffffffffffff+0x3fffffffffffffff)
  37. assert_equal(-0x40000001, (-0x40000000)+(-1))
  38. assert_equal(-0x4000000000000001, (-0x4000000000000000)+(-1))
  39. assert_equal(-0x7ffffffe, (-0x3fffffff)+(-0x3fffffff))
  40. assert_equal(-0x80000000, (-0x40000000)+(-0x40000000))
  41. assert_equal(-0x8000000000000000, (-0x4000000000000000)+(-0x4000000000000000))
  42. end
  43. def test_sub
  44. assert_equal(0x40000000, 0x3fffffff-(-1))
  45. assert_equal(0x4000000000000000, 0x3fffffffffffffff-(-1))
  46. assert_equal(-0x40000001, (-0x40000000)-1)
  47. assert_equal(-0x4000000000000001, (-0x4000000000000000)-1)
  48. assert_equal(-0x80000000, (-0x40000000)-0x40000000)
  49. assert_equal(0x7fffffffffffffff, 0x3fffffffffffffff-(-0x4000000000000000))
  50. assert_equal(-0x8000000000000000, -0x4000000000000000-0x4000000000000000)
  51. end
  52. def test_mult
  53. assert_equal(0x40000000, 0x20000000*2)
  54. assert_equal(0x4000000000000000, 0x2000000000000000*2)
  55. assert_equal(-0x40000001, 33025*(-32513))
  56. assert_equal(-0x4000000000000001, 1380655685*(-3340214413))
  57. assert_equal(0x40000000, (-0x40000000)*(-1))
  58. end
  59. def test_div
  60. assert_equal(2, 5/2)
  61. assert_equal(0, 1/2)
  62. assert_equal(-1, -1/2)
  63. assert_equal(0, -(1/2))
  64. assert_equal(-1, (-1)/2)
  65. assert_equal(0, (-1)/(-2))
  66. assert_equal(-1, 1/(-2))
  67. assert_equal(1, -(1/(-2)))
  68. assert_equal(0x3fffffff, 0xbffffffd/3)
  69. assert_equal(0x40000000, 0xc0000000/3)
  70. assert_equal(0x4000000000000000, 0xc000000000000000/3)
  71. assert_equal(-0x40000001, 0xc0000003/(-3))
  72. assert_equal(-0x4000000000000001, 0xc000000000000003/(-3))
  73. assert_equal(0x40000000, (-0x40000000)/(-1), "[ruby-dev:31210]")
  74. assert_equal(0x4000000000000000, (-0x4000000000000000)/(-1))
  75. assert_raise(FloatDomainError) { 2.div(Float::NAN).nan? }
  76. end
  77. def test_mod
  78. assert_equal(2, (-0x40000000) % 3)
  79. assert_equal(0, (-0x40000000) % (-1))
  80. end
  81. def test_divmod
  82. (-5).upto(5) {|a|
  83. (-5).upto(5) {|b|
  84. next if b == 0
  85. q, r = a.divmod(b)
  86. assert_equal(a, b*q+r)
  87. assert_operator(r.abs, :<, b.abs)
  88. if 0 < b
  89. assert_operator(r, :>=, 0)
  90. assert_operator(r, :<, b)
  91. else
  92. assert_operator(r, :>, b)
  93. assert_operator(r, :<=, 0)
  94. end
  95. assert_equal(q, a/b)
  96. assert_equal(q, a.div(b))
  97. assert_equal(r, a%b)
  98. assert_equal(r, a.modulo(b))
  99. }
  100. }
  101. assert_raise(FloatDomainError) { 2.divmod(Float::NAN) }
  102. end
  103. def test_not
  104. assert_equal(-0x40000000, ~0x3fffffff)
  105. assert_equal(0x3fffffff, ~-0x40000000)
  106. end
  107. def test_lshift
  108. assert_equal(0x40000000, 0x20000000 << 1)
  109. assert_equal(-0x40000000, (-0x20000000) << 1)
  110. assert_equal(-0x80000000, (-0x40000000) << 1)
  111. end
  112. def test_rshift
  113. assert_equal(0x20000000, 0x40000000 >> 1)
  114. assert_equal(-0x20000000, (-0x40000000) >> 1)
  115. assert_equal(-0x40000000, (-0x80000000) >> 1)
  116. end
  117. def test_abs
  118. assert_equal(0x40000000, (-0x40000000).abs)
  119. assert_equal(0x4000000000000000, (-0x4000000000000000).abs)
  120. end
  121. def test_to_s
  122. assert_equal("1010", 10.to_s(2))
  123. assert_equal("a", 10.to_s(36))
  124. assert_raise(ArgumentError) { 10.to_s(1) }
  125. end
  126. def test_plus2
  127. assert_equal(2, 1 + 1)
  128. assert_equal(4294967297, 1 + 2**32)
  129. assert_equal(2.0, 1 + 1.0)
  130. assert_raise(TypeError) { 1 + nil }
  131. end
  132. def test_minus
  133. assert_equal(0, 1 - 1)
  134. assert_equal(-4294967295, 1 - 2**32)
  135. assert_equal(0.0, 1 - 1.0)
  136. assert_raise(TypeError) { 1 - nil }
  137. end
  138. def test_mul
  139. assert_equal(6, 2.send(:*, 3))
  140. a = 2**30-1
  141. assert_equal(1152921502459363329, a.send(:*, a))
  142. assert_equal(6.0, 2 * 3.0)
  143. assert_raise(TypeError) { 2 * nil }
  144. end
  145. def test_divide
  146. assert_equal(2.0, 4.quo(2))
  147. assert_equal(2.0, 4 / 2)
  148. assert_equal(2.0, 4.div(2))
  149. assert_equal(0.5**32, 1.quo(2**32))
  150. assert_equal(0, 1 / (2**32))
  151. assert_equal(0, 1.div(2**32))
  152. assert_kind_of(Float, 1.quo(2.0))
  153. assert_equal(0.5, 1.quo(2.0))
  154. assert_equal(0.5, 1 / 2.0)
  155. assert_equal(0, 1.div(2.0))
  156. ### rational changes the behavior of Fixnum#quo
  157. #assert_raise(TypeError) { 2.quo(nil) }
  158. assert_raise(TypeError, NoMethodError) { 2.quo(nil) }
  159. assert_raise(TypeError) { 2 / nil }
  160. assert_raise(TypeError) { 2.div(nil) }
  161. assert_equal(0, 4.modulo(2))
  162. assert_equal(1, 1.modulo(2**32))
  163. assert_equal(1, 1.modulo(2.0))
  164. assert_raise(TypeError) { 2.modulo(nil) }
  165. assert_equal([2, 0], 4.divmod(2))
  166. assert_equal([0, 1], 1.divmod(2**32))
  167. assert_equal([0, 1], 1.divmod(2.0))
  168. assert_raise(TypeError) { 2.divmod(nil) }
  169. end
  170. def test_pow2
  171. assert_equal(65536, 2**16)
  172. assert_equal(4294967296, 2**32)
  173. assert_equal(0.5**16, 2**-16)
  174. assert_equal(1, (-1)**4294967296)
  175. assert_equal(-1, (-1)**4294967295)
  176. assert_equal(4, 2**((2**32).coerce(2).first))
  177. assert_equal(2, 4**0.5)
  178. assert_equal(0, 0**0.5)
  179. assert_equal(1, (0**-1.0).infinite?)
  180. ### rational changes the behavior of Fixnum#**
  181. #assert_raise(TypeError) { 1 ** nil }
  182. assert_raise(TypeError, NoMethodError) { 1 ** nil }
  183. end
  184. def test_cmp
  185. assert_operator(1, :!=, nil)
  186. assert_equal(0, 1 <=> 1)
  187. assert_equal(-1, 1 <=> 4294967296)
  188. assert_equal(-1, 1 <=> 1 << 100)
  189. assert_equal(0, 1 <=> 1.0)
  190. assert_nil(1 <=> nil)
  191. assert_operator(1, :>, 0)
  192. assert_not_operator(1, :>, 1)
  193. assert_not_operator(1, :>, 2)
  194. assert_not_operator(1, :>, 4294967296)
  195. assert_operator(1, :>, 0.0)
  196. assert_raise(ArgumentError) { 1 > nil }
  197. assert_operator(1, :>=, 0)
  198. assert_operator(1, :>=, 1)
  199. assert_not_operator(1, :>=, 2)
  200. assert_not_operator(1, :>=, 4294967296)
  201. assert_operator(1, :>=, 0.0)
  202. assert_raise(ArgumentError) { 1 >= nil }
  203. assert_not_operator(1, :<, 0)
  204. assert_not_operator(1, :<, 1)
  205. assert_operator(1, :<, 2)
  206. assert_operator(1, :<, 4294967296)
  207. assert_not_operator(1, :<, 0.0)
  208. assert_raise(ArgumentError) { 1 < nil }
  209. assert_not_operator(1, :<=, 0)
  210. assert_operator(1, :<=, 1)
  211. assert_operator(1, :<=, 2)
  212. assert_operator(1, :<=, 4294967296)
  213. assert_not_operator(1, :<=, 0.0)
  214. assert_raise(ArgumentError) { 1 <= nil }
  215. end
  216. class DummyNumeric < Numeric
  217. def to_int
  218. 1
  219. end
  220. end
  221. def test_and_with_float
  222. assert_raise(TypeError) { 1 & 1.5 }
  223. end
  224. def test_and_with_rational
  225. assert_raise(TypeError, "#1792") { 1 & Rational(3, 2) }
  226. end
  227. def test_and_with_nonintegral_numeric
  228. assert_raise(TypeError, "#1792") { 1 & DummyNumeric.new }
  229. end
  230. def test_or_with_float
  231. assert_raise(TypeError) { 1 | 1.5 }
  232. end
  233. def test_or_with_rational
  234. assert_raise(TypeError, "#1792") { 1 | Rational(3, 2) }
  235. end
  236. def test_or_with_nonintegral_numeric
  237. assert_raise(TypeError, "#1792") { 1 | DummyNumeric.new }
  238. end
  239. def test_xor_with_float
  240. assert_raise(TypeError) { 1 ^ 1.5 }
  241. end
  242. def test_xor_with_rational
  243. assert_raise(TypeError, "#1792") { 1 ^ Rational(3, 2) }
  244. end
  245. def test_xor_with_nonintegral_numeric
  246. assert_raise(TypeError, "#1792") { 1 ^ DummyNumeric.new }
  247. end
  248. def test_singleton_method
  249. assert_raise(TypeError) { a = 1; def a.foo; end }
  250. end
  251. def test_frozen
  252. assert_equal(true, 1.frozen?)
  253. end
  254. def assert_eql(a, b, mess)
  255. assert a.eql?(b), "expected #{a} & #{b} to be eql? #{mess}"
  256. end
  257. def test_power_of_1_and_minus_1
  258. bug5715 = '[ruby-core:41498]'
  259. big = 1 << 66
  260. assert_eql 1, 1 ** -big , bug5715
  261. assert_eql 1, (-1) ** -big , bug5715
  262. assert_eql (-1), (-1) ** -(big+1), bug5715
  263. end
  264. def test_power_of_0
  265. bug5713 = '[ruby-core:41494]'
  266. big = 1 << 66
  267. assert_raise(ZeroDivisionError, bug5713) { 0 ** -big }
  268. assert_raise(ZeroDivisionError, bug5713) { 0 ** Rational(-2,3) }
  269. end
  270. def test_remainder
  271. assert_equal(1, 5.remainder(4))
  272. assert_predicate(4.remainder(Float::NAN), :nan?)
  273. end
  274. def test_zero_p
  275. assert_predicate(0, :zero?)
  276. assert_not_predicate(1, :zero?)
  277. end
  278. def test_positive_p
  279. assert_predicate(1, :positive?)
  280. assert_not_predicate(0, :positive?)
  281. assert_not_predicate(-1, :positive?)
  282. end
  283. def test_negative_p
  284. assert_predicate(-1, :negative?)
  285. assert_not_predicate(0, :negative?)
  286. assert_not_predicate(1, :negative?)
  287. end
  288. def test_finite_p
  289. assert_predicate(1, :finite?)
  290. assert_predicate(0, :finite?)
  291. assert_predicate(-1, :finite?)
  292. end
  293. def test_infinite_p
  294. assert_nil(1.infinite?)
  295. assert_nil(0.infinite?)
  296. assert_nil(-1.infinite?)
  297. end
  298. end