123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- package main
- import (
- "github.com/stretchr/testify/assert"
- "testing"
- )
- func TestSimulation_SimulateOneInitialWord(t *testing.T) {
- type fields struct {
- Game *Game
- BestGuess string
- BestGuessRounds int
- SuccessCount int
- FailCount int
- TotalRounds int
- }
- type args struct {
- game *Game
- initialWord string
- }
- tests := []struct {
- name string
- fields fields
- args args
- wantedLossCount int
- wantedMaxRounds int
- wantedTotalRounds int
- }{
- {
- name: "simple game with no losses",
- fields: fields{
- Game: &Game{
- Words: map[string]*Word{
- "aaa": {Word: "aaa"},
- "aab": {Word: "aab"},
- "abb": {Word: "abb"},
- },
- },
- },
- args: args{
- game: &Game{
- Words: map[string]*Word{
- "aaa": {Word: "aaa"},
- "aab": {Word: "aab"},
- "abb": {Word: "abb"},
- },
- },
- initialWord: "aaa",
- },
- wantedLossCount: 0,
- wantedMaxRounds: 2,
- wantedTotalRounds: 5,
- },
- {
- name: "simple failed game",
- fields: fields{
- Game: &Game{
- Words: map[string]*Word{
- "aaa": {Word: "aaa"},
- "aab": {Word: "aab"},
- "aac": {Word: "aac"},
- "aad": {Word: "aad"},
- "aae": {Word: "aae"},
- },
- },
- },
- args: args{
- game: &Game{
- Words: map[string]*Word{
- "aaa": {Word: "aaa"},
- "aab": {Word: "aab"},
- "aac": {Word: "aac"},
- "aad": {Word: "aad"},
- "aae": {Word: "aae"},
- },
- },
- initialWord: "aaa",
- },
- wantedLossCount: 1,
- wantedMaxRounds: 5,
- wantedTotalRounds: 15,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- s := Simulation{
- Game: tt.fields.Game,
- BestGuess: tt.fields.BestGuess,
- BestGuessRounds: tt.fields.BestGuessRounds,
- SuccessCount: tt.fields.SuccessCount,
- FailCount: tt.fields.FailCount,
- TotalRounds: tt.fields.TotalRounds,
- }
- lossCount, maxRounds, totalRounds := s.SimulateOneInitialWord(tt.args.game, tt.args.initialWord)
- assert.Equalf(t, tt.wantedLossCount, lossCount, "loss count for %v => %v", tt.args.game, tt.args.initialWord)
- assert.Equalf(t, tt.wantedMaxRounds, maxRounds, "max rounds for %v => %v)", tt.args.game, tt.args.initialWord)
- assert.Equalf(t, tt.wantedTotalRounds, totalRounds, "total rounds for %v => %v)", tt.args.game, tt.args.initialWord)
- })
- }
- }
- func TestSimulation_SimulateOneGame(t *testing.T) {
- type fields struct {
- Game *Game
- BestGuess string
- BestGuessRounds int
- SuccessCount int
- FailCount int
- TotalRounds int
- }
- type args struct {
- simulatedGame *Game
- initialWord string
- answer string
- }
- tests := []struct {
- name string
- fields fields
- args args
- won bool
- numRounds int
- }{
- {
- name: "initial word is answer",
- args: args{
- simulatedGame: &Game{
- Words: map[string]*Word{
- "aaa": {Word: "aaa"},
- "aab": {Word: "aab"},
- "abb": {Word: "abb"},
- },
- },
- initialWord: "aaa",
- answer: "aaa",
- },
- won: true,
- numRounds: 1,
- },
- {
- name: "one guess to get answer",
- args: args{
- simulatedGame: &Game{
- Words: map[string]*Word{
- "aaa": {Word: "aaa"},
- "aab": {Word: "aab"},
- "abb": {Word: "abb"},
- },
- },
- initialWord: "aaa",
- answer: "aab",
- },
- won: true,
- numRounds: 2,
- },
- {
- name: "one guess to get answer",
- args: args{
- simulatedGame: &Game{
- Words: map[string]*Word{
- "aaa": {Word: "aaa"},
- "aab": {Word: "aab"},
- "aac": {Word: "aac"},
- "aad": {Word: "aad"},
- "aae": {Word: "aae"},
- },
- },
- initialWord: "aaa",
- answer: "aae",
- },
- won: false,
- numRounds: 5,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- s := Simulation{}
- got, got1 := s.SimulateOneGame(tt.args.simulatedGame, tt.args.initialWord, tt.args.answer)
- assert.Equalf(t, tt.won, got, "SimulateOneGame(%v, %v, %v)", tt.args.simulatedGame, tt.args.initialWord, tt.args.answer)
- assert.Equalf(t, tt.numRounds, got1, "SimulateOneGame(%v, %v, %v)", tt.args.simulatedGame, tt.args.initialWord, tt.args.answer)
- })
- }
- }
|