--- /dev/null
+/*
+ * Copyright 2008 ZXing authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.zxing.qrcode.encoder;
+
+import com.google.zxing.qrcode.encoder.MaskUtil;
+import junit.framework.TestCase;
+
+//#include "util/array/array2d-inl.h"
+//#include "wireless/qrcode/bit_vector-inl.h"
+//#include "base/google.h"
+//#include "Strings/Stringpiece.h"
+//#include "testing/base/benchmark.h"
+//#include "testing/base/gunit.h"
+//#include "util/reedsolomon/galois_field.h"
+//#include "util/reedsolomon/galois_poly.h"
+//#include "wireless/qrcode/qrcode_encoder.h"
+
+/**
+ * @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() {
+ // The first ten code points are numbers.
+ for (int i = 0; i < 10; ++i) {
+ assertEquals(i, Encoder.GetAlphanumericCode('0' + i));
+ }
+
+ // The next 26 code points are capital alphabet letters.
+ for (int i = 10; i < 36; ++i) {
+ assertEquals(i, Encoder.GetAlphanumericCode('A' + i - 10));
+ }
+
+ // Others are symbol letters
+ assertEquals(36, Encoder.GetAlphanumericCode(' '));
+ assertEquals(37, Encoder.GetAlphanumericCode('$'));
+ assertEquals(38, Encoder.GetAlphanumericCode('%'));
+ assertEquals(39, Encoder.GetAlphanumericCode('*'));
+ assertEquals(40, Encoder.GetAlphanumericCode('+'));
+ assertEquals(41, Encoder.GetAlphanumericCode('-'));
+ assertEquals(42, Encoder.GetAlphanumericCode('.'));
+ assertEquals(43, Encoder.GetAlphanumericCode('/'));
+ assertEquals(44, Encoder.GetAlphanumericCode(':'));
+
+ // Should return -1 for other letters;
+ assertEquals(-1, Encoder.GetAlphanumericCode('a'));
+ assertEquals(-1, Encoder.GetAlphanumericCode('#'));
+ assertEquals(-1, Encoder.GetAlphanumericCode('\0'));
+ }
+
+ public void testChooseMode() {
+ // Numeric mode.
+ assertEquals(QRCode.MODE_NUMERIC, Encoder.ChooseMode(new ByteArray("0")));
+ assertEquals(QRCode.MODE_NUMERIC,
+ Encoder.ChooseMode(new ByteArray("0123456789")));
+ // Alphanumeric mode.
+ assertEquals(QRCode.MODE_ALPHANUMERIC, Encoder.ChooseMode(new ByteArray("A")));
+ assertEquals(QRCode.MODE_ALPHANUMERIC,
+ Encoder.ChooseMode(
+ new ByteArray("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:")));
+ // 8-bit byte mode.
+ assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray("a")));
+ assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray("#")));
+ assertEquals(QRCode.MODE_8BIT_BYTE, Encoder.ChooseMode(new ByteArray("")));
+ // 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.
+ byte[] dat1 = {0x8,0xa,0x8,0xa,0x8,0xa,0x8,(byte)0xa6};
+ assertEquals(QRCode.MODE_8BIT_BYTE,
+ // AIUE in Hiragana in Shift_JIS
+ Encoder.ChooseMode(new ByteArray(dat1)));
+ byte[] dat2 = {0x9,0xf,0x9,0x7b};
+ assertEquals(QRCode.MODE_8BIT_BYTE,
+ // Nihon in Kanji in Shift_JIS.
+ Encoder.ChooseMode(new ByteArray(dat2)));
+ byte[] dat3 = {0xe,0x4,0x9,0x5,0x9,0x61};
+ assertEquals(QRCode.MODE_8BIT_BYTE,
+ // Sou-Utsu-Byou in Kanji in Shift_JIS.
+ Encoder.ChooseMode(new ByteArray(dat3)));
+ }
+
+ public void testEncode() {
+ QRCode qr_code = new QRCode();
+ assertTrue(Encoder.Encode(new ByteArray("ABCDEF"), QRCode.EC_LEVEL_H, qr_code));
+ // The following is a valid QR Code that can be read by cell phones.
+ String expected =
+ "<<\n" +
+ " mode: ALPHANUMERIC\n" +
+ " ec_level: H\n" +
+ " version: 1\n" +
+ " matrix_width: 21\n" +
+ " mask_pattern: 0\n" +
+ " num_total_bytes_: 26\n" +
+ " num_data_bytes: 9\n" +
+ " num_ec_bytes: 17\n" +
+ " num_rs_blocks: 1\n" +
+ " matrix:\n" +
+ " 1 1 1 1 1 1 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1\n" +
+ " 1 0 0 0 0 0 1 0 0 1 1 1 0 0 1 0 0 0 0 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 1 0 1 1 0 1 0 1 1 1 0 1\n" +
+ " 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 0 1 1 1 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1\n" +
+ " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
+ " 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0\n" +
+ " 0 0 1 0 1 1 1 0 1 1 0 0 1 1 0 0 0 1 0 0 1\n" +
+ " 1 0 1 1 1 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0\n" +
+ " 0 0 1 1 0 0 1 0 1 0 0 0 1 0 1 0 1 0 1 1 0\n" +
+ " 1 1 0 1 0 1 0 1 1 1 0 1 0 1 0 0 0 0 0 1 0\n" +
+ " 0 0 1 1 0 1 1 1 1 0 0 0 1 0 1 0 1 1 1 1 0\n" +
+ " 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0 1 0 1 0 0 0\n" +
+ " 1 1 1 1 1 1 1 0 0 0 1 0 1 0 1 1 0 0 0 0 1\n" +
+ " 1 0 0 0 0 0 1 0 1 1 1 1 0 1 0 1 1 1 1 0 1\n" +
+ " 1 0 1 1 1 0 1 0 1 0 1 1 0 1 0 1 0 0 0 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 1 1 0 1 1 1 1 0 1 0 1 0\n" +
+ " 1 0 1 1 1 0 1 0 1 0 0 0 1 0 1 0 1 1 1 0 1\n" +
+ " 1 0 0 0 0 0 1 0 0 1 1 0 1 1 0 1 0 0 0 1 1\n" +
+ " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1\n" +
+ ">>\n";
+ assertEquals(expected, qr_code.toString());
+ }
+
+ public void testAppendModeInfo() {
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendModeInfo(QRCode.MODE_NUMERIC, bits));
+ assertEquals("0001", bits.toString());
+ }
+
+ public void testAppendLengthInfo() {
+ {
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendLengthInfo(1, // 1 letter (1/1).
+ 1, // version 1.
+ QRCode.MODE_NUMERIC,
+ bits));
+ assertEquals("0000000001", bits.toString()); // 10 bits.
+ }
+ {
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendLengthInfo(2, // 2 letters (2/1).
+ 10, // version 10.
+ QRCode.MODE_ALPHANUMERIC,
+ bits));
+ assertEquals("00000000010", bits.toString()); // 11 bits.
+ }
+ {
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendLengthInfo(255, // 255 letter (255/1).
+ 27, // version 27.
+ QRCode.MODE_8BIT_BYTE,
+ bits));
+ assertEquals("0000000011111111", bits.toString()); // 16 bits.
+ }
+ {
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendLengthInfo(1024, // 512 letters (1024/2).
+ 40, // version 40.
+ QRCode.MODE_KANJI,
+ bits));
+ assertEquals("001000000000", bits.toString()); // 12 bits.
+ }
+ }
+
+ public void testAppendBytes() {
+ {
+ // Should use AppendNumericBytes.
+ // 1 = 01 = 0001 in 4 bits.
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendBytes(new ByteArray("1"), QRCode.MODE_NUMERIC, bits));
+ assertEquals("0001" , bits.toString());
+ // 'A' cannot be encoded in MODE_NUMERIC.
+ assertFalse(Encoder.AppendBytes(new ByteArray("A"), QRCode.MODE_NUMERIC, bits));
+ }
+ {
+ // Should use AppendAlphanumericBytes.
+ // A = 10 = 0xa = 001010 in 6 bits
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendBytes(new ByteArray("A"), QRCode.MODE_ALPHANUMERIC,
+ bits));
+ assertEquals("001010" , bits.toString());
+ // Lower letters such as 'a' cannot be encoded in MODE_ALPHANUMERIC.
+ assertFalse(Encoder.AppendBytes(new ByteArray("a"), QRCode.MODE_ALPHANUMERIC,
+ bits));
+ }
+ {
+ // Should use Append8BitBytes.
+ // 0x61, 0x62, 0x63
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendBytes(new ByteArray("abc"), QRCode.MODE_8BIT_BYTE,
+ bits));
+ assertEquals("01100001" + "01100010" + "01100011", bits.toString());
+ // Anything can be encoded in QRCode.MODE_8BIT_BYTE.
+ byte[] bytes = {0x00};
+ assertTrue(Encoder.AppendBytes(new ByteArray(bytes), QRCode.MODE_8BIT_BYTE,
+ bits));
+ }
+ {
+ // Should use AppendKanjiBytes.
+ // 0x93, 0x5f
+ BitVector bits = new BitVector();
+ byte[] bytes = {(byte)0x93,0x5f};
+ assertTrue(Encoder.AppendBytes(new ByteArray(bytes), QRCode.MODE_KANJI, bits));
+ assertEquals("0110110011111", bits.toString());
+ // ASCII characters can not be encoded in QRCode.MODE_KANJI.
+ assertFalse(Encoder.AppendBytes(new ByteArray("a"), QRCode.MODE_KANJI, bits));
+ }
+ }
+
+ public void testInit() {
+ // TODO: should be implemented.
+ }
+
+ public void testTerminateBits() {
+ {
+ BitVector v = new BitVector();
+ assertTrue(Encoder.TerminateBits(0, v));
+ assertEquals("", v.toString());
+ }
+ {
+ BitVector v = new BitVector();
+ assertTrue(Encoder.TerminateBits(1, v));
+ assertEquals("00000000", v.toString());
+ }
+ {
+ BitVector v = new BitVector();
+ v.AppendBits(0, 3); // Append 000
+ assertTrue(Encoder.TerminateBits(1, v));
+ assertEquals("00000000", v.toString());
+ }
+ {
+ BitVector v = new BitVector();
+ v.AppendBits(0, 5); // Append 00000
+ assertTrue(Encoder.TerminateBits(1, v));
+ assertEquals("00000000", v.toString());
+ }
+ {
+ BitVector v = new BitVector();
+ v.AppendBits(0, 8); // Append 00000000
+ assertTrue(Encoder.TerminateBits(1, v));
+ assertEquals("00000000", v.toString());
+ }
+ {
+ BitVector v = new BitVector();
+ assertTrue(Encoder.TerminateBits(2, v));
+ assertEquals("0000000011101100", v.toString());
+ }
+ {
+ BitVector v = new BitVector();
+ v.AppendBits(0, 1); // Append 0
+ assertTrue(Encoder.TerminateBits(3, v));
+ assertEquals("000000001110110000010001", v.toString());
+ }
+ }
+
+ public void testGetNumDataBytesAndNumECBytesForBlockID() {
+ int[] num_data_bytes = new int[0];
+ int[] num_ec_bytes = new int[0];
+ // Version 1-H.
+ Encoder.GetNumDataBytesAndNumECBytesForBlockID(
+ 26, 9, 1, 0, num_data_bytes, num_ec_bytes);
+ assertEquals(9, num_data_bytes[0]);
+ assertEquals(17, num_ec_bytes[0]);
+
+ // Version 3-H. 2 blocks.
+ Encoder.GetNumDataBytesAndNumECBytesForBlockID(
+ 70, 26, 2, 0, num_data_bytes, num_ec_bytes);
+ assertEquals(13, num_data_bytes[0]);
+ assertEquals(22, num_ec_bytes[0]);
+ Encoder.GetNumDataBytesAndNumECBytesForBlockID(
+ 70, 26, 2, 1, num_data_bytes, num_ec_bytes);
+ assertEquals(13, num_data_bytes[0]);
+ assertEquals(22, num_ec_bytes[0]);
+
+ // Version 7-H. (4 + 1) blocks.
+ Encoder.GetNumDataBytesAndNumECBytesForBlockID(
+ 196, 66, 5, 0, num_data_bytes, num_ec_bytes);
+ assertEquals(13, num_data_bytes[0]);
+ assertEquals(26, num_ec_bytes[0]);
+ Encoder.GetNumDataBytesAndNumECBytesForBlockID(
+ 196, 66, 5, 4, num_data_bytes, num_ec_bytes);
+ assertEquals(14, num_data_bytes[0]);
+ assertEquals(26, num_ec_bytes[0]);
+
+ // Version 40-H. (20 + 61) blocks.
+ Encoder.GetNumDataBytesAndNumECBytesForBlockID(
+ 3706, 1276, 81, 0, num_data_bytes, num_ec_bytes);
+ assertEquals(15, num_data_bytes[0]);
+ assertEquals(30, num_ec_bytes[0]);
+ Encoder.GetNumDataBytesAndNumECBytesForBlockID(
+ 3706, 1276, 81, 20, num_data_bytes, num_ec_bytes);
+ assertEquals(16, num_data_bytes[0]);
+ assertEquals(30, num_ec_bytes[0]);
+ Encoder.GetNumDataBytesAndNumECBytesForBlockID(
+ 3706, 1276, 81, 80, num_data_bytes, num_ec_bytes);
+ assertEquals(16, num_data_bytes[0]);
+ assertEquals(30, num_ec_bytes[0]);
+ }
+
+ public void testInterleaveWithECBytes() {
+ {
+ final char[] data_bytes = {32, 65, 205, 69, 41, 220, 46, 128, 236};
+ BitVector in = new BitVector();
+ for (char data_byte: data_bytes) {
+ in.AppendBits(data_byte, 8);
+ }
+ BitVector out = new BitVector();
+ assertTrue(Encoder.InterleaveWithECBytes(in, 26, 9, 1, out));
+ final char[] expected = {
+ // Data bytes.
+ 32, 65, 205, 69, 41, 220, 46, 128, 236,
+ // Error correction bytes.
+ 42, 159, 74, 221, 244, 169, 239, 150, 138, 70, 237, 85, 224,
+ 96, 74, 219, 61,
+ };
+ assertEquals(new String(expected), out.toString());
+ }
+ // Numbers are from http://www.swetake.com/qr/qr8.html
+ {
+ final char[] data_bytes = {
+ 67, 70, 22, 38, 54, 70, 86, 102, 118, 134, 150, 166, 182, 198, 214,
+ 230, 247, 7, 23, 39, 55, 71, 87, 103, 119, 135, 151, 166, 22, 38,
+ 54, 70, 86, 102, 118, 134, 150, 166, 182, 198, 214, 230, 247, 7, 23, 39,
+ 55, 71, 87, 103, 119, 135, 151, 160, 236, 17, 236, 17, 236, 17, 236, 17,
+ };
+ BitVector in = new BitVector();
+ for (char data_byte: data_bytes) {
+ in.AppendBits(data_byte, 8);
+ }
+ BitVector out = new BitVector();
+ assertTrue(Encoder.InterleaveWithECBytes(in, 134, 62, 4, out));
+ final char[] expected = {
+ // Data bytes.
+ 67, 230, 54, 55, 70, 247, 70, 71, 22, 7, 86, 87, 38, 23, 102, 103, 54,
+ 39, 118, 119, 70, 55, 134, 135, 86, 71, 150, 151, 102, 87, 166, 160,
+ 118, 103, 182, 236, 134, 119, 198, 17, 150, 135, 214, 236, 166, 151,
+ 230, 17, 182, 166, 247, 236, 198, 22, 7, 17, 214, 38, 23, 236, 39, 17,
+ // Error correction bytes.
+ 175, 155, 245, 236, 80, 146, 56, 74, 155, 165, 133, 142, 64, 183, 132,
+ 13, 178, 54, 132, 108, 45, 113, 53, 50, 214, 98, 193, 152, 233, 147, 50,
+ 71, 65, 190, 82, 51, 209, 199, 171, 54, 12, 112, 57, 113, 155, 117, 211,
+ 164, 117, 30, 158, 225, 31, 190, 242, 38, 140, 61, 179, 154, 214, 138,
+ 147, 87, 27, 96, 77, 47, 187, 49, 156, 214,
+ };
+ assertEquals(new String(expected), out.toString());
+ }
+ }
+
+ public void testAppendNumericBytes() {
+ {
+ // 1 = 01 = 0001 in 4 bits.
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendNumericBytes(new ByteArray("1"), bits));
+ assertEquals("0001" , bits.toString());
+ }
+ {
+ // 12 = 0xc = 0001100 in 7 bits.
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendNumericBytes(new ByteArray("12"), bits));
+ assertEquals("0001100" , bits.toString());
+ }
+ {
+ // 123 = 0x7b = 0001111011 in 10 bits.
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendNumericBytes(new ByteArray("123"), bits));
+ assertEquals("0001111011" , bits.toString());
+ }
+ {
+ // 1234 = "123" + "4" = 0001111011 + 0100
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendNumericBytes(new ByteArray("1234"), bits));
+ assertEquals("0001111011" + "0100" , bits.toString());
+ }
+ {
+ // Empty.
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendNumericBytes(new ByteArray(""), bits));
+ assertEquals("" , bits.toString());
+ }
+ {
+ // Invalid data.
+ BitVector bits = new BitVector();
+ assertFalse(Encoder.AppendNumericBytes(new ByteArray("abc"), bits));
+ }
+ }
+
+ public void testAppendAlphanumericBytes() {
+ {
+ // A = 10 = 0xa = 001010 in 6 bits
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray("A"), bits));
+ assertEquals("001010" , bits.toString());
+ }
+ {
+ // AB = 10 * 45 + 11 = 461 = 0x1cd = 00111001101 in 11 bits
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray("AB"), bits));
+ assertEquals("00111001101", bits.toString());
+ }
+ {
+ // ABC = "AB" + "C" = 00111001101 + 001100
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray("ABC"), bits));
+ assertEquals("00111001101" + "001100" , bits.toString());
+ }
+ {
+ // Empty.
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.AppendAlphanumericBytes(new ByteArray(""), bits));
+ assertEquals("" , bits.toString());
+ }
+ {
+ // Invalid data.
+ BitVector bits = new BitVector();
+ assertFalse(Encoder.AppendAlphanumericBytes(new ByteArray("abc"), bits));
+ }
+ }
+
+ public void testAppend8BitBytes() {
+ {
+ // 0x61, 0x62, 0x63
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.Append8BitBytes(new ByteArray("abc"), bits));
+ assertEquals("01100001" + "01100010" + "01100011", bits.toString());
+ }
+ {
+ // Empty.
+ BitVector bits = new BitVector();
+ assertTrue(Encoder.Append8BitBytes(new ByteArray(""), bits));
+ assertEquals("", bits.toString());
+ }
+ }
+
+ // Numbers are from page 21 of JISX0510:2004
+ public void testAppendKanjiBytes() {
+ {
+ BitVector bits = new BitVector();
+ byte[] dat1 = {(byte)0x93,0x5f};
+ assertTrue(Encoder.AppendKanjiBytes(new ByteArray(dat1), bits));
+ assertEquals("0110110011111", bits.toString());
+ byte[] dat2 = {(byte)0xe4,(byte)0xaa};
+ assertTrue(Encoder.AppendKanjiBytes(new ByteArray(dat2), bits));
+ assertEquals("0110110011111" + "1101010101010", bits.toString());
+ }
+ }
+
+ 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() {
+ {
+ String ec_bytes;
+ final char[] data_bytes = {32, 65, 205, 69, 41, 220, 46, 128, 236};
+ Encoder.GenerateECBytes(new ByteArray(new String(data_bytes)), 17, new ByteArray(ec_bytes));
+ final char[] expected = {
+ 42, 159, 74, 221, 244, 169, 239, 150, 138, 70, 237, 85, 224,
+ 96, 74, 219, 61};
+ assertEquals(new String(expected), ec_bytes);
+ }
+ {
+ String ec_bytes;
+ final char[] data_bytes = {67, 70, 22, 38, 54, 70, 86, 102, 118,
+ 134, 150, 166, 182, 198, 214};
+ Encoder.GenerateECBytes(new ByteArray(new String(data_bytes)), 18, new ByteArray(ec_bytes));
+ final char[] expected = {
+ 175, 80, 155, 64, 178, 45, 214, 233, 65, 209, 12, 155, 117, 31, 140,
+ 214, 27, 187};
+ assertEquals(new String(expected), ec_bytes);
+ }
+ {
+ // High-order zero cofficient case.
+ String ec_bytes;
+ final char[] data_bytes = {32, 49, 205, 69, 42, 20, 0, 236, 17};
+ Encoder.GenerateECBytes(new ByteArray(new String(data_bytes)), 17, new ByteArray(ec_bytes));
+ final char[] expected = {
+ 0, 3, 130, 179, 194, 0, 55, 211, 110, 79, 98, 72, 170, 96, 211, 137, 213
+ };
+ assertEquals(new String(expected), ec_bytes);
+ }
+ }
+
+ 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 = {0x8,0x4,0x8,0x4,0x8,0x4,0x8,0x4,0x8,0x49};
+ assertTrue(Encoder.IsValidKanjiSequence(new ByteArray(dat1)));
+ // 012345 in multi-byte letters.
+ byte[] dat2 = {0x8,0x4,0x8,0x5,0x8,0x5,0x8,0x5,0x8,0x5,0x8,0x54};
+ assertTrue(Encoder.IsValidKanjiSequence(new ByteArray(dat2)));
+ // Yoroshiku in Kanji.
+ byte[] dat3 = {0x9,0xe,0x9,0x4,0x8,0x8,0x8,(byte)0xea};
+ assertTrue(Encoder.IsValidKanjiSequence(new ByteArray(dat3)));
+ assertFalse(Encoder.IsValidKanjiSequence(new ByteArray("0123")));
+ assertFalse(Encoder.IsValidKanjiSequence(new ByteArray("ABC")));
+ }
+}
--- /dev/null
+/*
+ * Copyright 2008 ZXing authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.zxing.qrcode.encoder;
+
+import com.google.zxing.common.ByteMatrix;
+import com.google.zxing.qrcode.encoder.MatrixUtil;
+import junit.framework.TestCase;
+
+//#include "util/array/array2d-inl.h"
+//#include "wireless/qrcode/bit_vector-inl.h"
+//#include "Strings/Stringpiece.h"
+//#include "testing/base/gunit.h"
+//#include "wireless/qrcode/qrcode.h"
+//#include "wireless/qrcode/qrcode_matrix_util.h"
+
+/**
+ * @author satorux@google.com (Satoru Takabayashi) - creator
+ * @author mysen@google.com (Chris Mysen) - ported from C++
+ */
+public final class MatrixUtilTestCase extends TestCase {
+ public void testtoString() {
+ ByteMatrix array = new ByteMatrix(3, 3);
+ array.set(0, 0, 0);
+ array.set(0, 1, 1);
+ array.set(0, 2, 0);
+ array.set(1, 0, 1);
+ array.set(1, 1, 0);
+ array.set(1, 2, 1);
+ array.set(2, 0, -1);
+ array.set(2, 1, -1);
+ array.set(2, 2, -1);
+ String expected = " 0 1 0\n" + " 1 0 1\n" + " \n";
+ assertEquals(expected, array.toString());
+ }
+
+ public void testClearMatrix() {
+ ByteMatrix matrix = new ByteMatrix(2, 2);
+ MatrixUtil.ClearMatrix(matrix);
+ assertEquals(-1, matrix.get(0, 0));
+ assertEquals(-1, matrix.get(0, 1));
+ assertEquals(-1, matrix.get(1, 0));
+ assertEquals(-1, matrix.get(1, 1));
+ }
+
+ public void testEmbedBasicPatterns() {
+ {
+ // Version 1.
+ String expected =
+ " 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1\n" +
+ " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" +
+ " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
+ " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 1 \n" +
+ " 0 \n" +
+ " 1 \n" +
+ " 0 \n" +
+ " 1 \n" +
+ " 0 0 0 0 0 0 0 0 1 \n" +
+ " 1 1 1 1 1 1 1 0 \n" +
+ " 1 0 0 0 0 0 1 0 \n" +
+ " 1 0 1 1 1 0 1 0 \n" +
+ " 1 0 1 1 1 0 1 0 \n" +
+ " 1 0 1 1 1 0 1 0 \n" +
+ " 1 0 0 0 0 0 1 0 \n" +
+ " 1 1 1 1 1 1 1 0 \n";
+ ByteMatrix matrix = new ByteMatrix(21, 21);
+ MatrixUtil.ClearMatrix(matrix);
+ assertTrue(MatrixUtil.EmbedBasicPatterns(1, matrix));
+ assertEquals(expected, matrix.toString());
+ }
+ {
+ // Version 2. Position adjustment pattern should apppear at right
+ // bottom corner.
+ String expected =
+ " 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1\n" +
+ " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" +
+ " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
+ " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 1 \n" +
+ " 0 \n" +
+ " 1 \n" +
+ " 0 \n" +
+ " 1 \n" +
+ " 0 \n" +
+ " 1 \n" +
+ " 0 \n" +
+ " 1 1 1 1 1 1 \n" +
+ " 0 0 0 0 0 0 0 0 1 1 0 0 0 1 \n" +
+ " 1 1 1 1 1 1 1 0 1 0 1 0 1 \n" +
+ " 1 0 0 0 0 0 1 0 1 0 0 0 1 \n" +
+ " 1 0 1 1 1 0 1 0 1 1 1 1 1 \n" +
+ " 1 0 1 1 1 0 1 0 \n" +
+ " 1 0 1 1 1 0 1 0 \n" +
+ " 1 0 0 0 0 0 1 0 \n" +
+ " 1 1 1 1 1 1 1 0 \n";
+ ByteMatrix matrix = new ByteMatrix(25, 25);
+ MatrixUtil.ClearMatrix(matrix);
+ assertTrue(MatrixUtil.EmbedBasicPatterns(2, matrix));
+ assertEquals(expected, matrix.toString());
+ }
+ }
+
+ public void testEmbedTypeInfo() {
+ // Type info bits = 100000011001110.
+ String expected =
+ " 0 \n" +
+ " 1 \n" +
+ " 1 \n" +
+ " 1 \n" +
+ " 0 \n" +
+ " 0 \n" +
+ " \n" +
+ " 1 \n" +
+ " 1 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " 0 \n" +
+ " 0 \n" +
+ " 0 \n" +
+ " 0 \n" +
+ " 0 \n" +
+ " 0 \n" +
+ " 1 \n";
+ ByteMatrix matrix = new ByteMatrix(21, 21);
+ MatrixUtil.ClearMatrix(matrix);
+ boolean info_okay = MatrixUtil.EmbedTypeInfo(QRCode.EC_LEVEL_M, 5, matrix);
+ System.out.println(info_okay + "\n" + matrix.toString());
+ assertTrue(info_okay);
+ assertEquals(expected, matrix.toString());
+ assertFalse(true);
+ }
+
+ public void testEmbedVersionInfo() {
+ // Version info bits = 000111 110010 010100
+ String expected =
+ " 0 0 1 \n" +
+ " 0 1 0 \n" +
+ " 0 1 0 \n" +
+ " 0 1 1 \n" +
+ " 1 1 1 \n" +
+ " 0 0 0 \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " 0 0 0 0 1 0 \n" +
+ " 0 1 1 1 1 0 \n" +
+ " 1 0 0 1 1 0 \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n";
+ // Actually, version 7 QR Code has 45x45 matrix but we use 21x21 here
+ // since 45x45 matrix is too big to depict.
+ ByteMatrix matrix = new ByteMatrix(21, 21);
+ MatrixUtil.ClearMatrix(matrix);
+ assertTrue(MatrixUtil.MaybeEmbedVersionInfo(7, matrix));
+ assertEquals(expected, matrix.toString());
+ }
+
+ public void testEmbedDataBits() {
+ // Cells other than basic patterns should be filled with zero.
+ String expected =
+ " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1\n" +
+ " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n" +
+ " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
+ " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n";
+ BitVector bits = new BitVector();
+ ByteMatrix matrix = new ByteMatrix(21, 21);
+ MatrixUtil.ClearMatrix(matrix);
+ MatrixUtil.EmbedBasicPatterns(1, matrix);
+ assertTrue(MatrixUtil.EmbedDataBits(bits, -1, matrix));
+ assertEquals(expected, matrix.toString());
+ }
+
+ public void testBuildMatrix() {
+ // From http://www.swetake.com/qr/qr7.html
+ String expected =
+ " 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1\n" +
+ " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 1 1 0 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 1 1 1 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 1\n" +
+ " 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1\n" +
+ " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
+ " 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0\n" +
+ " 0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 0 0 0 0\n" +
+ " 1 0 1 0 1 0 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0\n" +
+ " 1 1 1 1 0 1 1 0 1 0 1 1 1 0 0 1 1 1 0 1 0\n" +
+ " 1 0 1 0 1 1 0 1 1 1 0 0 1 1 1 0 0 1 0 1 0\n" +
+ " 0 0 1 0 0 1 1 1 0 0 0 0 0 0 1 0 1 1 1 1 1\n" +
+ " 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 1 1\n" +
+ " 1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 0 1 0 1 1 0\n" +
+ " 1 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 0 0 0 0 0\n" +
+ " 1 0 1 1 1 0 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1\n" +
+ " 1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 0 1 1 1 0\n" +
+ " 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0\n" +
+ " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0\n" +
+ " 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 0\n";
+ char bytes[] = {32, 65, 205, 69, 41, 220, 46, 128, 236,
+ 42, 159, 74, 221, 244, 169, 239, 150, 138,
+ 70, 237, 85, 224, 96, 74, 219 , 61};
+ BitVector bits = new BitVector();
+ for (char c: bytes) {
+ bits.AppendBits(c, 8);
+ }
+ ByteMatrix matrix = new ByteMatrix(21, 21);
+ assertTrue(MatrixUtil.BuildMatrix(bits,
+ QRCode.EC_LEVEL_H,
+ 1, // Version 1
+ 3, // Mask pattern 3
+ matrix));
+ }
+
+ public void testFindMSBSet() {
+ assertEquals(0, MatrixUtil.FindMSBSet(0));
+ assertEquals(1, MatrixUtil.FindMSBSet(1));
+ assertEquals(8, MatrixUtil.FindMSBSet(0x80));
+ assertEquals(32, MatrixUtil.FindMSBSet(0x80000000));
+ }
+
+ public void testCalculateBCHCode() {
+ // Encoding of type information.
+ // From Appendix C in JISX0510:2004 (p 65)
+ assertEquals(0xdc, MatrixUtil.CalculateBCHCode(5, 0x537));
+ // From http://www.swetake.com/qr/qr6.html
+ assertEquals(0x1c2, MatrixUtil.CalculateBCHCode(0x13, 0x537));
+ // From http://www.swetake.com/qr/qr11.html
+ assertEquals(0x214, MatrixUtil.CalculateBCHCode(0x1b, 0x537));
+
+ // Encofing of version information.
+ // From Appendix D in JISX0510:2004 (p 68)
+ assertEquals(0xc94, MatrixUtil.CalculateBCHCode(7, 0x1f25));
+ assertEquals(0x5bc, MatrixUtil.CalculateBCHCode(8, 0x1f25));
+ assertEquals(0xa99, MatrixUtil.CalculateBCHCode(9, 0x1f25));
+ assertEquals(0x4d3, MatrixUtil.CalculateBCHCode(10, 0x1f25));
+ assertEquals(0x9a6, MatrixUtil.CalculateBCHCode(20, 0x1f25));
+ assertEquals(0xd75, MatrixUtil.CalculateBCHCode(30, 0x1f25));
+ assertEquals(0xc69, MatrixUtil.CalculateBCHCode(40, 0x1f25));
+ }
+
+ // We don't test a lot of cases in this function since we've already
+ // tested them in TEST(CalculateBCHCode).
+ public void testMakeVersionInfoBits() {
+ // From Appendix D in JISX0510:2004 (p 68)
+ BitVector bits = new BitVector();
+ assertTrue(MatrixUtil.MakeVersionInfoBits(7, bits));
+ assertEquals("000111110010010100", bits.toString());
+ }
+
+ // We don't test a lot of cases in this function since we've already
+ // tested them in TEST(CalculateBCHCode).
+ public void testMakeTypeInfoInfoBits() {
+ // From Appendix C in JISX0510:2004 (p 65)
+ BitVector bits = new BitVector();
+ assertTrue(MatrixUtil.MakeTypeInfoBits(QRCode.EC_LEVEL_M,
+ 5, bits));
+ assertEquals("100000011001110", bits.toString());
+ }
+}
--- /dev/null
+/**
+ * Copyright 2008 ZXing authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.zxing.qrcode.encoder;
+
+import com.google.zxing.common.ByteMatrix;
+import junit.framework.TestCase;
+
+//#include "util/array/array2d-inl.h"
+//#include "testing/base/gunit.h"
+//#include "wireless/qrcode/qrcode.h"
+
+/**
+ * @author satorux@google.com (Satoru Takabayashi) - creator
+ * @author mysen@google.com (Chris Mysen) - ported from C++
+ */
+public final class QRCodeTestCase extends TestCase {
+ public void test() {
+ QRCode qr_code = new QRCode();
+ // Initially the QR Code should be invalid.
+ assertFalse(qr_code.IsValid());
+
+ // First, test simple setters and getters.
+ // We use numbers of version 7-H.
+ qr_code.set_mode(QRCode.MODE_8BIT_BYTE);
+ qr_code.set_ec_level(QRCode.EC_LEVEL_H);
+ qr_code.set_version(7);
+ qr_code.set_matrix_width(45);
+ qr_code.set_mask_pattern(3);
+ qr_code.set_num_total_bytes(196);
+ qr_code.set_num_data_bytes(66);
+ qr_code.set_num_ec_bytes(130);
+ qr_code.set_num_rs_blocks(5);
+
+ assertEquals(QRCode.MODE_8BIT_BYTE, qr_code.mode());
+ assertEquals(QRCode.EC_LEVEL_H, qr_code.ec_level());
+ assertEquals(7, qr_code.version());
+ assertEquals(45, qr_code.matrix_width());
+ assertEquals(3, qr_code.mask_pattern());
+ assertEquals(196, qr_code.num_total_bytes());
+ assertEquals(66, qr_code.num_data_bytes());
+ assertEquals(130, qr_code.num_ec_bytes());
+ assertEquals(5, qr_code.num_rs_blocks());
+
+ // It still should be invalid.
+ assertFalse(qr_code.IsValid());
+
+ // Prepare the matrix.
+ ByteMatrix matrix = new ByteMatrix(45, 45);
+ // Just set bogus zero/one values.
+ for (int y = 0; y < 45; ++y) {
+ for (int x = 0; x < 45; ++x) {
+ matrix.set(y, x, (y + x) % 2);
+ }
+ }
+
+ // Set the matrix.
+ qr_code.set_matrix(matrix);
+ assertEquals(matrix, qr_code.matrix());
+
+ // Finally, it should be valid.
+ assertTrue(qr_code.IsValid());
+
+ // Make sure "at()" returns the same value.
+ for (int y = 0; y < 45; ++y) {
+ for (int x = 0; x < 45; ++x) {
+ assertEquals((y + x) % 2, qr_code.at(x, y));
+ }
+ }
+ }
+
+ public void testToString() {
+ {
+ QRCode qr_code = new QRCode();
+ String expected =
+ "<<\n" +
+ " mode: UNDEFINED\n" +
+ " ec_level: UNDEFINED\n" +
+ " version: -1\n" +
+ " matrix_width: -1\n" +
+ " mask_pattern: -1\n" +
+ " num_total_bytes_: -1\n" +
+ " num_data_bytes: -1\n" +
+ " num_ec_bytes: -1\n" +
+ " num_rs_blocks: -1\n" +
+ " matrix: NULL\n" +
+ ">>\n";
+ assertEquals(expected, qr_code.toString());
+ }
+ {
+ String expected =
+ "<<\n" +
+ " mode: 8BIT_BYTE\n" +
+ " ec_level: H\n" +
+ " version: 1\n" +
+ " matrix_width: 21\n" +
+ " mask_pattern: 3\n" +
+ " num_total_bytes_: 26\n" +
+ " num_data_bytes: 9\n" +
+ " num_ec_bytes: 17\n" +
+ " num_rs_blocks: 1\n" +
+ " matrix:\n" +
+ " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
+ " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
+ " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
+ " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
+ " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
+ " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
+ " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
+ " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
+ " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
+ " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
+ " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
+ " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
+ " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
+ " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
+ " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
+ " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
+ " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
+ " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
+ " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
+ " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
+ " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
+ ">>\n";
+ QRCode qr_code = new QRCode();
+ qr_code.set_mode(QRCode.MODE_8BIT_BYTE);
+ qr_code.set_ec_level(QRCode.EC_LEVEL_H);
+ qr_code.set_version(1);
+ qr_code.set_matrix_width(21);
+ qr_code.set_mask_pattern(3);
+ qr_code.set_num_total_bytes(26);
+ qr_code.set_num_data_bytes(9);
+ qr_code.set_num_ec_bytes(17);
+ qr_code.set_num_rs_blocks(1);
+ ByteMatrix matrix = new ByteMatrix(21, 21);
+ for (int y = 0; y < 21; ++y) {
+ for (int x = 0; x < 21; ++x) {
+ matrix.set(y, x, (y + x) % 2);
+ }
+ }
+ qr_code.set_matrix(matrix);
+ assertTrue(qr_code.IsValid());
+ assertEquals(expected, qr_code.toString());
+ }
+ }
+
+ public void testIsValidVersion() {
+ assertFalse(QRCode.IsValidVersion(0));
+ assertTrue(QRCode.IsValidVersion(1));
+ assertTrue(QRCode.IsValidVersion(40));
+ assertFalse(QRCode.IsValidVersion(0));
+ }
+
+ public void testIsValidECLevel() {
+ assertFalse(QRCode.IsValidECLevel(QRCode.EC_LEVEL_UNDEFINED));
+ assertTrue(QRCode.IsValidECLevel(QRCode.EC_LEVEL_L));
+ assertTrue(QRCode.IsValidECLevel(QRCode.EC_LEVEL_Q));
+ assertTrue(QRCode.IsValidECLevel(QRCode.EC_LEVEL_M));
+ assertTrue(QRCode.IsValidECLevel(QRCode.EC_LEVEL_H));
+ assertFalse(QRCode.IsValidECLevel(QRCode.NUM_EC_LEVELS));
+ }
+
+ public void testIsValidMode() {
+ assertFalse(QRCode.IsValidMode(QRCode.MODE_UNDEFINED));
+ assertTrue(QRCode.IsValidMode(QRCode.MODE_NUMERIC));
+ assertTrue(QRCode.IsValidMode(QRCode.MODE_ALPHANUMERIC));
+ assertTrue(QRCode.IsValidMode(QRCode.MODE_8BIT_BYTE));
+ assertFalse(QRCode.IsValidMode(QRCode.NUM_MODES));
+ }
+
+ public void testIsValidMatrixWidth() {
+ assertFalse(QRCode.IsValidMatrixWidth(20));
+ assertTrue(QRCode.IsValidMatrixWidth(21));
+ assertTrue(QRCode.IsValidMatrixWidth(177));
+ assertFalse(QRCode.IsValidMatrixWidth(178));
+ }
+
+ public void testIsValidMaskPattern() {
+ assertFalse(QRCode.IsValidMaskPattern(-1));
+ assertTrue(QRCode.IsValidMaskPattern(0));
+ assertTrue(QRCode.IsValidMaskPattern(7));
+ assertFalse(QRCode.IsValidMaskPattern(8));
+ }
+
+ public void testModeToString() {
+ assertEquals("UNDEFINED", QRCode.ModeToString(QRCode.MODE_UNDEFINED));
+ assertEquals("NUMERIC", QRCode.ModeToString(QRCode.MODE_NUMERIC));
+ assertEquals("ALPHANUMERIC",
+ QRCode.ModeToString(QRCode.MODE_ALPHANUMERIC));
+ assertEquals("8BIT_BYTE", QRCode.ModeToString(QRCode.MODE_8BIT_BYTE));
+ assertEquals("UNKNOWN", QRCode.ModeToString(QRCode.NUM_MODES));
+ }
+
+ public void testECLevelToString() {
+ assertEquals("UNDEFINED",
+ QRCode.ECLevelToString(QRCode.EC_LEVEL_UNDEFINED));
+ assertEquals("L", QRCode.ECLevelToString(QRCode.EC_LEVEL_L));
+ assertEquals("M", QRCode.ECLevelToString(QRCode.EC_LEVEL_M));
+ assertEquals("Q", QRCode.ECLevelToString(QRCode.EC_LEVEL_Q));
+ assertEquals("H", QRCode.ECLevelToString(QRCode.EC_LEVEL_H));
+ assertEquals("UNKNOWN", QRCode.ECLevelToString(QRCode.NUM_EC_LEVELS));
+ }
+
+ public void testGetModeCode() {
+ assertEquals(1, QRCode.GetModeCode(QRCode.MODE_NUMERIC));
+ assertEquals(2, QRCode.GetModeCode(QRCode.MODE_ALPHANUMERIC));
+ assertEquals(4, QRCode.GetModeCode(QRCode.MODE_8BIT_BYTE));
+ assertEquals(8, QRCode.GetModeCode(QRCode.MODE_KANJI));
+ assertEquals(-1, QRCode.GetModeCode(QRCode.MODE_UNDEFINED));
+ }
+
+ public void testGetECLevelCode() {
+ assertEquals(1, QRCode.GetECLevelCode(QRCode.EC_LEVEL_L));
+ assertEquals(0, QRCode.GetECLevelCode(QRCode.EC_LEVEL_M));
+ assertEquals(3, QRCode.GetECLevelCode(QRCode.EC_LEVEL_Q));
+ assertEquals(2, QRCode.GetECLevelCode(QRCode.EC_LEVEL_H));
+ assertEquals(-1, QRCode.GetECLevelCode(QRCode.EC_LEVEL_UNDEFINED));
+ }
+}