github.com/XiupengMa/poker_evaluator@v0.0.0-20201019041058-8d3c44dfc709/evaluator_test.go (about) 1 package evaluator 2 3 import ( 4 "testing" 5 ) 6 7 func TestEvaluateHand(t *testing.T) { 8 e, err := NewEvaluator() 9 if err != nil { 10 t.Error(err) 11 return 12 } 13 14 tests := []struct { 15 hand []string 16 expectHandType string 17 }{ 18 { 19 hand: []string{"SA", "S2", "S3", "S4", "S5"}, 20 expectHandType: "STRAIGHT FLUSH", 21 }, { 22 hand: []string{"HA", "S2", "S3", "S4", "S5"}, 23 expectHandType: "STRAIGHT", 24 }, { 25 hand: []string{"S7", "S2", "S3", "S4", "S5"}, 26 expectHandType: "FLUSH", 27 }, { 28 hand: []string{"H2", "S2", "S3", "S4", "S5"}, 29 expectHandType: "PAIR", 30 }, { 31 hand: []string{"H2", "S2", "C2", "S4", "S5"}, 32 expectHandType: "THREE OF A KIND", 33 }, { 34 hand: []string{"DK", "S2", "S3", "S4", "S5"}, 35 expectHandType: "HIGH CARD", 36 }, 37 } 38 39 for _, test := range tests { 40 _, handType := e.EvalHand(test.hand) 41 if handType != test.expectHandType { 42 t.Errorf("Expected %s, got %s", test.expectHandType, handType) 43 } 44 } 45 } 46 47 func BenchmarkEvaluateAllHands(b *testing.B) { 48 e, err := NewEvaluator() 49 if err != nil { 50 b.Error(e) 51 return 52 } 53 54 handTypeFrequency := make(map[string]uint32) 55 _, cards := GenerateCardEncodes() 56 57 for c1 := 0; c1 < len(cards)-6; c1++ { 58 for c2 := c1 + 1; c2 < len(cards)-5; c2++ { 59 for c3 := c2 + 1; c3 < len(cards)-4; c3++ { 60 for c4 := c3 + 1; c4 < len(cards)-3; c4++ { 61 for c5 := c4 + 1; c5 < len(cards)-2; c5++ { 62 for c6 := c5 + 1; c6 < len(cards)-1; c6++ { 63 for c7 := c6 + 1; c7 < len(cards); c7++ { 64 hand := []string{ 65 cards[c1], 66 cards[c2], 67 cards[c3], 68 cards[c4], 69 cards[c5], 70 cards[c6], 71 cards[c7], 72 } 73 _, handType := e.EvalHand(hand) 74 handTypeFrequency[handType] = handTypeFrequency[handType] + 1 75 } 76 } 77 } 78 } 79 } 80 } 81 } 82 83 tests := []struct { 84 handType string 85 expectCounts uint32 86 }{ 87 { 88 handType: "HIGH CARD", 89 expectCounts: 23294460, 90 }, { 91 handType: "PAIR", 92 expectCounts: 58627800, 93 }, { 94 handType: "TWO PAIRS", 95 expectCounts: 31433400, 96 }, { 97 handType: "THREE OF A KIND", 98 expectCounts: 6461620, 99 }, { 100 handType: "STRAIGHT", 101 expectCounts: 6180020, 102 }, { 103 handType: "FLUSH", 104 expectCounts: 4047644, 105 }, { 106 handType: "FULL HOUSE", 107 expectCounts: 3473184, 108 }, { 109 handType: "FOUR OF A KIND", 110 expectCounts: 224848, 111 }, { 112 handType: "STRAIGHT FLUSH", 113 expectCounts: 41584, 114 }, 115 } 116 117 for _, test := range tests { 118 if frequency, ok := handTypeFrequency[test.handType]; !ok || frequency != test.expectCounts { 119 b.Errorf("Incorrect frequence for hand type %s. Expect %d, got %d.", test.handType, test.expectCounts, frequency) 120 } 121 } 122 }