encoder_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package aztec
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/boombuler/barcode/utils"
  6. )
  7. func Test_StuffBits(t *testing.T) {
  8. testStuffBits := func(wordSize int, bits string, expected string) {
  9. bl := new(utils.BitList)
  10. for _, r := range bits {
  11. if r == 'X' {
  12. bl.AddBit(true)
  13. } else if r == '.' {
  14. bl.AddBit(false)
  15. }
  16. }
  17. stuffed := stuffBits(bl, wordSize)
  18. expectedBits := strings.Replace(expected, " ", "", -1)
  19. result := bitStr(stuffed)
  20. if result != expectedBits {
  21. t.Errorf("stuffBits failed for %q\nGot: %q", bits, result)
  22. }
  23. }
  24. testStuffBits(5, ".X.X. X.X.X .X.X.",
  25. ".X.X. X.X.X .X.X.")
  26. testStuffBits(5, ".X.X. ..... .X.X",
  27. ".X.X. ....X ..X.X")
  28. testStuffBits(3, "XX. ... ... ..X XXX .X. ..",
  29. "XX. ..X ..X ..X ..X .XX XX. .X. ..X")
  30. testStuffBits(6, ".X.X.. ...... ..X.XX",
  31. ".X.X.. .....X. ..X.XX XXXX.")
  32. testStuffBits(6, ".X.X.. ...... ...... ..X.X.",
  33. ".X.X.. .....X .....X ....X. X.XXXX")
  34. testStuffBits(6, ".X.X.. XXXXXX ...... ..X.XX",
  35. ".X.X.. XXXXX. X..... ...X.X XXXXX.")
  36. testStuffBits(6,
  37. "...... ..XXXX X..XX. .X.... .X.X.X .....X .X.... ...X.X .....X ....XX ..X... ....X. X..XXX X.XX.X",
  38. ".....X ...XXX XX..XX ..X... ..X.X. X..... X.X... ....X. X..... X....X X..X.. .....X X.X..X XXX.XX .XXXXX")
  39. }
  40. func Test_ModeMessage(t *testing.T) {
  41. testModeMessage := func(compact bool, layers, words int, expected string) {
  42. result := bitStr(generateModeMessage(compact, layers, words))
  43. expectedBits := strings.Replace(expected, " ", "", -1)
  44. if result != expectedBits {
  45. t.Errorf("generateModeMessage(%v, %d, %d) failed.\nGot:%s", compact, layers, words, result)
  46. }
  47. }
  48. testModeMessage(true, 2, 29, ".X .XXX.. ...X XX.. ..X .XX. .XX.X")
  49. testModeMessage(true, 4, 64, "XX XXXXXX .X.. ...X ..XX .X.. XX..")
  50. testModeMessage(false, 21, 660, "X.X.. .X.X..X..XX .XXX ..X.. .XXX. .X... ..XXX")
  51. testModeMessage(false, 32, 4096, "XXXXX XXXXXXXXXXX X.X. ..... XXX.X ..X.. X.XXX")
  52. }