port QRDataMaskTest

This commit is contained in:
Henry Schimke
2023-04-14 11:55:56 -05:00
parent de01f5c749
commit 8462c7e877
3 changed files with 137 additions and 100 deletions

View File

@@ -1,100 +0,0 @@
/*
* Copyright 2017 Huy Cuong Nguyen
* Copyright 2007 ZXing authors
*/
// SPDX-License-Identifier: Apache-2.0
#include "qrcode/QRDataMask.h"
#include "BitMatrix.h"
#include "gtest/gtest.h"
#include <functional>
using namespace ZXing;
using namespace ZXing::QRCode;
namespace {
void TestMaskAcrossDimensionsImpl(int maskIndex, bool isMicro, const int versionMax, const int dimensionStart, const int dimensionStep, std::function<bool(int, int)> condition)
{
for (int version = 1; version <= versionMax; version++) {
int dimension = dimensionStart + dimensionStep * version;
BitMatrix bits(dimension);
for (int i = 0; i < dimension; i++)
for (int j = 0; j < dimension; j++)
EXPECT_EQ(GetMaskedBit(bits, j, i, maskIndex, isMicro), condition(i, j)) << "(" << i << ',' << j << ')';
}
}
void TestMaskAcrossDimensions(int maskIndex, std::function<bool(int, int)> condition)
{
TestMaskAcrossDimensionsImpl(maskIndex, false, 40, 17, 4, condition);
}
void TestMicroMaskAcrossDimensions(int maskIndex, std::function<bool(int, int)> condition)
{
TestMaskAcrossDimensionsImpl(maskIndex, true, 4, 9, 2, condition);
}
}
TEST(QRDataMaskTest, Mask0)
{
TestMaskAcrossDimensions(0, [](int i, int j) { return (i + j) % 2 == 0; });
}
TEST(QRDataMaskTest, Mask1)
{
TestMaskAcrossDimensions(1, [](int i, int) { return i % 2 == 0; });
}
TEST(QRDataMaskTest, Mask2)
{
TestMaskAcrossDimensions(2, [](int, int j) { return j % 3 == 0; });
}
TEST(QRDataMaskTest, Mask3)
{
TestMaskAcrossDimensions(3, [](int i, int j) { return (i + j) % 3 == 0; });
}
TEST(QRDataMaskTest, Mask4)
{
TestMaskAcrossDimensions(4, [](int i, int j) { return (i / 2 + j / 3) % 2 == 0; });
}
TEST(QRDataMaskTest, Mask5)
{
TestMaskAcrossDimensions(5, [](int i, int j) { return (i * j) % 2 + (i * j) % 3 == 0; });
}
TEST(QRDataMaskTest, Mask6)
{
TestMaskAcrossDimensions(6, [](int i, int j) { return ((i * j) % 2 + (i * j) % 3) % 2 == 0; });
}
TEST(QRDataMaskTest, Mask7)
{
TestMaskAcrossDimensions(7, [](int i, int j) { return ((i + j) % 2 + (i * j) % 3) % 2 == 0; });
}
TEST(QRDataMaskTest, MicroMask0)
{
TestMicroMaskAcrossDimensions(0, [](int i, int) { return i % 2 == 0; });
}
TEST(QRDataMaskTest, MicroMask1)
{
TestMicroMaskAcrossDimensions(1, [](int i, int j) { return (i / 2 + j / 3) % 2 == 0; });
}
TEST(QRDataMaskTest, MicroMask2)
{
TestMicroMaskAcrossDimensions(2, [](int i, int j) { return ((i * j) % 2 + (i * j) % 3) % 2 == 0; });
}
TEST(QRDataMaskTest, MicroMask3)
{
TestMicroMaskAcrossDimensions(3, [](int i, int j) { return ((i + j) % 2 + (i * j) % 3) % 2 == 0; });
}

View File

@@ -0,0 +1,135 @@
/*
* Copyright 2017 Huy Cuong Nguyen
* Copyright 2007 ZXing authors
*/
// SPDX-License-Identifier: Apache-2.0
use crate::{common::BitMatrix, qrcode::cpp_port::data_mask::GetMaskedBit};
#[test]
fn Mask0() {
TestMaskAcrossDimensions(0, |i, j| {
return (i + j) % 2 == 0;
});
}
#[test]
fn Mask1() {
TestMaskAcrossDimensions(1, |i, _| {
return i % 2 == 0;
});
}
#[test]
fn Mask2() {
TestMaskAcrossDimensions(2, |_, j| {
return j % 3 == 0;
});
}
#[test]
fn Mask3() {
TestMaskAcrossDimensions(3, |i, j| {
return (i + j) % 3 == 0;
});
}
#[test]
fn Mask4() {
TestMaskAcrossDimensions(4, |i, j| {
return (i / 2 + j / 3) % 2 == 0;
});
}
#[test]
fn Mask5() {
TestMaskAcrossDimensions(5, |i, j| {
return (i * j) % 2 + (i * j) % 3 == 0;
});
}
#[test]
fn Mask6() {
TestMaskAcrossDimensions(6, |i, j| {
return ((i * j) % 2 + (i * j) % 3) % 2 == 0;
});
}
#[test]
fn Mask7() {
TestMaskAcrossDimensions(7, |i, j| {
return ((i + j) % 2 + (i * j) % 3) % 2 == 0;
});
}
#[test]
fn MicroMask0() {
TestMicroMaskAcrossDimensions(0, |i, _| {
return i % 2 == 0;
});
}
#[test]
fn MicroMask1() {
TestMicroMaskAcrossDimensions(1, |i, j| {
return (i / 2 + j / 3) % 2 == 0;
});
}
#[test]
fn MicroMask2() {
TestMicroMaskAcrossDimensions(2, |i, j| {
return ((i * j) % 2 + (i * j) % 3) % 2 == 0;
});
}
#[test]
fn MicroMask3() {
TestMicroMaskAcrossDimensions(3, |i, j| {
return ((i + j) % 2 + (i * j) % 3) % 2 == 0;
});
}
fn TestMaskAcrossDimensionsImpl<F>(
maskIndex: u32,
isMicro: bool,
versionMax: u32,
dimensionStart: u32,
dimensionStep: u32,
condition: F,
) where
F: Fn(u32, u32) -> bool,
{
for version in 1..=versionMax {
// for (int version = 1; version <= versionMax; version++) {
let dimension = dimensionStart + dimensionStep * version;
let bits = BitMatrix::with_single_dimension(dimension).expect("couldn't create bitmatrix");
for i in 0..dimension {
// for (int i = 0; i < dimension; i++)
for j in 0..dimension {
// for (int j = 0; j < dimension; j++)
assert_eq!(
GetMaskedBit(&bits, j, i, maskIndex, Some(isMicro))
.expect("could not get mask bit"),
condition(i, j),
"({i},{j})"
);
}
}
}
}
fn TestMaskAcrossDimensions<F>(maskIndex: u32, condition: F)
where
F: Fn(u32, u32) -> bool,
{
TestMaskAcrossDimensionsImpl(maskIndex, false, 40, 17, 4, condition);
}
fn TestMicroMaskAcrossDimensions<F>(maskIndex: u32, condition: F)
where
F: Fn(u32, u32) -> bool,
{
TestMaskAcrossDimensionsImpl(maskIndex, true, 4, 9, 2, condition);
}

View File

@@ -6,3 +6,5 @@ mod QRFormatInformationTest;
mod QRErrorCorrectionLevelTest;
mod QRDecodedBitStreamParserTest;
mod QRDataMaskTest;