diff options
| author | cirakg <cirakg@cirakg.xyz> | 2026-10-20 02:43:08 +0200 |
|---|---|---|
| committer | cirakg <cirakg@cirakg.xyz> | 2026-10-20 02:43:08 +0200 |
| commit | fa9c695256de1c7fe007ba5d6bb9b28b1deb5a5a (patch) | |
| tree | bd418bc860349833d5a19c4af6bb8a61666477ca /tools/tests/test_barcode_tools.py | |
| parent | 59255b1a55a81ba2b5de967eefced11910597b48 (diff) | |
test(codes): compare with zxing-cpp reference rows and decode encoder output and screenshots
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tools/tests/test_barcode_tools.py')
| -rw-r--r-- | tools/tests/test_barcode_tools.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/tools/tests/test_barcode_tools.py b/tools/tests/test_barcode_tools.py new file mode 100644 index 0000000..7be1f18 --- /dev/null +++ b/tools/tests/test_barcode_tools.py @@ -0,0 +1,65 @@ +# Author: cirakg & claude ai +# Summary: Tests for the barcode reference tools: zxing-cpp rows, rendering, decoding, check digits, generator, checker. +from __future__ import annotations + +import random +from pathlib import Path + +import barcode_ref as ref +import check_codes +import gen_code_vectors + +ROOT = Path(__file__).resolve().parents[2] + + +def test_reference_rows_have_the_standard_lengths() -> None: + assert ref.reference_modules(ref.EAN13, "590123412345") == ( + "10100010110100111011001100100110111101001110101010110011011011001000010101110010011101000100101" + ) + assert len(ref.reference_modules(ref.EAN8, "1234567")) == 67 + assert len(ref.reference_modules(ref.UPCE, "0425261")) == 51 + + +def test_rendered_codes_decode() -> None: + modules = ref.reference_modules(ref.CODE39, "ABC") + for scale in (1, 2, 3): + assert ref.decode(ref.render_linear(modules, scale), ref.CODE39) == ["ABC"] + + +def test_check_digits() -> None: + assert ref.gs1_check("590123412345") == 7 + assert ref.gs1_check("1234567") == 0 + assert ref.code39_check("CODE 39") == "R" # (12+24+13+14+38+3+9) % 43 = 27 + + +def test_expected_reading_normalises_upc() -> None: + assert ref.expected_reading(ref.UPCA, "012345678905") == "0012345678905" + assert ref.upce_expand("01234565") == "0012345000065" + + +def test_generator_only_makes_canonical_upce() -> None: + rng = random.Random(5) + for _ in range(200): + d = gen_code_vectors.valid_upce(rng) + ref.reference_modules(ref.UPCE, "0" + d) # zxing-cpp rejects non-canonical ones + + +def test_committed_vectors_match_the_generator() -> None: + rows = gen_code_vectors.cases(random.Random(1212), 25) + committed = (ROOT / "test" / "data" / "CodeVectors.mc").read_text(encoding="utf-8") + assert committed == gen_code_vectors.render(rows, 1212) + + +def test_check_codes_reads_dump_lines(tmp_path: Path) -> None: + good = ref.reference_modules(ref.EAN8, "1234567") + log = tmp_path / "dev.log" + log.write_text( + f"CODEDUMP {ref.EAN8} {b'12345670'.hex()} {good}\nCODEDUMP {ref.EAN8} {b'12345670'.hex()} {good[:-5]}00000\n", + encoding="utf-8", + ) + checked, failures = check_codes.check_log(log) + assert checked == 2 and len(failures) == 3 # the damaged code fails at all three sizes + assert check_codes.main([str(log)]) == 1 + empty = tmp_path / "empty.log" + empty.write_text("nothing\n", encoding="utf-8") + assert check_codes.main([str(empty)]) == 2 |
