test hangs due to fill inf looping

main
Bel LaPointe 2024-12-15 10:19:04 -07:00
parent 6340469d53
commit eb3a30ec8a
2 changed files with 190 additions and 14 deletions

View File

@ -465,23 +465,40 @@ func (m AllKillWords) assigned() []string {
return result return result
} }
//go:embed holiday.txt
var wordsHoliday string
func (m AllKillWords) FillKillWords() AllKillWords { func (m AllKillWords) FillKillWords() AllKillWords {
return m.fillKillWords(
strings.Fields(wordsHoliday),
strings.Fields(wordsHoliday), // TODO medium difficulty
strings.Fields(wordsHoliday), // TODO hard difficulty
)
}
func (m AllKillWords) fillKillWords(
poolGlobal,
poolPublic,
poolPrivate []string,
) AllKillWords {
result := maps.Clone(m)
m = result
for k, v := range m { for k, v := range m {
if v.Global.Word == "" { if v.Global.Word == "" {
v.Global = KillWord{Word: m.unusedGlobal(), Points: -1} v.Global = KillWord{Word: m.unusedGlobal(poolGlobal), Points: -1}
m[k] = v m[k] = v
} }
if len(v.Assignment.Public) == 0 { if len(v.Assignment.Public) == 0 {
v.Assignment.Public = []KillWord{} v.Assignment.Public = []KillWord{}
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
v.Assignment.Public = append(v.Assignment.Public, KillWord{Word: m.unusedPublic(), Points: 50}) v.Assignment.Public = append(v.Assignment.Public, KillWord{Word: m.unusedPublic(poolPublic), Points: 50})
m[k] = v m[k] = v
} }
} }
if len(v.Assignment.Private) == 0 { if len(v.Assignment.Private) == 0 {
v.Assignment.Private = []KillWord{} v.Assignment.Private = []KillWord{}
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
v.Assignment.Private = append(v.Assignment.Private, KillWord{Word: m.unusedPrivate(), Points: 100}) v.Assignment.Private = append(v.Assignment.Private, KillWord{Word: m.unusedPrivate(poolPrivate), Points: 100})
m[k] = v m[k] = v
} }
} }
@ -489,11 +506,7 @@ func (m AllKillWords) FillKillWords() AllKillWords {
return m return m
} }
//go:embed holiday.txt func (m AllKillWords) unusedGlobal(pool []string) string {
var wordsHoliday string
func (m AllKillWords) unusedGlobal() string {
pool := strings.Fields(wordsHoliday)
inUse := func() []string { inUse := func() []string {
result := []string{} result := []string{}
for _, killWords := range m { for _, killWords := range m {
@ -509,9 +522,7 @@ func (m AllKillWords) unusedGlobal() string {
} }
} }
// TODO hard difficulty func (m AllKillWords) unusedPrivate(pool []string) string {
func (m AllKillWords) unusedPrivate() string {
pool := strings.Fields(wordsHoliday)
inUse := func() []string { inUse := func() []string {
result := []string{} result := []string{}
for _, killWords := range m { for _, killWords := range m {
@ -529,9 +540,7 @@ func (m AllKillWords) unusedPrivate() string {
} }
} }
// TODO medium difficulty func (m AllKillWords) unusedPublic(pool []string) string {
func (m AllKillWords) unusedPublic() string {
pool := strings.Fields(wordsHoliday)
inUse := func() []string { inUse := func() []string {
result := []string{} result := []string{}
for _, killWords := range m { for _, killWords := range m {

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -213,3 +214,169 @@ func TestParseEvent(t *testing.T) {
}) })
} }
} }
func TestAllKillWordsFill(t *testing.T) {
kw := func(w string) KillWord {
return KillWord{Word: w}
}
kws := func(w string) []KillWord {
if w == "" {
return nil
}
return []KillWord{kw(w)}
}
ass := func(pub, pri string) Assignment {
return Assignment{
Public: kws(pub),
Private: kws(pri),
}
}
cases := map[string]struct {
given KillWords
expect KillWords
}{
"full": {
given: KillWords{
Global: kw("global"),
Assignment: ass("pub", "pri"),
},
expect: KillWords{
Global: kw("global"),
Assignment: ass("pub", "pri"),
},
},
"no ass": {
given: KillWords{
Global: kw("global"),
Assignment: Assignment{},
},
expect: KillWords{
Global: kw("global"),
Assignment: ass("filled-public", "filled-private"),
},
},
"no pub": {
given: KillWords{
Global: kw("global"),
Assignment: ass("", "pri"),
},
expect: KillWords{
Global: kw("global"),
Assignment: ass("filled-public", "filled-private"),
},
},
"no pri": {
given: KillWords{
Global: kw("global"),
Assignment: ass("pub", ""),
},
expect: KillWords{
Global: kw("global"),
Assignment: ass("pub", "filled-private"),
},
},
"empty": {
given: KillWords{},
expect: KillWords{
Global: kw("filled-global"),
Assignment: ass("filled-public", "filled-private"),
},
},
"no global": {
given: KillWords{
Assignment: ass("pub", "pri"),
},
expect: KillWords{
Global: kw("filled-global"),
Assignment: ass("pub", "pri"),
},
},
}
equal := func(a, b KillWords) bool {
ba, _ := json.Marshal(a)
bb, _ := json.Marshal(b)
return bytes.Equal(ba, bb)
}
for name, d := range cases {
c := d
t.Run(name, func(t *testing.T) {
akw := make(AllKillWords)
akw[name] = c.given
akw = akw.fillKillWords(
[]string{"filled-global"},
[]string{"filled-public"},
[]string{"filled-private"},
)
got := akw[name]
if !equal(c.expect, got) {
t.Errorf("expected \n\t%+v but got \n\t%+v", c.expect, got)
}
})
}
}
func TestAllKillWordsUnused(t *testing.T) {
t.Run("empty", func(t *testing.T) {
akw := make(AllKillWords)
if got := akw.unusedPublic([]string{"x"}); got != "x" {
t.Error("empty playerbase didnt think only option was unused")
}
if got := akw.unusedPrivate([]string{"x"}); got != "x" {
t.Error("empty playerbase didnt think only option was unused")
}
if got := akw.unusedGlobal([]string{"x"}); got != "x" {
t.Error("empty playerbase didnt think only option was unused")
}
})
t.Run("dont return used", func(t *testing.T) {
t.Run("private", func(t *testing.T) {
akw := make(AllKillWords)
akw["k"] = KillWords{
Global: KillWord{Word: "x"},
Assignment: Assignment{
Private: []KillWord{{}, {Word: "y"}},
Public: []KillWord{{}, {Word: "x"}},
},
}
got := akw.unusedPrivate([]string{"x", "y"})
if got != "x" {
t.Error("didnt return only unused option")
}
})
t.Run("global", func(t *testing.T) {
akw := make(AllKillWords)
akw["k"] = KillWords{
Global: KillWord{Word: "y"},
Assignment: Assignment{
Private: []KillWord{{}, {Word: "x"}},
Public: []KillWord{{}, {Word: "x"}},
},
}
got := akw.unusedGlobal([]string{"x", "y"})
if got != "x" {
t.Error("didnt return only unused option")
}
})
t.Run("public", func(t *testing.T) {
akw := make(AllKillWords)
akw["k"] = KillWords{
Global: KillWord{Word: "x"},
Assignment: Assignment{
Private: []KillWord{{}, {Word: "x"}},
Public: []KillWord{{}, {Word: "y"}},
},
}
got := akw.unusedPublic([]string{"x", "y"})
if got != "x" {
t.Error("didnt return only unused option")
}
})
})
}