#!/usr/bin/env python3 # SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie # SPDX-License-Identifier: MIT """Makes the test vectors in tests/data with the reference compressor. Every input is generated here from a fixed seed, so nothing of anyone else's is checked in and the whole set can be made again exactly: nix develop -c python3 tools/make_vectors.py Each input is compressed at a spread of qualities, window sizes and modes, which between them make the compressor use everything a decompressor has to handle: uncompressed and compressed meta-blocks, every context mode, block switching, the static dictionary and its transforms, and windows small enough to wrap many times over. Only the compressed streams are kept. tests/vectors.zig records the length and SHA-256 of what each must decompress to, and each is checked here against the reference decoder before it is written. """ import hashlib import math import os import random import struct import brotli HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.dirname(HERE) DATA = os.path.join(ROOT, "tests", "data") def dictionary_words(): """The ASCII words of the static dictionary, which is what makes word salad compress through dictionary references.""" data = open(os.path.join(ROOT, "src", "dictionary.bin"), "rb").read() ndbits = [0, 0, 0, 0, 10, 10, 11, 11, 10, 10, 10, 10, 10, 9, 9, 8, 7, 7, 8, 7, 7, 6, 6, 5, 5] words, offset = [], 0 for length, bits in enumerate(ndbits): if length < 4: continue for i in range(1 << bits): w = data[offset + i * length : offset + (i + 1) * length] if all(97 <= c <= 122 for c in w): words.append(w) offset += length << bits return words def text(size, seed): """Sentences of dictionary words, with capitals, punctuation and the odd HTML tag, which is the kind of input the dictionary's transforms were made for.""" rng = random.Random(seed) words = dictionary_words() common = words[:300] out = bytearray() while len(out) < size: n = rng.randint(4, 16) sentence = [] for i in range(n): w = rng.choice(common if rng.random() < 0.7 else words) if i == 0 or rng.random() < 0.05: w = w[:1].upper() + w[1:] if rng.random() < 0.02: w = w.upper() sentence.append(w) s = b" ".join(sentence) + rng.choice([b". ", b", ", b"! ", b"? ", b".\n", b"\n\n"]) if rng.random() < 0.05: s = b'' + s + b"" out += s return bytes(out[:size]) def noise(size, seed): return random.Random(seed).randbytes(size) def samples(size, seed): """Little-endian 16-bit samples of a noisy chord: what the signed context mode is for.""" rng = random.Random(seed) out = bytearray() for i in range(size // 2): v = 8000 * math.sin(i / 20) + 3000 * math.sin(i / 7.3) + rng.gauss(0, 200) out += struct.pack(" %8d" % (name, len(data), len(c))) with open(os.path.join(ROOT, "tests", "vectors.zig"), "w") as f: # REUSE-IgnoreStart f.write("// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie \n") f.write("// SPDX-License-Identifier: MIT\n\n") # REUSE-IgnoreEnd f.write("//! Streams made by the reference compressor, with the length and SHA-256 of\n") f.write("//! what each decompresses to. Generated by tools/make_vectors.py; do not\n") f.write("//! edit.\n\n") f.write("pub const Vector = struct { name: []const u8, compressed: []const u8, length: usize, sha256: []const u8 };\n\n") f.write("pub const vectors = [_]Vector{\n") for name, length, digest in rows: f.write(' .{ .name = "%s", .compressed = @embedFile("data/%s.br"), .length = %d, .sha256 = "%s" },\n' % (name, name, length, digest)) f.write("};\n") if __name__ == "__main__": main()