EncoderTest.java (zxing-zxing-3.4.1) | : | EncoderTest.java (zxing-zxing-3.5.0) | ||
---|---|---|---|---|
skipping to change at line 47 | skipping to change at line 47 | |||
import java.util.regex.Pattern; | import java.util.regex.Pattern; | |||
/** | /** | |||
* Aztec 2D generator unit tests. | * Aztec 2D generator unit tests. | |||
* | * | |||
* @author Rustam Abdullaev | * @author Rustam Abdullaev | |||
* @author Frank Yellin | * @author Frank Yellin | |||
*/ | */ | |||
public final class EncoderTest extends Assert { | public final class EncoderTest extends Assert { | |||
private static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1; | ||||
private static final Charset UTF_8 = StandardCharsets.UTF_8; | ||||
private static final Charset SHIFT_JIS = Charset.forName("Shift_JIS"); | ||||
private static final Charset ISO_8859_15 = Charset.forName("ISO-8859-15"); | ||||
private static final Charset WINDOWS_1252 = Charset.forName("Windows-1252"); | ||||
private static final Pattern DOTX = Pattern.compile("[^.X]"); | private static final Pattern DOTX = Pattern.compile("[^.X]"); | |||
private static final Pattern SPACES = Pattern.compile("\\s+"); | private static final Pattern SPACES = Pattern.compile("\\s+"); | |||
private static final ResultPoint[] NO_POINTS = new ResultPoint[0]; | private static final ResultPoint[] NO_POINTS = new ResultPoint[0]; | |||
// real life tests | // real life tests | |||
@Test | @Test | |||
public void testEncode1() { | public void testEncode1() { | |||
testEncode("This is an example Aztec symbol for Wikipedia.", true, 3, | testEncode("This is an example Aztec symbol for Wikipedia.", true, 3, | |||
"X X X X X X X X \n" + | "X X X X X X X X \n" + | |||
skipping to change at line 131 | skipping to change at line 137 | |||
"X X X X X X X X X X X X X X X X X X X X X X X \n" + | "X X X X X X X X X X X X X X X X X X X X X X X \n" + | |||
"X X X X X X X X X X X X X X X X X X X X X \n" + | "X X X X X X X X X X X X X X X X X X X X X \n" + | |||
"X X X X X X X X X X X X X X X X \n" + | "X X X X X X X X X X X X X X X X \n" + | |||
"X X X X X X X X X X X X X X X X X X X X X \n" + | "X X X X X X X X X X X X X X X X X X X X X \n" + | |||
" X X X X X X X X X X X X X X X X \n" + | " X X X X X X X X X X X X X X X X \n" + | |||
"X X X X X X X X X X X X X \n"); | "X X X X X X X X X X X X X \n"); | |||
} | } | |||
@Test | @Test | |||
public void testAztecWriter() throws Exception { | public void testAztecWriter() throws Exception { | |||
testWriter("\u20AC 1 sample data.", "ISO-8859-1", 25, true, 2); | testWriter("Espa\u00F1ol", null, 25, true, 1); // Without | |||
testWriter("\u20AC 1 sample data.", "ISO-8859-15", 25, true, 2); | ECI (implicit ISO-8859-1) | |||
testWriter("\u20AC 1 sample data.", "UTF-8", 25, true, 2); | testWriter("Espa\u00F1ol", ISO_8859_1, 25, true, 1); // Explicit | |||
testWriter("\u20AC 1 sample data.", "UTF-8", 100, true, 3); | ISO-8859-1 | |||
testWriter("\u20AC 1 sample data.", "UTF-8", 300, true, 4); | testWriter("\u20AC 1 sample data.", WINDOWS_1252, 25, true, 2); // ISO-8859 | |||
testWriter("\u20AC 1 sample data.", "UTF-8", 500, false, 5); | -1 can't encode Euro; Windows-1252 can | |||
testWriter("\u20AC 1 sample data.", ISO_8859_15, 25, true, 2); | ||||
testWriter("\u20AC 1 sample data.", UTF_8, 25, true, 2); | ||||
testWriter("\u20AC 1 sample data.", UTF_8, 100, true, 3); | ||||
testWriter("\u20AC 1 sample data.", UTF_8, 300, true, 4); | ||||
testWriter("\u20AC 1 sample data.", UTF_8, 500, false, 5); | ||||
testWriter("The capital of Japan is named \u6771\u4EAC.", SHIFT_JIS, 25, tru | ||||
e, 3); | ||||
// Test AztecWriter defaults | // Test AztecWriter defaults | |||
String data = "In ut magna vel mauris malesuada"; | String data = "In ut magna vel mauris malesuada"; | |||
AztecWriter writer = new AztecWriter(); | AztecWriter writer = new AztecWriter(); | |||
BitMatrix matrix = writer.encode(data, BarcodeFormat.AZTEC, 0, 0); | BitMatrix matrix = writer.encode(data, BarcodeFormat.AZTEC, 0, 0); | |||
AztecCode aztec = Encoder.encode(data.getBytes(StandardCharsets.ISO_8859_1), | AztecCode aztec = Encoder.encode(data, | |||
Encoder.DEFAULT_EC_PERCENT, Encoder.DEFAULT_AZTEC_LAYERS); | Encoder.DEFAULT_EC_PERCENT, Encoder.DEFAULT_AZTEC_LAYERS); | |||
BitMatrix expectedMatrix = aztec.getMatrix(); | BitMatrix expectedMatrix = aztec.getMatrix(); | |||
assertEquals(matrix, expectedMatrix); | assertEquals(matrix, expectedMatrix); | |||
} | } | |||
// synthetic tests (encode-decode round-trip) | // synthetic tests (encode-decode round-trip) | |||
@Test | @Test | |||
public void testEncodeDecode1() throws Exception { | public void testEncodeDecode1() throws Exception { | |||
testEncodeDecode("Abc123!", true, 1); | testEncodeDecode("Abc123!", true, 1); | |||
skipping to change at line 214 | skipping to change at line 223 | |||
" ullamcorper metus quis diam cursus facilisis. Sed mollis quam id justo rutrum" + | " ullamcorper metus quis diam cursus facilisis. Sed mollis quam id justo rutrum" + | |||
" sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum s it amet." + | " sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum s it amet." + | |||
" Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat, eg et hendrerit" + | " Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat, eg et hendrerit" + | |||
" felis turpis nec lorem. Nulla ultrices, elit pellentesque aliquet laor eet, justo" + | " felis turpis nec lorem. Nulla ultrices, elit pellentesque aliquet laor eet, justo" + | |||
" erat pulvinar nisi, id elementum sapien dolor et diam.", false, 23); | " erat pulvinar nisi, id elementum sapien dolor et diam.", false, 23); | |||
} | } | |||
@Test | @Test | |||
public void testEncodeDecode31() throws Exception { | public void testEncodeDecode31() throws Exception { | |||
testEncodeDecode("In ut magna vel mauris malesuada dictum. Nulla ullamcorper metus quis diam" + | testEncodeDecode("In ut magna vel mauris malesuada dictum. Nulla ullamcorper metus quis diam" + | |||
" cursus facilisis. Sed mollis quam id justo rutrum sagittis. Donec laoree | " cursus facilisis. Sed mollis quam id justo rutrum sagittis. Donec laor | |||
t rutrum" + | eet rutrum" + | |||
" est, nec convallis mauris condimentum sit amet. Phasellus gravida, justo | " est, nec convallis mauris condimentum sit amet. Phasellus gravida, jus | |||
et congue" + | to et congue" + | |||
" auctor, nisi ipsum viverra erat, eget hendrerit felis turpis nec lorem. | " auctor, nisi ipsum viverra erat, eget hendrerit felis turpis nec lorem | |||
Nulla" + | . Nulla" + | |||
" ultrices, elit pellentesque aliquet laoreet, justo erat pulvinar nisi, i | " ultrices, elit pellentesque aliquet laoreet, justo erat pulvinar nisi, | |||
d" + | id" + | |||
" elementum sapien dolor et diam. Donec ac nunc sodales elit placerat elei | " elementum sapien dolor et diam. Donec ac nunc sodales elit placerat el | |||
fend." + | eifend." + | |||
" Sed ornare luctus ornare. Vestibulum vehicula, massa at pharetra fringil | " Sed ornare luctus ornare. Vestibulum vehicula, massa at pharetra fring | |||
la, risus" + | illa, risus" + | |||
" justo faucibus erat, nec porttitor nibh tellus sed est. Ut justo diam, l | " justo faucibus erat, nec porttitor nibh tellus sed est. Ut justo diam, | |||
obortis eu" + | lobortis eu" + | |||
" tristique ac, p.In ut magna vel mauris malesuada dictum. Nulla ullamcorp | " tristique ac, p.In ut magna vel mauris malesuada dictum. Nulla ullamco | |||
er metus" + | rper metus" + | |||
" quis diam cursus facilisis. Sed mollis quam id justo rutrum sagittis. Do | " quis diam cursus facilisis. Sed mollis quam id justo rutrum sagittis. | |||
nec" + | Donec" + | |||
" laoreet rutrum est, nec convallis mauris condimentum sit amet. Phasellus | " laoreet rutrum est, nec convallis mauris condimentum sit amet. Phasell | |||
gravida," + | us gravida," + | |||
" justo et congue auctor, nisi ipsum viverra erat, eget hendrerit felis tu | " justo et congue auctor, nisi ipsum viverra erat, eget hendrerit felis | |||
rpis nec" + | turpis nec" + | |||
" lorem. Nulla ultrices, elit pellentesque aliquet laoreet, justo erat pul | " lorem. Nulla ultrices, elit pellentesque aliquet laoreet, justo erat p | |||
vinar" + | ulvinar" + | |||
" nisi, id elementum sapien dolor et diam. Donec ac nunc sodales elit plac | " nisi, id elementum sapien dolor et diam. Donec ac nunc sodales elit pl | |||
erat" + | acerat" + | |||
" eleifend. Sed ornare luctus ornare. Vestibulum vehicula, massa at pharet | " eleifend. Sed ornare luctus ornare. Vestibulum vehicula, massa at phar | |||
ra" + | etra" + | |||
" fringilla, risus justo faucibus erat, nec porttitor nibh tellus sed est. | " fringilla, risus justo faucibus erat, nec porttitor nibh tellus sed es | |||
Ut justo" + | t. Ut justo" + | |||
" diam, lobortis eu tristique ac, p. In ut magna vel mauris malesuada dict | " diam, lobortis eu tristique ac, p. In ut magna vel mauris malesuada di | |||
um. Nulla" + | ctum. Nulla" + | |||
" ullamcorper metus quis diam cursus facilisis. Sed mollis quam id justo r | " ullamcorper metus quis diam cursus facilisis. Sed mollis quam id justo | |||
utrum" + | rutrum" + | |||
" sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum sit | " sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum s | |||
amet." + | it amet." + | |||
" Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat, eget | " Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat, eg | |||
hendrerit" + | et hendrerit" + | |||
" felis turpis nec lorem. Nulla ultrices, elit pellentesque aliquet laoree | " felis turpis nec lorem. Nulla ultrices, elit pellentesque aliquet laor | |||
t, justo" + | eet, justo" + | |||
" erat pulvinar nisi, id elementum sapien dolor et diam. Donec ac nunc sod | " erat pulvinar nisi, id elementum sapien dolor et diam. Donec ac nunc s | |||
ales elit" + | odales elit" + | |||
" placerat eleifend. Sed ornare luctus ornare. Vestibulum vehicula, massa | " placerat eleifend. Sed ornare luctus ornare. Vestibulum vehicula, mass | |||
at" + | a at" + | |||
" pharetra fringilla, risus justo faucibus erat, nec porttitor nibh tellus | " pharetra fringilla, risus justo faucibus erat, nec porttitor nibh tell | |||
sed est." + | us sed est." + | |||
" Ut justo diam, lobortis eu tristique ac, p.In ut magna vel mauris malesu | " Ut justo diam, lobortis eu tristique ac, p.In ut magna vel mauris male | |||
ada" + | suada" + | |||
" dictum. Nulla ullamcorper metus quis diam cursus facilisis. Sed mollis q | " dictum. Nulla ullamcorper metus quis diam cursus facilisis. Sed mollis | |||
uam id" + | quam id" + | |||
" justo rutrum sagittis. Donec laoreet rutrum est, nec convallis mauris co | " justo rutrum sagittis. Donec laoreet rutrum est, nec convallis mauris | |||
ndimentum" + | condimentum" + | |||
" sit amet. Phasellus gravida, justo et congue auctor, nisi ipsum viverra | " sit amet. Phasellus gravida, justo et congue auctor, nisi ipsum viverr | |||
erat," + | a erat," + | |||
" eget hendrerit felis turpis nec lorem. Nulla ultrices, elit pellentesque | " eget hendrerit felis turpis nec lorem. Nulla ultrices, elit pellentesq | |||
aliquet" + | ue aliquet" + | |||
" laoreet, justo erat pulvinar nisi, id elementum sapien dolor et diam. Do | " laoreet, justo erat pulvinar nisi, id elementum sapien dolor et diam. | |||
nec ac" + | Donec ac" + | |||
" nunc sodales elit placerat eleifend. Sed ornare luctus ornare. Vestibulu | " nunc sodales elit placerat eleifend. Sed ornare luctus ornare. Vestibu | |||
m vehicula," + | lum vehicula," + | |||
" massa at pharetra fringilla, risus justo faucibus erat, nec porttitor ni | " massa at pharetra fringilla, risus justo faucibus erat, nec porttitor | |||
bh tellus" + | nibh tellus" + | |||
" sed est. Ut justo diam, lobortis eu tris. In ut magna vel mauris malesua | " sed est. Ut justo diam, lobortis eu tris. In ut magna vel mauris males | |||
da dictum." + | uada dictum." + | |||
" Nulla ullamcorper metus quis diam cursus facilisis. Sed mollis quam id j | " Nulla ullamcorper metus quis diam cursus facilisis. Sed mollis quam id | |||
usto rutrum" + | justo rutrum" + | |||
" sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum sit | " sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum s | |||
amet." + | it amet." + | |||
" Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat, eget | " Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat, eg | |||
" + | et" + | |||
" hendrerit felis turpis nec lorem.", false, 31); | " hendrerit felis turpis nec lorem.", false, 31); | |||
} | } | |||
@Test | @Test | |||
public void testGenerateModeMessage() { | public void testGenerateModeMessage() { | |||
testModeMessage(true, 2, 29, ".X .XXX.. ...X XX.. ..X .XX. .XX.X"); | testModeMessage(true, 2, 29, ".X .XXX.. ...X XX.. ..X .XX. .XX.X"); | |||
testModeMessage(true, 4, 64, "XX XXXXXX .X.. ...X ..XX .X.. XX.."); | testModeMessage(true, 4, 64, "XX XXXXXX .X.. ...X ..XX .X.. XX.."); | |||
testModeMessage(false, 21, 660, "X.X.. .X.X..X..XX .XXX ..X.. .XXX. .X... . .XXX"); | testModeMessage(false, 21, 660, "X.X.. .X.X..X..XX .XXX ..X.. .XXX. .X... . .XXX"); | |||
testModeMessage(false, 32, 4096, "XXXXX XXXXXXXXXXX X.X. ..... XXX.X ..X.. X .XXX"); | testModeMessage(false, 32, 4096, "XXXXX XXXXXXXXXXX X.X. ..... XXX.X ..X.. X .XXX"); | |||
} | } | |||
skipping to change at line 280 | skipping to change at line 289 | |||
testStuffBits(6, ".X.X.. ...... ...... ..X.X.", | testStuffBits(6, ".X.X.. ...... ...... ..X.X.", | |||
".X.X.. .....X .....X ....X. X.XXXX"); | ".X.X.. .....X .....X ....X. X.XXXX"); | |||
testStuffBits(6, ".X.X.. XXXXXX ...... ..X.XX", | testStuffBits(6, ".X.X.. XXXXXX ...... ..X.XX", | |||
".X.X.. XXXXX. X..... ...X.X XXXXX."); | ".X.X.. XXXXX. X..... ...X.X XXXXX."); | |||
testStuffBits(6, | testStuffBits(6, | |||
"...... ..XXXX X..XX. .X.... .X.X.X .....X .X.... ...X.X .....X ....XX . .X... ....X. X..XXX X.XX.X", | "...... ..XXXX X..XX. .X.... .X.X.X .....X .X.... ...X.X .....X ....XX . .X... ....X. X..XXX X.XX.X", | |||
".....X ...XXX XX..XX ..X... ..X.X. X..... X.X... ....X. X..... X....X X ..X.. .....X X.X..X XXX.XX .XXXXX"); | ".....X ...XXX XX..XX ..X... ..X.X. X..... X.X... ....X. X..... X....X X ..X.. .....X X.X..X XXX.XX .XXXXX"); | |||
} | } | |||
@Test | @Test | |||
public void testHighLevelEncode() { | public void testHighLevelEncode() throws FormatException { | |||
testHighLevelEncodeString("A. b.", | testHighLevelEncodeString("A. b.", | |||
// 'A' P/S '. ' L/L b D/L '.' | // 'A' P/S '. ' L/L b D/L '.' | |||
"...X. ..... ...XX XXX.. ...XX XXXX. XX.X"); | "...X. ..... ...XX XXX.. ...XX XXXX. XX.X"); | |||
testHighLevelEncodeString("Lorem ipsum.", | testHighLevelEncodeString("Lorem ipsum.", | |||
// 'L' L/L 'o' 'r' 'e' 'm' ' ' 'i' 'p' 's' 'u' 'm' D/L '.' | // 'L' L/L 'o' 'r' 'e' 'm' ' ' 'i' 'p' 's' 'u' 'm' D/L '.' | |||
".XX.X XXX.. X.... X..XX ..XX. .XXX. ....X .X.X. X...X X.X.. X.XX. .XXX. XXXX. XX.X"); | ".XX.X XXX.. X.... X..XX ..XX. .XXX. ....X .X.X. X...X X.X.. X.XX. .XXX. XXXX. XX.X"); | |||
testHighLevelEncodeString("Lo. Test 123.", | testHighLevelEncodeString("Lo. Test 123.", | |||
// 'L' L/L 'o' P/S '. ' U/S 'T' 'e' 's' 't' D/L ' ' '1' '2' '3' '.' | // 'L' L/L 'o' P/S '. ' U/S 'T' 'e' 's' 't' D/L ' ' '1' '2' '3' '.' | |||
".XX.X XXX.. X.... ..... ...XX XXX.. X.X.X ..XX. X.X.. X.X.X XXXX. ...X ..XX .X.. .X.X XX.X"); | ".XX.X XXX.. X.... ..... ...XX XXX.. X.X.X ..XX. X.X.. X.X.X XXXX. ...X ..XX .X.. .X.X XX.X"); | |||
testHighLevelEncodeString("Lo...x", | testHighLevelEncodeString("Lo...x", | |||
skipping to change at line 310 | skipping to change at line 319 | |||
testHighLevelEncodeString( | testHighLevelEncodeString( | |||
// Found on an airline boarding pass. Several stretches of Binary shift are | // Found on an airline boarding pass. Several stretches of Binary shift are | |||
// necessary to keep the bitcount so low. | // necessary to keep the bitcount so low. | |||
"09 UAG ^160MEUCIQC0sYS/HpKxnBELR1uB85R20OoqqwFGa0q2uEi" | "09 UAG ^160MEUCIQC0sYS/HpKxnBELR1uB85R20OoqqwFGa0q2uEi" | |||
+ "Ygh6utAIgLl1aBVM4EOTQtMQQYH9M2Z3Dp4qnA/fwWuQ+M8L3V8U=", | + "Ygh6utAIgLl1aBVM4EOTQtMQQYH9M2Z3Dp4qnA/fwWuQ+M8L3V8U=", | |||
823); | 823); | |||
} | } | |||
@Test | @Test | |||
public void testHighLevelEncodeBinary() { | public void testHighLevelEncodeBinary() throws FormatException { | |||
// binary short form single byte | // binary short form single byte | |||
testHighLevelEncodeString("N\0N", | testHighLevelEncodeString("N\0N", | |||
// 'N' B/S =1 '\0' N | // 'N' B/S =1 '\0' N | |||
".XXXX XXXXX ....X ........ .XXXX"); // Encode "N" in UPPER | ".XXXX XXXXX ....X ........ .XXXX"); // Encode "N" in UPPER | |||
testHighLevelEncodeString("N\0n", | testHighLevelEncodeString("N\0n", | |||
// 'N' B/S =2 '\0' 'n' | // 'N' B/S =2 '\0' 'n' | |||
".XXXX XXXXX ...X. ........ .XX.XXX."); // Encode "n" in BINARY | ".XXXX XXXXX ...X. ........ .XX.XXX."); // Encode "n" in BINARY | |||
// binary short form consecutive bytes | // binary short form consecutive bytes | |||
skipping to change at line 397 | skipping to change at line 406 | |||
sb = new StringBuilder(); | sb = new StringBuilder(); | |||
for (int i = 0; i < 64; i++) { | for (int i = 0; i < 64; i++) { | |||
sb.append('§'); | sb.append('§'); | |||
} | } | |||
sb.setCharAt(30, 'A'); | sb.setCharAt(30, 'A'); | |||
// expect B/S(64) | // expect B/S(64) | |||
testHighLevelEncodeString(sb.toString(), 21 + 64 * 8); | testHighLevelEncodeString(sb.toString(), 21 + 64 * 8); | |||
} | } | |||
@Test | @Test | |||
public void testHighLevelEncodePairs() { | public void testHighLevelEncodePairs() throws FormatException { | |||
// Typical usage | // Typical usage | |||
testHighLevelEncodeString("ABC. DEF\r\n", | testHighLevelEncodeString("ABC. DEF\r\n", | |||
// A B C P/S .<sp> D E F P/S \r\n | // A B C P/S .<sp> D E F P/S \r\n | |||
"...X. ...XX ..X.. ..... ...XX ..X.X ..XX. ..XXX ..... ...X."); | "...X. ...XX ..X.. ..... ...XX ..X.X ..XX. ..XXX ..... ...X."); | |||
// We should latch to PUNCT mode, rather than shift. Also check all pairs | // We should latch to PUNCT mode, rather than shift. Also check all pairs | |||
testHighLevelEncodeString("A. : , \r\n", | testHighLevelEncodeString("A. : , \r\n", | |||
// 'A' M/L P/L ". " ": " ", " "\r\n" | // 'A' M/L P/L ". " ": " ", " "\r\n" | |||
"...X. XXX.X XXXX. ...XX ..X.X ..X.. ...X."); | "...X. XXX.X XXXX. ...XX ..X.X ..X.. ...X."); | |||
// Latch to DIGIT rather than shift to PUNCT | // Latch to DIGIT rather than shift to PUNCT | |||
testHighLevelEncodeString("A. 1234", | testHighLevelEncodeString("A. 1234", | |||
// 'A' D/L '.' ' ' '1' '2' '3' '4' | // 'A' D/L '.' ' ' '1' '2' '3' '4' | |||
"...X. XXXX. XX.X ...X ..XX .X.. .X.X .X X." | "...X. XXXX. XX.X ...X ..XX .X.. .X.X .X X."); | |||
); | ||||
// Don't bother leaving Binary Shift. | // Don't bother leaving Binary Shift. | |||
testHighLevelEncodeString("A\200. \200", | testHighLevelEncodeString("A\200. \200", | |||
// 'A' B/S =2 \200 "." " " \200 | // 'A' B/S =2 \200 "." " " \200 | |||
"...X. XXXXX ..X.. X....... ..X.XXX. ..X..... X......."); | "...X. XXXXX ..X.. X....... ..X.XXX. ..X..... X......."); | |||
} | } | |||
@Test | @Test(expected = IllegalArgumentException.class) | |||
public void testUserSpecifiedLayers() { | public void testUserSpecifiedLayers() { | |||
byte[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(StandardCharsets.ISO | doTestUserSpecifiedLayers(33); | |||
_8859_1); | } | |||
@Test(expected = IllegalArgumentException.class) | ||||
public void testUserSpecifiedLayers2() { | ||||
doTestUserSpecifiedLayers(-1); | ||||
} | ||||
private void doTestUserSpecifiedLayers(int userSpecifiedLayers) { | ||||
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||||
AztecCode aztec = Encoder.encode(alphabet, 25, -2); | AztecCode aztec = Encoder.encode(alphabet, 25, -2); | |||
assertEquals(2, aztec.getLayers()); | assertEquals(2, aztec.getLayers()); | |||
assertTrue(aztec.isCompact()); | assertTrue(aztec.isCompact()); | |||
aztec = Encoder.encode(alphabet, 25, 32); | aztec = Encoder.encode(alphabet, 25, 32); | |||
assertEquals(32, aztec.getLayers()); | assertEquals(32, aztec.getLayers()); | |||
assertFalse(aztec.isCompact()); | assertFalse(aztec.isCompact()); | |||
try { | Encoder.encode(alphabet, 25, userSpecifiedLayers); | |||
Encoder.encode(alphabet, 25, 33); | } | |||
fail("Encode should have failed. No such thing as 33 layers"); | ||||
} catch (IllegalArgumentException expected) { | ||||
// continue | ||||
} | ||||
try { | @Test(expected = IllegalArgumentException.class) | |||
Encoder.encode(alphabet, 25, -1); | public void testBorderCompact4CaseFailed() { | |||
fail("Encode should have failed. Text can't fit in 1-layer compact"); | // Compact(4) con hold 608 bits of information, but at most 504 can be data. | |||
} catch (IllegalArgumentException expected) { | Rest must | |||
// continue | // be error correction | |||
} | String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |||
// encodes as 26 * 5 * 4 = 520 bits of data | ||||
String alphabet4 = alphabet + alphabet + alphabet + alphabet; | ||||
Encoder.encode(alphabet4, 0, -4); | ||||
} | } | |||
@Test | @Test | |||
public void testBorderCompact4Case() { | public void testBorderCompact4Case() { | |||
// Compact(4) con hold 608 bits of information, but at most 504 can be data. Rest must | // Compact(4) con hold 608 bits of information, but at most 504 can be data. Rest must | |||
// be error correction | // be error correction | |||
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |||
// encodes as 26 * 5 * 4 = 520 bits of data | // encodes as 26 * 5 * 4 = 520 bits of data | |||
String alphabet4 = alphabet + alphabet + alphabet + alphabet; | String alphabet4 = alphabet + alphabet + alphabet + alphabet; | |||
byte[] data = alphabet4.getBytes(StandardCharsets.ISO_8859_1); | ||||
try { | ||||
Encoder.encode(data, 0, -4); | ||||
fail("Encode should have failed. Text can't fit in 1-layer compact"); | ||||
} catch (IllegalArgumentException expected) { | ||||
// continue | ||||
} | ||||
// If we just try to encode it normally, it will go to a non-compact 4 layer | // If we just try to encode it normally, it will go to a non-compact 4 layer | |||
AztecCode aztecCode = Encoder.encode(data, 0, Encoder.DEFAULT_AZTEC_LAYERS); | AztecCode aztecCode = Encoder.encode(alphabet4, 0, Encoder.DEFAULT_AZTEC_LAY ERS); | |||
assertFalse(aztecCode.isCompact()); | assertFalse(aztecCode.isCompact()); | |||
assertEquals(4, aztecCode.getLayers()); | assertEquals(4, aztecCode.getLayers()); | |||
// But shortening the string to 100 bytes (500 bits of data), compact works fine, even if we | // But shortening the string to 100 bytes (500 bits of data), compact works fine, even if we | |||
// include more error checking. | // include more error checking. | |||
aztecCode = Encoder.encode(alphabet4.substring(0, 100).getBytes(StandardChar sets.ISO_8859_1), 10, Encoder.DEFAULT_AZTEC_LAYERS); | aztecCode = Encoder.encode(alphabet4.substring(0, 100), 10, Encoder.DEFAULT_ AZTEC_LAYERS); | |||
assertTrue(aztecCode.isCompact()); | assertTrue(aztecCode.isCompact()); | |||
assertEquals(4, aztecCode.getLayers()); | assertEquals(4, aztecCode.getLayers()); | |||
} | } | |||
// Helper routines | // Helper routines | |||
private static void testEncode(String data, boolean compact, int layers, Strin g expected) { | private static void testEncode(String data, boolean compact, int layers, Strin g expected) { | |||
AztecCode aztec = Encoder.encode(data.getBytes(StandardCharsets.ISO_8859_1), 33, Encoder.DEFAULT_AZTEC_LAYERS); | AztecCode aztec = Encoder.encode(data, 33, Encoder.DEFAULT_AZTEC_LAYERS); | |||
assertEquals("Unexpected symbol format (compact)", compact, aztec.isCompact( )); | assertEquals("Unexpected symbol format (compact)", compact, aztec.isCompact( )); | |||
assertEquals("Unexpected nr. of layers", layers, aztec.getLayers()); | assertEquals("Unexpected nr. of layers", layers, aztec.getLayers()); | |||
BitMatrix matrix = aztec.getMatrix(); | BitMatrix matrix = aztec.getMatrix(); | |||
assertEquals("encode() failed", expected, matrix.toString()); | assertEquals("encode() failed", expected, matrix.toString()); | |||
} | } | |||
private static void testEncodeDecode(String data, boolean compact, int layers) throws Exception { | private static void testEncodeDecode(String data, boolean compact, int layers) throws Exception { | |||
AztecCode aztec = Encoder.encode(data.getBytes(StandardCharsets.ISO_8859_1), 25, Encoder.DEFAULT_AZTEC_LAYERS); | AztecCode aztec = Encoder.encode(data, 25, Encoder.DEFAULT_AZTEC_LAYERS); | |||
assertEquals("Unexpected symbol format (compact)", compact, aztec.isCompact( )); | assertEquals("Unexpected symbol format (compact)", compact, aztec.isCompact( )); | |||
assertEquals("Unexpected nr. of layers", layers, aztec.getLayers()); | assertEquals("Unexpected nr. of layers", layers, aztec.getLayers()); | |||
BitMatrix matrix = aztec.getMatrix(); | BitMatrix matrix = aztec.getMatrix(); | |||
AztecDetectorResult r = | AztecDetectorResult r = | |||
new AztecDetectorResult(matrix, NO_POINTS, aztec.isCompact(), aztec.getC odeWords(), aztec.getLayers()); | new AztecDetectorResult(matrix, NO_POINTS, aztec.isCompact(), aztec.getC odeWords(), aztec.getLayers()); | |||
DecoderResult res = new Decoder().decode(r); | DecoderResult res = new Decoder().decode(r); | |||
assertEquals(data, res.getText()); | assertEquals(data, res.getText()); | |||
// Check error correction by introducing a few minor errors | // Check error correction by introducing a few minor errors | |||
Random random = getPseudoRandom(); | Random random = getPseudoRandom(); | |||
matrix.flip(random.nextInt(matrix.getWidth()), random.nextInt(2)); | matrix.flip(random.nextInt(matrix.getWidth()), random.nextInt(2)); | |||
matrix.flip(random.nextInt(matrix.getWidth()), matrix.getHeight() - 2 + rand om.nextInt(2)); | matrix.flip(random.nextInt(matrix.getWidth()), matrix.getHeight() - 2 + rand om.nextInt(2)); | |||
matrix.flip(random.nextInt(2), random.nextInt(matrix.getHeight())); | matrix.flip(random.nextInt(2), random.nextInt(matrix.getHeight())); | |||
matrix.flip(matrix.getWidth() - 2 + random.nextInt(2), random.nextInt(matrix .getHeight())); | matrix.flip(matrix.getWidth() - 2 + random.nextInt(2), random.nextInt(matrix .getHeight())); | |||
r = new AztecDetectorResult(matrix, NO_POINTS, aztec.isCompact(), aztec.getC odeWords(), aztec.getLayers()); | r = new AztecDetectorResult(matrix, NO_POINTS, aztec.isCompact(), aztec.getC odeWords(), aztec.getLayers()); | |||
res = new Decoder().decode(r); | res = new Decoder().decode(r); | |||
assertEquals(data, res.getText()); | assertEquals(data, res.getText()); | |||
} | } | |||
private static void testWriter(String data, | private static void testWriter(String data, | |||
String charset, | Charset charset, | |||
int eccPercent, | int eccPercent, | |||
boolean compact, | boolean compact, | |||
int layers) throws FormatException { | int layers) throws FormatException { | |||
// 1. Perform an encode-decode round-trip because it can be lossy. | // Perform an encode-decode round-trip because it can be lossy. | |||
// 2. Aztec Decoder currently always decodes the data with a LATIN-1 charset | ||||
: | ||||
String expectedData = new String(data.getBytes(Charset.forName(charset)), St | ||||
andardCharsets.ISO_8859_1); | ||||
Map<EncodeHintType,Object> hints = new EnumMap<>(EncodeHintType.class); | Map<EncodeHintType,Object> hints = new EnumMap<>(EncodeHintType.class); | |||
hints.put(EncodeHintType.CHARACTER_SET, charset); | if (null != charset) { | |||
hints.put(EncodeHintType.CHARACTER_SET, charset.name()); | ||||
} | ||||
hints.put(EncodeHintType.ERROR_CORRECTION, eccPercent); | hints.put(EncodeHintType.ERROR_CORRECTION, eccPercent); | |||
AztecWriter writer = new AztecWriter(); | AztecWriter writer = new AztecWriter(); | |||
BitMatrix matrix = writer.encode(data, BarcodeFormat.AZTEC, 0, 0, hints); | BitMatrix matrix = writer.encode(data, BarcodeFormat.AZTEC, 0, 0, hints); | |||
AztecCode aztec = Encoder.encode(data.getBytes(Charset.forName(charset)), ec | AztecCode aztec = Encoder.encode(data, eccPercent, | |||
cPercent, | Encoder.DEFAULT_AZTEC_LAYERS, charset); | |||
Encoder.DEFAULT_AZTEC_LAYERS); | ||||
assertEquals("Unexpected symbol format (compact)", compact, aztec.isCompact( )); | assertEquals("Unexpected symbol format (compact)", compact, aztec.isCompact( )); | |||
assertEquals("Unexpected nr. of layers", layers, aztec.getLayers()); | assertEquals("Unexpected nr. of layers", layers, aztec.getLayers()); | |||
BitMatrix matrix2 = aztec.getMatrix(); | BitMatrix matrix2 = aztec.getMatrix(); | |||
assertEquals(matrix, matrix2); | assertEquals(matrix, matrix2); | |||
AztecDetectorResult r = | AztecDetectorResult r = | |||
new AztecDetectorResult(matrix, NO_POINTS, aztec.isCompact(), aztec.getC odeWords(), aztec.getLayers()); | new AztecDetectorResult(matrix, NO_POINTS, aztec.isCompact(), aztec.getC odeWords(), aztec.getLayers()); | |||
DecoderResult res = new Decoder().decode(r); | DecoderResult res = new Decoder().decode(r); | |||
assertEquals(expectedData, res.getText()); | assertEquals(data, res.getText()); | |||
// Check error correction by introducing up to eccPercent/2 errors | // Check error correction by introducing up to eccPercent/2 errors | |||
int ecWords = aztec.getCodeWords() * eccPercent / 100 / 2; | int ecWords = aztec.getCodeWords() * eccPercent / 100 / 2; | |||
Random random = getPseudoRandom(); | Random random = getPseudoRandom(); | |||
for (int i = 0; i < ecWords; i++) { | for (int i = 0; i < ecWords; i++) { | |||
// don't touch the core | // don't touch the core | |||
int x = random.nextBoolean() ? | int x = random.nextBoolean() ? | |||
random.nextInt(aztec.getLayers() * 2) | random.nextInt(aztec.getLayers() * 2) | |||
: matrix.getWidth() - 1 - random.nextInt(aztec.getLayers() * 2); | : matrix.getWidth() - 1 - random.nextInt(aztec.getLayers() * 2); | |||
int y = random.nextBoolean() ? | int y = random.nextBoolean() ? | |||
random.nextInt(aztec.getLayers() * 2) | random.nextInt(aztec.getLayers() * 2) | |||
: matrix.getHeight() - 1 - random.nextInt(aztec.getLayers() * 2) ; | : matrix.getHeight() - 1 - random.nextInt(aztec.getLayers() * 2) ; | |||
matrix.flip(x, y); | matrix.flip(x, y); | |||
} | } | |||
r = new AztecDetectorResult(matrix, NO_POINTS, aztec.isCompact(), aztec.getC odeWords(), aztec.getLayers()); | r = new AztecDetectorResult(matrix, NO_POINTS, aztec.isCompact(), aztec.getC odeWords(), aztec.getLayers()); | |||
res = new Decoder().decode(r); | res = new Decoder().decode(r); | |||
assertEquals(expectedData, res.getText()); | assertEquals(data, res.getText()); | |||
} | } | |||
private static Random getPseudoRandom() { | private static Random getPseudoRandom() { | |||
return new Random(0xDEADBEEF); | return new Random(0xDEADBEEF); | |||
} | } | |||
private static void testModeMessage(boolean compact, int layers, int words, St ring expected) { | private static void testModeMessage(boolean compact, int layers, int words, St ring expected) { | |||
BitArray in = Encoder.generateModeMessage(compact, layers, words); | BitArray in = Encoder.generateModeMessage(compact, layers, words); | |||
assertEquals("generateModeMessage() failed", stripSpace(expected), stripSpac e(in.toString())); | assertEquals("generateModeMessage() failed", stripSpace(expected), stripSpac e(in.toString())); | |||
} | } | |||
private static void testStuffBits(int wordSize, String bits, String expected) { | private static void testStuffBits(int wordSize, String bits, String expected) { | |||
BitArray in = toBitArray(bits); | BitArray in = toBitArray(bits); | |||
BitArray stuffed = Encoder.stuffBits(in, wordSize); | BitArray stuffed = Encoder.stuffBits(in, wordSize); | |||
assertEquals("stuffBits() failed for input string: " + bits, | assertEquals("stuffBits() failed for input string: " + bits, | |||
stripSpace(expected), stripSpace(stuffed.toString())); | stripSpace(expected), stripSpace(stuffed.toString())); | |||
} | } | |||
private static BitArray toBitArray(CharSequence bits) { | public static BitArray toBitArray(CharSequence bits) { | |||
BitArray in = new BitArray(); | BitArray in = new BitArray(); | |||
char[] str = DOTX.matcher(bits).replaceAll("").toCharArray(); | char[] str = DOTX.matcher(bits).replaceAll("").toCharArray(); | |||
for (char aStr : str) { | for (char aStr : str) { | |||
in.appendBit(aStr == 'X'); | in.appendBit(aStr == 'X'); | |||
} | } | |||
return in; | return in; | |||
} | } | |||
private static boolean[] toBooleanArray(BitArray bitArray) { | public static boolean[] toBooleanArray(BitArray bitArray) { | |||
boolean[] result = new boolean[bitArray.getSize()]; | boolean[] result = new boolean[bitArray.getSize()]; | |||
for (int i = 0; i < result.length; i++) { | for (int i = 0; i < result.length; i++) { | |||
result[i] = bitArray.get(i); | result[i] = bitArray.get(i); | |||
} | } | |||
return result; | return result; | |||
} | } | |||
private static void testHighLevelEncodeString(String s, String expectedBits) { | private static void testHighLevelEncodeString(String s, String expectedBits) t hrows FormatException { | |||
BitArray bits = new HighLevelEncoder(s.getBytes(StandardCharsets.ISO_8859_1) ).encode(); | BitArray bits = new HighLevelEncoder(s.getBytes(StandardCharsets.ISO_8859_1) ).encode(); | |||
String receivedBits = stripSpace(bits.toString()); | String receivedBits = stripSpace(bits.toString()); | |||
assertEquals("highLevelEncode() failed for input string: " + s, stripSpace(e xpectedBits), receivedBits); | assertEquals("highLevelEncode() failed for input string: " + s, stripSpace(e xpectedBits), receivedBits); | |||
assertEquals(s, Decoder.highLevelDecode(toBooleanArray(bits))); | assertEquals(s, Decoder.highLevelDecode(toBooleanArray(bits))); | |||
} | } | |||
private static void testHighLevelEncodeString(String s, int expectedReceivedBi ts) { | private static void testHighLevelEncodeString(String s, int expectedReceivedBi ts) throws FormatException { | |||
BitArray bits = new HighLevelEncoder(s.getBytes(StandardCharsets.ISO_8859_1) ).encode(); | BitArray bits = new HighLevelEncoder(s.getBytes(StandardCharsets.ISO_8859_1) ).encode(); | |||
int receivedBitCount = stripSpace(bits.toString()).length(); | int receivedBitCount = stripSpace(bits.toString()).length(); | |||
assertEquals("highLevelEncode() failed for input string: " + s, | assertEquals("highLevelEncode() failed for input string: " + s, | |||
expectedReceivedBits, receivedBitCount); | expectedReceivedBits, receivedBitCount); | |||
assertEquals(s, Decoder.highLevelDecode(toBooleanArray(bits))); | assertEquals(s, Decoder.highLevelDecode(toBooleanArray(bits))); | |||
} | } | |||
private static String stripSpace(String s) { | public static String stripSpace(String s) { | |||
return SPACES.matcher(s).replaceAll(""); | return SPACES.matcher(s).replaceAll(""); | |||
} | } | |||
} | } | |||
End of changes. 28 change blocks. | ||||
126 lines changed or deleted | 135 lines changed or added |