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 org.junit.Assert;
20 import org.junit.Test;
25 public final class FormatInformationTestCase extends Assert {
27 private static final int MASKED_TEST_FORMAT_INFO = 0x2BED;
28 private static final int UNMASKED_TEST_FORMAT_INFO = MASKED_TEST_FORMAT_INFO ^ 0x5412;
31 public void testBitsDiffering() {
32 assertEquals(0, FormatInformation.numBitsDiffering(1, 1));
33 assertEquals(1, FormatInformation.numBitsDiffering(0, 2));
34 assertEquals(2, FormatInformation.numBitsDiffering(1, 2));
35 assertEquals(32, FormatInformation.numBitsDiffering(-1, 0));
39 public void testDecode() {
41 FormatInformation expected =
42 FormatInformation.decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO);
43 assertEquals((byte) 0x07, expected.getDataMask());
44 assertSame(ErrorCorrectionLevel.Q, expected.getErrorCorrectionLevel());
45 // where the code forgot the mask!
46 assertEquals(expected,
47 FormatInformation.decodeFormatInformation(UNMASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO));
51 public void testDecodeWithBitDifference() {
52 FormatInformation expected =
53 FormatInformation.decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO);
54 // 1,2,3,4 bits difference
55 assertEquals(expected, FormatInformation.decodeFormatInformation(
56 MASKED_TEST_FORMAT_INFO ^ 0x01, MASKED_TEST_FORMAT_INFO ^ 0x01));
57 assertEquals(expected, FormatInformation.decodeFormatInformation(
58 MASKED_TEST_FORMAT_INFO ^ 0x03, MASKED_TEST_FORMAT_INFO ^ 0x03));
59 assertEquals(expected, FormatInformation.decodeFormatInformation(
60 MASKED_TEST_FORMAT_INFO ^ 0x07, MASKED_TEST_FORMAT_INFO ^ 0x07));
61 assertNull(FormatInformation.decodeFormatInformation(
62 MASKED_TEST_FORMAT_INFO ^ 0x0F, MASKED_TEST_FORMAT_INFO ^ 0x0F));
66 public void testDecodeWithMisread() {
67 FormatInformation expected =
68 FormatInformation.decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO);
69 assertEquals(expected, FormatInformation.decodeFormatInformation(
70 MASKED_TEST_FORMAT_INFO ^ 0x03, MASKED_TEST_FORMAT_INFO ^ 0x0F));