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