aboutsummaryrefslogtreecommitdiff
path: root/test/codes/CodeCacheTest.mc
diff options
context:
space:
mode:
authorcirakg <cirakg@cirakg.xyz>2026-10-21 03:55:01 +0200
committercirakg <cirakg@cirakg.xyz>2026-10-21 03:55:01 +0200
commit88f1cac4fa530fc30fd6ef01970af11c7a959b8e (patch)
tree4e2e52b36a6faa2dd50d3b6b07fbfe46fb5dc48c /test/codes/CodeCacheTest.mc
parenta69e23fb5266dd942d125239e7dcbe8a7c8ab956 (diff)
fix(codes): bound every 2D encoding step under the watchdog; draw large codes from runs
M6's step sizes were estimates, only exercised with small codes. With the largest inputs the app accepts, every 2D encoder tripped the watchdog on the Forerunner 255S (and the biggest steps were far over the 768 KB watches' limit), so a large code added from the phone would have stopped the app during settings processing at every start. - QR: resumable segmentation, Reed-Solomon, interleaving, function patterns and final mask; mask scoring on rows and columns packed 16 modules per word (same penalties, checked against the per-module rules). - Reed-Solomon: resumable field tables and generator, encoder shifting with native slice. - Data Matrix, Aztec, PDF417: resumable encodation, stuffing, compaction, placement and build; Data Matrix's grid as a byte array (90 KB -> 35 KB); PDF417 refuses text that can't fit the screen before compacting. - Cache format 2 with 16-bit sizes (wide PDF417 was cached wrong); packing in steps; codes drawn as runs, and from a bitmap painted in steps when a frame can't draw all runs. - The job runner does two steps per tick on 768 KB watches; gen_products --check verifies each product's watchdogCount fits that. - CodeView renews full brightness (AMOLED) or the backlight (MIP) for up to a minute from its single ticker; a tap restarts the minute. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'test/codes/CodeCacheTest.mc')
-rw-r--r--test/codes/CodeCacheTest.mc58
1 files changed, 56 insertions, 2 deletions
diff --git a/test/codes/CodeCacheTest.mc b/test/codes/CodeCacheTest.mc
index 812acc9..2ad7f05 100644
--- a/test/codes/CodeCacheTest.mc
+++ b/test/codes/CodeCacheTest.mc
@@ -1,5 +1,5 @@
// Author: cirakg & claude ai
-// Summary: Tests for the encrypted barcode matrix cache: round trip, keys of locked entries, invalidation and tampering.
+// Summary: Tests for the encrypted barcode matrix cache: round trip, packed format, runs, keys of locked entries, invalidation and tampering.
import Toybox.Lang;
import Toybox.Test;
@@ -53,6 +53,60 @@ function codeCacheOfLockedEntriesNeedsTheSession(logger as Test.Logger) as Boole
pdf.rowHeight = 3;
var round = Codes.Matrix.fromBytes(pdf.toBytes());
return code == VaultError.LOCKED && back != null && Bytes.equal(back, [3, 1, 4, 1, 0xE0]b)
- && Codes.Matrix.fromBytes([3, 1, 4, 1, 0xE0]b) != null && Codes.Matrix.fromBytes([3, 1, 4]b) == null
+ && Codes.Matrix.fromBytes([2, 0, 3, 0, 1, 4, 1, 0xE0]b) != null && Codes.Matrix.fromBytes([2, 0, 3, 0, 1, 4]b) == null
&& round != null && round.rowHeight == 3 && Bytes.equal(round.modules, [1, 0, 1, 0, 1, 1]b);
}
+
+// Format 2 holds widths over 255 (wide PDF417); a 0.7 cache (one width
+// byte) is refused, so the code is encoded again.
+(:test)
+function codeCachePackedFormat(logger as Test.Logger) as Boolean {
+ var bits = new [300 * 2]b;
+ for (var i = 0; i < bits.size(); i++) {
+ bits[i] = (i * 7 / 3) % 2;
+ }
+ var wide = new Codes.Matrix(300, 2, 2, bits);
+ var bytes = wide.toBytes();
+ var back = Codes.Matrix.fromBytes(bytes);
+ var oldFormat = Codes.Packed.parse([3, 1, 4, 1, 0xE0]b);
+ var wrongSize = Codes.Packed.parse(bytes.slice(0, bytes.size() - 1));
+ return bytes[0] == 2 && back != null && back.width == 300 && back.height == 2 && Bytes.equal(back.modules, bits)
+ && oldFormat == null && wrongSize == null && Codes.Packed.parse(null) == null;
+}
+
+// The runs drawn on screen are exactly the dark modules, row by row,
+// including runs across byte boundaries and whole dark or light bytes.
+(:test)
+function codeRunsCoverExactlyTheDarkModules(logger as Test.Logger) as Boolean {
+ var codes = [Qr.encode("runs, runs, runs", 1, 0, -1) as Qr.MatrixCode];
+ var matrices = [new Codes.Matrix(codes[0].size, codes[0].size, 4, codes[0].modules)] as Array<Codes.Matrix>;
+ var stripes = new [37 * 5]b;
+ for (var i = 0; i < stripes.size(); i++) {
+ stripes[i] = (i % 37) < 20 || (i % 37) > 33 ? 1 : 0;
+ }
+ matrices.add(new Codes.Matrix(37, 5, 1, stripes));
+ for (var c = 0; c < matrices.size(); c++) {
+ var m = matrices[c];
+ var finder = new Codes.RunFinder(Codes.Packed.parse(m.toBytes()) as Codes.Packed);
+ Jobs.runToCompletion(finder, 1);
+ var drawn = new [m.width * m.height]b;
+ var runs = finder.result.runs;
+ for (var i = 0; i < runs.size(); i++) {
+ var r = runs[i];
+ var y = r >> 20;
+ var x = (r >> 10) & 0x3FF;
+ for (var k = 0; k < (r & 0x3FF); k++) {
+ if (drawn[y * m.width + x + k] == 1) {
+ logger.error("matrix " + c + ": run " + i + " overlaps");
+ return false;
+ }
+ drawn[y * m.width + x + k] = 1;
+ }
+ }
+ if (!Bytes.equal(drawn, m.modules)) {
+ logger.error("matrix " + c + ": runs differ from the modules");
+ return false;
+ }
+ }
+ return true;
+}