~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/ruby/test_rand.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
171
171
  def test_shuffle
172
172
    srand(0)
173
173
    assert_equal([1,4,2,5,3], [1,2,3,4,5].shuffle)
 
174
    assert_equal([1,4,2,5,3], [1,2,3,4,5].shuffle(random: Random.new(0)))
174
175
  end
175
176
 
176
177
  def test_big_seed
339
340
  end
340
341
 
341
342
  def test_random_bytes
342
 
    r = Random.new(0)
 
343
    assert_random_bytes(Random.new(0))
 
344
  end
 
345
 
 
346
  def assert_random_bytes(r)
343
347
    assert_equal("", r.bytes(0))
344
348
    assert_equal("\xAC".force_encoding("ASCII-8BIT"), r.bytes(1))
345
 
    assert_equal("/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT"),                                                                                                                              r.bytes(10))
 
349
    assert_equal("/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT"),
 
350
                 r.bytes(10))
346
351
  end
347
352
 
348
353
  def test_random_range
 
354
    srand(0)
349
355
    r = Random.new(0)
350
 
    %w(9 5 8).each {|w| assert_equal(w.to_i, r.rand(5..9)) }
351
 
    %w(-237 731 383).each {|w| assert_equal(w.to_i, r.rand(-1000..1000)) }
 
356
    %w(9 5 8).each {|w|
 
357
      assert_equal(w.to_i, rand(5..9))
 
358
      assert_equal(w.to_i, r.rand(5..9))
 
359
    }
 
360
    %w(-237 731 383).each {|w|
 
361
      assert_equal(w.to_i, rand(-1000..1000))
 
362
      assert_equal(w.to_i, r.rand(-1000..1000))
 
363
    }
352
364
    %w(1267650600228229401496703205382
353
365
       1267650600228229401496703205384
354
366
       1267650600228229401496703205383).each do |w|
 
367
      assert_equal(w.to_i, rand(2**100+5..2**100+9))
355
368
      assert_equal(w.to_i, r.rand(2**100+5..2**100+9))
356
369
    end
 
370
 
 
371
    v = rand(3.1..4)
 
372
    assert_instance_of(Float, v, '[ruby-core:24679]')
 
373
    assert_include(3.1..4, v)
 
374
 
357
375
    v = r.rand(3.1..4)
358
376
    assert_instance_of(Float, v, '[ruby-core:24679]')
359
 
    assert_includes(3.1..4, v)
 
377
    assert_include(3.1..4, v)
360
378
 
361
379
    now = Time.now
 
380
    assert_equal(now, rand(now..now))
362
381
    assert_equal(now, r.rand(now..now))
363
382
  end
364
383
 
378
397
    assert_in_delta(1.7151893663724195, r.rand(1.0...2.0), 0.0001, '[ruby-core:24655]')
379
398
    assert_in_delta(7.027633760716439, r.rand(1.0...11.0), 0.0001, '[ruby-core:24655]')
380
399
    assert_in_delta(3.0897663659937937, r.rand(2.0...4.0), 0.0001, '[ruby-core:24655]')
 
400
 
 
401
    assert_nothing_raised {r.rand(-Float::MAX..Float::MAX)}
381
402
  end
382
403
 
383
404
  def test_random_equal
416
437
    }
417
438
  end
418
439
 
 
440
  def test_default
 
441
    r1 = Random::DEFAULT.dup
 
442
    r2 = Random::DEFAULT.dup
 
443
    3.times do
 
444
      x0 = rand
 
445
      x1 = r1.rand
 
446
      x2 = r2.rand
 
447
      assert_equal(x0, x1)
 
448
      assert_equal(x0, x2)
 
449
    end
 
450
  end
 
451
 
419
452
  def test_marshal
420
453
    bug3656 = '[ruby-core:31622]'
421
454
    assert_raise(TypeError, bug3656) {