63cf01d46caef9c21c37d89e296de4a1af6f66fd
[zxing.git] / core / test / src / com / google / zxing / qrcode / encoder / QRCodeTestCase.java
1 /**
2  * Copyright 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.qrcode.encoder;
18
19 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
20 import com.google.zxing.qrcode.decoder.Mode;
21 import org.junit.Assert;
22 import org.junit.Test;
23
24 /**
25  * @author satorux@google.com (Satoru Takabayashi) - creator
26  * @author mysen@google.com (Chris Mysen) - ported from C++
27  */
28 public final class QRCodeTestCase extends Assert {
29
30   @Test
31   public void test() {
32     QRCode qrCode = new QRCode();
33     // Initially the QR Code should be invalid.
34     assertFalse(qrCode.isValid());
35
36     // First, test simple setters and getters.
37     // We use numbers of version 7-H.
38     qrCode.setMode(Mode.BYTE);
39     qrCode.setECLevel(ErrorCorrectionLevel.H);
40     qrCode.setVersion(7);
41     qrCode.setMatrixWidth(45);
42     qrCode.setMaskPattern(3);
43     qrCode.setNumTotalBytes(196);
44     qrCode.setNumDataBytes(66);
45     qrCode.setNumECBytes(130);
46     qrCode.setNumRSBlocks(5);
47
48     assertSame(Mode.BYTE, qrCode.getMode());
49     assertSame(ErrorCorrectionLevel.H, qrCode.getECLevel());
50     assertEquals(7, qrCode.getVersion());
51     assertEquals(45, qrCode.getMatrixWidth());
52     assertEquals(3, qrCode.getMaskPattern());
53     assertEquals(196, qrCode.getNumTotalBytes());
54     assertEquals(66, qrCode.getNumDataBytes());
55     assertEquals(130, qrCode.getNumECBytes());
56     assertEquals(5, qrCode.getNumRSBlocks());
57
58     // It still should be invalid.
59     assertFalse(qrCode.isValid());
60
61     // Prepare the matrix.
62     ByteMatrix matrix = new ByteMatrix(45, 45);
63     // Just set bogus zero/one values.
64     for (int y = 0; y < 45; ++y) {
65       for (int x = 0; x < 45; ++x) {
66         matrix.set(x, y, (y + x) % 2);
67       }
68     }
69
70     // Set the matrix.
71     qrCode.setMatrix(matrix);
72     assertEquals(matrix, qrCode.getMatrix());
73
74     // Finally, it should be valid.
75     assertTrue(qrCode.isValid());
76
77     // Make sure "at()" returns the same value.
78     for (int y = 0; y < 45; ++y) {
79       for (int x = 0; x < 45; ++x) {
80         assertEquals((y + x) % 2, qrCode.at(x, y));
81       }
82     }
83   }
84
85   @Test
86   public void testToString() {
87     {
88       QRCode qrCode = new QRCode();
89       String expected =
90         "<<\n" +
91         " mode: null\n" +
92         " ecLevel: null\n" +
93         " version: -1\n" +
94         " matrixWidth: -1\n" +
95         " maskPattern: -1\n" +
96         " numTotalBytes: -1\n" +
97         " numDataBytes: -1\n" +
98         " numECBytes: -1\n" +
99         " numRSBlocks: -1\n" +
100         " matrix: null\n" +
101         ">>\n";
102       assertEquals(expected, qrCode.toString());
103     }
104     {
105       String expected =
106         "<<\n" +
107         " mode: BYTE\n" +
108         " ecLevel: H\n" +
109         " version: 1\n" +
110         " matrixWidth: 21\n" +
111         " maskPattern: 3\n" +
112         " numTotalBytes: 26\n" +
113         " numDataBytes: 9\n" +
114         " numECBytes: 17\n" +
115         " numRSBlocks: 1\n" +
116         " matrix:\n" +
117         " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
118         " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
119         " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
120         " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
121         " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
122         " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
123         " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
124         " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
125         " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
126         " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
127         " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
128         " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
129         " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
130         " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
131         " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
132         " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
133         " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
134         " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
135         " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
136         " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
137         " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
138         ">>\n";
139       QRCode qrCode = new QRCode();
140       qrCode.setMode(Mode.BYTE);
141       qrCode.setECLevel(ErrorCorrectionLevel.H);
142       qrCode.setVersion(1);
143       qrCode.setMatrixWidth(21);
144       qrCode.setMaskPattern(3);
145       qrCode.setNumTotalBytes(26);
146       qrCode.setNumDataBytes(9);
147       qrCode.setNumECBytes(17);
148       qrCode.setNumRSBlocks(1);
149       ByteMatrix matrix = new ByteMatrix(21, 21);
150       for (int y = 0; y < 21; ++y) {
151         for (int x = 0; x < 21; ++x) {
152           matrix.set(x, y, (y + x) % 2);
153         }
154       }
155       qrCode.setMatrix(matrix);
156       assertTrue(qrCode.isValid());
157       assertEquals(expected, qrCode.toString());
158     }
159   }
160
161   @Test
162   public void testIsValidMaskPattern() {
163     assertFalse(QRCode.isValidMaskPattern(-1));
164     assertTrue(QRCode.isValidMaskPattern(0));
165     assertTrue(QRCode.isValidMaskPattern(7));
166     assertFalse(QRCode.isValidMaskPattern(8));
167   }
168
169 }