PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/test/ruby/test_bignum.rb

http://github.com/ruby/ruby
Ruby | 797 lines | 693 code | 100 blank | 4 comment | 15 complexity | 7870d59dbd982929626843ee3ade3eb2 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. begin
  4. require '-test-/integer'
  5. rescue LoadError
  6. else
  7. class TestBignum < Test::Unit::TestCase
  8. FIXNUM_MIN = RbConfig::LIMITS['FIXNUM_MIN']
  9. FIXNUM_MAX = RbConfig::LIMITS['FIXNUM_MAX']
  10. BIGNUM_MIN = FIXNUM_MAX + 1
  11. f = BIGNUM_MIN
  12. n = 0
  13. until f == 0
  14. f >>= 1
  15. n += 1
  16. end
  17. BIGNUM_MIN_BITS = n
  18. T_ZERO = 0.to_bignum
  19. T_ONE = 1.to_bignum
  20. T_MONE = (-1).to_bignum
  21. T31 = (2**31).to_bignum # 2147483648
  22. T31P = (T31 - 1).to_bignum # 2147483647
  23. T32 = (2**32).to_bignum # 4294967296
  24. T32P = (T32 - 1).to_bignum # 4294967295
  25. T64 = (2**64).to_bignum # 18446744073709551616
  26. T64P = (T64 - 1).to_bignum # 18446744073709551615
  27. T128 = (2**128).to_bignum
  28. T128P = (T128 - 1).to_bignum
  29. T1024 = (2**1024).to_bignum
  30. T1024P = (T1024 - 1).to_bignum
  31. def setup
  32. @verbose = $VERBOSE
  33. $VERBOSE = nil
  34. @fmax = Float::MAX.to_i
  35. @fmax2 = @fmax * 2
  36. @big = (1 << BIGNUM_MIN_BITS) - 1
  37. end
  38. def teardown
  39. $VERBOSE = @verbose
  40. end
  41. def fact(n)
  42. return 1 if n == 0
  43. f = 1
  44. while n>0
  45. f *= n
  46. n -= 1
  47. end
  48. return f
  49. end
  50. def test_prepare
  51. assert_bignum(@big)
  52. assert_bignum(T_ZERO)
  53. assert_bignum(T_ONE)
  54. assert_bignum(T_MONE)
  55. assert_bignum(T31)
  56. assert_bignum(T31P)
  57. assert_bignum(T32)
  58. assert_bignum(T32P)
  59. assert_bignum(T64)
  60. assert_bignum(T64P)
  61. assert_bignum(T1024)
  62. assert_bignum(T1024P)
  63. end
  64. def test_bignum
  65. $x = fact(40)
  66. assert_equal($x, $x)
  67. assert_equal($x, fact(40))
  68. assert_operator($x, :<, $x+2)
  69. assert_operator($x, :>, $x-2)
  70. assert_equal(815915283247897734345611269596115894272000000000, $x)
  71. assert_not_equal(815915283247897734345611269596115894272000000001, $x)
  72. assert_equal(815915283247897734345611269596115894272000000001, $x+1)
  73. assert_equal(335367096786357081410764800000, $x/fact(20))
  74. $x = -$x
  75. assert_equal(-815915283247897734345611269596115894272000000000, $x)
  76. b = 2*BIGNUM_MIN
  77. assert_equal(2-b, -(b-2))
  78. assert_equal(b - 5, (b-3)-2)
  79. for i in 1000..1014
  80. assert_equal(2 ** i, 1 << i)
  81. end
  82. n1 = 1 << 1000
  83. for i in 1000..1014
  84. assert_equal(n1, 1 << i)
  85. n1 *= 2
  86. end
  87. n2=n1
  88. for i in 1..10
  89. n1 = n1 / 2
  90. n2 = n2 >> 1
  91. assert_equal(n1, n2)
  92. end
  93. for i in 4000..4096
  94. n1 = 1 << i;
  95. assert_equal(n1-1, (n1**2-1) / (n1+1))
  96. end
  97. end
  98. def test_calc
  99. b = 10**80
  100. a = b * 9 + 7
  101. assert_equal(7, a.modulo(b))
  102. assert_equal(-b + 7, a.modulo(-b))
  103. assert_equal(b + -7, (-a).modulo(b))
  104. assert_equal(-7, (-a).modulo(-b))
  105. assert_equal(7, a.remainder(b))
  106. assert_equal(7, a.remainder(-b))
  107. assert_equal(-7, (-a).remainder(b))
  108. assert_equal(-7, (-a).remainder(-b))
  109. assert_equal(10000000000000000000100000000000000000000, 10**40+10**20)
  110. assert_equal(100000000000000000000, 10**40/10**20)
  111. a = 677330545177305025495135714080
  112. b = 14269972710765292560
  113. assert_equal(0, a % b)
  114. assert_equal(0, -a % b)
  115. end
  116. def shift_test(a)
  117. b = a / (2 ** 32)
  118. c = a >> 32
  119. assert_equal(b, c)
  120. b = a * (2 ** 32)
  121. c = a << 32
  122. assert_equal(b, c)
  123. end
  124. def test_shift
  125. shift_test(-4518325415524767873)
  126. shift_test(-0xfffffffffffffffff)
  127. end
  128. def test_to_s
  129. assert_equal("fvvvvvvvvvvvv" ,18446744073709551615.to_s(32), "[ruby-core:10686]")
  130. assert_equal("g000000000000" ,18446744073709551616.to_s(32), "[ruby-core:10686]")
  131. assert_equal("3w5e11264sgsf" ,18446744073709551615.to_s(36), "[ruby-core:10686]")
  132. assert_equal("3w5e11264sgsg" ,18446744073709551616.to_s(36), "[ruby-core:10686]")
  133. assert_equal("nd075ib45k86f" ,18446744073709551615.to_s(31), "[ruby-core:10686]")
  134. assert_equal("nd075ib45k86g" ,18446744073709551616.to_s(31), "[ruby-core:10686]")
  135. assert_equal("1777777777777777777777" ,18446744073709551615.to_s(8))
  136. assert_equal("-1777777777777777777777" ,-18446744073709551615.to_s(8))
  137. assert_match(/\A10{99}1\z/, (10**100+1).to_s)
  138. assert_match(/\A10{900}9{100}\z/, (10**1000+(10**100-1)).to_s)
  139. end
  140. def test_big_2comp
  141. assert_equal("-4294967296", (~T32P).to_s)
  142. assert_equal("..f00000000", "%x" % -T32)
  143. end
  144. def test_int2inum
  145. assert_equal([T31P], [T31P].pack("I").unpack("I"))
  146. assert_equal([T31P], [T31P].pack("i").unpack("i"))
  147. end
  148. def test_quad_pack
  149. assert_equal([ 1], [ 1].pack("q").unpack("q"))
  150. assert_equal([- 1], [- 1].pack("q").unpack("q"))
  151. assert_equal([ T31P], [ T31P].pack("q").unpack("q"))
  152. assert_equal([-T31P], [-T31P].pack("q").unpack("q"))
  153. assert_equal([ T64P], [ T64P].pack("Q").unpack("Q"))
  154. assert_equal([ 0], [ T64 ].pack("Q").unpack("Q"))
  155. end
  156. def test_str_to_inum
  157. assert_equal(1, " +1".to_i)
  158. assert_equal(-1, " -1".to_i)
  159. assert_equal(0, "++1".to_i)
  160. assert_equal(73, "111".oct)
  161. assert_equal(273, "0x111".oct)
  162. assert_equal(7, "0b111".oct)
  163. assert_equal(73, "0o111".oct)
  164. assert_equal(111, "0d111".oct)
  165. assert_equal(73, "0111".oct)
  166. assert_equal(111, Integer("111"))
  167. assert_equal(13, "111".to_i(3))
  168. assert_raise(ArgumentError) { "111".to_i(37) }
  169. assert_equal(1333, "111".to_i(36))
  170. assert_equal(1057, "111".to_i(32))
  171. assert_equal(0, "00a".to_i)
  172. assert_equal(1, Integer("1 "))
  173. assert_raise(ArgumentError) { Integer("1_") }
  174. assert_raise(ArgumentError) { Integer("1__") }
  175. assert_raise(ArgumentError) { Integer("1_0 x") }
  176. assert_equal(T31P, "1111111111111111111111111111111".to_i(2))
  177. assert_equal(0_2, '0_2'.to_i)
  178. assert_equal(00_2, '00_2'.to_i)
  179. assert_equal(00_02, '00_02'.to_i)
  180. end
  181. def test_to_s2
  182. assert_raise(ArgumentError) { T31P.to_s(37) }
  183. assert_equal("9" * 32768, (10**32768-1).to_s)
  184. assert_raise(RangeError) { Process.wait(1, T64P) }
  185. assert_equal("0", T_ZERO.to_s)
  186. assert_equal("1", T_ONE.to_s)
  187. end
  188. def test_to_f
  189. assert_nothing_raised { T31P.to_f.to_i }
  190. assert_raise(FloatDomainError) { (1024**1024).to_f.to_i }
  191. assert_equal(1, (2**50000).to_f.infinite?)
  192. assert_equal(-1, (-(2**50000)).to_f.infinite?)
  193. end
  194. def test_cmp
  195. assert_operator(T31P, :>, 1)
  196. assert_operator(T31P, :<, 2147483648.0)
  197. assert_operator(T31P, :<, T64P)
  198. assert_operator(T64P, :>, T31P)
  199. assert_raise(ArgumentError) { T31P < "foo" }
  200. assert_operator(T64, :<, (1.0/0.0))
  201. assert_not_operator(T64, :>, (1.0/0.0))
  202. end
  203. def test_eq
  204. assert_not_equal(T31P, 1)
  205. assert_equal(T31P, 2147483647.0)
  206. assert_not_equal(T31P, "foo")
  207. assert_not_equal(2**77889, (1.0/0.0), '[ruby-core:31603]')
  208. end
  209. def test_eql
  210. assert_send([T31P, :eql?, T31P])
  211. end
  212. def test_convert
  213. assert_equal([255], [T_MONE].pack("C").unpack("C"))
  214. assert_equal([0], [T32].pack("C").unpack("C"))
  215. assert_raise(RangeError) { 0.to_s(T32) }
  216. end
  217. def test_sub
  218. assert_equal(-T31, T32 - (T32 + T31))
  219. x = 2**100
  220. assert_equal(1, (x+2) - (x+1))
  221. assert_equal(-1, (x+1) - (x+2))
  222. assert_equal(0, (2**100) - (2.0**100))
  223. o = Object.new
  224. def o.coerce(x); [x, 2**100+2]; end
  225. assert_equal(-1, (2**100+1) - o)
  226. assert_equal(-1, T_ONE - 2)
  227. end
  228. def test_plus
  229. assert_equal(T32.to_f, T32P + 1.0)
  230. assert_raise(TypeError) { T32 + "foo" }
  231. assert_equal(1267651809154049016125877911552, (2**100) + (2**80))
  232. assert_equal(1267651809154049016125877911552, (2**80) + (2**100))
  233. assert_equal(2**101, (2**100) + (2.0**100))
  234. o = Object.new
  235. def o.coerce(x); [x, 2**80]; end
  236. assert_equal(1267651809154049016125877911552, (2**100) + o)
  237. end
  238. def test_minus
  239. assert_equal(T32P.to_f, T32 - 1.0)
  240. assert_raise(TypeError) { T32 - "foo" }
  241. end
  242. def test_mul
  243. assert_equal(T32.to_f, T32 * 1.0)
  244. assert_raise(TypeError) { T32 * "foo" }
  245. o = Object.new
  246. def o.coerce(x); [x, 2**100]; end
  247. assert_equal(2**180, (2**80) * o)
  248. end
  249. def test_positive_p
  250. assert_predicate(T_ONE, :positive?)
  251. assert_not_predicate(T_MONE, :positive?)
  252. assert_not_predicate(T_ZERO, :positive?)
  253. end
  254. def test_negative_p
  255. assert_not_predicate(T_ONE, :negative?)
  256. assert_predicate(T_MONE, :negative?)
  257. assert_not_predicate(T_ZERO, :negative?)
  258. end
  259. def test_mul_balance
  260. assert_equal(3**7000, (3**5000) * (3**2000))
  261. end
  262. def test_mul_large_numbers
  263. a = %w[
  264. 32580286268570032115047167942578356789222410206194227403993117616454027392
  265. 62501901985861926098797067562795526004375784403965882943322008991129440928
  266. 33855888840298794008677656280486901895499985197580043127115026675632969396
  267. 55040226415022070581995493731570435346323030715226718346725312551631168110
  268. 83966158581772380474470605428802018934282425947323171408377505151988776271
  269. 85865548747366001752375899635539662017095652855537225416899242508164949615
  270. 96848508410008685252121247181772953744297349638273854170932226446528911938
  271. 03430429031094465344063914822790537339912760237589085026016396616506014081
  272. 53557719631183538265614091691713138728177917059624255801026099255450058876
  273. 97412698978242128457751836011774504753020608663272925708049430557191193188
  274. 23212591809241860763625985763438355314593186083254640117460724730431447842
  275. 15432124830037389073162094304199742919767272162759192882136828372588787906
  276. 96027938532441670018954643423581446981760344524184231299785949158765352788
  277. 38452309862972527623669323263424418781899966895996672291193305401609553502
  278. 63893514163147729201340204483973131948541009975283778189609285614445485714
  279. 63843850089417416331356938086609682943037801440660232801570877143192251897
  280. 63026816485314923378023904237699794122181407920355722922555234540701118607
  281. 37971417665315821995516986204709574657462370947443531049033704997194647442
  282. 13711787319587466437795542850136751816475182349380345341647976135081955799
  283. 56787050815348701001765730577514591032367920292271016649813170789854524395
  284. 72571698998841196411826453893352760318867994518757872432266374568779920489
  285. 55597104558927387008506485038236352630863481679853742412042588244086070827
  286. 43705456833283086410967648483312972903432798923897357373793064381177468258
  287. 69131640408147806442422254638590386673344704147156793990832671592488742473
  288. 31524606724894164324227362735271650556732855509929890983919463699819116427
  289. ].join.to_i
  290. b = %w[
  291. 31519454770031243652776765515030872050264386564379909299874378289835540661
  292. 99756262835346828114038365624177182230027040172583473561802565238817167503
  293. 85144159132462819032164726177606533272071955542237648482852154879445654746
  294. 25061253606344846225905712926863168413666058602449408307586532461776530803
  295. 56810626880722653177544008166119272373179841889454920521993413902672848145
  296. 77974951972342194855267960390195830413354782136431833731467699250684103370
  297. 98571305167189174270854698169136844578685346745340041520068176478277580590
  298. 43810457765638903028049263788987034217272442328962400931269515791911786205
  299. 15357047519615932249418012945178659435259428163356223753159488306813844040
  300. 93609959555018799309373542926110109744437994067754004273450659607204900586
  301. 28878103661124568217617766580438460505513654179249613168352070584906185237
  302. 34829991855182473813233425492094534396541544295119674419522772382981982574
  303. 64708442087451070125274285088681225122475041996116377707892328889948526913
  304. 82239084041628877737628853240361038273348062246951097300286513836140601495
  305. 63604611754185656404194406869925540477185577643853560887894081047256701731
  306. 66884554460428760857958761948461476977864005799494946578017758268987123749
  307. 85937011490156431231903167442071541493304390639100774497107347884381581049
  308. 85451663323551635322518839895028929788021096587229364219084708576998525298
  309. 39594168681411529110089531428721005176467479027585291807482375043729783455
  310. 35827667428080449919778142400266842990117940984804919512360370451936835708
  311. 76338722049621773169385978521438867493162717866679193103745711403152099047
  312. 27294943901673885707639094215339506973982546487889199083181789561917985023
  313. 82368442718514694400160954955539704757794969665555505203532944598698824542
  314. 00599461848630034847211204029842422678421808487300084850702007663003230882
  315. 16645745324467830796203354080471008809087072562876681588151822072260738003
  316. ].join.to_i
  317. c = %w[
  318. 10269128594368631269792194698469828812223242061960065022209211719149714886
  319. 03494742299892841188636314745174778237781513956755034582435818316155459882
  320. 71422025990633195596790290038198841087091600598192959108790192789550336119
  321. 13849937951116346796903163312950010689963716629093190601532313463306463573
  322. 64436438673379454947908896258675634478867189655764364639888427350090856831
  323. 84369949421175534994092429682748078316130135651006102162888937624830856951
  324. 64818150356583421988135211585954838926347035741143424980258821170351244310
  325. 33072045488402539147707418016613224788469923473310249137422855065567940804
  326. 75231970365923936034328561426062696074717204901606475826224235014948198414
  327. 19979210494282212322919438926816203585575357874850252052656098969732107129
  328. 30639419804565653489687198910271702181183420960744232756057631336661646896
  329. 48734093497394719644969417287962767186599484579769717220518657324467736902
  330. 16947995288312851432262922140679347615046098863974141226499783975470926697
  331. 95970415188661518504275964397022973192968233221707696639386238428211541334
  332. 69925631385166494600401675904803418143232703594169525858261988389529181035
  333. 06048776134746377586210180203524132714354779486439559392942733781343640971
  334. 02430607931736785273011780813863748280091795277451796799961887248262211653
  335. 38966967509803488282644299584920109534552889962877144862747797551711984992
  336. 00726518175235286668236031649728858774545087668286506201943248842967749907
  337. 05345423019480534625965140632428736051632750698608916592720742728646191514
  338. 86268964807395494825321744802493138032936406889713953832376411900451422777
  339. 06372983421062172556566901346288286168790235741528630664513209619789835729
  340. 36999522461733403414326366959273556098219489572448083984779946889707480205
  341. 42459898495081687425132939473146331452400120169525968892769310016015870148
  342. 66821361032541586130017904207971120217385522074967066199941112154460026348
  343. 07223950375610474071278649031647998546085807777970592429037128484222394216
  344. 33776560239741740193444702279919018283324070210090106960567819910943036248
  345. 16660475627526085805165023447934326510232828674828006752369603151390527384
  346. 16810180735871644266726954590262010744712519045524839388305761859432443670
  347. 05188791334908140831469790180096209292338569623252372975043915954675335333
  348. 66614002146554533771788633057869340167604765688639181655208751680821446276
  349. 75871494160208888666798836473728725968253820774671626436794492530356258709
  350. 62318715778035246655925307167306434486713879511272648637608703497794724929
  351. 54912261106702913491290913962825303534484477936036071463820553314826894581
  352. 36951927032835690160443252405644718368516656317176848748544135126122940034
  353. 68454782581240953957381976073459570718038035358630417744490242611126043987
  354. 89191812971310096496208294948623403471433467614886863238916702384858514703
  355. 24327715474804343531844042107910755966152655912676456945146277848606406879
  356. 49724219295823540160221752189725460676360350860849986313532861445465771187
  357. 86822806696323658053947125253562001971534265078959827450518368635828010637
  358. 91977444206363529864361796188661941906329947840521598310396004328950804758
  359. 79728679236044038853668859284513594307352133390781441610395116807369310560
  360. 35193762565748328526426224069629084264376146174383444988110993194030351064
  361. 29660536743256949099972314033972121470913480844652490838985461134989129492
  362. 75577567064571716731774820127381261057956083604361635892088585967074514802
  363. 51958582645785905276289980534832170529946494815794770854644518463332458915
  364. 77572397432680871220602513555535017751714443325264019171753694163676670792
  365. 04353584782364068773777058727187323211012094819929720407636607815292764459
  366. 21851731257845562153822058534043916834839514338448582518847879059020959697
  367. 90538105704766415685100946308842788321400392381169436435078204622400475281
  368. ].join.to_i
  369. assert_equal(c, a*b, '[ruby-core:48552]')
  370. end
  371. def test_divrem
  372. assert_equal(0, T32 / T64)
  373. end
  374. def test_divide
  375. bug5490 = '[ruby-core:40429]'
  376. assert_raise(ZeroDivisionError, bug5490) {T1024./(0)}
  377. assert_equal(Float::INFINITY, T1024./(0.0), bug5490)
  378. end
  379. def test_div
  380. assert_equal(T32.to_f, T32 / 1.0)
  381. assert_raise(TypeError) { T32 / "foo" }
  382. assert_equal(0x20000000, 0x40000001.div(2.0), "[ruby-dev:34553]")
  383. bug5490 = '[ruby-core:40429]'
  384. assert_raise(ZeroDivisionError, bug5490) {T1024.div(0)}
  385. assert_raise(ZeroDivisionError, bug5490) {T1024.div(0.0)}
  386. end
  387. def test_idiv
  388. assert_equal(715827882, 1073741824.div(Rational(3,2)), ' [ruby-dev:34066]')
  389. end
  390. def test_modulo
  391. assert_raise(TypeError) { T32 % "foo" }
  392. end
  393. def test_remainder
  394. assert_equal(0, T32.remainder(1))
  395. assert_raise(TypeError) { T32.remainder("foo") }
  396. end
  397. def test_divmod
  398. assert_equal([T32, 0], T32.divmod(1))
  399. assert_equal([2, 0], T32.divmod(T31))
  400. assert_raise(TypeError) { T32.divmod("foo") }
  401. end
  402. def test_quo
  403. assert_kind_of(Float, T32.quo(1.0))
  404. assert_equal(T32.to_f, T32.quo(1))
  405. assert_equal(T32.to_f, T32.quo(1.0))
  406. assert_equal(T32.to_f, T32.quo(T_ONE))
  407. assert_raise(TypeError) { T32.quo("foo") }
  408. assert_equal(1024**1024, (1024**1024).quo(1))
  409. assert_equal(Float::INFINITY, (1024**1024).quo(1.0))
  410. assert_equal(1024**1024*2, (1024**1024*2).quo(1))
  411. inf = 1 / 0.0; nan = inf / inf
  412. assert_send([(1024**1024*2).quo(nan), :nan?])
  413. end
  414. def test_pow
  415. assert_equal(1.0, T32 ** 0.0)
  416. assert_equal(1.0 / T32, T32 ** -1)
  417. assert_equal(1, (T32 ** T32).infinite?)
  418. assert_equal(1, (T32 ** (2**30-1)).infinite?)
  419. ### rational changes the behavior of Bignum#**
  420. #assert_raise(TypeError) { T32**"foo" }
  421. assert_raise(TypeError, ArgumentError) { T32**"foo" }
  422. feature3429 = '[ruby-core:30735]'
  423. assert_kind_of(Integer, (2 ** 7830457), feature3429)
  424. end
  425. def test_and
  426. assert_equal(0, T32 & 1)
  427. assert_equal(-T32, (-T32) & (-T31))
  428. assert_equal(0, T32 & T64)
  429. end
  430. def test_or
  431. assert_equal(T32 + 1, T32 | 1)
  432. assert_equal(T32 + T31, T32 | T31)
  433. assert_equal(-T31, (-T32) | (-T31))
  434. assert_equal(T64 + T32, T32 | T64)
  435. assert_equal(FIXNUM_MAX, T_ZERO | FIXNUM_MAX)
  436. end
  437. def test_xor
  438. assert_equal(T32 + 1, T32 ^ 1)
  439. assert_equal(T32 + T31, T32 ^ T31)
  440. assert_equal(T31, (-T32) ^ (-T31))
  441. assert_equal(T64 + T32, T32 ^ T64)
  442. end
  443. class DummyNumeric < Numeric
  444. def to_int
  445. 1
  446. end
  447. end
  448. def test_and_with_float
  449. assert_raise(TypeError) { T1024 & 1.5 }
  450. end
  451. def test_and_with_rational
  452. assert_raise(TypeError, "#1792") { T1024 & Rational(3, 2) }
  453. end
  454. def test_and_with_nonintegral_numeric
  455. assert_raise(TypeError, "#1792") { T1024 & DummyNumeric.new }
  456. end
  457. def test_or_with_float
  458. assert_raise(TypeError) { T1024 | 1.5 }
  459. end
  460. def test_or_with_rational
  461. assert_raise(TypeError, "#1792") { T1024 | Rational(3, 2) }
  462. end
  463. def test_or_with_nonintegral_numeric
  464. assert_raise(TypeError, "#1792") { T1024 | DummyNumeric.new }
  465. end
  466. def test_xor_with_float
  467. assert_raise(TypeError) { T1024 ^ 1.5 }
  468. end
  469. def test_xor_with_rational
  470. assert_raise(TypeError, "#1792") { T1024 ^ Rational(3, 2) }
  471. end
  472. def test_xor_with_nonintegral_numeric
  473. assert_raise(TypeError, "#1792") { T1024 ^ DummyNumeric.new }
  474. end
  475. def test_shift2
  476. b = BIGNUM_MIN_BITS
  477. n = BIGNUM_MIN << 1
  478. assert_equal(2**(b+1), n << 1)
  479. assert_equal(2**(b-1), n << -1)
  480. assert_equal(2**(b+1), n << 1.0)
  481. assert_equal(2**(b-1), n << -1.0)
  482. assert_equal(2**(b+1), n << T_ONE)
  483. assert_equal(2**(b-1), n << T_MONE)
  484. assert_equal(2**(b-1), n >> 1)
  485. assert_equal(2**(b+1), n >> -1)
  486. assert_equal(2**(b-1), n >> 1.0)
  487. assert_equal(2**(b+1), n >> -1.0)
  488. assert_equal(2**(b-1), n >> T_ONE)
  489. assert_equal(2**(b+1), n >> T_MONE)
  490. assert_equal( 0, n >> n)
  491. assert_equal(-1, -n >> n)
  492. assert_equal( 0, n >> (b*4))
  493. assert_equal(-1, -n >> (b*4))
  494. assert_equal( 0, (n/2) >> b)
  495. assert_equal(-1, -(n/2) >> b)
  496. end
  497. def test_shift_bigshift
  498. big = 2**300
  499. assert_equal(2**65538 / (2**65537), 2**65538 >> big.coerce(65537).first)
  500. end
  501. def test_aref
  502. assert_equal(0, BIGNUM_MIN[0])
  503. assert_equal(0, BIGNUM_MIN[BIGNUM_MIN])
  504. assert_equal(0, BIGNUM_MIN[-BIGNUM_MIN])
  505. assert_equal(0, BIGNUM_MIN[T_ZERO])
  506. assert_equal(0, (-(BIGNUM_MIN*BIGNUM_MIN))[0])
  507. assert_equal(1, (-2**(BIGNUM_MIN_BITS*4))[BIGNUM_MIN_BITS*4])
  508. end
  509. def test_hash
  510. assert_nothing_raised { T31P.hash }
  511. end
  512. def test_coerce
  513. assert_equal([T64P, T31P], T31P.coerce(T64P))
  514. assert_raise(TypeError) { T31P.coerce(nil) }
  515. obj = eval("class C\u{1f5ff}; self; end").new
  516. assert_raise_with_message(TypeError, /C\u{1f5ff}/) { T31P.coerce(obj) }
  517. end
  518. def test_abs
  519. assert_equal(T31P, (-T31P).abs)
  520. end
  521. def test_size
  522. assert_kind_of(Integer, T31P.size)
  523. end
  524. def test_odd
  525. assert_equal(true, (BIGNUM_MIN+1).odd?)
  526. assert_equal(false, BIGNUM_MIN.odd?)
  527. end
  528. def test_even
  529. assert_equal(false, (BIGNUM_MIN+1).even?)
  530. assert_equal(true, BIGNUM_MIN.even?)
  531. end
  532. def test_interrupt_during_to_s
  533. if defined?(Integer::GMP_VERSION)
  534. return # GMP doesn't support interrupt during an operation.
  535. end
  536. time = Time.now
  537. end_flag = false
  538. num = (65536 ** 65536)
  539. q = Queue.new
  540. thread = Thread.new do
  541. assert_raise(RuntimeError) {
  542. q << true
  543. num.to_s
  544. end_flag = true
  545. }
  546. end
  547. q.pop # sync
  548. thread.raise
  549. thread.join
  550. time = Time.now - time
  551. skip "too fast cpu" if end_flag
  552. assert_operator(time, :<, 10)
  553. end
  554. def test_interrupt_during_bigdivrem
  555. if defined?(Integer::GMP_VERSION)
  556. return # GMP doesn't support interrupt during an operation.
  557. end
  558. return unless Process.respond_to?(:kill)
  559. begin
  560. trace = []
  561. oldtrap = Signal.trap(:INT) {|sig| trace << :int }
  562. a = 456 ** 100
  563. b = 123 ** 100
  564. c = nil
  565. 100.times do |n|
  566. a **= 3
  567. b **= 3
  568. trace.clear
  569. th = Thread.new do
  570. sleep 0.1; Process.kill :INT, $$
  571. sleep 0.1; Process.kill :INT, $$
  572. end
  573. c = a / b
  574. trace << :end
  575. th.join
  576. if trace == [:int, :int, :end]
  577. assert_equal(a / b, c)
  578. return
  579. end
  580. end
  581. skip "cannot create suitable test case"
  582. ensure
  583. Signal.trap(:INT, oldtrap) if oldtrap
  584. end
  585. end
  586. def test_too_big_to_s
  587. if (big = 2**31-1).fixnum?
  588. return
  589. end
  590. assert_raise_with_message(RangeError, /too big to convert/) {(1 << big).to_s}
  591. end
  592. def test_fix_fdiv
  593. assert_not_equal(0, 1.fdiv(@fmax2))
  594. assert_in_delta(0.5, 1.fdiv(@fmax2) * @fmax, 0.01)
  595. end
  596. def test_big_fdiv
  597. assert_equal(1, @big.fdiv(@big))
  598. assert_not_equal(0, @big.fdiv(@fmax2))
  599. assert_not_equal(0, @fmax2.fdiv(@big))
  600. assert_not_equal(0, @fmax2.fdiv(@fmax2))
  601. assert_in_delta(0.5, @fmax.fdiv(@fmax2), 0.01)
  602. assert_in_delta(1.0, @fmax2.fdiv(@fmax2), 0.01)
  603. end
  604. def test_float_fdiv
  605. b = 1E+300.to_i
  606. assert_equal(b, (b ** 2).fdiv(b))
  607. assert_send([@big.fdiv(0.0 / 0.0), :nan?])
  608. assert_in_delta(1E+300, (10**500).fdiv(1E+200), 1E+285)
  609. end
  610. def test_obj_fdiv
  611. o = Object.new
  612. def o.coerce(x); [x, 2**100]; end
  613. assert_equal((2**200).to_f, (2**300).fdiv(o))
  614. o = Object.new
  615. def o.coerce(x); [self, x]; end
  616. def o.fdiv(x); 1; end
  617. assert_equal(1.0, (2**300).fdiv(o))
  618. end
  619. def test_singleton_method
  620. # this test assumes 32bit/64bit platform
  621. assert_raise(TypeError) { a = 1 << 64; def a.foo; end }
  622. end
  623. def test_frozen
  624. assert_equal(true, (2**100).frozen?)
  625. end
  626. def test_bitwise_and_with_integer_mimic_object
  627. def (obj = Object.new).to_int
  628. 10
  629. end
  630. assert_raise(TypeError, '[ruby-core:39491]') { T1024 & obj }
  631. def obj.coerce(other)
  632. [other, 10]
  633. end
  634. assert_equal(T1024 & 10, T1024 & obj)
  635. end
  636. def test_bitwise_or_with_integer_mimic_object
  637. def (obj = Object.new).to_int
  638. 10
  639. end
  640. assert_raise(TypeError, '[ruby-core:39491]') { T1024 | obj }
  641. def obj.coerce(other)
  642. [other, 10]
  643. end
  644. assert_equal(T1024 | 10, T1024 | obj)
  645. end
  646. def test_bitwise_xor_with_integer_mimic_object
  647. def (obj = Object.new).to_int
  648. 10
  649. end
  650. assert_raise(TypeError, '[ruby-core:39491]') { T1024 ^ obj }
  651. def obj.coerce(other)
  652. [other, 10]
  653. end
  654. assert_equal(T1024 ^ 10, T1024 ^ obj)
  655. end
  656. def test_digits
  657. assert_equal([90, 78, 56, 34, 12], 1234567890.to_bignum.digits(100))
  658. assert_equal([7215, 2413, 6242], T1024P.digits(10_000).first(3))
  659. assert_equal([11], 11.digits(T1024P))
  660. assert_equal([T1024P - 1, 1], (T1024P + T1024P - 1).digits(T1024P))
  661. end
  662. def test_digits_for_negative_numbers
  663. assert_raise(Math::DomainError) { -11.digits(T1024P) }
  664. assert_raise(Math::DomainError) { (-T1024P).digits }
  665. assert_raise(Math::DomainError) { (-T1024P).digits(T1024P) }
  666. end
  667. def test_digits_for_invalid_base_numbers
  668. assert_raise(ArgumentError) { T1024P.to_bignum.digits(0) }
  669. assert_raise(ArgumentError) { T1024P.to_bignum.digits(-1) }
  670. assert_raise(ArgumentError) { T1024P.to_bignum.digits(0.to_bignum) }
  671. assert_raise(ArgumentError) { T1024P.to_bignum.digits(1.to_bignum) }
  672. assert_raise(ArgumentError) { T1024P.to_bignum.digits(-T1024P) }
  673. assert_raise(ArgumentError) { 10.digits(0.to_bignum) }
  674. assert_raise(ArgumentError) { 10.digits(1.to_bignum) }
  675. end
  676. def test_digits_for_non_integral_base_numbers
  677. assert_equal([11], 11.digits(T128P.to_r))
  678. assert_equal([11], 11.digits(T128P.to_f))
  679. t1024p_digits_in_t32 = [T32P]*32
  680. assert_equal(t1024p_digits_in_t32, T1024P.digits(T32.to_r))
  681. assert_equal(t1024p_digits_in_t32, T1024P.digits(T32.to_f))
  682. assert_raise(RangeError) { T128P.digits(10+1i) }
  683. end
  684. def test_digits_for_non_numeric_base_argument
  685. assert_raise(TypeError) { T1024P.digits("10") }
  686. assert_raise(TypeError) { T1024P.digits("a") }
  687. end
  688. def test_finite_p
  689. assert_predicate(T1024P, :finite?)
  690. assert_predicate(-T1024P, :finite?)
  691. end
  692. def test_infinite_p
  693. assert_nil(T1024P.infinite?)
  694. assert_nil((-T1024P).infinite?)
  695. end
  696. end
  697. end