Issue #61 : touch up style in test and simplify new logic

This commit is contained in:
Sean Owen 2014-02-17 22:56:49 +00:00
parent 525774a953
commit 9d4b8b9565
3 changed files with 123 additions and 127 deletions

View file

@ -77,6 +77,7 @@ Matthew Schulkind (Google)
Matt York (LifeMarks) Matt York (LifeMarks)
mike32767 mike32767
mikej06 mikej06
Mike Kicinski
Mohamad Fairol Mohamad Fairol
Morgan Courbet Morgan Courbet
Nikolaos Ftylitakis Nikolaos Ftylitakis

View file

@ -105,52 +105,30 @@ public final class Code128Writer extends OneDimensionalCodeWriter {
int patternIndex; int patternIndex;
if (newCodeSet == codeSet) { if (newCodeSet == codeSet) {
// Encode the current character // Encode the current character
if (codeSet == CODE_CODE_B) { // First handle escapes
switch (contents.charAt(position)) { switch (contents.charAt(position)) {
case ESCAPE_FNC_1: case ESCAPE_FNC_1:
patternIndex = CODE_FNC_1; patternIndex = CODE_FNC_1;
position++; break;
break; case ESCAPE_FNC_2:
case ESCAPE_FNC_2: patternIndex = CODE_FNC_2;
patternIndex = CODE_FNC_2; break;
position++; case ESCAPE_FNC_3:
break; patternIndex = CODE_FNC_3;
case ESCAPE_FNC_3: break;
patternIndex = CODE_FNC_3; case ESCAPE_FNC_4:
position++; patternIndex = CODE_FNC_4_B; // FIXME if this ever outputs Code A
break; break;
case ESCAPE_FNC_4: default:
patternIndex = CODE_FNC_4_B; // FIXME if this ever outputs Code A // Then handle normal characters otherwise
position++; if (codeSet == CODE_CODE_B) {
break;
default:
patternIndex = contents.charAt(position) - ' '; patternIndex = contents.charAt(position) - ' ';
position++; } else { // CODE_CODE_C
}
} else { // CODE_CODE_C
switch (contents.charAt(position)) {
case ESCAPE_FNC_1:
patternIndex = CODE_FNC_1;
position++;
break;
case ESCAPE_FNC_2:
patternIndex = CODE_FNC_2;
position++;
break;
case ESCAPE_FNC_3:
patternIndex = CODE_FNC_3;
position++;
break;
case ESCAPE_FNC_4:
patternIndex = CODE_FNC_4_B; // FIXME if this ever outputs Code A
position++;
break;
default:
patternIndex = Integer.parseInt(contents.substring(position, position + 2)); patternIndex = Integer.parseInt(contents.substring(position, position + 2));
position += 2; position++; // Also incremented below
break; }
}
} }
position++;
} else { } else {
// Should we change the current code? // Should we change the current code?
// Do we have a code set? // Do we have a code set?

View file

@ -1,84 +1,101 @@
package com.google.zxing.oned; /*
* Copyright 2014 ZXing authors
import static org.junit.Assert.assertEquals; *
* Licensed under the Apache License, Version 2.0 (the "License");
import org.junit.Before; * you may not use this file except in compliance with the License.
import org.junit.Test; * You may obtain a copy of the License at
*
import com.google.zxing.BarcodeFormat; * http://www.apache.org/licenses/LICENSE-2.0
import com.google.zxing.Writer; *
import com.google.zxing.WriterException; * Unless required by applicable law or agreed to in writing, software
import com.google.zxing.common.BitMatrix; * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
public class Code128WriterTestCase { * See the License for the specific language governing permissions and
private static final String FNC1 = "11110101110"; * limitations under the License.
private static final String FNC2 = "11110101000"; */
private static final String FNC3 = "10111100010";
private static final String FNC4 = "10111101110"; package com.google.zxing.oned;
private static final String START_CODE_B = "11010010000";
public static final String QUIET_SPACE = "00000"; import static org.junit.Assert.assertEquals;
public static final String STOP = "1100011101011";
import org.junit.Before;
Writer writer; import org.junit.Test;
@Before import com.google.zxing.BarcodeFormat;
public void setup() { import com.google.zxing.Writer;
writer = new Code128Writer(); import com.google.zxing.WriterException;
} import com.google.zxing.common.BitMatrix;
@Test public class Code128WriterTestCase {
public void testEncodeWithFunc3() throws WriterException {
String toEncode = "\u00f3" + "123"; private static final String FNC1 = "11110101110";
// "1" "2" "3" check digit 51 private static final String FNC2 = "11110101000";
String expected = QUIET_SPACE + START_CODE_B + FNC3 + "10011100110" + "11001110010" + "11001011100" + "11101000110" + STOP + QUIET_SPACE; private static final String FNC3 = "10111100010";
private static final String FNC4 = "10111101110";
BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0); private static final String START_CODE_B = "11010010000";
public static final String QUIET_SPACE = "00000";
String actual = matrixToString(result); public static final String STOP = "1100011101011";
assertEquals(expected, actual);
} private Writer writer;
@Test @Before
public void testEncodeWithFunc2() throws WriterException { public void setup() {
String toEncode = "\u00f2" + "123"; writer = new Code128Writer();
// "1" "2" "3" check digit 56 }
String expected = QUIET_SPACE + START_CODE_B + FNC2 + "10011100110" + "11001110010" + "11001011100" + "11100010110" + STOP + QUIET_SPACE;
@Test
BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0); public void testEncodeWithFunc3() throws WriterException {
String toEncode = "\u00f3" + "123";
String actual = matrixToString(result); // "1" "2" "3" check digit 51
assertEquals(expected, actual); String expected = QUIET_SPACE + START_CODE_B + FNC3 + "10011100110" + "11001110010" + "11001011100" + "11101000110" + STOP + QUIET_SPACE;
}
BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0);
@Test
public void testEncodeWithFunc1() throws WriterException { String actual = matrixToString(result);
String toEncode = "\u00f1" + "123"; assertEquals(expected, actual);
// "1" "2" "3" check digit 61 }
String expected = QUIET_SPACE + START_CODE_B + FNC1 + "10011100110" + "11001110010" + "11001011100" + "11001000010" + STOP + QUIET_SPACE;
@Test
BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0); public void testEncodeWithFunc2() throws WriterException {
String toEncode = "\u00f2" + "123";
String actual = matrixToString(result); // "1" "2" "3" check digit 56
assertEquals(expected, actual); String expected = QUIET_SPACE + START_CODE_B + FNC2 + "10011100110" + "11001110010" + "11001011100" + "11100010110" + STOP + QUIET_SPACE;
}
BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0);
@Test
public void testEncodeWithFunc4() throws WriterException { String actual = matrixToString(result);
String toEncode = "\u00f4" + "123"; assertEquals(expected, actual);
// "1" "2" "3" check digit 59 }
String expected = QUIET_SPACE + START_CODE_B + FNC4 + "10011100110" + "11001110010" + "11001011100" + "11100011010" + STOP + QUIET_SPACE;
@Test
BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0); public void testEncodeWithFunc1() throws WriterException {
String toEncode = "\u00f1" + "123";
String actual = matrixToString(result); // "1" "2" "3" check digit 61
assertEquals(expected, actual); String expected = QUIET_SPACE + START_CODE_B + FNC1 + "10011100110" + "11001110010" + "11001011100" + "11001000010" + STOP + QUIET_SPACE;
}
BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0);
private String matrixToString(BitMatrix result) {
StringBuilder builder = new StringBuilder(result.getWidth()); String actual = matrixToString(result);
for (int i = 0; i < result.getWidth(); i++) { assertEquals(expected, actual);
builder.append(result.get(i, 0) ? '1' : '0'); }
}
return builder.toString(); @Test
} public void testEncodeWithFunc4() throws WriterException {
} String toEncode = "\u00f4" + "123";
// "1" "2" "3" check digit 59
String expected = QUIET_SPACE + START_CODE_B + FNC4 + "10011100110" + "11001110010" + "11001011100" + "11100011010" + STOP + QUIET_SPACE;
BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0);
String actual = matrixToString(result);
assertEquals(expected, actual);
}
private String matrixToString(BitMatrix result) {
StringBuilder builder = new StringBuilder(result.getWidth());
for (int i = 0; i < result.getWidth(); i++) {
builder.append(result.get(i, 0) ? '1' : '0');
}
return builder.toString();
}
}