mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 12:22:34 +00:00
port and pass formatinformation tests
This commit is contained in:
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.qrcode.decoder;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Sean Owen
|
||||
*/
|
||||
public final class FormatInformationTestCase extends Assert {
|
||||
|
||||
private static final int MASKED_TEST_FORMAT_INFO = 0x2BED;
|
||||
private static final int UNMASKED_TEST_FORMAT_INFO = MASKED_TEST_FORMAT_INFO ^ 0x5412;
|
||||
|
||||
@Test
|
||||
public void testBitsDiffering() {
|
||||
assertEquals(0, FormatInformation.numBitsDiffering(1, 1));
|
||||
assertEquals(1, FormatInformation.numBitsDiffering(0, 2));
|
||||
assertEquals(2, FormatInformation.numBitsDiffering(1, 2));
|
||||
assertEquals(32, FormatInformation.numBitsDiffering(-1, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecode() {
|
||||
// Normal case
|
||||
FormatInformation expected =
|
||||
FormatInformation.decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO);
|
||||
assertNotNull(expected);
|
||||
assertEquals((byte) 0x07, expected.getDataMask());
|
||||
assertSame(ErrorCorrectionLevel.Q, expected.getErrorCorrectionLevel());
|
||||
// where the code forgot the mask!
|
||||
assertEquals(expected,
|
||||
FormatInformation.decodeFormatInformation(UNMASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeWithBitDifference() {
|
||||
FormatInformation expected =
|
||||
FormatInformation.decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO);
|
||||
// 1,2,3,4 bits difference
|
||||
assertEquals(expected, FormatInformation.decodeFormatInformation(
|
||||
MASKED_TEST_FORMAT_INFO ^ 0x01, MASKED_TEST_FORMAT_INFO ^ 0x01));
|
||||
assertEquals(expected, FormatInformation.decodeFormatInformation(
|
||||
MASKED_TEST_FORMAT_INFO ^ 0x03, MASKED_TEST_FORMAT_INFO ^ 0x03));
|
||||
assertEquals(expected, FormatInformation.decodeFormatInformation(
|
||||
MASKED_TEST_FORMAT_INFO ^ 0x07, MASKED_TEST_FORMAT_INFO ^ 0x07));
|
||||
assertNull(FormatInformation.decodeFormatInformation(
|
||||
MASKED_TEST_FORMAT_INFO ^ 0x0F, MASKED_TEST_FORMAT_INFO ^ 0x0F));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeWithMisread() {
|
||||
FormatInformation expected =
|
||||
FormatInformation.decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO);
|
||||
assertEquals(expected, FormatInformation.decodeFormatInformation(
|
||||
MASKED_TEST_FORMAT_INFO ^ 0x03, MASKED_TEST_FORMAT_INFO ^ 0x0F));
|
||||
}
|
||||
|
||||
}
|
||||
70
src/qrcode/decoder/FormatInformationTestCase.rs
Normal file
70
src/qrcode/decoder/FormatInformationTestCase.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2007 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::qrcode::decoder::{FormatInformation, ErrorCorrectionLevel};
|
||||
|
||||
/**
|
||||
* @author Sean Owen
|
||||
*/
|
||||
|
||||
|
||||
const MASKED_TEST_FORMAT_INFO : u32 = 0x2BED;
|
||||
const UNMASKED_TEST_FORMAT_INFO : u32= MASKED_TEST_FORMAT_INFO ^ 0x5412;
|
||||
|
||||
#[test]
|
||||
fn testBitsDiffering() {
|
||||
assert_eq!(0, FormatInformation::numBitsDiffering(1, 1));
|
||||
assert_eq!(1, FormatInformation::numBitsDiffering(0, 2));
|
||||
assert_eq!(2, FormatInformation::numBitsDiffering(1, 2));
|
||||
assert_eq!(32, FormatInformation::numBitsDiffering(-1i32 as u32, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn testDecode() {
|
||||
// Normal case
|
||||
let expected =
|
||||
FormatInformation::decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO);
|
||||
assert!(expected.is_some());
|
||||
let expected = expected.unwrap();
|
||||
assert_eq!( 0x07, expected.getDataMask());
|
||||
assert_eq!(ErrorCorrectionLevel::Q, expected.getErrorCorrectionLevel());
|
||||
// where the code forgot the mask!
|
||||
assert_eq!(expected,
|
||||
FormatInformation::decodeFormatInformation(UNMASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO).expect("return"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn testDecodeWithBitDifference() {
|
||||
let expected =
|
||||
FormatInformation::decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO).unwrap();
|
||||
// 1,2,3,4 bits difference
|
||||
assert_eq!(expected, FormatInformation::decodeFormatInformation(
|
||||
MASKED_TEST_FORMAT_INFO ^ 0x01, MASKED_TEST_FORMAT_INFO ^ 0x01).expect("return"));
|
||||
assert_eq!(expected, FormatInformation::decodeFormatInformation(
|
||||
MASKED_TEST_FORMAT_INFO ^ 0x03, MASKED_TEST_FORMAT_INFO ^ 0x03).expect("return"));
|
||||
assert_eq!(expected, FormatInformation::decodeFormatInformation(
|
||||
MASKED_TEST_FORMAT_INFO ^ 0x07, MASKED_TEST_FORMAT_INFO ^ 0x07).expect("return"));
|
||||
assert!(FormatInformation::decodeFormatInformation(
|
||||
MASKED_TEST_FORMAT_INFO ^ 0x0F, MASKED_TEST_FORMAT_INFO ^ 0x0F).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn testDecodeWithMisread() {
|
||||
let expected =
|
||||
FormatInformation::decodeFormatInformation(MASKED_TEST_FORMAT_INFO, MASKED_TEST_FORMAT_INFO).unwrap();
|
||||
assert_eq!(expected, FormatInformation::decodeFormatInformation(
|
||||
MASKED_TEST_FORMAT_INFO ^ 0x03, MASKED_TEST_FORMAT_INFO ^ 0x0F).unwrap());
|
||||
}
|
||||
@@ -64,7 +64,7 @@ const FORMAT_INFO_DECODE_LOOKUP: [[u32; 2]; 32] = [
|
||||
* @see DataMask
|
||||
* @see ErrorCorrectionLevel
|
||||
*/
|
||||
#[derive(Hash, Eq, PartialEq)]
|
||||
#[derive(Hash, Eq, PartialEq, Debug)]
|
||||
pub struct FormatInformation {
|
||||
error_correction_level: ErrorCorrectionLevel,
|
||||
data_mask: u8,
|
||||
@@ -95,22 +95,21 @@ impl FormatInformation {
|
||||
* @return information about the format it specifies, or {@code null}
|
||||
* if doesn't seem to match any known pattern
|
||||
*/
|
||||
fn decodeFormatInformation(
|
||||
maskedFormatInfo1: u32,
|
||||
maskedFormatInfo2: u32,
|
||||
) -> FormatInformation {
|
||||
let formatInfo = Self::doDecodeFormatInformation(maskedFormatInfo1, maskedFormatInfo2);
|
||||
pub fn decodeFormatInformation(
|
||||
masked_format_info1: u32,
|
||||
masked_format_info2: u32,
|
||||
) -> Option<FormatInformation> {
|
||||
let formatInfo = Self::doDecodeFormatInformation(masked_format_info1, masked_format_info2);
|
||||
if formatInfo.is_some() {
|
||||
return formatInfo.unwrap();
|
||||
return formatInfo
|
||||
}
|
||||
// Should return null, but, some QR codes apparently
|
||||
// do not mask this info. Try again by actually masking the pattern
|
||||
// first
|
||||
return Self::doDecodeFormatInformation(
|
||||
maskedFormatInfo1 ^ FORMAT_INFO_MASK_QR,
|
||||
maskedFormatInfo2 ^ FORMAT_INFO_MASK_QR,
|
||||
Self::doDecodeFormatInformation(
|
||||
masked_format_info1 ^ FORMAT_INFO_MASK_QR,
|
||||
masked_format_info2 ^ FORMAT_INFO_MASK_QR,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn doDecodeFormatInformation(
|
||||
@@ -149,8 +148,8 @@ impl FormatInformation {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn getErrorCorrectionLevel(&self) -> &ErrorCorrectionLevel {
|
||||
&self.error_correction_level
|
||||
pub fn getErrorCorrectionLevel(&self) -> ErrorCorrectionLevel {
|
||||
self.error_correction_level
|
||||
}
|
||||
|
||||
pub fn getDataMask(&self) -> u8 {
|
||||
|
||||
@@ -11,6 +11,8 @@ mod ErrorCorrectionLevelTestCase;
|
||||
mod ModeTestCase;
|
||||
#[cfg(test)]
|
||||
mod VersionTestCase;
|
||||
#[cfg(test)]
|
||||
mod FormatInformationTestCase;
|
||||
|
||||
pub use version::*;
|
||||
pub use mode::*;
|
||||
|
||||
Reference in New Issue
Block a user