Skip to content

refactor(test): read and parse the shared vector file once per run - #107

Merged
Andste82 merged 1 commit into
mainfrom
refactor/100-parse-vector-file-once
Aug 9, 2026
Merged

Andste82 merged 1 commit into
mainfrom
refactor/100-parse-vector-file-once

Conversation

@Andste82

@Andste82 Andste82 commented Aug 9, 2026

Copy link
Copy Markdown
Member

test/test_vectors.cpp decoded the shared vector envelope in two independent,
byte-for-byte duplicated loaders: loadVectors() and loadNegVectors() each
opened SOFAB_TEST_VECTORS_PATH, read it whole, JSON-parsed it, walked their own
top-level group (vectors / invalid_utf8) and freed the root. main() called
both, so the ~55 KB file was opened, read and fully parsed twice per ctest
run, and the prologue plus the requires-tag loop had to be kept in step by hand.

Fix

Parse once, walk twice:

  • VectorFile owns the text and the parsed root and frees it on scope exit;
    loadVectorFile() fills it.
  • loadVectors() / loadNegVectors() take const sofab_json_t *root instead of
    a path — no open/read/parse/free prologue, no sofab_json_free(root) on their
    error paths (the holder owns it).
  • The duplicated capability-tag loop becomes reqMask().

The duplication also hid a failure mode the issue calls out: a walker whose
top-level key upstream renames or drops just walked nothing and returned
success, which reads as "nothing to test" while the suite stays green. Both
walkers now pull their group through group(), which demands a present,
non-empty array and otherwise reports vector file has no non-empty "<key>" array.

Regression tests (in the normal suite)

  • vector-file-read-once — the file was read exactly once this run.
  • envelope-both-groups, envelope-no-invalid_utf8, envelope-no-vectors,
    envelope-empty-groups, envelope-renamed-keys — doctored envelopes are
    parsed in memory and fed to both walkers; each case requires the walker's
    verdict and its vector count to match, so a rejected group has to be
    rejected rather than silently empty (the two are indistinguishable if only the
    count is checked).

Verified against the pre-fix code: both loaders accept a {"vectors_v2":…, "invalid_utf8_v2":…} envelope with ok=1, count=0. Verified by mutation on the
fixed code: reintroducing a second loadVectorFile() fails
vector-file-read-once; loosening group() back to a silent empty walk fails
envelope-empty-groups; renaming a walker's key fails the run with the new
message.

Unchanged behaviour

Same failure text on a missing (cannot open vector file) or unparsable
(json parse: …) file, same vector set: 81 vectors, 11 invalid_utf8, 723
checks before and the same 723 + 6 new guard checks (729) after. Docs: README's
test_vectors bullet and test/shared/README.md §"Keep in sync" now state the
single-parse rule and what an envelope reshape looks like after a re-sync.

Closes #100

test_vectors.cpp carried two byte-for-byte identical loading prologues:
loadVectors() and loadNegVectors() each opened SOFAB_TEST_VECTORS_PATH,
read it whole and JSON-parsed it, then walked their own top-level group
("vectors" / "invalid_utf8") and freed the root. main() called both, so
the ~55 KB file was opened, read and parsed twice on every ctest run and
the prologue plus the "requires"-tag loop had to be kept in step by hand.

Parse once, walk twice: a VectorFile holder owns the text and the parsed
root (freeing it on scope exit), loadVectorFile() fills it, and both
walkers now take the parsed root instead of a path. The duplicated
capability-tag loop becomes reqMask().

The duplication also hid a failure mode: a walker whose top-level key has
been renamed or dropped upstream just walked nothing and returned
success, which reads as "nothing to test" while the suite stays green.
Both walkers now pull their group through group(), which demands a
present, non-empty array and reports "vector file has no non-empty
"<key>" array" otherwise.

Guards for both halves run in the normal suite: vector-file-read-once
asserts the file was read exactly once, and five envelope cases feed
doctored envelopes (both groups, each group alone, empty groups, renamed
keys) to the walkers in memory and require the verdict AND the vector
count to match — a rejected group must be rejected, not silently empty.
The vector set is unchanged: 81 vectors, 11 invalid_utf8, 723 checks
before and the same 723 plus the 6 new guard checks after.

Closes #100
@Andste82
Andste82 merged commit 2f5dd89 into main Aug 9, 2026
7 checks passed
@Andste82
Andste82 deleted the refactor/100-parse-vector-file-once branch August 9, 2026 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7.1: test_vectors.cpp reads and JSON-parses the shared vector file twice per run

1 participant