aa1295cf1fb138e4e6bbd134a132c148d225a1a6
[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 junit.framework.TestCase;
22
23 /**
24  * @author satorux@google.com (Satoru Takabayashi) - creator
25  * @author mysen@google.com (Chris Mysen) - ported from C++
26  */
27 public final class QRCodeTestCase extends TestCase {
28   public void test() {
29     QRCode qrCode = new QRCode();
30     // Initially the QR Code should be invalid.
31     assertFalse(qrCode.isValid());
32
33     // First, test simple setters and getters.
34     // We use numbers of version 7-H.
35     qrCode.setMode(Mode.BYTE);
36     qrCode.setECLevel(ErrorCorrectionLevel.H);
37     qrCode.setVersion(7);
38     qrCode.setMatrixWidth(45);
39     qrCode.setMaskPattern(3);
40     qrCode.setNumTotalBytes(196);
41     qrCode.setNumDataBytes(66);
42     qrCode.setNumECBytes(130);
43     qrCode.setNumRSBlocks(5);
44
45     assertEquals(Mode.BYTE, qrCode.getMode());
46     assertEquals(ErrorCorrectionLevel.H, qrCode.getECLevel());
47     assertEquals(7, qrCode.getVersion());
48     assertEquals(45, qrCode.getMatrixWidth());
49     assertEquals(3, qrCode.getMaskPattern());
50     assertEquals(196, qrCode.getNumTotalBytes());
51     assertEquals(66, qrCode.getNumDataBytes());
52     assertEquals(130, qrCode.getNumECBytes());
53     assertEquals(5, qrCode.getNumRSBlocks());
54
55     // It still should be invalid.
56     assertFalse(qrCode.isValid());
57
58     // Prepare the matrix.
59     ByteMatrix matrix = new ByteMatrix(45, 45);
60     // Just set bogus zero/one values.
61     for (int y = 0; y < 45; ++y) {
62       for (int x = 0; x < 45; ++x) {
63         matrix.set(x, y, (y + x) % 2);
64       }
65     }
66
67     // Set the matrix.
68     qrCode.setMatrix(matrix);
69     assertEquals(matrix, qrCode.getMatrix());
70
71     // Finally, it should be valid.
72     assertTrue(qrCode.isValid());
73
74     // Make sure "at()" returns the same value.
75     for (int y = 0; y < 45; ++y) {
76       for (int x = 0; x < 45; ++x) {
77         assertEquals((y + x) % 2, qrCode.at(x, y));
78       }
79     }
80   }
81
82   public void testToString() {
83     {
84       QRCode qrCode = new QRCode();
85       String expected =
86         "<<\n" +
87         " mode: null\n" +
88         " ecLevel: null\n" +
89         " version: -1\n" +
90         " matrixWidth: -1\n" +
91         " maskPattern: -1\n" +
92         " numTotalBytes: -1\n" +
93         " numDataBytes: -1\n" +
94         " numECBytes: -1\n" +
95         " numRSBlocks: -1\n" +
96         " matrix: null\n" +
97         ">>\n";
98       assertEquals(expected, qrCode.toString());
99     }
100     {
101       String expected =
102         "<<\n" +
103         " mode: BYTE\n" +
104         " ecLevel: H\n" +
105         " version: 1\n" +
106         " matrixWidth: 21\n" +
107         " maskPattern: 3\n" +
108         " numTotalBytes: 26\n" +
109         " numDataBytes: 9\n" +
110         " numECBytes: 17\n" +
111         " numRSBlocks: 1\n" +
112         " matrix:\n" +
113         " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
114         " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
115         " 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
116         " 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\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         ">>\n";
135       QRCode qrCode = new QRCode();
136       qrCode.setMode(Mode.BYTE);
137       qrCode.setECLevel(ErrorCorrectionLevel.H);
138       qrCode.setVersion(1);
139       qrCode.setMatrixWidth(21);
140       qrCode.setMaskPattern(3);
141       qrCode.setNumTotalBytes(26);
142       qrCode.setNumDataBytes(9);
143       qrCode.setNumECBytes(17);
144       qrCode.setNumRSBlocks(1);
145       ByteMatrix matrix = new ByteMatrix(21, 21);
146       for (int y = 0; y < 21; ++y) {
147         for (int x = 0; x < 21; ++x) {
148           matrix.set(x, y, (y + x) % 2);
149         }
150       }
151       qrCode.setMatrix(matrix);
152       assertTrue(qrCode.isValid());
153       assertEquals(expected, qrCode.toString());
154     }
155   }
156
157   public void testIsValidMaskPattern() {
158     assertFalse(QRCode.isValidMaskPattern(-1));
159     assertTrue(QRCode.isValidMaskPattern(0));
160     assertTrue(QRCode.isValidMaskPattern(7));
161     assertFalse(QRCode.isValidMaskPattern(8));
162   }
163
164 }