diff --git a/src/qrcode/cpp_port/test/QRDataMaskTest.cpp b/src/qrcode/cpp_port/test/QRDataMaskTest.cpp deleted file mode 100644 index 24baa4a..0000000 --- a/src/qrcode/cpp_port/test/QRDataMaskTest.cpp +++ /dev/null @@ -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 - -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 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 condition) - { - TestMaskAcrossDimensionsImpl(maskIndex, false, 40, 17, 4, condition); - } - - void TestMicroMaskAcrossDimensions(int maskIndex, std::function 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; }); -} diff --git a/src/qrcode/cpp_port/test/QRDataMaskTest.rs b/src/qrcode/cpp_port/test/QRDataMaskTest.rs new file mode 100644 index 0000000..12e7d0b --- /dev/null +++ b/src/qrcode/cpp_port/test/QRDataMaskTest.rs @@ -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( + 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(maskIndex: u32, condition: F) +where + F: Fn(u32, u32) -> bool, +{ + TestMaskAcrossDimensionsImpl(maskIndex, false, 40, 17, 4, condition); +} + +fn TestMicroMaskAcrossDimensions(maskIndex: u32, condition: F) +where + F: Fn(u32, u32) -> bool, +{ + TestMaskAcrossDimensionsImpl(maskIndex, true, 4, 9, 2, condition); +} diff --git a/src/qrcode/cpp_port/test/mod.rs b/src/qrcode/cpp_port/test/mod.rs index 9383384..5005ff1 100644 --- a/src/qrcode/cpp_port/test/mod.rs +++ b/src/qrcode/cpp_port/test/mod.rs @@ -6,3 +6,5 @@ mod QRFormatInformationTest; mod QRErrorCorrectionLevelTest; mod QRDecodedBitStreamParserTest; + +mod QRDataMaskTest;