Remove Debug and switch to eceptions in Encoder / Writer API
[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.common.ByteMatrix;
20 import com.google.zxing.WriterException;
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 qr_code = new QRCode();
30     // Initially the QR Code should be invalid.
31     assertFalse(qr_code.IsValid());
32
33     // First, test simple setters and getters.
34     // We use numbers of version 7-H.
35     qr_code.set_mode(QRCode.MODE_8BIT_BYTE);
36     qr_code.set_ec_level(QRCode.EC_LEVEL_H);
37     qr_code.set_version(7);
38     qr_code.set_matrix_width(45);
39     qr_code.set_mask_pattern(3);
40     qr_code.set_num_total_bytes(196);
41     qr_code.set_num_data_bytes(66);
42     qr_code.set_num_ec_bytes(130);
43     qr_code.set_num_rs_blocks(5);
44
45     assertEquals(QRCode.MODE_8BIT_BYTE, qr_code.mode());
46     assertEquals(QRCode.EC_LEVEL_H, qr_code.ec_level());
47     assertEquals(7, qr_code.version());
48     assertEquals(45, qr_code.matrix_width());
49     assertEquals(3, qr_code.mask_pattern());
50     assertEquals(196, qr_code.num_total_bytes());
51     assertEquals(66, qr_code.num_data_bytes());
52     assertEquals(130, qr_code.num_ec_bytes());
53     assertEquals(5, qr_code.num_rs_blocks());
54
55     // It still should be invalid.
56     assertFalse(qr_code.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(y, x, (y + x) % 2);
64       }
65     }
66
67     // Set the matrix.
68     qr_code.set_matrix(matrix);
69     assertEquals(matrix, qr_code.matrix());
70
71     // Finally, it should be valid.
72     assertTrue(qr_code.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, qr_code.at(x, y));
78       }
79     }
80   }
81
82   public void testToString() {
83     {
84       QRCode qr_code = new QRCode();
85       String expected =
86         "<<\n" +
87         " mode: UNDEFINED\n" +
88         " ec_level: UNDEFINED\n" +
89         " version: -1\n" +
90         " matrix_width: -1\n" +
91         " mask_pattern: -1\n" +
92         " num_total_bytes_: -1\n" +
93         " num_data_bytes: -1\n" +
94         " num_ec_bytes: -1\n" +
95         " num_rs_blocks: -1\n" +
96         " matrix: null\n" +
97         ">>\n";
98       assertEquals(expected, qr_code.toString());
99     }
100     {
101       String expected =
102         "<<\n" +
103         " mode: 8BIT_BYTE\n" +
104         " ec_level: H\n" +
105         " version: 1\n" +
106         " matrix_width: 21\n" +
107         " mask_pattern: 3\n" +
108         " num_total_bytes_: 26\n" +
109         " num_data_bytes: 9\n" +
110         " num_ec_bytes: 17\n" +
111         " num_rs_blocks: 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 qr_code = new QRCode();
136       qr_code.set_mode(QRCode.MODE_8BIT_BYTE);
137       qr_code.set_ec_level(QRCode.EC_LEVEL_H);
138       qr_code.set_version(1);
139       qr_code.set_matrix_width(21);
140       qr_code.set_mask_pattern(3);
141       qr_code.set_num_total_bytes(26);
142       qr_code.set_num_data_bytes(9);
143       qr_code.set_num_ec_bytes(17);
144       qr_code.set_num_rs_blocks(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(y, x, (y + x) % 2);
149         }
150       }
151       qr_code.set_matrix(matrix);
152       assertTrue(qr_code.IsValid());
153       assertEquals(expected, qr_code.toString());
154     }
155   }
156
157   public void testIsValidVersion() {
158     assertFalse(QRCode.IsValidVersion(0));
159     assertTrue(QRCode.IsValidVersion(1));
160     assertTrue(QRCode.IsValidVersion(40));
161     assertFalse(QRCode.IsValidVersion(0));
162   }
163
164   public void testIsValidECLevel() {
165     assertFalse(QRCode.IsValidECLevel(QRCode.EC_LEVEL_UNDEFINED));
166     assertTrue(QRCode.IsValidECLevel(QRCode.EC_LEVEL_L));
167     assertTrue(QRCode.IsValidECLevel(QRCode.EC_LEVEL_Q));
168     assertTrue(QRCode.IsValidECLevel(QRCode.EC_LEVEL_M));
169     assertTrue(QRCode.IsValidECLevel(QRCode.EC_LEVEL_H));
170     assertFalse(QRCode.IsValidECLevel(QRCode.NUM_EC_LEVELS));
171   }
172
173   public void testIsValidMode() {
174     assertFalse(QRCode.IsValidMode(QRCode.MODE_UNDEFINED));
175     assertTrue(QRCode.IsValidMode(QRCode.MODE_NUMERIC));
176     assertTrue(QRCode.IsValidMode(QRCode.MODE_ALPHANUMERIC));
177     assertTrue(QRCode.IsValidMode(QRCode.MODE_8BIT_BYTE));
178     assertFalse(QRCode.IsValidMode(QRCode.NUM_MODES));
179   }
180
181   public void testIsValidMatrixWidth() {
182     assertFalse(QRCode.IsValidMatrixWidth(20));
183     assertTrue(QRCode.IsValidMatrixWidth(21));
184     assertTrue(QRCode.IsValidMatrixWidth(177));
185     assertFalse(QRCode.IsValidMatrixWidth(178));
186   }
187
188   public void testIsValidMaskPattern() {
189     assertFalse(QRCode.IsValidMaskPattern(-1));
190     assertTrue(QRCode.IsValidMaskPattern(0));
191     assertTrue(QRCode.IsValidMaskPattern(7));
192     assertFalse(QRCode.IsValidMaskPattern(8));
193   }
194
195   public void testModeToString() {
196     assertEquals("UNDEFINED", QRCode.ModeToString(QRCode.MODE_UNDEFINED));
197     assertEquals("NUMERIC", QRCode.ModeToString(QRCode.MODE_NUMERIC));
198     assertEquals("ALPHANUMERIC", QRCode.ModeToString(QRCode.MODE_ALPHANUMERIC));
199     assertEquals("8BIT_BYTE", QRCode.ModeToString(QRCode.MODE_8BIT_BYTE));
200     assertEquals("UNKNOWN", QRCode.ModeToString(QRCode.NUM_MODES));
201   }
202
203   public void testECLevelToString() {
204     assertEquals("UNDEFINED", QRCode.ECLevelToString(QRCode.EC_LEVEL_UNDEFINED));
205     assertEquals("L", QRCode.ECLevelToString(QRCode.EC_LEVEL_L));
206     assertEquals("M", QRCode.ECLevelToString(QRCode.EC_LEVEL_M));
207     assertEquals("Q", QRCode.ECLevelToString(QRCode.EC_LEVEL_Q));
208     assertEquals("H", QRCode.ECLevelToString(QRCode.EC_LEVEL_H));
209     assertEquals("UNKNOWN", QRCode.ECLevelToString(QRCode.NUM_EC_LEVELS));
210   }
211
212   public void testGetModeCode() throws WriterException {
213     assertEquals(1, QRCode.GetModeCode(QRCode.MODE_NUMERIC));
214     assertEquals(2, QRCode.GetModeCode(QRCode.MODE_ALPHANUMERIC));
215     assertEquals(4, QRCode.GetModeCode(QRCode.MODE_8BIT_BYTE));
216     assertEquals(8, QRCode.GetModeCode(QRCode.MODE_KANJI));
217     try {
218       QRCode.GetModeCode(QRCode.MODE_UNDEFINED);
219       fail("Should have thrown exception");      
220     } catch (WriterException we) {
221       // good
222     }
223   }
224
225   public void testGetECLevelCode() throws WriterException {
226     assertEquals(1, QRCode.GetECLevelCode(QRCode.EC_LEVEL_L));
227     assertEquals(0, QRCode.GetECLevelCode(QRCode.EC_LEVEL_M));
228     assertEquals(3, QRCode.GetECLevelCode(QRCode.EC_LEVEL_Q));
229     assertEquals(2, QRCode.GetECLevelCode(QRCode.EC_LEVEL_H));
230     try {
231       QRCode.GetECLevelCode(QRCode.EC_LEVEL_UNDEFINED);
232       fail("Should have thrown exception");
233     } catch (WriterException we) {
234       // good
235     }
236   }
237 }