Standardize and update all copyright statements to name "ZXing authors" as suggested...
[zxing.git] / cpp / core / tests / src / qrcode / decoder / FormatInformationTest.cpp
1 /*
2  *  FormatInformationTest.cpp
3  *  zxing
4  *
5  *  Created by Christian Brunschen on 19/05/2008.
6  *  Copyright 2008 ZXing authors All rights reserved.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include "FormatInformationTest.h"
22
23 #include "ErrorCorrectionLevelTest.h"
24 #include "../../../../src/ReaderException.h"
25 #include "../../../../src/qrcode/decoder/FormatInformation.h"
26 #include "../../../../src/qrcode/decoder/ErrorCorrectionLevel.h"
27
28 namespace qrcode {
29   namespace decoder {
30     
31     CPPUNIT_TEST_SUITE_REGISTRATION(FormatInformationTest);
32     
33     static void assertEquals(Ref<FormatInformation> a,
34                              Ref<FormatInformation> b) {
35       FormatInformation &aa = *a;
36       FormatInformation &bb = *b;
37       CPPUNIT_ASSERT_EQUAL(aa, bb);
38     }
39     
40     void FormatInformationTest::testBitsDiffering() {
41       CPPUNIT_ASSERT_EQUAL(0, FormatInformation::numBitsDiffering(1, 1));
42       CPPUNIT_ASSERT_EQUAL(1, FormatInformation::numBitsDiffering(0, 2));
43       CPPUNIT_ASSERT_EQUAL(2, FormatInformation::numBitsDiffering(1, 2));
44       CPPUNIT_ASSERT_EQUAL(32, FormatInformation::numBitsDiffering(0xffffffff,
45                                                                    0));
46     }
47     
48     void FormatInformationTest::testDecode() {
49       // Normal case
50       Ref<FormatInformation> expected = 
51       FormatInformation::decodeFormatInformation(0x2BED ^ 0x5412);
52       CPPUNIT_ASSERT_EQUAL((unsigned char) 0x07, expected->getDataMask());
53       CPPUNIT_ASSERT_EQUAL(&ErrorCorrectionLevel::Q,
54                            &expected->getErrorCorrectionLevel());
55       // where the code forgot the mask!
56       assertEquals(expected, 
57                    FormatInformation::decodeFormatInformation(0x2BED));
58       
59       // 1,2,3,4 bits difference
60       assertEquals(expected, 
61                    FormatInformation::decodeFormatInformation(0x2BEF ^ 0x5412));
62       assertEquals(expected, 
63                    FormatInformation::decodeFormatInformation(0x2BEE ^ 0x5412));
64       assertEquals(expected, 
65                    FormatInformation::decodeFormatInformation(0x2BEA ^ 0x5412));
66       CPPUNIT_ASSERT_EQUAL(true, FormatInformation::
67                            decodeFormatInformation(0x2BE2 ^ 0x5412) == 0);
68     }
69   }
70 }