simulator_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "testing"
  5. )
  6. func TestSimulation_SimulateOneInitialWord(t *testing.T) {
  7. type fields struct {
  8. Words []string
  9. }
  10. type args struct {
  11. initialWord string
  12. }
  13. tests := []struct {
  14. name string
  15. words []string
  16. fields fields
  17. args args
  18. wantedLossCount int
  19. wantedMaxRounds int
  20. wantedTotalRounds int
  21. }{
  22. {
  23. name: "simple game with no losses",
  24. words: []string{"aaa", "aab", "abb"},
  25. args: args{
  26. initialWord: "aaa",
  27. },
  28. wantedLossCount: 0,
  29. wantedMaxRounds: 2,
  30. wantedTotalRounds: 5,
  31. },
  32. {
  33. name: "simple failed game",
  34. words: []string{"aaa", "aab", "aac", "aad", "aae"},
  35. args: args{
  36. initialWord: "aaa",
  37. },
  38. wantedLossCount: 1,
  39. wantedMaxRounds: 5,
  40. wantedTotalRounds: 15,
  41. },
  42. }
  43. for _, tt := range tests {
  44. t.Run(tt.name, func(t *testing.T) {
  45. game := &Game{}
  46. game.Words = make(map[string]*Word)
  47. for _, word := range tt.words {
  48. game.Words[word] = &Word{Word: word}
  49. }
  50. s := Simulation{
  51. Game: game,
  52. }
  53. lossCount, maxRounds, totalRounds := s.SimulateOneInitialWord(game, tt.args.initialWord)
  54. assert.Equalf(t, tt.wantedLossCount, lossCount, "loss count for %v => %v", game, tt.args.initialWord)
  55. assert.Equalf(t, tt.wantedMaxRounds, maxRounds, "max rounds for %v => %v)", game, tt.args.initialWord)
  56. assert.Equalf(t, tt.wantedTotalRounds, totalRounds, "total rounds for %v => %v)", game, tt.args.initialWord)
  57. })
  58. }
  59. }
  60. func TestSimulation_SimulateOneGame(t *testing.T) {
  61. type fields struct {
  62. BestGuess string
  63. BestGuessRounds int
  64. SuccessCount int
  65. FailCount int
  66. TotalRounds int
  67. }
  68. type args struct {
  69. initialWord string
  70. answer string
  71. }
  72. tests := []struct {
  73. name string
  74. words []string
  75. fields fields
  76. args args
  77. won bool
  78. numRounds int
  79. }{
  80. {
  81. name: "initial word is answer",
  82. words: []string{"aaa", "aab", "abb"},
  83. args: args{
  84. initialWord: "aaa",
  85. answer: "aaa",
  86. },
  87. won: true,
  88. numRounds: 1,
  89. },
  90. {
  91. name: "one guess to get answer",
  92. words: []string{"aaa", "aab", "abb"},
  93. args: args{
  94. initialWord: "aaa",
  95. answer: "aab",
  96. },
  97. won: true,
  98. numRounds: 2,
  99. },
  100. {
  101. name: "one guess to get answer",
  102. words: []string{"aaa", "aab", "aac", "aad", "aae"},
  103. args: args{
  104. initialWord: "aaa",
  105. answer: "aae",
  106. },
  107. won: false,
  108. numRounds: 5,
  109. },
  110. }
  111. for _, tt := range tests {
  112. t.Run(tt.name, func(t *testing.T) {
  113. game := &Game{}
  114. game.Words = make(map[string]*Word)
  115. for _, word := range tt.words {
  116. game.Words[word] = &Word{Word: word}
  117. }
  118. s := Simulation{
  119. Game: game,
  120. }
  121. got, got1 := s.SimulateOneGame(game, tt.args.initialWord, tt.args.answer)
  122. assert.Equalf(t, tt.won, got, "SimulateOneGame(%v, %v, %v)", game, tt.args.initialWord, tt.args.answer)
  123. assert.Equalf(t, tt.numRounds, got1, "SimulateOneGame(%v, %v, %v)", game, tt.args.initialWord, tt.args.answer)
  124. })
  125. }
  126. }