aboutsummaryrefslogtreecommitdiff
path: root/test/codes/AztecTest.mc
blob: 6ebc2bbec26840a2a28176f776e4cc44d3718422 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Author:  cirakg & claude ai
// Summary: Tests for the Aztec encoder: aztec_code_generator references, decoding dumps (mixed text, UTF-8), limits and stuffing.

import Toybox.Lang;
import Toybox.Test;

// README 12.4 check 1 where comparable: single-class payloads give the same
// bits as the reference's optimal sequence, so the symbols must match,
// compact and full range (with reference grids), at all four EC levels.
(:test)
function aztecMatchesReference(logger as Test.Logger) as Boolean {
    var rows = CodeVectors.AZTEC;
    for (var i = 0; i < rows.size(); i++) {
        var row = rows[i] as Array;
        var payload = row[0] as String;
        var result = Aztec.encode(payload, row[1] as Number);
        if (result instanceof Codes.CodeError) {
            logger.error("row " + i + ": " + result.message);
            return false;
        }
        var expectedSize = row[2] as Number;
        var expected = row[3] as String;
        if (result.width != expectedSize || !MatrixFixtures.packedHex(result).equals(expected)) {
            logger.error("row " + i + " '" + Text.clip(payload, 20) + "': size " + result.width + ", reference " + expectedSize);
            return false;
        }
    }
    logger.debug("CODES aztec reference rows=" + rows.size());
    return true;
}

// README 12.4 check 2: mixed content (Binary Shift, mode latches), UTF-8
// and long payloads, decoded by tools/check_codes.py.
(:test)
function aztecDumpForDecoding(logger as Test.Logger) as Boolean {
    var payloads = ["Boarding pass FAKE 0042, seat 12A", "Zürich ✓ Ünïcödé", "https://example.com/m/fake?id=0042",
        "abcDEF123 xyz", "!@#$%^&*()", "a", "MiXeD 12 cAsE 34 ."];
    var long = "";
    for (var i = 0; i < 40; i++) {
        long += "Fake-Ticket/" + i + " ";
    }
    payloads.add(long);
    for (var i = 0; i < payloads.size(); i++) {
        var text = payloads[i] as String;
        var result = Aztec.encode(text, i % 4);
        if (result instanceof Codes.CodeError) {
            logger.error("'" + text + "': " + result.message);
            return false;
        }
        MatrixFixtures.dump(logger, Codes.AZTEC, text, result);
    }
    return true;
}

(:test)
function aztecStuffingAndLimits(logger as Test.Logger) as Boolean {
    // Six zero bits in 6-bit codewords: 00000 gets a stuffed 1, then the
    // last 0 is padded with ones (and its all-ones last bit cleared).
    var words = Aztec.stuff([0, 0, 0, 0, 0, 0]b, 6);
    if (words.size() != 2 || words[0] != 1 || words[1] != 0x1F) {
        logger.error("stuffing gave " + words.toString());
        return false;
    }
    if (!(Aztec.encode("", 1) instanceof Codes.CodeError)) {
        logger.error("empty text accepted");
        return false;
    }
    var big = "";
    for (var i = 0; i < 400; i++) {
        big += "~~~~~~~~~~";
    }
    return Aztec.encode(big, 3) instanceof Codes.CodeError;
}