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

     1  package pdf417
     2  
     3  import "testing"
     4  
     5  func compareIntSlice(t *testing.T, expected, actual []int, testStr string) {
     6  	if len(actual) != len(expected) {
     7  		t.Errorf("Invalid slice size. Expected %d got %d while encoding %q", len(expected), len(actual), testStr)
     8  		return
     9  	}
    10  	for i, a := range actual {
    11  		if e := expected[i]; e != a {
    12  			t.Errorf("Unexpected value at position %d. Expected %d got %d while encoding %q", i, e, a, testStr)
    13  		}
    14  	}
    15  }
    16  
    17  func TestHighlevelEncode(t *testing.T) {
    18  	runTest := func(msg string, expected ...int) {
    19  		if codes, err := highlevelEncode(msg); err != nil {
    20  			t.Error(err)
    21  		} else {
    22  			compareIntSlice(t, expected, codes, msg)
    23  		}
    24  	}
    25  
    26  	runTest("01234", 902, 112, 434)
    27  	runTest("Super !", 567, 615, 137, 809, 329)
    28  	runTest("Super ", 567, 615, 137, 809)
    29  	runTest("ABC123", 1, 88, 32, 119)
    30  	runTest("123ABC", 841, 63, 840, 32)
    31  }
    32  
    33  func TestBinaryEncoder(t *testing.T) {
    34  	runTest := func(msg string, expected ...int) {
    35  		codes := encodeBinary([]byte(msg), encText)
    36  		compareIntSlice(t, expected, codes, msg)
    37  	}
    38  
    39  	runTest("alcool", 924, 163, 238, 432, 766, 244)
    40  	runTest("alcoolique", 901, 163, 238, 432, 766, 244, 105, 113, 117, 101)
    41  }