Another attack on integrating encoder and decoder: Version is done. Attempted to...
[zxing.git] / core / test / src / com / google / zxing / qrcode / encoder / EncoderTestCase.java
index d189798..ca3ac1f 100644 (file)
@@ -22,13 +22,15 @@ import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 import com.google.zxing.qrcode.decoder.Mode;
 import junit.framework.TestCase;
 
+import java.io.UnsupportedEncodingException;
+
 /**
  * @author satorux@google.com (Satoru Takabayashi) - creator
  * @author mysen@google.com (Chris Mysen) - ported from C++
  */
 public final class EncoderTestCase extends TestCase {
 
-  public void testGetAlphanumericCode() throws WriterException {
+  public void testGetAlphanumericCode() {
     // The first ten code points are numbers.
     for (int i = 0; i < 10; ++i) {
       assertEquals(i, Encoder.getAlphanumericCode('0' + i));
@@ -58,36 +60,33 @@ public final class EncoderTestCase extends TestCase {
 
   public void testChooseMode() throws WriterException {
     // Numeric mode.
-    assertEquals(Mode.NUMERIC, Encoder.chooseMode(new ByteArray("0")));
-    assertEquals(Mode.NUMERIC, Encoder.chooseMode(new ByteArray("0123456789")));
+    assertEquals(Mode.NUMERIC, Encoder.chooseMode("0"));
+    assertEquals(Mode.NUMERIC, Encoder.chooseMode("0123456789"));
     // Alphanumeric mode.
-    assertEquals(Mode.ALPHANUMERIC, Encoder.chooseMode(new ByteArray("A")));
+    assertEquals(Mode.ALPHANUMERIC, Encoder.chooseMode("A"));
     assertEquals(Mode.ALPHANUMERIC,
-        Encoder.chooseMode(new ByteArray("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:")));
+        Encoder.chooseMode("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"));
     // 8-bit byte mode.
-    assertEquals(Mode.BYTE, Encoder.chooseMode(new ByteArray("a")));
-    assertEquals(Mode.BYTE, Encoder.chooseMode(new ByteArray("#")));
-    assertEquals(Mode.BYTE, Encoder.chooseMode(new ByteArray("")));
+    assertEquals(Mode.BYTE, Encoder.chooseMode("a"));
+    assertEquals(Mode.BYTE, Encoder.chooseMode("#"));
+    assertEquals(Mode.BYTE, Encoder.chooseMode(""));
     // Kanji mode.  We used to use MODE_KANJI for these, but we stopped
     // doing that as we cannot distinguish Shift_JIS from other encodings
     // from data bytes alone.  See also comments in qrcode_encoder.h.
 
     // AIUE in Hiragana in Shift_JIS
-    byte[] dat1 = {0x8,0xa,0x8,0xa,0x8,0xa,0x8,(byte)0xa6};
-    assertEquals(Mode.BYTE, Encoder.chooseMode(new ByteArray(dat1)));
+    assertEquals(Mode.BYTE, Encoder.chooseMode(shiftJISString(new byte[] {0x8,0xa,0x8,0xa,0x8,0xa,0x8,(byte)0xa6})));
 
     // Nihon in Kanji in Shift_JIS.
-    byte[] dat2 = {0x9,0xf,0x9,0x7b};
-    assertEquals(Mode.BYTE, Encoder.chooseMode(new ByteArray(dat2)));
+    assertEquals(Mode.BYTE, Encoder.chooseMode(shiftJISString(new byte[] {0x9,0xf,0x9,0x7b})));
 
     // Sou-Utsu-Byou in Kanji in Shift_JIS.
-    byte[] dat3 = {0xe,0x4,0x9,0x5,0x9,0x61};
-    assertEquals(Mode.BYTE, Encoder.chooseMode(new ByteArray(dat3)));
+    assertEquals(Mode.BYTE, Encoder.chooseMode(shiftJISString(new byte[] {0xe,0x4,0x9,0x5,0x9,0x61})));
   }
 
   public void testEncode() throws WriterException {
     QRCode qrCode = new QRCode();
-    Encoder.encode(new ByteArray("ABCDEF"), ErrorCorrectionLevel.H, qrCode);
+    Encoder.encode("ABCDEF", ErrorCorrectionLevel.H, qrCode);
     // The following is a valid QR Code that can be read by cell phones.
     String expected =
       "<<\n" +
@@ -159,7 +158,7 @@ public final class EncoderTestCase extends TestCase {
     }
     {
       BitVector bits = new BitVector();
-      Encoder.appendLengthInfo(1024,  // 512 letters (1024/2).
+      Encoder.appendLengthInfo(512,  // 512 letters (1024/2).
                                                  40,  // version 40.
                                                  Mode.KANJI,
                                                  bits);
@@ -172,25 +171,18 @@ public final class EncoderTestCase extends TestCase {
       // Should use appendNumericBytes.
       // 1 = 01 = 0001 in 4 bits.
       BitVector bits = new BitVector();
-      Encoder.appendBytes(new ByteArray("1"), Mode.NUMERIC, bits);
+      Encoder.appendBytes("1", Mode.NUMERIC, bits);
       assertEquals("0001" , bits.toString());
-      // 'A' cannot be encoded in MODE_NUMERIC.
-      try {
-        Encoder.appendBytes(new ByteArray("A"), Mode.NUMERIC, bits);
-        fail("Should have thrown exception");
-      } catch (WriterException we) {
-        // good
-      }
     }
     {
       // Should use appendAlphanumericBytes.
       // A = 10 = 0xa = 001010 in 6 bits
       BitVector bits = new BitVector();
-      Encoder.appendBytes(new ByteArray("A"), Mode.ALPHANUMERIC, bits);
+      Encoder.appendBytes("A", Mode.ALPHANUMERIC, bits);
       assertEquals("001010" , bits.toString());
       // Lower letters such as 'a' cannot be encoded in MODE_ALPHANUMERIC.
       try {
-        Encoder.appendBytes(new ByteArray("a"), Mode.ALPHANUMERIC, bits);
+        Encoder.appendBytes("a", Mode.ALPHANUMERIC, bits);
       } catch (WriterException we) {
         // good
       }
@@ -199,33 +191,20 @@ public final class EncoderTestCase extends TestCase {
       // Should use append8BitBytes.
       // 0x61, 0x62, 0x63
       BitVector bits = new BitVector();
-      Encoder.appendBytes(new ByteArray("abc"), Mode.BYTE, bits);
-      assertEquals("01100001" + "01100010" + "01100011", bits.toString());
+      Encoder.appendBytes("abc", Mode.BYTE, bits);
+      assertEquals("011000010110001001100011", bits.toString());
       // Anything can be encoded in QRCode.MODE_8BIT_BYTE.
-      byte[] bytes = {0x00};
-      Encoder.appendBytes(new ByteArray(bytes), Mode.BYTE, bits);
+      Encoder.appendBytes("\0", Mode.BYTE, bits);
     }
     {
       // Should use appendKanjiBytes.
       // 0x93, 0x5f
       BitVector bits = new BitVector();
-      byte[] bytes = {(byte)0x93,0x5f};
-      Encoder.appendBytes(new ByteArray(bytes), Mode.KANJI, bits);
+      Encoder.appendBytes(shiftJISString(new byte[] {(byte)0x93,0x5f}), Mode.KANJI, bits);
       assertEquals("0110110011111", bits.toString());
-      // ASCII characters can not be encoded in QRCode.MODE_KANJI.
-
-      try {
-        Encoder.appendBytes(new ByteArray("a"), Mode.KANJI, bits);
-      } catch (WriterException we) {
-        // good
-      }
     }
   }
 
-  public void testInit() {
-    // TODO: should be implemented.
-  }
-
   public void testTerminateBits() throws WriterException {
     {
       BitVector v = new BitVector();
@@ -306,14 +285,14 @@ public final class EncoderTestCase extends TestCase {
 
   public void testInterleaveWithECBytes() throws WriterException {
     {
-      final byte[] dataBytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
+      byte[] dataBytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
       BitVector in = new BitVector();
       for (byte dataByte: dataBytes) {
         in.appendBits(dataByte, 8);
       }
       BitVector out = new BitVector();
       Encoder.interleaveWithECBytes(in, 26, 9, 1, out);
-      final byte[] expected = {
+      byte[] expected = {
           // Data bytes.
           32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236,
           // Error correction bytes.
@@ -321,7 +300,7 @@ public final class EncoderTestCase extends TestCase {
           (byte)237, 85, (byte)224, 96, 74, (byte)219, 61,
       };
       assertEquals(expected.length, out.sizeInBytes());
-      final byte[] outArray = out.getArray();
+      byte[] outArray = out.getArray();
       // Can't use Arrays.equals(), because outArray may be longer than out.sizeInBytes()
       for (int x = 0; x < expected.length; x++) {
         assertEquals(expected[x], outArray[x]);
@@ -329,7 +308,7 @@ public final class EncoderTestCase extends TestCase {
     }
     // Numbers are from http://www.swetake.com/qr/qr8.html
     {
-      final byte[] dataBytes = {
+      byte[] dataBytes = {
           67, 70, 22, 38, 54, 70, 86, 102, 118, (byte)134, (byte)150, (byte)166, (byte)182,
           (byte)198, (byte)214, (byte)230, (byte)247, 7, 23, 39, 55, 71, 87, 103, 119, (byte)135,
           (byte)151, (byte)166, 22, 38, 54, 70, 86, 102, 118, (byte)134, (byte)150, (byte)166,
@@ -343,7 +322,7 @@ public final class EncoderTestCase extends TestCase {
       }
       BitVector out = new BitVector();
       Encoder.interleaveWithECBytes(in, 134, 62, 4, out);
-      final byte[] expected = {
+      byte[] expected = {
           // Data bytes.
           67, (byte)230, 54, 55, 70, (byte)247, 70, 71, 22, 7, 86, 87, 38, 23, 102, 103, 54, 39,
           118, 119, 70, 55, (byte)134, (byte)135, 86, 71, (byte)150, (byte)151, 102, 87, (byte)166,
@@ -361,85 +340,76 @@ public final class EncoderTestCase extends TestCase {
           (byte)187, 49, (byte)156, (byte)214,
       };
       assertEquals(expected.length, out.sizeInBytes());
-      final byte[] outArray = out.getArray();
+      byte[] outArray = out.getArray();
       for (int x = 0; x < expected.length; x++) {
         assertEquals(expected[x], outArray[x]);
       }
     }
   }
 
-  public void testAppendNumericBytes() throws WriterException {
+  public void testAppendNumericBytes() {
     {
       // 1 = 01 = 0001 in 4 bits.
       BitVector bits = new BitVector();
-      Encoder.appendNumericBytes(new ByteArray("1"), bits);
+      Encoder.appendNumericBytes("1", bits);
       assertEquals("0001" , bits.toString());
     }
     {
       // 12 = 0xc = 0001100 in 7 bits.
       BitVector bits = new BitVector();
-      Encoder.appendNumericBytes(new ByteArray("12"), bits);
+      Encoder.appendNumericBytes("12", bits);
       assertEquals("0001100" , bits.toString());
     }
     {
       // 123 = 0x7b = 0001111011 in 10 bits.
       BitVector bits = new BitVector();
-      Encoder.appendNumericBytes(new ByteArray("123"), bits);
+      Encoder.appendNumericBytes("123", bits);
       assertEquals("0001111011" , bits.toString());
     }
     {
       // 1234 = "123" + "4" = 0001111011 + 0100
       BitVector bits = new BitVector();
-      Encoder.appendNumericBytes(new ByteArray("1234"), bits);
+      Encoder.appendNumericBytes("1234", bits);
       assertEquals("0001111011" + "0100" , bits.toString());
     }
     {
       // Empty.
       BitVector bits = new BitVector();
-      Encoder.appendNumericBytes(new ByteArray(""), bits);
+      Encoder.appendNumericBytes("", bits);
       assertEquals("" , bits.toString());
     }
-    {
-      // Invalid data.
-      BitVector bits = new BitVector();
-      try {
-        Encoder.appendNumericBytes(new ByteArray("abc"), bits);
-      } catch (WriterException we) {
-        // good
-      }
-    }
   }
 
   public void testAppendAlphanumericBytes() throws WriterException {
     {
       // A = 10 = 0xa = 001010 in 6 bits
       BitVector bits = new BitVector();
-      Encoder.appendAlphanumericBytes(new ByteArray("A"), bits);
+      Encoder.appendAlphanumericBytes("A", bits);
       assertEquals("001010" , bits.toString());
     }
     {
       // AB = 10 * 45 + 11 = 461 = 0x1cd = 00111001101 in 11 bits
       BitVector bits = new BitVector();
-      Encoder.appendAlphanumericBytes(new ByteArray("AB"), bits);
+      Encoder.appendAlphanumericBytes("AB", bits);
       assertEquals("00111001101", bits.toString());
     }
     {
       // ABC = "AB" + "C" = 00111001101 + 001100
       BitVector bits = new BitVector();
-      Encoder.appendAlphanumericBytes(new ByteArray("ABC"), bits);
+      Encoder.appendAlphanumericBytes("ABC", bits);
       assertEquals("00111001101" + "001100" , bits.toString());
     }
     {
       // Empty.
       BitVector bits = new BitVector();
-      Encoder.appendAlphanumericBytes(new ByteArray(""), bits);
+      Encoder.appendAlphanumericBytes("", bits);
       assertEquals("" , bits.toString());
     }
     {
       // Invalid data.
       BitVector bits = new BitVector();
       try {
-        Encoder.appendAlphanumericBytes(new ByteArray("abc"), bits);
+        Encoder.appendAlphanumericBytes("abc", bits);
       } catch (WriterException we) {
         // good
       }
@@ -450,93 +420,26 @@ public final class EncoderTestCase extends TestCase {
     {
       // 0x61, 0x62, 0x63
       BitVector bits = new BitVector();
-      Encoder.append8BitBytes(new ByteArray("abc"), bits);
+      Encoder.append8BitBytes("abc", bits);
       assertEquals("01100001" + "01100010" + "01100011", bits.toString());
     }
     {
       // Empty.
       BitVector bits = new BitVector();
-      Encoder.append8BitBytes(new ByteArray(""), bits);
+      Encoder.append8BitBytes("", bits);
       assertEquals("", bits.toString());
     }
   }
 
   // Numbers are from page 21 of JISX0510:2004
   public void testAppendKanjiBytes() throws WriterException {
-    {
       BitVector bits = new BitVector();
-      byte[] dat1 = {(byte)0x93,0x5f};
-      Encoder.appendKanjiBytes(new ByteArray(dat1), bits);
+      Encoder.appendKanjiBytes(shiftJISString(new byte[] {(byte)0x93,0x5f}), bits);
       assertEquals("0110110011111", bits.toString());
-      byte[] dat2 = {(byte)0xe4,(byte)0xaa};
-      Encoder.appendKanjiBytes(new ByteArray(dat2), bits);
+      Encoder.appendKanjiBytes(shiftJISString(new byte[] {(byte)0xe4,(byte)0xaa}), bits);
       assertEquals("0110110011111" + "1101010101010", bits.toString());
-    }
   }
 
-  // JAVAPORT: Uncomment and fix up with new Reed Solomon objects
-//  static boolean ComparePoly(final int[] expected, final int size, final GF_Poly poly) {
-//    if (size != poly.degree() + 1) {
-//      return false;
-//    }
-//    for (int i = 0; i < size; ++i) {
-//      // "expected" is ordered in a reverse order.  We reverse the coeff
-//      // index for comparison.
-//      final int coeff = GaloisField.GetField(8).Log(
-//          poly.coeff(size - i - 1));
-//      if (expected[i] != coeff) {
-//        Debug.LOG_ERROR("values don't match at " + i + ": " +
-//            expected[i] + " vs. " + coeff);
-//        return false;
-//      }
-//    }
-//    return true;
-//  }
-//
-//  // Numbers are from Appendix A of JISX0510 2004 (p.59).
-//  public void testGetECPoly() {
-//    {
-//      final GF_Poly poly = Encoder.GetECPoly(7);
-//      final int[] expected = {0, 87, 229, 146, 149, 238, 102, 21};
-//      assertTrue(ComparePoly(expected, expected.length, poly));
-//    }
-//    {
-//      final GF_Poly poly = Encoder.GetECPoly(17);
-//      final int[] expected = {
-//          0, 43, 139, 206, 78, 43, 239, 123, 206, 214, 147, 24, 99, 150,
-//          39, 243, 163, 136
-//      };
-//      assertTrue(ComparePoly(expected, expected.length, poly));
-//    }
-//    {
-//      final GF_Poly poly = Encoder.GetECPoly(34);
-//      final int[] expected = {
-//          0, 111, 77, 146, 94, 26, 21, 108, 19,
-//          105, 94, 113, 193, 86, 140, 163, 125,
-//          58,
-//          158, 229, 239, 218, 103, 56, 70, 114,
-//          61, 183, 129, 167, 13, 98, 62, 129, 51
-//      };
-//      assertTrue(ComparePoly(expected, expected.length, poly));
-//    }
-//    {
-//      final GF_Poly poly = Encoder.GetECPoly(68);
-//      final int[] expected = {
-//          0, 247, 159, 223, 33, 224, 93, 77, 70,
-//          90, 160, 32, 254, 43, 150, 84, 101,
-//          190,
-//          205, 133, 52, 60, 202, 165, 220, 203,
-//          151, 93, 84, 15, 84, 253, 173, 160,
-//          89, 227, 52, 199, 97, 95, 231, 52,
-//          177, 41, 125, 137, 241, 166, 225, 118,
-//          2, 54,
-//          32, 82, 215, 175, 198, 43, 238, 235,
-//          27, 101, 184, 127, 3, 5, 8, 163, 238
-//      };
-//      assertTrue(ComparePoly(expected, expected.length, poly));
-//    }
-//  }
-
   // Numbers are from http://www.swetake.com/qr/qr3.html and
   // http://www.swetake.com/qr/qr9.html
   public void testGenerateECBytes() {
@@ -577,39 +480,6 @@ public final class EncoderTestCase extends TestCase {
     }
   }
 
-  public void testIsValidKanji() {
-    assertTrue(Encoder.isValidKanji(0x82, 0xa0));  // Hiragana "A".
-    assertTrue(Encoder.isValidKanji(0x93, 0xfa));  // Nichi in Kanji.
-    assertTrue(Encoder.isValidKanji(0x8a, 0xbf));  // Kan in Kanji.
-    assertTrue(Encoder.isValidKanji(0xe7, 0x4e));  // Sou in Kanji.
-    assertTrue(Encoder.isValidKanji(0xea, 0xa2));  // Haruka in Kanji.
-
-    assertFalse(Encoder.isValidKanji('0', '1'));
-    assertFalse(Encoder.isValidKanji(0x82, 0x7f));
-    assertFalse(Encoder.isValidKanji(0xa0, 0xa0));
-  }
-
-  public void testIsValidKanjiSequence() {
-    // AIUEO in Katakana
-    byte[] dat1 = {
-        (byte)0x83, 0x41, (byte)0x83, 0x43, (byte)0x83, 0x45, (byte)0x83, 0x47, (byte)0x83, 0x49
-    };
-    assertTrue(Encoder.isValidKanjiSequence(new ByteArray(dat1)));
-    // 012345 in multi-byte letters.
-    byte[] dat2 = {
-        (byte)0x82, 0x4f, (byte)0x82, 0x50, (byte)0x82, 0x51, (byte)0x82, 0x52, (byte)0x82, 0x53,
-        (byte)0x82, 0x54
-    };
-    assertTrue(Encoder.isValidKanjiSequence(new ByteArray(dat2)));
-    // Yoroshiku in Kanji.
-    byte[] dat3 = {
-        (byte)0x96, (byte)0xe9, (byte)0x98, 0x49, (byte)0x8e, (byte)0x80, (byte)0x8b, (byte)0xea
-    };
-    assertTrue(Encoder.isValidKanjiSequence(new ByteArray(dat3)));
-    assertFalse(Encoder.isValidKanjiSequence(new ByteArray("0123")));
-    assertFalse(Encoder.isValidKanjiSequence(new ByteArray("ABC")));
-  }
-
   public void testBugInBitVectorNumBytes() throws WriterException {
     // There was a bug in BitVector.sizeInBytes() that caused it to return a
     // smaller-by-one value (ex. 1465 instead of 1466) if the number of bits
@@ -639,12 +509,20 @@ public final class EncoderTestCase extends TestCase {
     //   - To be precise, it needs 11727 + 4 (getMode info) + 14 (length info) =
     //     11745 bits = 1468.125 bytes are needed (i.e. cannot fit in 1468
     //     bytes).
-    final int arraySize = 3518;
-    byte[] dataBytes = new byte[arraySize];
-    for (int x = 0; x < arraySize; x++) {
-      dataBytes[x] = '0';
+    StringBuilder builder = new StringBuilder(3518);
+    for (int x = 0; x < 3518; x++) {
+      builder.append('0');
     }
     QRCode qrCode = new QRCode();
-    Encoder.encode(new ByteArray(dataBytes), ErrorCorrectionLevel.L, qrCode);
+    Encoder.encode(builder.toString(), ErrorCorrectionLevel.L, qrCode);
   }
+
+  private static String shiftJISString(byte[] bytes) throws WriterException {
+    try {
+      return new String(bytes, "Shift_JIS");
+    } catch (UnsupportedEncodingException uee) {
+      throw new WriterException(uee.toString());
+    }
+  }
+
 }