$LOAD_PATH.unshift(File.dirname(__FILE__)) require 'test_helper' include RandomLab class TestRandoms < Test::Unit::TestCase def test_00_banner print "\nRandomLab" end # The prng_sequence method makes a sequence of numbers using a linear congruential # method with the specified a, c, and m values def test_01_prng_sequence sched8 = prng_sequence(1, 8, 12) assert_equal 12, sched8.length assert_equal 3, sched8.uniq.length sched7 = prng_sequence(1, 7, 12) assert_equal 12, sched7.length assert_equal 12, sched7.uniq.length seq1 = prng_sequence(3, 337, 1000) assert_equal [0, 337, 348, 381], seq1[0..3] assert_equal 100, seq1.uniq.length seq2 = prng_sequence(81, 337, 1000) assert_equal [0, 337, 634, 691], seq2[0..3] assert_equal 1000, seq2.uniq.length end # A PRNG object generates the same sequence of values as the prng_sequence method, # but makes them on demand. Test the sequence, test resetting the seed, test again. # Also tests the random(n,m) method, which maps a sequence value to n..m def test_02_PRNG p = PRNG.new(81, 337, 1000) assert_equal 0, p.state assert_equal 337, p.advance assert_equal 634, p.advance p.seed(0) assert_equal 0, p.state assert_equal 337, p.advance assert_equal 634, p.advance p.seed(0) assert_equal 2, p.random(1,6) # (337 % 6) + 1 assert_equal 5, p.random(1,6) # (634 % 6) + 1 end # For the Card tests, a call to the Card constructor with an integer argument # makes a specified card, starting with 0 = A ♠ and ending with 51 = 2 ♣. def test_03_cards c1 = Card.new(0) assert_equal :ace, c1.rank assert_equal :spades, c1.suit c2 = Card.new(51) assert_equal :two, c2.rank assert_equal :clubs, c2.suit end # Make a full deck, make sure every card is present. Then shuffle the deck, # check again. Sort the shuffled deck, make sure all cards are there and in # order. def test_04_deck d = new_deck check_all_cards(d) permute(d) check_all_cards(d) d.sort! assert_equal new_deck, d end # Make one of each kind of poker hand. def test_05_poker_ranks assert_equal poker_rank(deal([0,1,2,3,4])), :straight_flush assert_equal poker_rank(deal([0,13,26,39,1])), :four_of_a_kind assert_equal poker_rank(deal([0,13,26,1,14])), :full_house assert_equal poker_rank(deal([0,2,4,6,8])), :flush assert_equal poker_rank(deal([0,14,2,3,4])), :straight assert_equal poker_rank(deal([0,13,26,1,15])), :three_of_a_kind assert_equal poker_rank(deal([0,13,1,14,15])), :two_pair assert_equal poker_rank(deal([0,13,1,2,3])), :pair assert_equal poker_rank(deal([0,14,29,44,45])), :high_card end =begin rdoc The +bins+ method makes a hash using keys based on suits or ranks and initialized with a count of 0. +check_all_cards+ makes sure there are 13 of each suit and 4 of each rank. +deal(a)+ maps all ints in +a+ into the corresponding Card (used to create a known hand for testing poker ranks) =end def bins(a) h = Hash.new a.each do |x| h[x] = 0 end return h end def check_all_cards(a) rb = bins(Card::Ranks) sb = bins(Card::Suits) a.each { |x| rb[x.rank] += 1; sb[x.suit] += 1 } rb.each { |x,n| assert_equal n, 4 } sb.each { |x,n| assert_equal n, 13 } end def deal(a) return a.map { |x| Card.new(x) } end end