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

     1  package code128
     2  
     3  import (
     4  	"image/color"
     5  	"testing"
     6  )
     7  
     8  func testEncode(t *testing.T, txt, testResult string) {
     9  	code, err := Encode(txt)
    10  	if err != nil || code == nil {
    11  		t.Error(err)
    12  	} else {
    13  		if code.Bounds().Max.X != len(testResult) {
    14  			t.Errorf("%v: length missmatch. Got %d expected %d", txt, code.Bounds().Max.X, len(testResult))
    15  		} else {
    16  			encoded := ""
    17  			failed := false
    18  			for i, r := range testResult {
    19  				if code.At(i, 0) == color.Black {
    20  					encoded += "1"
    21  				} else {
    22  					encoded += "0"
    23  				}
    24  
    25  				if (code.At(i, 0) == color.Black) != (r == '1') {
    26  					failed = true
    27  					t.Errorf("%v: code missmatch on position %d", txt, i)
    28  				}
    29  			}
    30  			if failed {
    31  				t.Log("Encoded: ", encoded)
    32  			}
    33  		}
    34  	}
    35  }
    36  
    37  func Test_EncodeFunctionChars(t *testing.T) {
    38  	encFNC1 := "11110101110"
    39  	encFNC2 := "11110101000"
    40  	encFNC3 := "10111100010"
    41  	encFNC4 := "10111101110"
    42  	encStartB := "11010010000"
    43  	encStop := "1100011101011"
    44  
    45  	// Special Case FC1 can also be encoded to C Table therefor using 123 as suffix might have unexpected results.
    46  	testEncode(t, string(FNC1)+"A23", encStartB+encFNC1+"10100011000"+"11001110010"+"11001011100"+"10100011110"+encStop)
    47  	testEncode(t, string(FNC2)+"123", encStartB+encFNC2+"10011100110"+"11001110010"+"11001011100"+"11100010110"+encStop)
    48  	testEncode(t, string(FNC3)+"123", encStartB+encFNC3+"10011100110"+"11001110010"+"11001011100"+"11101000110"+encStop)
    49  	testEncode(t, string(FNC4)+"123", encStartB+encFNC4+"10011100110"+"11001110010"+"11001011100"+"11100011010"+encStop)
    50  }
    51  
    52  func Test_Unencodable(t *testing.T) {
    53  	if _, err := Encode(""); err == nil {
    54  		t.Fail()
    55  	}
    56  	if _, err := Encode("รค"); err == nil {
    57  		t.Fail()
    58  	}
    59  }
    60  
    61  func Test_EncodeCTable(t *testing.T) {
    62  	testEncode(t, "HI345678H", "110100100001100010100011000100010101110111101000101100011100010110110000101001011110111011000101000111011000101100011101011")
    63  	testEncode(t, "334455", "11010011100101000110001000110111011101000110100100111101100011101011")
    64  
    65  	testEncode(t, string(FNC1)+"1234",
    66  		"11010011100"+ // Start C
    67  			"11110101110"+ // FNC1
    68  			"10110011100"+ // 12
    69  			"10001011000"+ // 34
    70  			"11101001100"+ // CheckSum == 24
    71  			"1100011101011") // Stop
    72  }
    73  
    74  func Test_shouldUseCTable(t *testing.T) {
    75  	if !shouldUseCTable([]rune{FNC1, '1', '2'}, startCSymbol) {
    76  		t.Error("[FNC1]12 failed")
    77  	}
    78  	if shouldUseCTable([]rune{FNC1, '1'}, startCSymbol) {
    79  		t.Error("[FNC1]1 failed")
    80  	}
    81  	if shouldUseCTable([]rune{'0', FNC1, '1'}, startCSymbol) {
    82  		t.Error("0[FNC1]1 failed")
    83  	}
    84  	if !shouldUseCTable([]rune{'0', '1', FNC1, '2', '3'}, startBSymbol) {
    85  		t.Error("01[FNC1]23 failed")
    86  	}
    87  	if shouldUseCTable([]rune{'0', '1', FNC1}, startBSymbol) {
    88  		t.Error("01[FNC1] failed")
    89  	}
    90  }
    91  
    92  func Test_Issue16(t *testing.T) {
    93  	if !shouldUseATable([]rune{'\r', 'A'}, 0) {
    94  		t.Error("Code should start with A-Table if the text start with \\r")
    95  	}
    96  	if !shouldUseATable([]rune{FNC1, '\r'}, 0) {
    97  		t.Error("Code should start with A-Table if the text start with <FNC1>\\r")
    98  	}
    99  	if shouldUseATable([]rune{FNC1, '1', '2', '3'}, 0) {
   100  		t.Error("Code should not start with A-Table if the text start with <FNC1>123")
   101  	}
   102  	testEncode(t, string(FNC3)+"$P\rI", "110100001001011110001010010001100111011101101111011101011000100010110001010001100011101011")
   103  }
   104  
   105  func Test_Datalogic(t *testing.T) {
   106  	// <Start A><FNC3>$P\r<checksum><STOP>
   107  	testEncode(t, string(FNC3)+"$P\r",
   108  		"11010000100"+ // <Start A>
   109  			"10111100010"+ // <FNC3>
   110  			"10010001100"+ // $
   111  			"11101110110"+ // P
   112  			"11110111010"+ // CR
   113  			"11000100010"+ // checksum = 'I'
   114  			"1100011101011") // STOP
   115  
   116  	// <Start B><FNC3>$P,Ae,P<CR><checksum><STOP>
   117  	testEncode(t, string(FNC3)+"$P,Ae,P\r",
   118  		"11010010000"+ // <Start B>
   119  			"10111100010"+ // <FNC3>
   120  			"10010001100"+ // $
   121  			"11101110110"+ // P
   122  			"10110011100"+ // ,
   123  			"10100011000"+ // A
   124  			"10110010000"+ // e
   125  			"10110011100"+ // ,
   126  			"11101110110"+ // P
   127  			"11101011110"+ // <Code A>
   128  			"11110111010"+ // <CR>
   129  			"10110001000"+ // checksum = 'D'
   130  			"1100011101011") // STOP
   131  }