encoder_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package twooffive
  2. import (
  3. "image/color"
  4. "testing"
  5. )
  6. func Test_AddCheckSum(t *testing.T) {
  7. if sum, err := AddCheckSum("1234567"); err != nil || sum != "12345670" {
  8. t.Fail()
  9. }
  10. if _, err := AddCheckSum("1ABC"); err == nil {
  11. t.Fail()
  12. }
  13. if _, err := AddCheckSum(""); err == nil {
  14. t.Fail()
  15. }
  16. }
  17. func Test_Encode(t *testing.T) {
  18. _, err := Encode("FOOBAR", false)
  19. if err == nil {
  20. t.Error("\"FOOBAR\" should not be encodable")
  21. }
  22. testEncode := func(interleaved bool, txt, testResult string) {
  23. code, err := Encode(txt, interleaved)
  24. if err != nil || code == nil {
  25. t.Fail()
  26. } else {
  27. if code.Bounds().Max.X != len(testResult) {
  28. t.Errorf("%v: length missmatch! %v != %v", txt, code.Bounds().Max.X, len(testResult))
  29. } else {
  30. for i, r := range testResult {
  31. if (code.At(i, 0) == color.Black) != (r == '1') {
  32. t.Errorf("%v: code missmatch on position %d", txt, i)
  33. }
  34. }
  35. }
  36. }
  37. }
  38. testEncode(false, "12345670", "1101101011101010101110101110101011101110111010101010101110101110111010111010101011101110101010101011101110101011101110101101011")
  39. testEncode(true, "12345670", "10101110100010101110001110111010001010001110100011100010101010100011100011101101")
  40. }