From f0dfcdfed5c97adb5a80d1d74eca46a9014e2c95 Mon Sep 17 00:00:00 2001 From: Taneli Huuskonen Date: Sun, 8 May 2016 17:01:41 +0300 Subject: Fix for bug #593 * Fixed bug #593 Bug #593 was misleadingly reported as pertaining to another project, but testing revealed it affected this project as well. The Code128 encoding algorithm failed when an odd number of digits were followed by FNC1 and other digits. The code selection algorithm was rewritten. A test case showing the bug was added, and an old test case was updated to expect the result of the new algorithm - another valid encoding of the same length as the old one. * Fixed bug #593 Bug #593 was misleadingly reported as pertaining to another project, but testing revealed it affected this project as well. The Code128 encoding algorithm failed when an odd number of digits were followed by FNC1 and other digits. The code selection algorithm was rewritten. A test case showing the bug was added, and an old test case was updated to expect the result of the new algorithm - another valid encoding of the same length as the old one. * Cleaning up Added a private enum and changed some names to improve readability. Also some trivial cosmetic changes. * Further cleanup of Code128 writer test Removed a private one-liner method used exactly once, alphabetized imports. --- .../google/zxing/oned/Code128WriterTestCase.java | 23 ++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'core/src/test/java/com') diff --git a/core/src/test/java/com/google/zxing/oned/Code128WriterTestCase.java b/core/src/test/java/com/google/zxing/oned/Code128WriterTestCase.java index 1d26507d4..ad403fb2a 100644 --- a/core/src/test/java/com/google/zxing/oned/Code128WriterTestCase.java +++ b/core/src/test/java/com/google/zxing/oned/Code128WriterTestCase.java @@ -21,8 +21,11 @@ import org.junit.Before; import org.junit.Test; import com.google.zxing.BarcodeFormat; +import com.google.zxing.ReaderException; +import com.google.zxing.Result; import com.google.zxing.Writer; import com.google.zxing.WriterException; +import com.google.zxing.common.BitArray; import com.google.zxing.common.BitMatrix; public class Code128WriterTestCase extends Assert { @@ -32,14 +35,18 @@ public class Code128WriterTestCase extends Assert { private static final String FNC3 = "10111100010"; private static final String FNC4 = "10111101110"; private static final String START_CODE_B = "11010010000"; + private static final String START_CODE_C = "11010011100"; + private static final String SWITCH_CODE_B = "10111101110"; private static final String QUIET_SPACE = "00000"; private static final String STOP = "1100011101011"; private Writer writer; + private Code128Reader reader; @Before public void setup() { writer = new Code128Writer(); + reader = new Code128Reader(); } @Test @@ -69,8 +76,8 @@ public class Code128WriterTestCase extends Assert { @Test public void testEncodeWithFunc1() throws WriterException { String toEncode = "\u00f1" + "123"; - // "1" "2" "3" check digit 61 - String expected = QUIET_SPACE + START_CODE_B + FNC1 + "10011100110" + "11001110010" + "11001011100" + "11001000010" + STOP + QUIET_SPACE; + // "12" "3" check digit 92 + String expected = QUIET_SPACE + START_CODE_C + FNC1 + "10110011100" + SWITCH_CODE_B + "11001011100" + "10101111000" + STOP + QUIET_SPACE; BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0); @@ -78,6 +85,18 @@ public class Code128WriterTestCase extends Assert { assertEquals(expected, actual); } + @Test + public void testRoundtrip() throws WriterException, ReaderException { + String toEncode = "\u00f1" + "10958" + "\u00f1" + "17160526"; + String expected = "1095817160526"; + + BitMatrix encResult = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0); + BitArray row = encResult.getRow(0, null); + Result rtResult = reader.decodeRow(0, row, null); + String actual = rtResult.getText(); + assertEquals(expected, actual); + } + @Test public void testEncodeWithFunc4() throws WriterException { String toEncode = "\u00f4" + "123"; -- cgit v1.2.3