git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/barcode/qr/versioninfo_test.go (about)

     1  package qr
     2  
     3  import "testing"
     4  
     5  var testvi = &versionInfo{7, M, 0, 1, 10, 2, 5} // Fake versionInfo to run some of the tests
     6  
     7  func Test_ErrorCorrectionStringer(t *testing.T) {
     8  	tests := map[ErrorCorrectionLevel]string{
     9  		L: "L", M: "M", Q: "Q", H: "H", ErrorCorrectionLevel(99): "unknown",
    10  	}
    11  	for ecl, str := range tests {
    12  		if ecl.String() != str {
    13  			t.Fail()
    14  		}
    15  	}
    16  }
    17  
    18  func Test_CharCountBits(t *testing.T) {
    19  	v1 := &versionInfo{5, M, 0, 0, 0, 0, 0}
    20  	v2 := &versionInfo{15, M, 0, 0, 0, 0, 0}
    21  	v3 := &versionInfo{30, M, 0, 0, 0, 0, 0}
    22  
    23  	if v1.charCountBits(numericMode) != 10 {
    24  		t.Fail()
    25  	}
    26  	if v1.charCountBits(alphaNumericMode) != 9 {
    27  		t.Fail()
    28  	}
    29  	if v1.charCountBits(byteMode) != 8 {
    30  		t.Fail()
    31  	}
    32  	if v1.charCountBits(kanjiMode) != 8 {
    33  		t.Fail()
    34  	}
    35  	if v2.charCountBits(numericMode) != 12 {
    36  		t.Fail()
    37  	}
    38  	if v2.charCountBits(alphaNumericMode) != 11 {
    39  		t.Fail()
    40  	}
    41  	if v2.charCountBits(byteMode) != 16 {
    42  		t.Fail()
    43  	}
    44  	if v2.charCountBits(kanjiMode) != 10 {
    45  		t.Fail()
    46  	}
    47  	if v3.charCountBits(numericMode) != 14 {
    48  		t.Fail()
    49  	}
    50  	if v3.charCountBits(alphaNumericMode) != 13 {
    51  		t.Fail()
    52  	}
    53  	if v3.charCountBits(byteMode) != 16 {
    54  		t.Fail()
    55  	}
    56  	if v3.charCountBits(kanjiMode) != 12 {
    57  		t.Fail()
    58  	}
    59  	if v1.charCountBits(encodingMode(3)) != 0 {
    60  		t.Fail()
    61  	}
    62  }
    63  
    64  func Test_TotalDataBytes(t *testing.T) {
    65  	if testvi.totalDataBytes() != 20 {
    66  		t.Fail()
    67  	}
    68  }
    69  
    70  func Test_ModulWidth(t *testing.T) {
    71  	if testvi.modulWidth() != 45 {
    72  		t.Fail()
    73  	}
    74  }
    75  
    76  func Test_FindSmallestVersionInfo(t *testing.T) {
    77  	if findSmallestVersionInfo(H, alphaNumericMode, 10208) != nil {
    78  		t.Error("there should be no version with this capacity")
    79  	}
    80  	test := func(cap int, tVersion byte) {
    81  		v := findSmallestVersionInfo(H, alphaNumericMode, cap)
    82  		if v == nil || v.Version != tVersion {
    83  			t.Errorf("version %d should be returned.", tVersion)
    84  		}
    85  	}
    86  	test(10191, 40)
    87  	test(5591, 29)
    88  	test(5592, 30)
    89  	test(190, 3)
    90  	test(200, 4)
    91  }
    92  
    93  type aligmnentTest struct {
    94  	version  byte
    95  	patterns []int
    96  }
    97  
    98  var allAligmnentTests = []*aligmnentTest{
    99  	&aligmnentTest{1, []int{}},
   100  	&aligmnentTest{2, []int{6, 18}},
   101  	&aligmnentTest{3, []int{6, 22}},
   102  	&aligmnentTest{4, []int{6, 26}},
   103  	&aligmnentTest{5, []int{6, 30}},
   104  	&aligmnentTest{6, []int{6, 34}},
   105  	&aligmnentTest{7, []int{6, 22, 38}},
   106  	&aligmnentTest{8, []int{6, 24, 42}},
   107  	&aligmnentTest{9, []int{6, 26, 46}},
   108  	&aligmnentTest{10, []int{6, 28, 50}},
   109  	&aligmnentTest{11, []int{6, 30, 54}},
   110  	&aligmnentTest{12, []int{6, 32, 58}},
   111  	&aligmnentTest{13, []int{6, 34, 62}},
   112  	&aligmnentTest{14, []int{6, 26, 46, 66}},
   113  	&aligmnentTest{15, []int{6, 26, 48, 70}},
   114  	&aligmnentTest{16, []int{6, 26, 50, 74}},
   115  	&aligmnentTest{17, []int{6, 30, 54, 78}},
   116  	&aligmnentTest{18, []int{6, 30, 56, 82}},
   117  	&aligmnentTest{19, []int{6, 30, 58, 86}},
   118  	&aligmnentTest{20, []int{6, 34, 62, 90}},
   119  	&aligmnentTest{21, []int{6, 28, 50, 72, 94}},
   120  	&aligmnentTest{22, []int{6, 26, 50, 74, 98}},
   121  	&aligmnentTest{23, []int{6, 30, 54, 78, 102}},
   122  	&aligmnentTest{24, []int{6, 28, 54, 80, 106}},
   123  	&aligmnentTest{25, []int{6, 32, 58, 84, 110}},
   124  	&aligmnentTest{26, []int{6, 30, 58, 86, 114}},
   125  	&aligmnentTest{27, []int{6, 34, 62, 90, 118}},
   126  	&aligmnentTest{28, []int{6, 26, 50, 74, 98, 122}},
   127  	&aligmnentTest{29, []int{6, 30, 54, 78, 102, 126}},
   128  	&aligmnentTest{30, []int{6, 26, 52, 78, 104, 130}},
   129  	&aligmnentTest{31, []int{6, 30, 56, 82, 108, 134}},
   130  	&aligmnentTest{32, []int{6, 34, 60, 86, 112, 138}},
   131  	&aligmnentTest{33, []int{6, 30, 58, 86, 114, 142}},
   132  	&aligmnentTest{34, []int{6, 34, 62, 90, 118, 146}},
   133  	&aligmnentTest{35, []int{6, 30, 54, 78, 102, 126, 150}},
   134  	&aligmnentTest{36, []int{6, 24, 50, 76, 102, 128, 154}},
   135  	&aligmnentTest{37, []int{6, 28, 54, 80, 106, 132, 158}},
   136  	&aligmnentTest{38, []int{6, 32, 58, 84, 110, 136, 162}},
   137  	&aligmnentTest{39, []int{6, 26, 54, 82, 110, 138, 166}},
   138  	&aligmnentTest{40, []int{6, 30, 58, 86, 114, 142, 170}},
   139  }
   140  
   141  func Test_AlignmentPatternPlacements(t *testing.T) {
   142  	for _, at := range allAligmnentTests {
   143  		vi := &versionInfo{at.version, M, 0, 0, 0, 0, 0}
   144  
   145  		res := vi.alignmentPatternPlacements()
   146  		if len(res) != len(at.patterns) {
   147  			t.Errorf("number of alignmentpatterns missmatch for version %d", at.version)
   148  		}
   149  		for i := 0; i < len(res); i++ {
   150  			if res[i] != at.patterns[i] {
   151  				t.Errorf("alignmentpatterns for version %d missmatch on index %d", at.version, i)
   152  			}
   153  		}
   154  
   155  	}
   156  
   157  }