diff options
| author | AlexGeller1 <87009702+AlexGeller1@users.noreply.github.com> | 2022-03-02 17:27:40 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-02 17:27:40 +0300 |
| commit | 9460f0620a24f36ac90670da39b7e7efa3092cce (patch) | |
| tree | b131afb4950b62ad1deb003402be9ad7251af497 /core/src/test/java | |
| parent | feed14328ee5441c957063af7b93f8569370b213 (diff) | |
| download | zxing-9460f0620a24f36ac90670da39b7e7efa3092cce.tar.xz | |
Adding more PDF417 decoding tests (#1504)
* Added more PDF417 decoding tests
Diffstat (limited to 'core/src/test/java')
| -rw-r--r-- | core/src/test/java/com/google/zxing/pdf417/decoder/PDF417DecoderTestCase.java | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/core/src/test/java/com/google/zxing/pdf417/decoder/PDF417DecoderTestCase.java b/core/src/test/java/com/google/zxing/pdf417/decoder/PDF417DecoderTestCase.java index 50cc9e48f..5f6ac8c08 100644 --- a/core/src/test/java/com/google/zxing/pdf417/decoder/PDF417DecoderTestCase.java +++ b/core/src/test/java/com/google/zxing/pdf417/decoder/PDF417DecoderTestCase.java @@ -17,10 +17,16 @@ package com.google.zxing.pdf417.decoder; import com.google.zxing.FormatException; +import com.google.zxing.WriterException; import com.google.zxing.pdf417.PDF417ResultMetadata; import com.google.zxing.common.DecoderResult; +import com.google.zxing.pdf417.encoder.Compaction; +import com.google.zxing.pdf417.encoder.PDF417HighLevelEncoderTestAdapter; + import org.junit.Assert; import org.junit.Test; +import java.nio.charset.StandardCharsets; +import java.util.Random; /** * Tests {@link DecodedBitStreamParser}. @@ -175,4 +181,161 @@ public class PDF417DecoderTestCase extends Assert { DecodedBitStreamParser.decode(sampleCodes, "0"); } + @Test + public void testUppercase() throws WriterException, FormatException { + //encodeDecode("", 0); + performEncodeTest('A', new int[] { 3, 4, 5, 6, 4, 4, 5, 5}); + } + + @Test + public void testNumeric() throws WriterException, FormatException { + performEncodeTest('1', new int[] { 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10}); + } + + @Test + public void testByte() throws WriterException, FormatException { + performEncodeTest('\u00c4', new int[] { 3, 4, 5, 6, 7, 7, 8}); + } + + @Test + public void testUppercaseLowercaseMix1() throws WriterException, FormatException { + encodeDecode("aA", 4); + encodeDecode("aAa", 5); + encodeDecode("Aa", 4); + encodeDecode("Aaa", 5); + encodeDecode("AaA", 5); + encodeDecode("AaaA", 6); + encodeDecode("Aaaa", 6); + encodeDecode("AaAaA", 5); + encodeDecode("AaaAaaA", 6); + encodeDecode("AaaAAaaA", 7); + } + + @Test + public void testPunctuation() throws WriterException, FormatException { + performEncodeTest(';', new int[] { 3, 4, 5, 6, 6, 7, 8}); + encodeDecode(";;;;;;;;;;;;;;;;", 17); + } + + @Test + public void testUppercaseLowercaseMix2() throws WriterException, FormatException { + performPermutationTest(new char[] {'A', 'a'}, 10, 8972); + } + + @Test + public void testUppercaseNumericMix() throws WriterException, FormatException { + performPermutationTest(new char[] {'A', '1'}, 14, 192510); + } + + @Test + public void testUppercaseMixedMix() throws WriterException, FormatException { + performPermutationTest(new char[] {'A', '1', ' ', ';'}, 7, 106060); + } + + @Test + public void testUppercasePunctuationMix() throws WriterException, FormatException { + performPermutationTest(new char[] {'A', ';'}, 10, 8967); + } + + @Test + public void testUppercaseByteMix() throws WriterException, FormatException { + performPermutationTest(new char[] {'A', '\u00c4'}, 10, 11222); + } + + @Test + public void testLowercaseByteMix() throws WriterException, FormatException { + performPermutationTest(new char[] {'a', '\u00c4'}, 10, 11233); + } + + public void testUppercaseLowercaseNumericMix() throws WriterException, FormatException { + performPermutationTest(new char[] {'A', 'a', '1'}, 7, 15491); + } + + @Test + public void testUppercaseLowercasePunctuationMix() throws WriterException, FormatException { + performPermutationTest(new char[] {'A', 'a', ';'}, 7, 15491); + } + + @Test + public void testUppercaseLowercaseByteMix() throws WriterException, FormatException { + performPermutationTest(new char[] {'A', 'a', '\u00c4'}, 7, 17288); + } + + @Test + public void testLowercasePunctuationByteMix() throws WriterException, FormatException { + performPermutationTest(new char[] {'a', ';', '\u00c4'}, 7, 17427); + } + + @Test + public void testUppercaseLowercaseNumericPunctuationMix() throws WriterException, FormatException { + performPermutationTest(new char[] {'A', 'a', '1', ';'}, 7, 120479); + } + + @Test + public void testBinaryData() throws WriterException, FormatException { + byte[] bytes = new byte[500]; + Random random = new Random(0); + int total = 0; + for (int i = 0; i < 10000; i++) { + random.nextBytes(bytes); + total += encodeDecode(new String(bytes, StandardCharsets.ISO_8859_1)); + } + assertEquals(4190044, total); + } + + private static void encodeDecode(String input, int expectedLength) throws WriterException, FormatException { + assertEquals(expectedLength, encodeDecode(input)); + } + + private static int encodeDecode(String input) throws WriterException, FormatException { + String s = PDF417HighLevelEncoderTestAdapter.encodeHighLevel(input, Compaction.AUTO, null); + int[] codewords = new int[s.length() + 1]; + codewords[0] = codewords.length; + for (int i = 1; i < codewords.length; i++) { + codewords[i] = s.charAt(i - 1); + } + DecoderResult result = DecodedBitStreamParser.decode(codewords, "0"); + + assertEquals(input, result.getText()); + return codewords.length; + } + + private static int getEndIndex(int length, char[] chars) { + double decimalLength = Math.log10(chars.length); + return (int) Math.ceil(Math.pow(10, decimalLength * length)); + } + + private static String generatePermutation(int index, int length, char[] chars) { + int N = chars.length; + String baseNNumber = Integer.toString(index, N); + while (baseNNumber.length() < length) { + baseNNumber = "0" + baseNNumber; + } + String prefix = ""; + for (int i = 0; i < baseNNumber.length(); i++) { + prefix += chars[baseNNumber.charAt(i) - '0']; + } + return prefix; + } + + private static void performPermutationTest(char[] chars, int length, int expectedTotal) throws WriterException, + FormatException { + int endIndex = getEndIndex(length, chars); + int total = 0; + for (int i = 0; i < endIndex; i++) { + total += encodeDecode(generatePermutation(i, length, chars)); + } + assertEquals(expectedTotal, total); + } + + private static void performEncodeTest(char c, int[] expectedLengths) throws WriterException, FormatException { + for (int i = 0; i < expectedLengths.length; i++) { + StringBuilder sb = new StringBuilder(); + for (int j = 0; j <= i; j++) { + sb.append(c); + } + encodeDecode(sb.toString(), expectedLengths[i]); + } + } + } |
