Overdue unit tests for some QR code classes
[zxing.git] / core / test / src / com / google / zxing / qrcode / decoder / VersionTestCase.java
1 /*
2  * Copyright 2008 Google Inc.
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.decoder;
18
19 import com.google.zxing.ReaderException;
20 import junit.framework.TestCase;
21
22 /**
23  * @author srowen@google.com (Sean Owen)
24  */
25 public final class VersionTestCase extends TestCase {
26
27   public void testVersionForNumber() throws ReaderException {
28     try {
29       Version.getVersionForNumber(0);
30       fail("Should have thrown an exception");
31     } catch (ReaderException re) {
32       // good
33     }
34     for (int i = 1; i <= 40; i++) {
35       checkVersion(Version.getVersionForNumber(i), i, 4*i + 17);
36     }
37   }
38
39   private static void checkVersion(Version version, int number, int dimension) {
40     assertNotNull(version);
41     assertEquals(number, version.getVersionNumber());
42     assertNotNull(version.getAlignmentPatternCenters());
43     if (number > 1) {
44       assertTrue(version.getAlignmentPatternCenters().length > 0);
45     }
46     assertEquals(dimension, version.getDimensionForVersion());
47     assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel.H));
48     assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel.L));
49     assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel.M));
50     assertNotNull(version.getECBlocksForLevel(ErrorCorrectionLevel.Q));
51     assertNotNull(version.buildFunctionPattern());
52   }
53
54   public void testGetProvisionalVersionForDimension() throws ReaderException {
55     for (int i = 1; i <= 40; i++) {
56       assertEquals(i, Version.getProvisionalVersionForDimension(4*i + 17).getVersionNumber());
57     }
58   }
59
60   public void testDecodeVersionInformation() throws ReaderException {
61     // Spot check
62     assertEquals(7, Version.decodeVersionInformation(0x07C94).getVersionNumber());
63     assertEquals(12, Version.decodeVersionInformation(0x0C762).getVersionNumber());
64     assertEquals(17, Version.decodeVersionInformation(0x1145D).getVersionNumber());
65     assertEquals(22, Version.decodeVersionInformation(0x168C9).getVersionNumber());
66     assertEquals(27, Version.decodeVersionInformation(0x1B08E).getVersionNumber());
67     assertEquals(32, Version.decodeVersionInformation(0x209D5).getVersionNumber());    
68   }
69
70 }