simulator_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. Game *Game
  9. BestGuess string
  10. BestGuessRounds int
  11. SuccessCount int
  12. FailCount int
  13. TotalRounds int
  14. }
  15. type args struct {
  16. game *Game
  17. initialWord string
  18. }
  19. tests := []struct {
  20. name string
  21. fields fields
  22. args args
  23. wantedLossCount int
  24. wantedMaxRounds int
  25. wantedTotalRounds int
  26. }{
  27. {
  28. name: "simple game with no losses",
  29. fields: fields{
  30. Game: &Game{
  31. Words: map[string]*Word{
  32. "aaa": {Word: "aaa"},
  33. "aab": {Word: "aab"},
  34. "abb": {Word: "abb"},
  35. },
  36. },
  37. },
  38. args: args{
  39. game: &Game{
  40. Words: map[string]*Word{
  41. "aaa": {Word: "aaa"},
  42. "aab": {Word: "aab"},
  43. "abb": {Word: "abb"},
  44. },
  45. },
  46. initialWord: "aaa",
  47. },
  48. wantedLossCount: 0,
  49. wantedMaxRounds: 2,
  50. wantedTotalRounds: 5,
  51. },
  52. {
  53. name: "simple failed game",
  54. fields: fields{
  55. Game: &Game{
  56. Words: map[string]*Word{
  57. "aaa": {Word: "aaa"},
  58. "aab": {Word: "aab"},
  59. "aac": {Word: "aac"},
  60. "aad": {Word: "aad"},
  61. "aae": {Word: "aae"},
  62. },
  63. },
  64. },
  65. args: args{
  66. game: &Game{
  67. Words: map[string]*Word{
  68. "aaa": {Word: "aaa"},
  69. "aab": {Word: "aab"},
  70. "aac": {Word: "aac"},
  71. "aad": {Word: "aad"},
  72. "aae": {Word: "aae"},
  73. },
  74. },
  75. initialWord: "aaa",
  76. },
  77. wantedLossCount: 1,
  78. wantedMaxRounds: 5,
  79. wantedTotalRounds: 15,
  80. },
  81. }
  82. for _, tt := range tests {
  83. t.Run(tt.name, func(t *testing.T) {
  84. s := Simulation{
  85. Game: tt.fields.Game,
  86. BestGuess: tt.fields.BestGuess,
  87. BestGuessRounds: tt.fields.BestGuessRounds,
  88. SuccessCount: tt.fields.SuccessCount,
  89. FailCount: tt.fields.FailCount,
  90. TotalRounds: tt.fields.TotalRounds,
  91. }
  92. lossCount, maxRounds, totalRounds := s.SimulateOneInitialWord(tt.args.game, tt.args.initialWord)
  93. assert.Equalf(t, tt.wantedLossCount, lossCount, "loss count for %v => %v", tt.args.game, tt.args.initialWord)
  94. assert.Equalf(t, tt.wantedMaxRounds, maxRounds, "max rounds for %v => %v)", tt.args.game, tt.args.initialWord)
  95. assert.Equalf(t, tt.wantedTotalRounds, totalRounds, "total rounds for %v => %v)", tt.args.game, tt.args.initialWord)
  96. })
  97. }
  98. }
  99. func TestSimulation_SimulateOneGame(t *testing.T) {
  100. type fields struct {
  101. Game *Game
  102. BestGuess string
  103. BestGuessRounds int
  104. SuccessCount int
  105. FailCount int
  106. TotalRounds int
  107. }
  108. type args struct {
  109. simulatedGame *Game
  110. initialWord string
  111. answer string
  112. }
  113. tests := []struct {
  114. name string
  115. fields fields
  116. args args
  117. won bool
  118. numRounds int
  119. }{
  120. {
  121. name: "initial word is answer",
  122. args: args{
  123. simulatedGame: &Game{
  124. Words: map[string]*Word{
  125. "aaa": {Word: "aaa"},
  126. "aab": {Word: "aab"},
  127. "abb": {Word: "abb"},
  128. },
  129. },
  130. initialWord: "aaa",
  131. answer: "aaa",
  132. },
  133. won: true,
  134. numRounds: 1,
  135. },
  136. {
  137. name: "one guess to get answer",
  138. args: args{
  139. simulatedGame: &Game{
  140. Words: map[string]*Word{
  141. "aaa": {Word: "aaa"},
  142. "aab": {Word: "aab"},
  143. "abb": {Word: "abb"},
  144. },
  145. },
  146. initialWord: "aaa",
  147. answer: "aab",
  148. },
  149. won: true,
  150. numRounds: 2,
  151. },
  152. {
  153. name: "one guess to get answer",
  154. args: args{
  155. simulatedGame: &Game{
  156. Words: map[string]*Word{
  157. "aaa": {Word: "aaa"},
  158. "aab": {Word: "aab"},
  159. "aac": {Word: "aac"},
  160. "aad": {Word: "aad"},
  161. "aae": {Word: "aae"},
  162. },
  163. },
  164. initialWord: "aaa",
  165. answer: "aae",
  166. },
  167. won: false,
  168. numRounds: 5,
  169. },
  170. }
  171. for _, tt := range tests {
  172. t.Run(tt.name, func(t *testing.T) {
  173. s := Simulation{}
  174. got, got1 := s.SimulateOneGame(tt.args.simulatedGame, tt.args.initialWord, tt.args.answer)
  175. assert.Equalf(t, tt.won, got, "SimulateOneGame(%v, %v, %v)", tt.args.simulatedGame, tt.args.initialWord, tt.args.answer)
  176. assert.Equalf(t, tt.numRounds, got1, "SimulateOneGame(%v, %v, %v)", tt.args.simulatedGame, tt.args.initialWord, tt.args.answer)
  177. })
  178. }
  179. }