qrcode_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // go-qrcode
  2. // Copyright 2014 Tom Harwood
  3. package qrcode
  4. import (
  5. "strings"
  6. "testing"
  7. )
  8. func TestQRCodeMaxCapacity(t *testing.T) {
  9. if testing.Short() {
  10. t.Skip("Skipping TestQRCodeCapacity")
  11. }
  12. tests := []struct {
  13. string string
  14. numRepetitions int
  15. }{
  16. {
  17. "0",
  18. 7089,
  19. },
  20. {
  21. "A",
  22. 4296,
  23. },
  24. {
  25. "#",
  26. 2953,
  27. },
  28. // Alternate byte/numeric data types. Optimises to 2,952 bytes.
  29. {
  30. "#1",
  31. 1476,
  32. },
  33. }
  34. for _, test := range tests {
  35. _, err := New(strings.Repeat(test.string, test.numRepetitions), Low)
  36. if err != nil {
  37. t.Errorf("%d x '%s' got %s expected success", test.numRepetitions,
  38. test.string, err.Error())
  39. }
  40. }
  41. for _, test := range tests {
  42. _, err := New(strings.Repeat(test.string, test.numRepetitions+1), Low)
  43. if err == nil {
  44. t.Errorf("%d x '%s' chars encodable, expected not encodable",
  45. test.numRepetitions+1, test.string)
  46. }
  47. }
  48. }
  49. func TestQRCodeVersionCapacity(t *testing.T) {
  50. tests := []struct {
  51. version int
  52. level RecoveryLevel
  53. maxNumeric int
  54. maxAlphanumeric int
  55. maxByte int
  56. }{
  57. {
  58. 1,
  59. Low,
  60. 41,
  61. 25,
  62. 17,
  63. },
  64. {
  65. 2,
  66. Low,
  67. 77,
  68. 47,
  69. 32,
  70. },
  71. {
  72. 2,
  73. Highest,
  74. 34,
  75. 20,
  76. 14,
  77. },
  78. {
  79. 40,
  80. Low,
  81. 7089,
  82. 4296,
  83. 2953,
  84. },
  85. {
  86. 40,
  87. Highest,
  88. 3057,
  89. 1852,
  90. 1273,
  91. },
  92. }
  93. for i, test := range tests {
  94. numericData := strings.Repeat("1", test.maxNumeric)
  95. alphanumericData := strings.Repeat("A", test.maxAlphanumeric)
  96. byteData := strings.Repeat("#", test.maxByte)
  97. var n *QRCode
  98. var a *QRCode
  99. var b *QRCode
  100. var err error
  101. n, err = New(numericData, test.level)
  102. if err != nil {
  103. t.Fatal(err.Error())
  104. }
  105. a, err = New(alphanumericData, test.level)
  106. if err != nil {
  107. t.Fatal(err.Error())
  108. }
  109. b, err = New(byteData, test.level)
  110. if err != nil {
  111. t.Fatal(err.Error())
  112. }
  113. if n.VersionNumber != test.version {
  114. t.Fatalf("Test #%d numeric has version #%d, expected #%d", i,
  115. n.VersionNumber, test.version)
  116. }
  117. if a.VersionNumber != test.version {
  118. t.Fatalf("Test #%d alphanumeric has version #%d, expected #%d", i,
  119. a.VersionNumber, test.version)
  120. }
  121. if b.VersionNumber != test.version {
  122. t.Fatalf("Test #%d byte has version #%d, expected #%d", i,
  123. b.VersionNumber, test.version)
  124. }
  125. }
  126. }
  127. func TestQRCodeISOAnnexIExample(t *testing.T) {
  128. var q *QRCode
  129. q, err := New("01234567", Medium)
  130. if err != nil {
  131. t.Fatalf("Error producing ISO Annex I Example: %s, expected success",
  132. err.Error())
  133. }
  134. const expectedMask int = 2
  135. if q.mask != 2 {
  136. t.Errorf("ISO Annex I example mask got %d, expected %d\n", q.mask,
  137. expectedMask)
  138. }
  139. }
  140. func BenchmarkQRCodeURLSize(b *testing.B) {
  141. for n := 0; n < b.N; n++ {
  142. New("http://www.example.org", Medium)
  143. }
  144. }
  145. func BenchmarkQRCodeMaximumSize(b *testing.B) {
  146. for n := 0; n < b.N; n++ {
  147. // 7089 is the maximum encodable number of numeric digits.
  148. New(strings.Repeat("0", 7089), Low)
  149. }
  150. }