PageRenderTime 44ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/test/ruby/test_integer.rb

https://gitlab.com/lfauts/ruby
Ruby | 348 lines | 288 code | 51 blank | 9 comment | 2 complexity | e763f24be7aef71abec0f01c46043aeb MD5 | raw file
  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. class TestInteger < Test::Unit::TestCase
  4. BDSIZE = 0x4000000000000000.coerce(0)[0].size
  5. def self.bdsize(x)
  6. ((x + 1) / 8 + BDSIZE) / BDSIZE * BDSIZE
  7. end
  8. def bdsize(x)
  9. self.class.bdsize(x)
  10. end
  11. def test_aref
  12. # assert_equal(1, (1 << 0x40000000)[0x40000000], "[ruby-dev:31271]")
  13. # assert_equal(0, (-1 << 0x40000001)[0x40000000], "[ruby-dev:31271]")
  14. big_zero = 0x40000000.coerce(0)[0]
  15. assert_equal(0, (-0x40000002)[big_zero], "[ruby-dev:31271]")
  16. assert_equal(1, 0x400000001[big_zero], "[ruby-dev:31271]")
  17. end
  18. def test_pow
  19. assert_not_equal(0, begin
  20. 0**-1
  21. rescue
  22. nil
  23. end, "[ruby-dev:32084] [ruby-dev:34547]")
  24. end
  25. def test_lshift
  26. assert_equal(0, 1 << -0x40000000)
  27. assert_equal(0, 1 << -0x40000001)
  28. assert_equal(0, 1 << -0x80000000)
  29. assert_equal(0, 1 << -0x80000001)
  30. # assert_equal(bdsize(0x80000000), (1 << 0x80000000).size)
  31. end
  32. def test_rshift
  33. # assert_equal(bdsize(0x40000001), (1 >> -0x40000001).size)
  34. assert_predicate((1 >> 0x80000000), :zero?)
  35. assert_predicate((1 >> 0xffffffff), :zero?)
  36. assert_predicate((1 >> 0x100000000), :zero?)
  37. # assert_equal((1 << 0x40000000), (1 >> -0x40000000))
  38. # assert_equal((1 << 0x40000001), (1 >> -0x40000001))
  39. end
  40. def test_Integer
  41. assert_raise(ArgumentError) {Integer("0x-1")}
  42. assert_raise(ArgumentError) {Integer("-0x-1")}
  43. assert_raise(ArgumentError) {Integer("0x 123")}
  44. assert_raise(ArgumentError) {Integer("0x 123")}
  45. assert_raise(ArgumentError) {Integer("0x0x5")}
  46. assert_raise(ArgumentError) {Integer("0x0x000000005")}
  47. assert_nothing_raised(ArgumentError) {
  48. assert_equal(1540841, "0x0x5".to_i(36))
  49. }
  50. assert_raise(ArgumentError) { Integer("--0") }
  51. assert_raise(ArgumentError) { Integer("-+0") }
  52. assert_raise(ArgumentError) { Integer("++1") }
  53. assert_raise(ArgumentError) { Integer("") }
  54. assert_raise(ArgumentError) { Integer("10 x") }
  55. assert_raise(ArgumentError) { Integer("1__2") }
  56. assert_raise(ArgumentError) { Integer("1z") }
  57. assert_raise(ArgumentError) { Integer("46116860184273__87904") }
  58. assert_raise(ArgumentError) { Integer("4611686018427387904_") }
  59. assert_raise(ArgumentError) { Integer("4611686018427387904 :") }
  60. assert_equal(0x4000000000000000, Integer("46_11_686_0184273_87904"))
  61. assert_raise(ArgumentError) { Integer("\0") }
  62. assert_nothing_raised(ArgumentError, "[ruby-core:13873]") {
  63. assert_equal(0, Integer("0 "))
  64. }
  65. assert_nothing_raised(ArgumentError, "[ruby-core:14139]") {
  66. assert_equal(0377, Integer("0_3_7_7"))
  67. }
  68. assert_raise(ArgumentError, "[ruby-core:14139]") {Integer("0__3_7_7")}
  69. assert_equal(1234, Integer(1234))
  70. assert_equal(1, Integer(1.234))
  71. # base argument
  72. assert_equal(1234, Integer("1234", 10))
  73. assert_equal(668, Integer("1234", 8))
  74. assert_equal(4660, Integer("1234", 16))
  75. assert_equal(49360, Integer("1234", 36))
  76. # decimal, not octal
  77. assert_equal(1234, Integer("01234", 10))
  78. assert_raise(ArgumentError) { Integer("0x123", 10) }
  79. assert_raise(ArgumentError) { Integer(1234, 10) }
  80. assert_raise(ArgumentError) { Integer(12.34, 10) }
  81. assert_raise(ArgumentError) { Integer(Object.new, 1) }
  82. assert_raise(ArgumentError) { Integer(1, 1, 1) }
  83. assert_equal(2 ** 50, Integer(2.0 ** 50))
  84. assert_raise(TypeError) { Integer(nil) }
  85. bug6192 = '[ruby-core:43566]'
  86. assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-16be"))}
  87. assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-16le"))}
  88. assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-32be"))}
  89. assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-32le"))}
  90. assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("iso-2022-jp"))}
  91. assert_raise_with_message(ArgumentError, /\u{1f4a1}/) {Integer("\u{1f4a1}")}
  92. end
  93. def test_int_p
  94. assert_not_predicate(1.0, :integer?)
  95. assert_predicate(1, :integer?)
  96. end
  97. def test_succ
  98. assert_equal(2, 1.send(:succ))
  99. Fixnum.class_eval do
  100. alias succ_bak succ
  101. remove_method :succ
  102. end
  103. assert_equal(2, 1.succ)
  104. assert_equal(4294967297, 4294967296.succ)
  105. ensure
  106. Fixnum.class_eval do
  107. alias succ succ_bak
  108. remove_method :succ_bak
  109. end
  110. end
  111. def test_chr
  112. assert_equal("a", "a".ord.chr)
  113. assert_raise(RangeError) { (-1).chr }
  114. assert_raise(RangeError) { 0x100.chr }
  115. end
  116. def test_upto
  117. a = []
  118. 1.upto(3) {|x| a << x }
  119. assert_equal([1, 2, 3], a)
  120. a = []
  121. 1.upto(0) {|x| a << x }
  122. assert_equal([], a)
  123. y = 2**30 - 1
  124. a = []
  125. y.upto(y+2) {|x| a << x }
  126. assert_equal([y, y+1, y+2], a)
  127. end
  128. def test_downto
  129. a = []
  130. -1.downto(-3) {|x| a << x }
  131. assert_equal([-1, -2, -3], a)
  132. a = []
  133. 1.downto(2) {|x| a << x }
  134. assert_equal([], a)
  135. y = -(2**30)
  136. a = []
  137. y.downto(y-2) {|x| a << x }
  138. assert_equal([y, y-1, y-2], a)
  139. end
  140. def test_times
  141. (2**32).times do |i|
  142. break if i == 2
  143. end
  144. end
  145. def assert_int_equal(expected, result, mesg = nil)
  146. assert_kind_of(Integer, result, mesg)
  147. assert_equal(expected, result, mesg)
  148. end
  149. def assert_float_equal(expected, result, mesg = nil)
  150. assert_kind_of(Float, result, mesg)
  151. assert_equal(expected, result, mesg)
  152. end
  153. def test_round
  154. assert_int_equal(11111, 11111.round)
  155. assert_int_equal(11111, 11111.round(0))
  156. assert_float_equal(11111.0, 11111.round(1))
  157. assert_float_equal(11111.0, 11111.round(2))
  158. assert_int_equal(11110, 11111.round(-1))
  159. assert_int_equal(11100, 11111.round(-2))
  160. assert_int_equal(+200, +249.round(-2))
  161. assert_int_equal(+300, +250.round(-2))
  162. assert_int_equal(-200, -249.round(-2))
  163. assert_int_equal(-300, -250.round(-2))
  164. assert_int_equal(+30 * 10**70, (+25 * 10**70).round(-71))
  165. assert_int_equal(-30 * 10**70, (-25 * 10**70).round(-71))
  166. assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).round(-71))
  167. assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).round(-71))
  168. assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.round(-1))
  169. assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).round(-1))
  170. end
  171. def test_floor
  172. assert_int_equal(11111, 11111.floor)
  173. assert_int_equal(11111, 11111.floor(0))
  174. assert_float_equal(11111.0, 11111.floor(1))
  175. assert_float_equal(11111.0, 11111.floor(2))
  176. assert_int_equal(11110, 11110.floor(-1))
  177. assert_int_equal(11110, 11119.floor(-1))
  178. assert_int_equal(11100, 11100.floor(-2))
  179. assert_int_equal(11100, 11199.floor(-2))
  180. assert_int_equal(0, 11111.floor(-5))
  181. assert_int_equal(+200, +299.floor(-2))
  182. assert_int_equal(+300, +300.floor(-2))
  183. assert_int_equal(-300, -299.floor(-2))
  184. assert_int_equal(-300, -300.floor(-2))
  185. assert_int_equal(+20 * 10**70, (+25 * 10**70).floor(-71))
  186. assert_int_equal(-30 * 10**70, (-25 * 10**70).floor(-71))
  187. assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).floor(-71))
  188. assert_int_equal(-30 * 10**70, (-25 * 10**70 + 1).floor(-71))
  189. assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.floor(-1))
  190. assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1120, (-1111_1111_1111_1111_1111_1111_1111_1111).floor(-1))
  191. end
  192. def test_ceil
  193. assert_int_equal(11111, 11111.ceil)
  194. assert_int_equal(11111, 11111.ceil(0))
  195. assert_float_equal(11111.0, 11111.ceil(1))
  196. assert_float_equal(11111.0, 11111.ceil(2))
  197. assert_int_equal(11110, 11110.ceil(-1))
  198. assert_int_equal(11120, 11119.ceil(-1))
  199. assert_int_equal(11200, 11101.ceil(-2))
  200. assert_int_equal(11200, 11200.ceil(-2))
  201. assert_int_equal(100000, 11111.ceil(-5))
  202. assert_int_equal(300, 299.ceil(-2))
  203. assert_int_equal(300, 300.ceil(-2))
  204. assert_int_equal(-200, -299.ceil(-2))
  205. assert_int_equal(-300, -300.ceil(-2))
  206. assert_int_equal(+30 * 10**70, (+25 * 10**70).ceil(-71))
  207. assert_int_equal(-20 * 10**70, (-25 * 10**70).ceil(-71))
  208. assert_int_equal(+30 * 10**70, (+25 * 10**70 - 1).ceil(-71))
  209. assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).ceil(-71))
  210. assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1120, 1111_1111_1111_1111_1111_1111_1111_1111.ceil(-1))
  211. assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).ceil(-1))
  212. end
  213. def test_truncate
  214. assert_int_equal(11111, 11111.truncate)
  215. assert_int_equal(11111, 11111.truncate(0))
  216. assert_float_equal(11111.0, 11111.truncate(1))
  217. assert_float_equal(11111.0, 11111.truncate(2))
  218. assert_int_equal(11110, 11110.truncate(-1))
  219. assert_int_equal(11110, 11119.truncate(-1))
  220. assert_int_equal(11100, 11100.truncate(-2))
  221. assert_int_equal(11100, 11199.truncate(-2))
  222. assert_int_equal(0, 11111.truncate(-5))
  223. assert_int_equal(+200, +299.truncate(-2))
  224. assert_int_equal(+300, +300.truncate(-2))
  225. assert_int_equal(-200, -299.truncate(-2))
  226. assert_int_equal(-300, -300.truncate(-2))
  227. assert_int_equal(+20 * 10**70, (+25 * 10**70).truncate(-71))
  228. assert_int_equal(-20 * 10**70, (-25 * 10**70).truncate(-71))
  229. assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).truncate(-71))
  230. assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).truncate(-71))
  231. assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.truncate(-1))
  232. assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).truncate(-1))
  233. end
  234. def test_bitwise_and_with_integer_mimic_object
  235. def (obj = Object.new).to_int
  236. 10
  237. end
  238. assert_raise(TypeError, '[ruby-core:39491]') { 3 & obj }
  239. def obj.coerce(other)
  240. [other, 10]
  241. end
  242. assert_equal(3 & 10, 3 & obj)
  243. end
  244. def test_bitwise_or_with_integer_mimic_object
  245. def (obj = Object.new).to_int
  246. 10
  247. end
  248. assert_raise(TypeError, '[ruby-core:39491]') { 3 | obj }
  249. def obj.coerce(other)
  250. [other, 10]
  251. end
  252. assert_equal(3 | 10, 3 | obj)
  253. end
  254. def test_bitwise_xor_with_integer_mimic_object
  255. def (obj = Object.new).to_int
  256. 10
  257. end
  258. assert_raise(TypeError, '[ruby-core:39491]') { 3 ^ obj }
  259. def obj.coerce(other)
  260. [other, 10]
  261. end
  262. assert_equal(3 ^ 10, 3 ^ obj)
  263. end
  264. def test_bit_length
  265. assert_equal(13, (-2**12-1).bit_length)
  266. assert_equal(12, (-2**12).bit_length)
  267. assert_equal(12, (-2**12+1).bit_length)
  268. assert_equal(9, -0x101.bit_length)
  269. assert_equal(8, -0x100.bit_length)
  270. assert_equal(8, -0xff.bit_length)
  271. assert_equal(1, -2.bit_length)
  272. assert_equal(0, -1.bit_length)
  273. assert_equal(0, 0.bit_length)
  274. assert_equal(1, 1.bit_length)
  275. assert_equal(8, 0xff.bit_length)
  276. assert_equal(9, 0x100.bit_length)
  277. assert_equal(9, 0x101.bit_length)
  278. assert_equal(12, (2**12-1).bit_length)
  279. assert_equal(13, (2**12).bit_length)
  280. assert_equal(13, (2**12+1).bit_length)
  281. assert_equal(10001, (-2**10000-1).bit_length)
  282. assert_equal(10000, (-2**10000).bit_length)
  283. assert_equal(10000, (-2**10000+1).bit_length)
  284. assert_equal(10000, (2**10000-1).bit_length)
  285. assert_equal(10001, (2**10000).bit_length)
  286. assert_equal(10001, (2**10000+1).bit_length)
  287. 2.upto(1000) {|i|
  288. n = 2**i
  289. assert_equal(i+1, (-n-1).bit_length, "(#{-n-1}).bit_length")
  290. assert_equal(i, (-n).bit_length, "(#{-n}).bit_length")
  291. assert_equal(i, (-n+1).bit_length, "(#{-n+1}).bit_length")
  292. assert_equal(i, (n-1).bit_length, "#{n-1}.bit_length")
  293. assert_equal(i+1, (n).bit_length, "#{n}.bit_length")
  294. assert_equal(i+1, (n+1).bit_length, "#{n+1}.bit_length")
  295. }
  296. end
  297. end