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