main_test.go 556 B

123456789101112131415161718192021222324252627282930313233
  1. package main
  2. import (
  3. "testing"
  4. )
  5. func TestCounter(t *testing.T) {
  6. cases := []struct {
  7. Name string
  8. Expected int
  9. Input string
  10. }{
  11. {"No hits", 0, "XXXAAMSMMA"},
  12. {"One hit", 1, "XXMSAXMASA"},
  13. {"Two hits", 2, "SAMXMMXMAS"},
  14. }
  15. ctr := new(Counter)
  16. buf := make([]byte, 0, 4)
  17. for _, c := range cases {
  18. ctr.cnt = 0
  19. ctr.Init(buf)
  20. ctr.Reset()
  21. for _, e := range c.Input {
  22. ctr.Event(byte(e))
  23. }
  24. actual := ctr.Count()
  25. if actual != c.Expected {
  26. t.Errorf("%s: Expected %d hits; got %d", c.Name, c.Expected, actual)
  27. }
  28. }
  29. }