# frozen_string_literal: false
require 'test/unit'

class TestRand < Test::Unit::TestCase
  def assert_random_int(ws, m, init = 0)
    srand(init)
    rnds = [Random.new(init)]
    rnds2 = [rnds[0].dup]
    rnds3 = [rnds[0].dup]
    ws.each_with_index do |w, i|
      w = w.to_i
      assert_equal(w, rand(m))
      rnds.each do |rnd|
        assert_equal(w, rnd.rand(m))
      end
      rnds2.each do |rnd|
        r=rnd.rand(i...(m+i))
        assert_equal(w+i, r)
      end
      rnds3.each do |rnd|
        r=rnd.rand(i..(m+i-1))
        assert_equal(w+i, r)
      end
      rnds << Marshal.load(Marshal.dump(rnds[-1]))
      rnds2 << Marshal.load(Marshal.dump(rnds2[-1]))
    end
  end

  def test_mt
    assert_random_int(%w(1067595299  955945823  477289528 4107218783 4228976476),
                      0x100000000, 0x00000456_00000345_00000234_00000123)
  end

  def test_0x3fffffff
    assert_random_int(%w(209652396 398764591 924231285 404868288 441365315),
                      0x3fffffff)
  end

  def test_0x40000000
    assert_random_int(%w(209652396 398764591 924231285 404868288 441365315),
                      0x40000000)
  end

  def test_0x40000001
    assert_random_int(%w(209652396 398764591 924231285 441365315 192771779),
                      0x40000001)
  end

  def test_0xffffffff
    assert_random_int(%w(2357136044 2546248239 3071714933 3626093760 2588848963),
                      0xffffffff)
  end

  def test_0x100000000
    assert_random_int(%w(2357136044 2546248239 3071714933 3626093760 2588848963),
                      0x100000000)
  end

  def test_0x100000001
    assert_random_int(%w(2546248239 1277901399 243580376 1171049868 2051556033),
                      0x100000001)
  end

  def test_rand_0x100000000
    assert_random_int(%w(4119812344 3870378946 80324654 4294967296 410016213),
                      0x100000001, 311702798)
  end

  def test_0x1000000000000
    assert_random_int(%w(11736396900911
                         183025067478208
                         197104029029115
                         130583529618791
                         180361239846611),
                      0x1000000000000)
  end

  def test_0x1000000000001
    assert_random_int(%w(187121911899765
                         197104029029115
                         180361239846611
                         236336749852452
                         208739549485656),
                      0x1000000000001)
  end

  def test_0x3fffffffffffffff
    assert_random_int(%w(900450186894289455
                         3969543146641149120
                         1895649597198586619
                         827948490035658087
                         3203365596207111891),
                      0x3fffffffffffffff)
  end

  def test_0x4000000000000000
    assert_random_int(%w(900450186894289455
                         3969543146641149120
                         1895649597198586619
                         827948490035658087
                         3203365596207111891),
                      0x4000000000000000)
  end

  def test_0x4000000000000001
    assert_random_int(%w(900450186894289455
                         3969543146641149120
                         1895649597198586619
                         827948490035658087
                         2279347887019741461),
                      0x4000000000000001)
  end

  def test_0x10000000000
    ws = %w(455570294424 1073054410371 790795084744 2445173525 1088503892627)
    assert_random_int(ws, 0x10000000000, 3)
  end

  def test_0x10000
    ws = %w(2732 43567 42613 52416 45891)
    assert_random_int(ws, 0x10000)
  end

  def test_types
    srand(0)
    rnd = Random.new(0)
    assert_equal(44, rand(100.0))
    assert_equal(44, rnd.rand(100))
    assert_equal(1245085576965981900420779258691, rand((2**100).to_f))
    assert_equal(1245085576965981900420779258691, rnd.rand(2**100))
    assert_equal(914679880601515615685077935113, rand(-(2**100).to_f))

    srand(0)
    rnd = Random.new(0)
    assert_equal(997707939797331598305742933184, rand(2**100))
    assert_equal(997707939797331598305742933184, rnd.rand(2**100))
    assert_in_delta(0.602763376071644, rand((2**100).coerce(0).first),
                    0.000000000000001)
    assert_raise(ArgumentError) {rnd.rand((2**100).coerce(0).first)}

    srand(0)
    rnd = Random.new(0)
    assert_in_delta(0.548813503927325, rand(nil),
                    0.000000000000001)
    assert_in_delta(0.548813503927325, rnd.rand(),
                    0.000000000000001)
    srand(0)
    rnd = Random.new(0)
    o = Object.new
    def o.to_int; 100; end
    assert_equal(44, rand(o))
    assert_equal(44, rnd.rand(o))
    assert_equal(47, rand(o))
    assert_equal(47, rnd.rand(o))
    assert_equal(64, rand(o))
    assert_equal(64, rnd.rand(o))
  end

  def test_srand
    srand
    assert_kind_of(Integer, rand(2))
    assert_kind_of(Integer, Random.new.rand(2))

    srand(2**100)
    rnd = Random.new(2**100)
    %w(3258412053).each {|w|
      assert_equal(w.to_i, rand(0x100000000))
      assert_equal(w.to_i, rnd.rand(0x100000000))
    }
  end

  def test_shuffle
    srand(0)
    result = [*1..5].shuffle
    assert_equal([*1..5], result.sort)
    assert_equal(result, [*1..5].shuffle(random: Random.new(0)))
  end

  def test_big_seed
    assert_random_int(%w(2757555016), 0x100000000, 2**1000000-1)
  end

  def test_random_gc
    r = Random.new(0)
    %w(2357136044 2546248239 3071714933).each do |w|
      assert_equal(w.to_i, r.rand(0x100000000))
    end
    GC.start
    %w(3626093760 2588848963 3684848379).each do |w|
      assert_equal(w.to_i, r.rand(0x100000000))
    end
  end

  def test_random_type_error
    assert_raise(TypeError) { Random.new(Object.new) }
    assert_raise(TypeError) { Random.new(0).rand(Object.new) }
  end

  def test_random_argument_error
    r = Random.new(0)
    assert_raise(ArgumentError) { r.rand(0, 0) }
    assert_raise(ArgumentError, '[ruby-core:24677]') { r.rand(-1) }
    assert_raise(ArgumentError, '[ruby-core:24677]') { r.rand(-1.0) }
    assert_raise(ArgumentError, '[ruby-core:24677]') { r.rand(0) }
    assert_equal(0, r.rand(1), '[ruby-dev:39166]')
    assert_equal(0, r.rand(0...1), '[ruby-dev:39166]')
    assert_equal(0, r.rand(0..0), '[ruby-dev:39166]')
    assert_equal(0.0, r.rand(0.0..0.0), '[ruby-dev:39166]')
    assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0...0) }
    assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0..-1) }
    assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0.0...0.0) }
    assert_raise(ArgumentError, '[ruby-dev:39166]') { r.rand(0.0...-0.1) }
    bug3027 = '[ruby-core:29075]'
    assert_raise(ArgumentError, bug3027) { r.rand(nil) }
  end

  def test_random_seed
    assert_equal(0, Random.new(0).seed)
    assert_equal(0x100000000, Random.new(0x100000000).seed)
    assert_equal(2**100, Random.new(2**100).seed)
  end

  def test_random_dup
    r1 = Random.new(0)
    r2 = r1.dup
    %w(2357136044 2546248239 3071714933).each do |w|
      assert_equal(w.to_i, r1.rand(0x100000000))
    end
    %w(2357136044 2546248239 3071714933).each do |w|
      assert_equal(w.to_i, r2.rand(0x100000000))
    end
    r2 = r1.dup
    %w(3626093760 2588848963 3684848379).each do |w|
      assert_equal(w.to_i, r1.rand(0x100000000))
    end
    %w(3626093760 2588848963 3684848379).each do |w|
      assert_equal(w.to_i, r2.rand(0x100000000))
    end
  end

  def test_random_state
    state = <<END
[encoded data, 6080 characters]
2254080
END
    state = state.split.join.to_i
    r = Random.new(0)
    srand(0)
    assert_equal(state, r.instance_eval { state })
    assert_equal(state, Random.instance_eval { state })
    r.rand(0x100)
    assert_equal(state, r.instance_eval { state })
  end

  def test_random_left
    r = Random.new(0)
    assert_equal(1, r.instance_eval { left })
    r.rand(0x100)
    assert_equal(624, r.instance_eval { left })
    r.rand(0x100)
    assert_equal(623, r.instance_eval { left })
    srand(0)
    assert_equal(1, Random.instance_eval { left })
    rand(0x100)
    assert_equal(624, Random.instance_eval { left })
    rand(0x100)
    assert_equal(623, Random.instance_eval { left })
  end

  def test_random_bytes
    assert_random_bytes(Random.new(0))
  end

  def assert_random_bytes(r)
    srand(0)
    assert_equal("", r.bytes(0))
    assert_equal("", Random.bytes(0))
    x = "\xAC".force_encoding("ASCII-8BIT")
    assert_equal(x, r.bytes(1))
    assert_equal(x, Random.bytes(1))
    x = "/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT")
    assert_equal(x, r.bytes(10))
    assert_equal(x, Random.bytes(10))
  end

  def test_random_range
    srand(0)
    r = Random.new(0)
    %w(9 5 8).each {|w|
      assert_equal(w.to_i, rand(5..9))
      assert_equal(w.to_i, r.rand(5..9))
    }
    %w(-237 731 383).each {|w|
      assert_equal(w.to_i, rand(-1000..1000))
      assert_equal(w.to_i, r.rand(-1000..1000))
    }
    %w(1267650600228229401496703205382
       1267650600228229401496703205384
       1267650600228229401496703205383).each do |w|
      assert_equal(w.to_i, rand(2**100+5..2**100+9))
      assert_equal(w.to_i, r.rand(2**100+5..2**100+9))
    end

    v = rand(3.1..4)
    assert_instance_of(Float, v, '[ruby-core:24679]')
    assert_include(3.1..4, v)

    v = r.rand(3.1..4)
    assert_instance_of(Float, v, '[ruby-core:24679]')
    assert_include(3.1..4, v)

    now = Time.now
    assert_equal(now, rand(now..now))
    assert_equal(now, r.rand(now..now))
  end

  def test_random_float
    r = Random.new(0)
    assert_in_delta(0.5488135039273248, r.rand, 0.0001)
    assert_in_delta(0.7151893663724195, r.rand, 0.0001)
    assert_in_delta(0.6027633760716439, r.rand, 0.0001)
    assert_in_delta(1.0897663659937937, r.rand(2.0), 0.0001)
    assert_in_delta(5.3704626067153264e+29, r.rand((2**100).to_f), 10**25)

    assert_raise(Errno::EDOM, Errno::ERANGE) { r.rand(1.0 / 0.0) }
    assert_raise(Errno::EDOM, Errno::ERANGE) { r.rand(0.0 / 0.0) }
    assert_raise(Errno::EDOM) {r.rand(1..)}

    r = Random.new(0)
    assert_in_delta(1.5488135039273248, r.rand(1.0...2.0), 0.0001, '[ruby-core:24655]')
    assert_in_delta(1.7151893663724195, r.rand(1.0...2.0), 0.0001, '[ruby-core:24655]')
    assert_in_delta(7.027633760716439, r.rand(1.0...11.0), 0.0001, '[ruby-core:24655]')
    assert_in_delta(3.0897663659937937, r.rand(2.0...4.0), 0.0001, '[ruby-core:24655]')

    assert_nothing_raised {r.rand(-Float::MAX..Float::MAX)}
  end

  def test_random_equal
    r = Random.new(0)
    assert_equal(r, r)
    assert_equal(r, r.dup)
    r1 = r.dup
    r2 = r.dup
    r1.rand(0x100)
    assert_not_equal(r1, r2)
    r2.rand(0x100)
    assert_equal(r1, r2)
  end

  def test_fork_shuffle
    pid = fork do
      (1..10).to_a.shuffle
      raise 'default seed is not set' if srand == 0
    end
    _, st = Process.waitpid2(pid)
    assert_predicate(st, :success?, "#{st.inspect}")
  rescue NotImplementedError, ArgumentError
  end

  def assert_fork_status(n, mesg, &block)
    IO.pipe do |r, w|
      (1..n).map do
        st = desc = nil
        IO.pipe do |re, we|
          p1 = fork {
            re.close
            STDERR.reopen(we)
            w.puts(block.call.to_s)
          }
          we.close
          err = Thread.start {re.read}
          _, st = Process.waitpid2(p1)
          desc = FailDesc[st, mesg, err.value]
        end
        assert(!st.signaled?, desc)
        assert(st.success?, mesg)
        r.gets.strip
      end
    end
  end

  def test_rand_reseed_on_fork
    GC.start
    bug5661 = '[ruby-core:41209]'

    assert_fork_status(1, bug5661) {Random.rand(4)}
    r1, r2 = *assert_fork_status(2, bug5661) {Random.rand}
    assert_not_equal(r1, r2, bug5661)

    assert_fork_status(1, bug5661) {rand(4)}
    r1, r2 = *assert_fork_status(2, bug5661) {rand}
    assert_not_equal(r1, r2, bug5661)

    stable = Random.new
    assert_fork_status(1, bug5661) {stable.rand(4)}
    r1, r2 = *assert_fork_status(2, bug5661) {stable.rand}
    assert_equal(r1, r2, bug5661)

    assert_fork_status(1, '[ruby-core:82100] [Bug #13753]') do
      Random::DEFAULT.rand(4)
    end
  rescue NotImplementedError
  end

  def test_seed
    bug3104 = '[ruby-core:29292]'
    rand_1 = Random.new(-1).rand
    assert_not_equal(rand_1, Random.new((1 << 31) -1).rand, "#{bug3104} (2)")
    assert_not_equal(rand_1, Random.new((1 << 63) -1).rand, "#{bug3104} (2)")

    [-1, -2**10, -2**40].each {|n|
      b = (2**64).coerce(n)[0]
      r1 = Random.new(n).rand
      r2 = Random.new(b).rand
      assert_equal(r1, r2)
    }
  end

  def test_default
    r1 = Random::DEFAULT.dup
    r2 = Random::DEFAULT.dup
    3.times do
      x0 = rand
      x1 = r1.rand
      x2 = r2.rand
      assert_equal(x0, x1)
      assert_equal(x0, x2)
    end
  end

  def test_marshal
    bug3656 = '[ruby-core:31622]'
    assert_raise(TypeError, bug3656) {
      Random.new.__send__(:marshal_load, 0)
    }
  end

  def test_initialize_frozen
    r = Random.new(0)
    r.freeze
    assert_raise(FrozenError, '[Bug #6540]') do
      r.__send__(:initialize, r)
    end
  end

  def test_marshal_load_frozen
    r = Random.new(0)
    d = r.__send__(:marshal_dump)
    r.freeze
    assert_raise(FrozenError, '[Bug #6540]') do
      r.__send__(:marshal_load, d)
    end
  end

  def test_random_ulong_limited
    def (gen = Object.new).rand(*) 1 end
    assert_equal([2], (1..100).map {[1,2,3].sample(random: gen)}.uniq)

    def (gen = Object.new).rand(*) 100 end
    assert_raise_with_message(RangeError, /big 100\z/) {[1,2,3].sample(random: gen)}

    bug7903 = '[ruby-dev:47061] [Bug #7903]'
    def (gen = Object.new).rand(*) -1 end
    assert_raise_with_message(RangeError, /small -1\z/, bug7903) {[1,2,3].sample(random: gen)}

    bug7935 = '[ruby-core:52779] [Bug #7935]'
    class << (gen = Object.new)
      def rand(limit) @limit = limit; 0 end
      attr_reader :limit
    end
    [1, 2].sample(1, random: gen)
    assert_equal(2, gen.limit, bug7935)
  end

  def test_random_ulong_limited_no_rand
    c = Class.new do
      undef rand
      def bytes(n)
        "\0"*n
      end
    end
    gen = c.new.extend(Random::Formatter)
    assert_equal(1, [1, 2].sample(random: gen))
  end

  def test_default_seed
    assert_separately([], <<-End)
      seed = Random::DEFAULT::seed
      rand1 = Random::DEFAULT::rand
      rand2 = Random.new(seed).rand
      assert_equal(rand1, rand2)

      srand seed
      rand3 = rand
      assert_equal(rand1, rand3)
    End
  end

  def test_urandom
    [0, 1, 100].each do |size|
      v = Random.urandom(size)
      assert_kind_of(String, v)
      assert_equal(size, v.bytesize)
    end
  end

  def test_new_seed
    size = 0
    n = 8
    n.times do
      v = Random.new_seed
      assert_kind_of(Integer, v)
      size += v.size
    end
    # probability of failure <= 1/256**8
    assert_operator(size.fdiv(n), :>, 15)
  end
end
