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