2 * Copyright 2007 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.qrcode.decoder;
19 import junit.framework.TestCase;
24 public final class FormatInformationTestCase extends TestCase {
26 private static final int MASKED_TEST_FORMAT_INFO = 0x2BED;
27 private static final int UNMASKED_TEST_FORMAT_INFO = MASKED_TEST_FORMAT_INFO ^ 0x5412;
29 public void testBitsDiffering() {
30 assertEquals(0, FormatInformation.numBitsDiffering(1, 1));
31 assertEquals(1, FormatInformation.numBitsDiffering(0, 2));
32 assertEquals(2, FormatInformation.numBitsDiffering(1, 2));
33 assertEquals(32, FormatInformation.numBitsDiffering(-1, 0));
36 public void testDecode() {
38 FormatInformation expected =
39 FormatInformation.decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO);
40 assertEquals((byte) 0x07, expected.getDataMask());
41 assertSame(ErrorCorrectionLevel.Q, expected.getErrorCorrectionLevel());
42 // where the code forgot the mask!
43 assertEquals(expected,
44 FormatInformation.decodeFormatInformation(UNMASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO));
47 public void testDecodeWithBitDifference() {
48 FormatInformation expected =
49 FormatInformation.decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO);
50 // 1,2,3,4 bits difference
51 assertEquals(expected, FormatInformation.decodeFormatInformation(
52 MASKED_TEST_FORMAT_INFO ^ 0x01, MASKED_TEST_FORMAT_INFO ^ 0x01));
53 assertEquals(expected, FormatInformation.decodeFormatInformation(
54 MASKED_TEST_FORMAT_INFO ^ 0x03, MASKED_TEST_FORMAT_INFO ^ 0x03));
55 assertEquals(expected, FormatInformation.decodeFormatInformation(
56 MASKED_TEST_FORMAT_INFO ^ 0x07, MASKED_TEST_FORMAT_INFO ^ 0x07));
57 assertNull(FormatInformation.decodeFormatInformation(
58 MASKED_TEST_FORMAT_INFO ^ 0x0F, MASKED_TEST_FORMAT_INFO ^ 0x0F));
61 public void testDecodeWithMisread() {
62 FormatInformation expected =
63 FormatInformation.decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO);
64 assertEquals(expected, FormatInformation.decodeFormatInformation(
65 MASKED_TEST_FORMAT_INFO ^ 0x03, MASKED_TEST_FORMAT_INFO ^ 0x0F));