PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.8/ruby/test_integer.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 653 lines | 608 code | 38 blank | 7 comment | 32 complexity | 0eae16e428c53a71bcc3d0be5567dd98 MD5 | raw file
  1. require 'test/unit'
  2. class TestInteger < Test::Unit::TestCase
  3. VS = [
  4. -0x1000000000000000000000000000000000000000000000002,
  5. -0x1000000000000000000000000000000000000000000000001,
  6. -0x1000000000000000000000000000000000000000000000000,
  7. -0xffffffffffffffffffffffffffffffffffffffffffffffff,
  8. -0x1000000000000000000000002,
  9. -0x1000000000000000000000001,
  10. -0x1000000000000000000000000,
  11. -0xffffffffffffffffffffffff,
  12. -0x10000000000000002,
  13. -0x10000000000000001,
  14. -0x10000000000000000,
  15. -0xffffffffffffffff,
  16. -0x4000000000000002,
  17. -0x4000000000000001,
  18. -0x4000000000000000,
  19. -0x3fffffffffffffff,
  20. -0x100000002,
  21. -0x100000001,
  22. -0x100000000,
  23. -0xffffffff,
  24. -0xc717a08d, # 0xc717a08d * 0x524b2245 = 0x4000000000000001
  25. -0x80000002,
  26. -0x80000001,
  27. -0x80000000,
  28. -0x7fffffff,
  29. -0x524b2245,
  30. -0x40000002,
  31. -0x40000001,
  32. -0x40000000,
  33. -0x3fffffff,
  34. -0x10002,
  35. -0x10001,
  36. -0x10000,
  37. -0xffff,
  38. -0x8101, # 0x8101 * 0x7f01 = 0x40000001
  39. -0x8002,
  40. -0x8001,
  41. -0x8000,
  42. -0x7fff,
  43. -0x7f01,
  44. -65,
  45. -64,
  46. -63,
  47. -62,
  48. -33,
  49. -32,
  50. -31,
  51. -30,
  52. -3,
  53. -2,
  54. -1,
  55. 0,
  56. 1,
  57. 2,
  58. 3,
  59. 30,
  60. 31,
  61. 32,
  62. 33,
  63. 62,
  64. 63,
  65. 64,
  66. 65,
  67. 0x7f01,
  68. 0x7ffe,
  69. 0x7fff,
  70. 0x8000,
  71. 0x8001,
  72. 0x8101,
  73. 0xfffe,
  74. 0xffff,
  75. 0x10000,
  76. 0x10001,
  77. 0x3ffffffe,
  78. 0x3fffffff,
  79. 0x40000000,
  80. 0x40000001,
  81. 0x524b2245,
  82. 0x7ffffffe,
  83. 0x7fffffff,
  84. 0x80000000,
  85. 0x80000001,
  86. 0xc717a08d,
  87. 0xfffffffe,
  88. 0xffffffff,
  89. 0x100000000,
  90. 0x100000001,
  91. 0x3ffffffffffffffe,
  92. 0x3fffffffffffffff,
  93. 0x4000000000000000,
  94. 0x4000000000000001,
  95. 0xfffffffffffffffe,
  96. 0xffffffffffffffff,
  97. 0x10000000000000000,
  98. 0x10000000000000001,
  99. 0xffffffffffffffffffffffff,
  100. 0x1000000000000000000000000,
  101. 0x1000000000000000000000001,
  102. 0xffffffffffffffffffffffffffffffffffffffffffffffff,
  103. 0x1000000000000000000000000000000000000000000000000,
  104. 0x1000000000000000000000000000000000000000000000001
  105. ]
  106. #VS.map! {|v| 0x4000000000000000.coerce(v)[0] }
  107. BDSIZE = 0x4000000000000000.coerce(0)[0].size
  108. def self.bdsize(x)
  109. ((x + 1) / 8 + BDSIZE) / BDSIZE * BDSIZE
  110. end
  111. def bdsize(x)
  112. self.class.bdsize(x)
  113. end
  114. min = -1
  115. min *= 2 while min.class == Fixnum
  116. FIXNUM_MIN = min/2
  117. max = 1
  118. max *= 2 while (max-1).class == Fixnum
  119. FIXNUM_MAX = max/2-1
  120. def test_fixnum_range
  121. assert_instance_of(Bignum, FIXNUM_MIN-1)
  122. assert_instance_of(Fixnum, FIXNUM_MIN)
  123. assert_instance_of(Fixnum, FIXNUM_MAX)
  124. assert_instance_of(Bignum, FIXNUM_MAX+1)
  125. end
  126. def check_class(n)
  127. if FIXNUM_MIN <= n && n <= FIXNUM_MAX
  128. assert_instance_of(Fixnum, n)
  129. else
  130. assert_instance_of(Bignum, n)
  131. end
  132. end
  133. def test_aref
  134. VS.each {|a|
  135. 100.times {|i|
  136. assert_equal((a >> i).odd? ? 1 : 0, a[i], "(#{a})[#{i}]")
  137. }
  138. }
  139. VS.each {|a|
  140. VS.each {|b|
  141. c = nil
  142. assert_nothing_raised("(#{a})[#{b}]") { c = a[b] }
  143. check_class(c)
  144. if b < 0
  145. assert_equal(0, c, "(#{a})[#{b}]")
  146. else
  147. assert_equal((a >> b).odd? ? 1 : 0, c, "(#{a})[#{b}]")
  148. end
  149. }
  150. }
  151. # assert_equal(1, (1 << 0x40000000)[0x40000000], "[ruby-dev:31271]")
  152. # assert_equal(0, (-1 << 0x40000001)[0x40000000], "[ruby-dev:31271]")
  153. big_zero = 0x40000000.coerce(0)[0]
  154. assert_equal(0, (-0x40000002)[big_zero], "[ruby-dev:31271]")
  155. assert_equal(1, 0x400000001[big_zero], "[ruby-dev:31271]")
  156. end
  157. def test_plus
  158. VS.each {|a|
  159. VS.each {|b|
  160. c = a + b
  161. check_class(c)
  162. assert_equal(b + a, c, "#{a} + #{b}")
  163. assert_equal(a, c - b, "(#{a} + #{b}) - #{b}")
  164. assert_equal(a-~b-1, c, "#{a} + #{b}") # Hacker's Delight
  165. assert_equal((a^b)+2*(a&b), c, "#{a} + #{b}") # Hacker's Delight
  166. assert_equal((a|b)+(a&b), c, "#{a} + #{b}") # Hacker's Delight
  167. assert_equal(2*(a|b)-(a^b), c, "#{a} + #{b}") # Hacker's Delight
  168. }
  169. }
  170. end
  171. def test_minus
  172. VS.each {|a|
  173. VS.each {|b|
  174. c = a - b
  175. check_class(c)
  176. assert_equal(a, c + b, "(#{a} - #{b}) + #{b}")
  177. assert_equal(-b, c - a, "(#{a} - #{b}) - #{a}")
  178. assert_equal(a+~b+1, c, "#{a} - #{b}") # Hacker's Delight
  179. assert_equal((a^b)-2*(b&~a), c, "#{a} - #{b}") # Hacker's Delight
  180. assert_equal((a&~b)-(b&~a), c, "#{a} - #{b}") # Hacker's Delight
  181. assert_equal(2*(a&~b)-(a^b), c, "#{a} - #{b}") # Hacker's Delight
  182. }
  183. }
  184. end
  185. def test_mult
  186. VS.each {|a|
  187. VS.each {|b|
  188. c = a * b
  189. check_class(c)
  190. assert_equal(b * a, c, "#{a} * #{b}")
  191. assert_equal(b, c / a, "(#{a} * #{b}) / #{a}") if a != 0
  192. assert_equal(a.abs * b.abs, (a * b).abs, "(#{a} * #{b}).abs")
  193. assert_equal((a-100)*(b-100)+(a-100)*100+(b-100)*100+10000, c, "#{a} * #{b}")
  194. assert_equal((a+100)*(b+100)-(a+100)*100-(b+100)*100+10000, c, "#{a} * #{b}")
  195. }
  196. }
  197. end
  198. def test_divmod
  199. VS.each {|a|
  200. VS.each {|b|
  201. if b == 0
  202. assert_raise(ZeroDivisionError) { a.divmod(b) }
  203. else
  204. q, r = a.divmod(b)
  205. check_class(q)
  206. check_class(r)
  207. assert_equal(a, b*q+r)
  208. assert(r.abs < b.abs)
  209. assert(0 < b ? (0 <= r && r < b) : (b < r && r <= 0))
  210. assert_equal(q, a/b)
  211. assert_equal(q, a.div(b))
  212. assert_equal(r, a%b)
  213. assert_equal(r, a.modulo(b))
  214. end
  215. }
  216. }
  217. end
  218. def test_pow
  219. small_values = VS.find_all {|v| 0 <= v && v < 1000 }
  220. VS.each {|a|
  221. small_values.each {|b|
  222. c = a ** b
  223. check_class(c)
  224. d = 1
  225. b.times { d *= a }
  226. assert_equal(d, c, "(#{a}) ** #{b}")
  227. if a != 0
  228. d = c
  229. b.times { d /= a }
  230. assert_equal(1, d, "((#{a}) ** #{b}) / #{a} / ...(#{b} times)...")
  231. end
  232. }
  233. }
  234. assert_equal(0**-1 == 0, false)
  235. end
  236. def test_not
  237. VS.each {|a|
  238. b = ~a
  239. check_class(b)
  240. assert_equal(-1 ^ a, b, "~#{a}")
  241. assert_equal(-a-1, b, "~#{a}") # Hacker's Delight
  242. assert_equal(0, a & b, "#{a} & ~#{a}")
  243. assert_equal(-1, a | b, "#{a} | ~#{a}")
  244. }
  245. end
  246. def test_or
  247. VS.each {|a|
  248. VS.each {|b|
  249. c = a | b
  250. check_class(c)
  251. assert_equal(b | a, c, "#{a} | #{b}")
  252. assert_equal(a + b - (a&b), c, "#{a} | #{b}")
  253. assert_equal((a & ~b) + b, c, "#{a} | #{b}") # Hacker's Delight
  254. assert_equal(-1, c | ~a, "(#{a} | #{b}) | ~#{a})")
  255. }
  256. }
  257. end
  258. def test_and
  259. VS.each {|a|
  260. VS.each {|b|
  261. c = a & b
  262. check_class(c)
  263. assert_equal(b & a, c, "#{a} & #{b}")
  264. assert_equal(a + b - (a|b), c, "#{a} & #{b}")
  265. assert_equal((~a | b) - ~a, c, "#{a} & #{b}") # Hacker's Delight
  266. assert_equal(0, c & ~a, "(#{a} & #{b}) & ~#{a}")
  267. }
  268. }
  269. end
  270. def test_xor
  271. VS.each {|a|
  272. VS.each {|b|
  273. c = a ^ b
  274. check_class(c)
  275. assert_equal(b ^ a, c, "#{a} ^ #{b}")
  276. assert_equal((a|b)-(a&b), c, "#{a} ^ #{b}") # Hacker's Delight
  277. assert_equal(b, c ^ a, "(#{a} ^ #{b}) ^ #{a}")
  278. }
  279. }
  280. end
  281. def test_lshift
  282. small_values = VS.find_all {|v| v < 8000 }
  283. VS.each {|a|
  284. small_values.each {|b|
  285. c = a << b
  286. check_class(c)
  287. if 0 <= b
  288. assert_equal(a, c >> b, "(#{a} << #{b}) >> #{b}")
  289. assert_equal(a * 2**b, c, "#{a} << #{b}")
  290. end
  291. 0.upto(c.size*8+10) {|nth|
  292. assert_equal(a[nth-b], c[nth], "(#{a} << #{b})[#{nth}]")
  293. }
  294. }
  295. }
  296. assert_equal(0, 1 << -0x40000000)
  297. assert_equal(0, 1 << -0x40000001)
  298. assert_equal(0, 1 << -0x80000000)
  299. assert_equal(0, 1 << -0x80000001)
  300. # assert_equal(bdsize(0x80000000), (1 << 0x80000000).size)
  301. end
  302. def test_rshift
  303. small_values = VS.find_all {|v| -8000 < v }
  304. VS.each {|a|
  305. small_values.each {|b|
  306. c = a >> b
  307. check_class(c)
  308. if b <= 0
  309. assert_equal(a, c << b, "(#{a} >> #{b}) << #{b}")
  310. assert_equal(a * 2**(-b), c, "#{a} >> #{b}")
  311. end
  312. 0.upto(c.size*8+10) {|nth|
  313. assert_equal(a[nth+b], c[nth], "(#{a} >> #{b})[#{nth}]")
  314. }
  315. }
  316. }
  317. # assert_equal(bdsize(0x40000001), (1 >> -0x40000001).size)
  318. assert((1 >> 0x80000000).zero?)
  319. assert((1 >> 0xffffffff).zero?)
  320. assert((1 >> 0x100000000).zero?)
  321. # assert_equal((1 << 0x40000000), (1 >> -0x40000000))
  322. # assert_equal((1 << 0x40000001), (1 >> -0x40000001))
  323. end
  324. def test_succ
  325. VS.each {|a|
  326. b = a.succ
  327. check_class(b)
  328. assert_equal(a+1, b, "(#{a}).succ")
  329. assert_equal(a, b.pred, "(#{a}).succ.pred")
  330. assert_equal(a, b-1, "(#{a}).succ - 1")
  331. }
  332. end
  333. def test_pred
  334. VS.each {|a|
  335. b = a.pred
  336. check_class(b)
  337. assert_equal(a-1, b, "(#{a}).pred")
  338. assert_equal(a, b.succ, "(#{a}).pred.succ")
  339. assert_equal(a, b + 1, "(#{a}).pred + 1")
  340. }
  341. end
  342. def test_unary_plus
  343. VS.each {|a|
  344. b = +a
  345. check_class(b)
  346. assert_equal(a, b, "+(#{a})")
  347. }
  348. end
  349. def test_unary_minus
  350. VS.each {|a|
  351. b = -a
  352. check_class(b)
  353. assert_equal(0-a, b, "-(#{a})")
  354. assert_equal(~a+1, b, "-(#{a})")
  355. assert_equal(0, a+b, "#{a}+(-(#{a}))")
  356. }
  357. end
  358. def test_cmp
  359. VS.each_with_index {|a, i|
  360. VS.each_with_index {|b, j|
  361. assert_equal(i <=> j, a <=> b, "#{a} <=> #{b}")
  362. assert_equal(i < j, a < b, "#{a} < #{b}")
  363. assert_equal(i <= j, a <= b, "#{a} <= #{b}")
  364. assert_equal(i > j, a > b, "#{a} > #{b}")
  365. assert_equal(i >= j, a >= b, "#{a} >= #{b}")
  366. }
  367. }
  368. end
  369. def test_eq
  370. VS.each_with_index {|a, i|
  371. VS.each_with_index {|b, j|
  372. c = a == b
  373. assert_equal(b == a, c, "#{a} == #{b}")
  374. assert_equal(i == j, c, "#{a} == #{b}")
  375. }
  376. }
  377. end
  378. def test_abs
  379. VS.each {|a|
  380. b = a.abs
  381. check_class(b)
  382. if a < 0
  383. assert_equal(-a, b, "(#{a}).abs")
  384. else
  385. assert_equal(a, b, "(#{a}).abs")
  386. end
  387. }
  388. end
  389. def test_ceil
  390. VS.each {|a|
  391. b = a.ceil
  392. check_class(b)
  393. assert_equal(a, b, "(#{a}).ceil")
  394. }
  395. end
  396. def test_floor
  397. VS.each {|a|
  398. b = a.floor
  399. check_class(b)
  400. assert_equal(a, b, "(#{a}).floor")
  401. }
  402. end
  403. def test_round
  404. VS.each {|a|
  405. b = a.round
  406. check_class(b)
  407. assert_equal(a, b, "(#{a}).round")
  408. }
  409. end
  410. def test_truncate
  411. VS.each {|a|
  412. b = a.truncate
  413. check_class(b)
  414. assert_equal(a, b, "(#{a}).truncate")
  415. }
  416. end
  417. def test_remainder
  418. VS.each {|a|
  419. VS.each {|b|
  420. if b == 0
  421. assert_raise(ZeroDivisionError) { a.divmod(b) }
  422. else
  423. r = a.remainder(b)
  424. check_class(r)
  425. if a < 0
  426. assert_operator(-b.abs, :<, r, "#{a}.remainder(#{b})")
  427. assert_operator(0, :>=, r, "#{a}.remainder(#{b})")
  428. elsif 0 < a
  429. assert_operator(0, :<=, r, "#{a}.remainder(#{b})")
  430. assert_operator(b.abs, :>, r, "#{a}.remainder(#{b})")
  431. else
  432. assert_equal(0, r, "#{a}.remainder(#{b})")
  433. end
  434. end
  435. }
  436. }
  437. end
  438. def test_zero_nonzero
  439. VS.each {|a|
  440. z = a.zero?
  441. n = a.nonzero?
  442. if a == 0
  443. assert_equal(true, z, "(#{a}).zero?")
  444. assert_equal(nil, n, "(#{a}).nonzero?")
  445. else
  446. assert_equal(false, z, "(#{a}).zero?")
  447. assert_equal(a, n, "(#{a}).nonzero?")
  448. check_class(n)
  449. end
  450. assert(z ^ n, "(#{a}).zero? ^ (#{a}).nonzero?")
  451. }
  452. end
  453. def test_even_odd
  454. VS.each {|a|
  455. e = a.even?
  456. o = a.odd?
  457. assert_equal((a % 2) == 0, e, "(#{a}).even?")
  458. assert_equal((a % 2) == 1, o, "(#{a}).odd")
  459. assert_equal((a & 1) == 0, e, "(#{a}).even?")
  460. assert_equal((a & 1) == 1, o, "(#{a}).odd")
  461. assert(e ^ o, "(#{a}).even? ^ (#{a}).odd?")
  462. }
  463. end
  464. def test_to_s
  465. 2.upto(36) {|radix|
  466. VS.each {|a|
  467. s = a.to_s(radix)
  468. b = s.to_i(radix)
  469. assert_equal(a, b, "(#{a}).to_s(#{radix}).to_i(#{radix})")
  470. }
  471. }
  472. end
  473. def test_printf_x
  474. VS.reverse_each {|a|
  475. s = sprintf("%x", a)
  476. if /\A\.\./ =~ s
  477. b = -($'.tr('0123456789abcdef', 'fedcba9876543210').to_i(16) + 1)
  478. else
  479. b = s.to_i(16)
  480. end
  481. assert_equal(a, b, "sprintf('%x', #{a}) = #{s.inspect}")
  482. }
  483. end
  484. def test_printf_x_sign
  485. VS.reverse_each {|a|
  486. s = sprintf("%+x", a)
  487. b = s.to_i(16)
  488. assert_equal(a, b, "sprintf('%+x', #{a}) = #{s.inspect}")
  489. s = sprintf("% x", a)
  490. b = s.to_i(16)
  491. assert_equal(a, b, "sprintf('% x', #{a}) = #{s.inspect}")
  492. }
  493. end
  494. def test_printf_o
  495. VS.reverse_each {|a|
  496. s = sprintf("%o", a)
  497. if /\A\.\./ =~ s
  498. b = -($'.tr('01234567', '76543210').to_i(8) + 1)
  499. else
  500. b = s.to_i(8)
  501. end
  502. assert_equal(a, b, "sprintf('%o', #{a}) = #{s.inspect}")
  503. }
  504. end
  505. def test_printf_o_sign
  506. VS.reverse_each {|a|
  507. s = sprintf("%+o", a)
  508. b = s.to_i(8)
  509. assert_equal(a, b, "sprintf('%+o', #{a}) = #{s.inspect}")
  510. s = sprintf("% o", a)
  511. b = s.to_i(8)
  512. assert_equal(a, b, "sprintf('% o', #{a}) = #{s.inspect}")
  513. }
  514. end
  515. def test_printf_b
  516. VS.reverse_each {|a|
  517. s = sprintf("%b", a)
  518. if /\A\.\./ =~ s
  519. b = -($'.tr('01', '10').to_i(2) + 1)
  520. else
  521. b = s.to_i(2)
  522. end
  523. assert_equal(a, b, "sprintf('%b', #{a}) = #{s.inspect}")
  524. }
  525. end
  526. def test_printf_b_sign
  527. VS.reverse_each {|a|
  528. s = sprintf("%+b", a)
  529. b = s.to_i(2)
  530. assert_equal(a, b, "sprintf('%+b', #{a}) = #{s.inspect}")
  531. s = sprintf("% b", a)
  532. b = s.to_i(2)
  533. assert_equal(a, b, "sprintf('% b', #{a}) = #{s.inspect}")
  534. }
  535. end
  536. def test_printf_di
  537. VS.reverse_each {|a|
  538. s = sprintf("%d", a)
  539. b = s.to_i
  540. assert_equal(a, b, "sprintf('%d', #{a}) = #{s.inspect}")
  541. s = sprintf("%i", a)
  542. b = s.to_i
  543. assert_equal(a, b, "sprintf('%i', #{a}) = #{s.inspect}")
  544. }
  545. end
  546. def test_marshal
  547. VS.reverse_each {|a|
  548. s = Marshal.dump(a)
  549. b = Marshal.load(s)
  550. assert_equal(a, b, "Marshal.load(Marshal.dump(#{a}))")
  551. }
  552. end
  553. def test_pack_ber
  554. template = "w"
  555. VS.reverse_each {|a|
  556. if a < 0
  557. assert_raise(ArgumentError) { [a].pack(template) }
  558. else
  559. s = [a].pack(template)
  560. b = s.unpack(template)[0]
  561. assert_equal(a, b, "[#{a}].pack(#{template.dump}).unpack(#{template.dump})")
  562. end
  563. }
  564. end
  565. def test_pack_utf8
  566. template = "U"
  567. VS.reverse_each {|a|
  568. if a < 0 || 0x7fffffff < a
  569. assert_raise(RangeError) { [a].pack(template) }
  570. else
  571. s = [a].pack(template)
  572. b = s.unpack(template)[0]
  573. assert_equal(a, b, "[#{a}].pack(#{template.dump}).unpack(#{template.dump})")
  574. end
  575. }
  576. end
  577. def test_Integer
  578. assert_raise(ArgumentError) {Integer("0x-1")}
  579. assert_raise(ArgumentError) {Integer("-0x-1")}
  580. assert_raise(ArgumentError) {Integer("0x 123")}
  581. assert_raise(ArgumentError) {Integer("0x 123")}
  582. assert_raise(ArgumentError) {Integer("0x0x5")}
  583. assert_raise(ArgumentError) {Integer("0x0x000000005")}
  584. assert_nothing_raised(ArgumentError) {
  585. assert_equal(1540841, "0x0x5".to_i(36))
  586. }
  587. assert_raise(ArgumentError) { Integer("--0") }
  588. assert_raise(ArgumentError) { Integer("-+0") }
  589. assert_raise(ArgumentError) { Integer("++1") }
  590. assert_raise(ArgumentError) { Integer("") }
  591. assert_raise(ArgumentError) { Integer("10 x") }
  592. assert_raise(ArgumentError) { Integer("1__2") }
  593. assert_raise(ArgumentError) { Integer("1z") }
  594. assert_raise(ArgumentError) { Integer("46116860184273__87904") }
  595. assert_raise(ArgumentError) { Integer("4611686018427387904_") }
  596. assert_raise(ArgumentError) { Integer("4611686018427387904 :") }
  597. assert_equal(0x4000000000000000, Integer("46_11_686_0184273_87904"))
  598. assert_raise(ArgumentError) { Integer("\0") }
  599. assert_nothing_raised(ArgumentError, "[ruby-core:13873]") {
  600. assert_equal(0, Integer("0 "))
  601. }
  602. assert_nothing_raised(ArgumentError, "[ruby-core:14139]") {
  603. assert_equal(0377, Integer("0_3_7_7"))
  604. }
  605. assert_raise(ArgumentError, "[ruby-core:14139]") {Integer("0__3_7_7")}
  606. end
  607. end