internal/testutil/snake: Make a more advanced (recursive) snake package

This commit is contained in:
2025-10-29 15:06:23 -04:00
parent 1b43b92687
commit fb374c5cd5
2 changed files with 280 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
package snake
import "testing"
func TestSnakeA(test *testing.T) {
snake := O().AddL(1, 6).AddS(
L(1),
L(2),
L(3),
L(4),
L(5),
).AddL(9)
test.Log(snake)
ok, n := Check(snake, []byte { 1, 6, 1, 2, 3, 4, 5, 9 })
if !ok { test.Fatal("false negative:", n) }
ok, n = Check(snake, []byte { 1, 6, 5, 4, 3, 2, 1, 9 })
if !ok { test.Fatal("false negative:", n) }
ok, n = Check(snake, []byte { 1, 6, 3, 1, 4, 2, 5, 9 })
if !ok { test.Fatal("false negative:", n) }
ok, n = Check(snake, []byte { 1, 6, 9 })
if ok { test.Fatal("false positive:", n) }
ok, n = Check(snake, []byte { 1, 6, 1, 2, 3, 4, 5, 6, 9 })
if ok { test.Fatal("false positive:", n) }
ok, n = Check(snake, []byte { 1, 6, 0, 2, 3, 4, 5, 6, 9 })
if ok { test.Fatal("false positive:", n) }
ok, n = Check(snake, []byte { 1, 6, 7, 1, 4, 2, 5, 9 })
if ok { test.Fatal("false positive:", n) }
ok, n = Check(snake, []byte { 1, 6, 7, 3, 1, 4, 2, 5, 9 })
if ok { test.Fatal("false positive:", n) }
ok, n = Check(snake, []byte { 1, 6, 7, 3, 1, 4, 2, 5, 9 })
if ok { test.Fatal("false positive:", n) }
ok, n = Check(snake, []byte { 1, 6, 1, 2, 3, 4, 5, 9, 10})
if ok { test.Fatal("false positive:", n) }
}
func TestSnakeB(test *testing.T) {
snake := O().AddL(1, 6).AddS(
L(1),
L(2),
).AddL(9).AddS(
L(3, 2),
L(0),
L(1, 1, 2, 3),
)
test.Log(snake)
ok, n := Check(snake, []byte { 1, 6, 1, 2, 9, 3, 2, 0, 1, 1, 2, 3})
if !ok { test.Fatal("false negative:", n) }
ok, n = Check(snake, []byte { 1, 6, 2, 1, 9, 0, 1, 1, 2, 3, 3, 2})
if !ok { test.Fatal("false negative:", n) }
ok, n = Check(snake, []byte { 1, 6, 9 })
if ok { test.Fatal("false positive:", n) }
ok, n = Check(snake, []byte { 1, 6, 1, 2, 9 })
if ok { test.Fatal("false positive:", n) }
ok, n = Check(snake, []byte { 1, 6, 9, 3, 2, 0, 1, 1, 2, 3})
if ok { test.Fatal("false positive:", n) }
ok, n = Check(snake, []byte { 1, 6, 2, 9, 0, 1, 1, 2, 3, 3, 2})
if ok { test.Fatal("false positive:", n) }
ok, n = Check(snake, []byte { 1, 6, 1, 2, 9, 3, 2, 1, 1, 2, 3})
if ok { test.Fatal("false positive:", n) }
}