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

/projects/jruby-1.7.3/test/externals/ruby1.9/ruby/test_rand.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 458 lines | 412 code | 46 blank | 0 comment | 6 complexity | 8023da5bf709da1549724acf9a26b5a3 MD5 | raw file
  1. require 'test/unit'
  2. class TestRand < Test::Unit::TestCase
  3. def assert_random_int(ws, m, init = 0)
  4. srand(init)
  5. rnds = [Random.new(init)]
  6. rnds2 = [rnds[0].dup]
  7. rnds3 = [rnds[0].dup]
  8. ws.each_with_index do |w, i|
  9. w = w.to_i
  10. assert_equal(w, rand(m))
  11. rnds.each do |rnd|
  12. assert_equal(w, rnd.rand(m))
  13. end
  14. rnds2.each do |rnd|
  15. r=rnd.rand(i...(m+i))
  16. assert_equal(w+i, r)
  17. end
  18. rnds3.each do |rnd|
  19. r=rnd.rand(i..(m+i-1))
  20. assert_equal(w+i, r)
  21. end
  22. rnds << Marshal.load(Marshal.dump(rnds[-1]))
  23. rnds2 << Marshal.load(Marshal.dump(rnds2[-1]))
  24. end
  25. end
  26. def test_mt
  27. assert_random_int(%w(1067595299 955945823 477289528 4107218783 4228976476),
  28. 0x100000000, 0x00000456_00000345_00000234_00000123)
  29. end
  30. def test_0x3fffffff
  31. assert_random_int(%w(209652396 398764591 924231285 404868288 441365315),
  32. 0x3fffffff)
  33. end
  34. def test_0x40000000
  35. assert_random_int(%w(209652396 398764591 924231285 404868288 441365315),
  36. 0x40000000)
  37. end
  38. def test_0x40000001
  39. assert_random_int(%w(209652396 398764591 924231285 441365315 192771779),
  40. 0x40000001)
  41. end
  42. def test_0xffffffff
  43. assert_random_int(%w(2357136044 2546248239 3071714933 3626093760 2588848963),
  44. 0xffffffff)
  45. end
  46. def test_0x100000000
  47. assert_random_int(%w(2357136044 2546248239 3071714933 3626093760 2588848963),
  48. 0x100000000)
  49. end
  50. def test_0x100000001
  51. assert_random_int(%w(2546248239 1277901399 243580376 1171049868 2051556033),
  52. 0x100000001)
  53. end
  54. def test_rand_0x100000000
  55. assert_random_int(%w(4119812344 3870378946 80324654 4294967296 410016213),
  56. 0x100000001, 311702798)
  57. end
  58. def test_0x1000000000000
  59. assert_random_int(%w(11736396900911
  60. 183025067478208
  61. 197104029029115
  62. 130583529618791
  63. 180361239846611),
  64. 0x1000000000000)
  65. end
  66. def test_0x1000000000001
  67. assert_random_int(%w(187121911899765
  68. 197104029029115
  69. 180361239846611
  70. 236336749852452
  71. 208739549485656),
  72. 0x1000000000001)
  73. end
  74. def test_0x3fffffffffffffff
  75. assert_random_int(%w(900450186894289455
  76. 3969543146641149120
  77. 1895649597198586619
  78. 827948490035658087
  79. 3203365596207111891),
  80. 0x3fffffffffffffff)
  81. end
  82. def test_0x4000000000000000
  83. assert_random_int(%w(900450186894289455
  84. 3969543146641149120
  85. 1895649597198586619
  86. 827948490035658087
  87. 3203365596207111891),
  88. 0x4000000000000000)
  89. end
  90. def test_0x4000000000000001
  91. assert_random_int(%w(900450186894289455
  92. 3969543146641149120
  93. 1895649597198586619
  94. 827948490035658087
  95. 2279347887019741461),
  96. 0x4000000000000001)
  97. end
  98. def test_0x10000000000
  99. ws = %w(455570294424 1073054410371 790795084744 2445173525 1088503892627)
  100. assert_random_int(ws, 0x10000000000, 3)
  101. end
  102. def test_0x10000
  103. ws = %w(2732 43567 42613 52416 45891)
  104. assert_random_int(ws, 0x10000)
  105. end
  106. def test_types
  107. srand(0)
  108. rnd = Random.new(0)
  109. assert_equal(44, rand(100.0))
  110. assert_equal(44, rnd.rand(100))
  111. assert_equal(1245085576965981900420779258691, rand((2**100).to_f))
  112. assert_equal(1245085576965981900420779258691, rnd.rand(2**100))
  113. assert_equal(914679880601515615685077935113, rand(-(2**100).to_f))
  114. srand(0)
  115. rnd = Random.new(0)
  116. assert_equal(997707939797331598305742933184, rand(2**100))
  117. assert_equal(997707939797331598305742933184, rnd.rand(2**100))
  118. assert_in_delta(0.602763376071644, rand((2**100).coerce(0).first),
  119. 0.000000000000001)
  120. assert_raise(ArgumentError) {rnd.rand((2**100).coerce(0).first)}
  121. srand(0)
  122. rnd = Random.new(0)
  123. assert_in_delta(0.548813503927325, rand(nil),
  124. 0.000000000000001)
  125. assert_in_delta(0.548813503927325, rnd.rand(),
  126. 0.000000000000001)
  127. srand(0)
  128. rnd = Random.new(0)
  129. o = Object.new
  130. def o.to_int; 100; end
  131. assert_equal(44, rand(o))
  132. assert_equal(44, rnd.rand(o))
  133. assert_equal(47, rand(o))
  134. assert_equal(47, rnd.rand(o))
  135. assert_equal(64, rand(o))
  136. assert_equal(64, rnd.rand(o))
  137. end
  138. def test_srand
  139. srand
  140. assert_kind_of(Integer, rand(2))
  141. assert_kind_of(Integer, Random.new.rand(2))
  142. srand(2**100)
  143. rnd = Random.new(2**100)
  144. %w(3258412053).each {|w|
  145. assert_equal(w.to_i, rand(0x100000000))
  146. assert_equal(w.to_i, rnd.rand(0x100000000))
  147. }
  148. end
  149. def test_shuffle
  150. srand(0)
  151. assert_equal([1,4,2,5,3], [1,2,3,4,5].shuffle)
  152. assert_equal([1,4,2,5,3], [1,2,3,4,5].shuffle(random: Random.new(0)))
  153. end
  154. def test_big_seed
  155. assert_random_int(%w(1143843490), 0x100000000, 2**1000000-1)
  156. end
  157. def test_random_gc
  158. r = Random.new(0)
  159. %w(2357136044 2546248239 3071714933).each do |w|
  160. assert_equal(w.to_i, r.rand(0x100000000))
  161. end
  162. GC.start
  163. %w(3626093760 2588848963 3684848379).each do |w|
  164. assert_equal(w.to_i, r.rand(0x100000000))
  165. end
  166. end
  167. def test_random_type_error
  168. assert_raise(TypeError) { Random.new(Object.new) }
  169. assert_raise(TypeError) { Random.new(0).rand(Object.new) }
  170. end
  171. def test_random_argument_error
  172. r = Random.new(0)
  173. assert_raise(ArgumentError) { r.rand(0, 0) }
  174. assert_raise(ArgumentError, '[ruby-core:24677]') { r.rand(-1) }
  175. assert_raise(ArgumentError, '[ruby-core:24677]') { r.rand(-1.0) }
  176. assert_raise(ArgumentError, '[ruby-core:24677]') { r.rand(0) }
  177. assert_equal(0, r.rand(1), '[ruby-dev:39166]')
  178. assert_equal(0, r.rand(0...1), '[ruby-dev:39166]')
  179. assert_equal(0, r.rand(0..0), '[ruby-dev:39166]')
  180. assert_equal(0.0, r.rand(0.0..0.0), '[ruby-dev:39166]')
  181. assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0...0) }
  182. assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0..-1) }
  183. assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0.0...0.0) }
  184. assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0.0...-0.1) }
  185. assert_raise(ArgumentError, bug3027 = '[ruby-core:29075]') { r.rand(nil) }
  186. end
  187. def test_random_seed
  188. assert_equal(0, Random.new(0).seed)
  189. assert_equal(0x100000000, Random.new(0x100000000).seed)
  190. assert_equal(2**100, Random.new(2**100).seed)
  191. end
  192. def test_random_dup
  193. r1 = Random.new(0)
  194. r2 = r1.dup
  195. %w(2357136044 2546248239 3071714933).each do |w|
  196. assert_equal(w.to_i, r1.rand(0x100000000))
  197. end
  198. %w(2357136044 2546248239 3071714933).each do |w|
  199. assert_equal(w.to_i, r2.rand(0x100000000))
  200. end
  201. r2 = r1.dup
  202. %w(3626093760 2588848963 3684848379).each do |w|
  203. assert_equal(w.to_i, r1.rand(0x100000000))
  204. end
  205. %w(3626093760 2588848963 3684848379).each do |w|
  206. assert_equal(w.to_i, r2.rand(0x100000000))
  207. end
  208. end
  209. def test_random_state
  210. state = <<END
  211. 3877134065023083674777481835852171977222677629000095857864323111193832400974413
  212. 4782302161934463784850675209112299537259006497924090422596764895633625964527441
  213. 6943943249411681406395713106007661119327771293929504639878577616749110507385924
  214. 0173026285378896836022134086386136835407107422834685854738117043791709411958489
  215. 3504364936306163473541948635570644161010981140452515307286926529085424765299100
  216. 1255453260115310687580777474046203049197643434654645011966794531914127596390825
  217. 0832232869378617194193100828000236737535657699356156021286278281306055217995213
  218. 8911536025132779573429499813926910299964681785069915877910855089314686097947757
  219. 2621451199734871158015842198110309034467412292693435515184023707918034746119728
  220. 8223459645048255809852819129671833854560563104716892857257229121211527031509280
  221. 2390605053896565646658122125171846129817536096211475312518457776328637574563312
  222. 8113489216547503743508184872149896518488714209752552442327273883060730945969461
  223. 6568672445225657265545983966820639165285082194907591432296265618266901318398982
  224. 0560425129536975583916120558652408261759226803976460322062347123360444839683204
  225. 9868507788028894111577023917218846128348302845774997500569465902983227180328307
  226. 3735301552935104196244116381766459468172162284042207680945316590536094294865648
  227. 5953156978630954893391701383648157037914019502853776972615500142898763385846315
  228. 8457790690531675205213829055442306187692107777193071680668153335688203945049935
  229. 3404449910419303330872435985327845889440458370416464132629866593538629877042969
  230. 7589948685901343135964343582727302330074331803900821801076139161904900333497836
  231. 6627028345784450211920229170280462474456504317367706849373957309797251052447898
  232. 8436235456995644876515032202483449807136139063937892187299239634252391529822163
  233. 9187055268750679730919937006195967184206072757082920250075756273182004964087790
  234. 3812024063897424219316687828337007888030994779068081666133751070479581394604974
  235. 6022215489604777611774245181115126041390161592199230774968475944753915533936834
  236. 4740049163514318351045644344358598159463605453475585370226041981040238023241538
  237. 4958436364776113598408428801867643946791659645708540669432995503575075657406359
  238. 8086928867900590554805639837071298576728564946552163206007997000988745940681607
  239. 4542883814997403673656291618517107133421335645430345871041730410669209035640945
  240. 5024601618318371192091626092482640364669969307766919645222516407626038616667754
  241. 5781148898846306894862390724358039251444333889446128074209417936830253204064223
  242. 3424784857908022314095011879203259864909560830176189727132432100010493659154644
  243. 8407326292884826469503093409465946018496358514999175268200846200025235441426140
  244. 7783386235191526371372655894290440356560751680752191224383460972099834655086068
  245. 9989413443881686756951804138910911737670495391762470293978321414964443502180391
  246. 4665982575919524372985773336921990352313629822677022891307943536442258282401255
  247. 5387646898976193134193506239982621725093291970351083631367582570375381334759004
  248. 1784150668048523676387894646666460369896619585113435743180899362844070393586212
  249. 5023920017185866399742380706352739465848708746963693663004068892056705603018655
  250. 8686663087894205699555906146534549176352859823832196938386172810274748624517052
  251. 8356758650653040545267425513047130342286119889879774951060662713807133125543465
  252. 5104086026298674827575216701372513525846650644773241437066782037334367982012148
  253. 7987782004646896468089089826267467005660035604553432197455616078731159778086155
  254. 9443250946037119223468305483694093795324036812927501783593256716590840500905291
  255. 2096608538205270323065573109227029887731553399324547696295234105140157179430410
  256. 4003109602564833086703863221058381556776789018351726488797845637981974580864082
  257. 1630093543020854542240690897858757985640869209737744458407777584279553258261599
  258. 0246922348101147034463235613998979344685018577901996218099622190722307356620796
  259. 5137485271371502385527388080824050288371607602101805675021790116223360483508538
  260. 8832149997794718410946818866375912486788005950091851067237358294899771385995876
  261. 7088239104394332452501033090159333224995108984871426750597513314521294001864578
  262. 2353528356752869732412552685554334966798888534847483030947310518891788722418172
  263. 6008607577773612004956373863580996793809969715725508939568919714424871639667201
  264. 7922255031431159347210833846575355772055570279673262115911154370983086189948124
  265. 4653677615895887099814174914248255026619941911735341818489822197472499295786997
  266. 7728418516719104857455960900092226749725407204388193002835497055305427730656889
  267. 1508308778869166073740855838213112709306743479676740893150000714099064468263284
  268. 1873435518542972182497755500300784177067568586395485329021157235696300013490087
  269. 2866571034916258390528533374944905429089028336079264760836949419754851422499614
  270. 5732326011260304142074554782259843903215064144396140106592193961703288125005023
  271. 5334375212799817540775536847622032852415253966587517800661605905489339306359573
  272. 2234947905196298436841723673626428243649931398749552780311877734063703985375067
  273. 1239508613417041942487245370152912391885566432830659640677893488723724763120121
  274. 4111855277511356759926232894062814360449757490961653026194107761340614059045172
  275. 1123363102660719217740126157997033682099769790976313166682432732518101889210276
  276. 9574144065390305904944821051736021310524344626348851573631697771556587859836330
  277. 6997324121866564283654784470215100159122764509197570402997911258816526554863326
  278. 9877535269005418736225944874608987238997316999444215865249840762640949599725696
  279. 0773083894168959823152054508672272612355108904098579447774398451678239199426513
  280. 3439507737424049578587487505080347686371029156845461151278198605267053408259090
  281. 3158676794894709281917034995611352710898103415304769654883981727681820369090169
  282. 9295163908214854813365413456264812190842699054830709079275249714169405719140093
  283. 1347572458245530016346604698682269779841803667099480215265926316505737171177810
  284. 9969036572310084022695109125200937135540995157279354438704321290061646592229860
  285. 0156566013602344870223183295508278359111174872740360473845615437106413256386849
  286. 2286259982118315248148847764929974917157683083659364623458927512616369119194574
  287. 2254080
  288. END
  289. state = state.split.join.to_i
  290. r = Random.new(0)
  291. srand(0)
  292. assert_equal(state, r.instance_eval { state })
  293. assert_equal(state, Random.instance_eval { state })
  294. r.rand(0x100)
  295. assert_equal(state, r.instance_eval { state })
  296. end
  297. def test_random_left
  298. r = Random.new(0)
  299. assert_equal(1, r.instance_eval { left })
  300. r.rand(0x100)
  301. assert_equal(624, r.instance_eval { left })
  302. r.rand(0x100)
  303. assert_equal(623, r.instance_eval { left })
  304. srand(0)
  305. assert_equal(1, Random.instance_eval { left })
  306. rand(0x100)
  307. assert_equal(624, Random.instance_eval { left })
  308. rand(0x100)
  309. assert_equal(623, Random.instance_eval { left })
  310. end
  311. def test_random_bytes
  312. assert_random_bytes(Random.new(0))
  313. end
  314. def assert_random_bytes(r)
  315. assert_equal("", r.bytes(0))
  316. assert_equal("\xAC".force_encoding("ASCII-8BIT"), r.bytes(1))
  317. assert_equal("/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT"),
  318. r.bytes(10))
  319. end
  320. def test_random_range
  321. srand(0)
  322. r = Random.new(0)
  323. %w(9 5 8).each {|w|
  324. assert_equal(w.to_i, rand(5..9))
  325. assert_equal(w.to_i, r.rand(5..9))
  326. }
  327. %w(-237 731 383).each {|w|
  328. assert_equal(w.to_i, rand(-1000..1000))
  329. assert_equal(w.to_i, r.rand(-1000..1000))
  330. }
  331. %w(1267650600228229401496703205382
  332. 1267650600228229401496703205384
  333. 1267650600228229401496703205383).each do |w|
  334. assert_equal(w.to_i, rand(2**100+5..2**100+9))
  335. assert_equal(w.to_i, r.rand(2**100+5..2**100+9))
  336. end
  337. v = rand(3.1..4)
  338. assert_instance_of(Float, v, '[ruby-core:24679]')
  339. assert_include(3.1..4, v)
  340. v = r.rand(3.1..4)
  341. assert_instance_of(Float, v, '[ruby-core:24679]')
  342. assert_include(3.1..4, v)
  343. now = Time.now
  344. assert_equal(now, rand(now..now))
  345. assert_equal(now, r.rand(now..now))
  346. end
  347. def test_random_float
  348. r = Random.new(0)
  349. assert_in_delta(0.5488135039273248, r.rand, 0.0001)
  350. assert_in_delta(0.7151893663724195, r.rand, 0.0001)
  351. assert_in_delta(0.6027633760716439, r.rand, 0.0001)
  352. assert_in_delta(1.0897663659937937, r.rand(2.0), 0.0001)
  353. assert_in_delta(5.3704626067153264e+29, r.rand((2**100).to_f), 10**25)
  354. assert_raise(Errno::EDOM, Errno::ERANGE) { r.rand(1.0 / 0.0) }
  355. assert_raise(Errno::EDOM, Errno::ERANGE) { r.rand(0.0 / 0.0) }
  356. r = Random.new(0)
  357. assert_in_delta(1.5488135039273248, r.rand(1.0...2.0), 0.0001, '[ruby-core:24655]')
  358. assert_in_delta(1.7151893663724195, r.rand(1.0...2.0), 0.0001, '[ruby-core:24655]')
  359. assert_in_delta(7.027633760716439, r.rand(1.0...11.0), 0.0001, '[ruby-core:24655]')
  360. assert_in_delta(3.0897663659937937, r.rand(2.0...4.0), 0.0001, '[ruby-core:24655]')
  361. assert_nothing_raised {r.rand(-Float::MAX..Float::MAX)}
  362. end
  363. def test_random_equal
  364. r = Random.new(0)
  365. assert(r == r)
  366. assert(r == r.dup)
  367. r1 = r.dup
  368. r2 = r.dup
  369. r1.rand(0x100)
  370. assert(r1 != r2)
  371. r2.rand(0x100)
  372. assert(r1 == r2)
  373. end
  374. def test_fork_shuffle
  375. pid = fork do
  376. (1..10).to_a.shuffle
  377. raise 'default seed is not set' if srand == 0
  378. end
  379. p2, st = Process.waitpid2(pid)
  380. assert(st.success?, "#{st.inspect}")
  381. rescue NotImplementedError, ArgumentError
  382. end
  383. def test_seed
  384. bug3104 = '[ruby-core:29292]'
  385. rand_1 = Random.new(-1).rand
  386. assert_not_equal(rand_1, Random.new((1 << 31) -1).rand, "#{bug3104} (2)")
  387. assert_not_equal(rand_1, Random.new((1 << 63) -1).rand, "#{bug3104} (2)")
  388. [-1, -2**10, -2**40].each {|n|
  389. b = (2**64).coerce(n)[0]
  390. r1 = Random.new(n).rand
  391. r2 = Random.new(b).rand
  392. assert_equal(r1, r2)
  393. }
  394. end
  395. def test_default
  396. r1 = Random::DEFAULT.dup
  397. r2 = Random::DEFAULT.dup
  398. 3.times do
  399. x0 = rand
  400. x1 = r1.rand
  401. x2 = r2.rand
  402. assert_equal(x0, x1)
  403. assert_equal(x0, x2)
  404. end
  405. end
  406. def test_marshal
  407. bug3656 = '[ruby-core:31622]'
  408. assert_raise(TypeError, bug3656) {
  409. Random.new.marshal_load(0)
  410. }
  411. end
  412. end