From 7e5c6148030b525a78a528a22fd4ada458a3a49d Mon Sep 17 00:00:00 2001 From: Rick Hull Date: Mon, 10 Jun 2024 14:58:42 -0400 Subject: [PATCH] flesh out Player tests --- test/elo.rb | 65 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/test/elo.rb b/test/elo.rb index 5a2d299..46781a2 100644 --- a/test/elo.rb +++ b/test/elo.rb @@ -74,10 +74,67 @@ end describe Elo::Player do - it "must be initialized with an Elo object" do - expect { Elo::Player.new }.must_raise ArgumentError - expect { Elo::Player.new(1234) }.must_raise ArgumentError - expect(Elo::Player.new(Elo.new)).must_be_kind_of Elo::Player + it "has sensible defaults" do + expect(Elo::Player.new).must_be_kind_of Elo::Player + end + + it "may be initialized with an Elo object" do + expect(Elo::Player.new(elo: Elo.new)).must_be_kind_of Elo::Player + end + + it "is Comparable, based on Elo rating" do + a = Elo::Player.new + b = Elo::Player.new + + expect(a <=> b).must_equal 0 + a.rating = 2 * b.rating + expect(a <=> b).must_equal 1 + b.rating = a.rating + 1 + expect(a <=> b).must_equal(-1) + + a.rating = b.rating + expect(a == b).must_equal true + expect(a == a).must_equal true + expect(a != b).must_equal false + + a.rating = b.rating + 1 + expect(a > b).must_equal true + end + + it "can initialize a pool of players" do + pool = Elo::Player.init_pool(99) + expect(pool).must_be_kind_of Array + pool.each { |player| player.rating = rand(2000) } + sorted = pool.sort # sorts on rating because of <=> and Comparable + expect(sorted.first.rating).must_be :<, sorted.last.rating + end + + it "updates both self and opponent with a match outcome" do + a = Elo::Player.new + b = Elo::Player.new + + # 5 wins for _a_ + 5.times { a.update(b, 1) } + + expect(a > b).must_equal true + + a_rating, b_rating = a.rating, b.rating + + # a draw, _a_'s rating goes down, _b_'s goes up + a.update(b, 0.5) + expect(a.rating).must_be :<, a_rating + expect(b.rating).must_be :>, b_rating + end + + it "can perform skill-based match simulations" do + a = Elo::Player.new(skill: 0.95) + b = Elo::Player.new(skill: 0.3) + + expect(a == b).must_equal true # ratings are the same + + # 99 matches + 99.times { a.simulate(b) } + expect(a > b).must_equal true # a's rating should increase end end end