cultcache-ts tests: numeric leaves held to the C# reference's reads and writes

The table is the reference's own answer for 120 one-slot reads and 46 writes, from a job-only probe of src/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-11 04:55:22 +02:00
commit 17df29cc9c
2 changed files with 221 additions and 3 deletions

View file

@ -0,0 +1,176 @@
// Oracle for the numeric leaves, written by the C# reference. Each READ row is a one-slot record [value] fed to a C#
// MessagePackSerializer.Deserialize of a class with one int, long, float or double field: ACCEPT <value> (float
// values as the double's IEEE bits) or REFUSE. Each WRITE row is the bytes this runtime writes for a value, which
// the C# reader read and re-serialized to the same bytes. Produced by a job-only probe of src/ at this commit's
// parent range; the C# test project owns no numeric vector.
export const referenceReads: readonly (readonly [leaf: string, value: string, outcome: string])[] = [
["int32", "05", "ACCEPT 5"],
["int32", "ff", "ACCEPT -1"],
["int32", "ccc8", "ACCEPT 200"],
["int32", "cd0100", "ACCEPT 256"],
["int32", "ceb2d05e00", "REFUSE"],
["int32", "d0ce", "ACCEPT -50"],
["int32", "d18000", "ACCEPT -32768"],
["int32", "d280000000", "ACCEPT -2147483648"],
["int32", "d38000000000000000", "REFUSE"],
["int32", "cf0020000000000001", "REFUSE"],
["int32", "cfffffffffffffffff", "REFUSE"],
["int32", "cf8000000000000000", "REFUSE"],
["int32", "d30020000000000001", "REFUSE"],
["int32", "d3fffffffffffffff0", "ACCEPT -16"],
["int32", "ca3fc00000", "REFUSE"],
["int32", "cb3ff8000000000000", "REFUSE"],
["int32", "cb401c000000000000", "REFUSE"],
["int32", "cb7ff8000000000000", "REFUSE"],
["int32", "ca7f800000", "REFUSE"],
["int32", "cb7e37e43c8800759c", "REFUSE"],
["int32", "cb47efffffe0000000", "REFUSE"],
["int32", "cb3fb999999999999a", "REFUSE"],
["int32", "ca3dcccccd", "REFUSE"],
["int32", "cb8000000000000000", "REFUSE"],
["int32", "c0", "REFUSE"],
["int32", "c3", "REFUSE"],
["int32", "a161", "REFUSE"],
["int32", "c40100", "REFUSE"],
["int32", "90", "REFUSE"],
["int32", "80", "REFUSE"],
["int64", "05", "ACCEPT 5"],
["int64", "ff", "ACCEPT -1"],
["int64", "ccc8", "ACCEPT 200"],
["int64", "cd0100", "ACCEPT 256"],
["int64", "ceb2d05e00", "ACCEPT 3000000000"],
["int64", "d0ce", "ACCEPT -50"],
["int64", "d18000", "ACCEPT -32768"],
["int64", "d280000000", "ACCEPT -2147483648"],
["int64", "d38000000000000000", "ACCEPT -9223372036854775808"],
["int64", "cf0020000000000001", "ACCEPT 9007199254740993"],
["int64", "cfffffffffffffffff", "REFUSE"],
["int64", "cf8000000000000000", "REFUSE"],
["int64", "d30020000000000001", "ACCEPT 9007199254740993"],
["int64", "d3fffffffffffffff0", "ACCEPT -16"],
["int64", "ca3fc00000", "REFUSE"],
["int64", "cb3ff8000000000000", "REFUSE"],
["int64", "cb401c000000000000", "REFUSE"],
["int64", "cb7ff8000000000000", "REFUSE"],
["int64", "ca7f800000", "REFUSE"],
["int64", "cb7e37e43c8800759c", "REFUSE"],
["int64", "cb47efffffe0000000", "REFUSE"],
["int64", "cb3fb999999999999a", "REFUSE"],
["int64", "ca3dcccccd", "REFUSE"],
["int64", "cb8000000000000000", "REFUSE"],
["int64", "c0", "REFUSE"],
["int64", "c3", "REFUSE"],
["int64", "a161", "REFUSE"],
["int64", "c40100", "REFUSE"],
["int64", "90", "REFUSE"],
["int64", "80", "REFUSE"],
["float32", "05", "ACCEPT 4014000000000000"],
["float32", "ff", "ACCEPT bff0000000000000"],
["float32", "ccc8", "ACCEPT 4069000000000000"],
["float32", "cd0100", "ACCEPT 4070000000000000"],
["float32", "ceb2d05e00", "ACCEPT 41e65a0bc0000000"],
["float32", "d0ce", "ACCEPT c049000000000000"],
["float32", "d18000", "ACCEPT c0e0000000000000"],
["float32", "d280000000", "ACCEPT c1e0000000000000"],
["float32", "d38000000000000000", "ACCEPT c3e0000000000000"],
["float32", "cf0020000000000001", "ACCEPT 4340000000000000"],
["float32", "cfffffffffffffffff", "ACCEPT 43f0000000000000"],
["float32", "cf8000000000000000", "ACCEPT 43e0000000000000"],
["float32", "d30020000000000001", "ACCEPT 4340000000000000"],
["float32", "d3fffffffffffffff0", "ACCEPT c030000000000000"],
["float32", "ca3fc00000", "ACCEPT 3ff8000000000000"],
["float32", "cb3ff8000000000000", "ACCEPT 3ff8000000000000"],
["float32", "cb401c000000000000", "ACCEPT 401c000000000000"],
["float32", "cb7ff8000000000000", "ACCEPT NaN"],
["float32", "ca7f800000", "ACCEPT 7ff0000000000000"],
["float32", "cb7e37e43c8800759c", "ACCEPT 7ff0000000000000"],
["float32", "cb47efffffe0000000", "ACCEPT 47efffffe0000000"],
["float32", "cb3fb999999999999a", "ACCEPT 3fb99999a0000000"],
["float32", "ca3dcccccd", "ACCEPT 3fb99999a0000000"],
["float32", "cb8000000000000000", "ACCEPT 8000000000000000"],
["float32", "c0", "REFUSE"],
["float32", "c3", "REFUSE"],
["float32", "a161", "REFUSE"],
["float32", "c40100", "REFUSE"],
["float32", "90", "REFUSE"],
["float32", "80", "REFUSE"],
["float64", "05", "ACCEPT 4014000000000000"],
["float64", "ff", "ACCEPT bff0000000000000"],
["float64", "ccc8", "ACCEPT 4069000000000000"],
["float64", "cd0100", "ACCEPT 4070000000000000"],
["float64", "ceb2d05e00", "ACCEPT 41e65a0bc0000000"],
["float64", "d0ce", "ACCEPT c049000000000000"],
["float64", "d18000", "ACCEPT c0e0000000000000"],
["float64", "d280000000", "ACCEPT c1e0000000000000"],
["float64", "d38000000000000000", "ACCEPT c3e0000000000000"],
["float64", "cf0020000000000001", "ACCEPT 4340000000000000"],
["float64", "cfffffffffffffffff", "ACCEPT 43f0000000000000"],
["float64", "cf8000000000000000", "ACCEPT 43e0000000000000"],
["float64", "d30020000000000001", "ACCEPT 4340000000000000"],
["float64", "d3fffffffffffffff0", "ACCEPT c030000000000000"],
["float64", "ca3fc00000", "ACCEPT 3ff8000000000000"],
["float64", "cb3ff8000000000000", "ACCEPT 3ff8000000000000"],
["float64", "cb401c000000000000", "ACCEPT 401c000000000000"],
["float64", "cb7ff8000000000000", "ACCEPT NaN"],
["float64", "ca7f800000", "ACCEPT 7ff0000000000000"],
["float64", "cb7e37e43c8800759c", "ACCEPT 7e37e43c8800759c"],
["float64", "cb47efffffe0000000", "ACCEPT 47efffffe0000000"],
["float64", "cb3fb999999999999a", "ACCEPT 3fb999999999999a"],
["float64", "ca3dcccccd", "ACCEPT 3fb99999a0000000"],
["float64", "cb8000000000000000", "ACCEPT 8000000000000000"],
["float64", "c0", "REFUSE"],
["float64", "c3", "REFUSE"],
["float64", "a161", "REFUSE"],
["float64", "c40100", "REFUSE"],
["float64", "90", "REFUSE"],
["float64", "80", "REFUSE"],
];
export const referenceWrites: readonly (readonly [leaf: string, value: string, hex: string])[] = [
["int32", "0", "9100"],
["int32", "1", "9101"],
["int32", "127", "917f"],
["int32", "128", "91cc80"],
["int32", "255", "91ccff"],
["int32", "256", "91cd0100"],
["int32", "65535", "91cdffff"],
["int32", "65536", "91ce00010000"],
["int32", "-1", "91ff"],
["int32", "-32", "91e0"],
["int32", "-33", "91d0df"],
["int32", "-128", "91d080"],
["int32", "-129", "91d1ff7f"],
["int32", "-32768", "91d18000"],
["int32", "-32769", "91d2ffff7fff"],
["int32", "2147483647", "91ce7fffffff"],
["int32", "-2147483648", "91d280000000"],
["int64", "0", "9100"],
["int64", "127", "917f"],
["int64", "128", "91cc80"],
["int64", "4294967295", "91ceffffffff"],
["int64", "4294967296", "91cf0000000100000000"],
["int64", "9007199254740991", "91cf001fffffffffffff"],
["int64", "-9007199254740991", "91d3ffe0000000000001"],
["int64", "-2147483649", "91d3ffffffff7fffffff"],
["int64", "9007199254740994n", "91cf0020000000000002"],
["int64", "9223372036854775807n", "91cf7fffffffffffffff"],
["int64", "-9223372036854775808n", "91d38000000000000000"],
["int64", "9007199254740993n", "91cf0020000000000001"],
["int64", "-9007199254740993n", "91d3ffdfffffffffffff"],
["float32", "1.5", "91ca3fc00000"],
["float32", "0.1", "91ca3dcccccd"],
["float32", "0", "91ca00000000"],
["float32", "-0", "91ca80000000"],
["float32", "3", "91ca40400000"],
["float32", "1e+30", "91ca7149f2ca"],
["float32", "Infinity", "91ca7f800000"],
["float32", "NaN", "91ca7fc00000"],
["float64", "1.5", "91cb3ff8000000000000"],
["float64", "0.1", "91cb3fb999999999999a"],
["float64", "0", "91cb0000000000000000"],
["float64", "-0", "91cb8000000000000000"],
["float64", "3", "91cb4008000000000000"],
["float64", "1e+300", "91cb7e37e43c8800759c"],
["float64", "Infinity", "91cb7ff0000000000000"],
["float64", "NaN", "91cb7ff8000000000000"],
];

View file

@ -12,9 +12,10 @@ import { codecs } from "../src";
import type { Codec } from "../src/codec";
import { CultCache } from "../src/cult-cache";
import { readWire, WireFloat, writeWire } from "../src/wire";
import { referenceReads, referenceWrites } from "./reference-numeric-table";
import { defineDocumentType } from "../src/document";
import { SingleFileMessagePackBackingStore } from "../src/single-file-messagepack-backing-store";
import type { CacheBackingStore, CultCacheEnvelope } from "../src/types";
import type { AnyCultCacheDocumentDefinition, CacheBackingStore, CultCacheEnvelope } from "../src/types";
const { array, boolean, bytes, float32, float64, int32, int64, lazy, list, nullable, slots, string, union, unit } = codecs;
@ -504,10 +505,10 @@ test("a codec and a formatter are refused together wherever the definition enter
});
test("a definition that skips defineDocumentType still writes its codec's bytes and its derived catalog", async () => {
const raw = { type: "tests.union_surface", schemaId: "tests.union_surface.v1", schema: surfaceSchema, codec: surfaceCodec, name: "id" as const };
const raw = { type: "tests.union_surface", schemaId: "tests.union_surface.v1", schema: surfaceSchema, codec: surfaceCodec, name: "id" } as unknown as AnyCultCacheDocumentDefinition;
const holder = memoryStore([]);
const cache = CultCache.builder().withDocumentType(raw).withGenericStore(holder).build();
await cache.put(raw, "surface", golden);
await cache.put(raw as never, "surface", golden as never);
assert.equal(hex(holder.entries[0].payload), hex(await read("union-surface.msgpack")));
const [snapshot] = cache.snapshot();
assert.deepEqual(snapshot.catalogEntry!.members!.map((member) => member.typeName).slice(0, 2), ["System.String", "System.Int32"]);
@ -531,6 +532,47 @@ test("every leaf and wrapper carries the reference's type name, and a catalog is
assert.equal(given.members!.length, 1);
});
// referenceReads and referenceWrites are the C# reference's own answers (see the table's header): a numeric leaf reads
// and refuses exactly what the reference does, and writes exactly the bytes the reference reads back unchanged.
const numericLeaves: Record<string, Codec<unknown>> = { int32, int64, float32, float64 };
const bitsOf = (value: number) => (Number.isNaN(value) ? "NaN" : Buffer.from(new Float64Array([value]).buffer).reverse().toString("hex"));
test("numeric leaves read and refuse exactly what the C# reference does", () => {
assert.equal(referenceReads.length, 120);
let accepted = 0;
let refused = 0;
for (const [leaf, value, outcome] of referenceReads) {
const record = slots<{ v: unknown }>({ 0: ["v", numericLeaves[leaf]] });
let got: string;
try {
const read = record.decode(readWire(fromHex(`91${value}`))).v;
got = `ACCEPT ${leaf.startsWith("float") ? bitsOf(read as number) : String(read)}`;
accepted += 1;
} catch (error) {
assert.ok(!(error as Error).message.includes(value), "a refusal never echoes the value");
got = "REFUSE";
refused += 1;
}
assert.equal(got, outcome, `${leaf} ${value}`);
}
assert.deepEqual([accepted, refused], [referenceReads.filter((row) => row[2].startsWith("ACCEPT")).length, referenceReads.filter((row) => row[2] === "REFUSE").length]);
assert.ok(accepted > 40 && refused > 40);
});
test("numeric leaves write the bytes the C# reference reads back unchanged, and 64-bit values lose nothing", () => {
assert.equal(referenceWrites.length, 46);
for (const [leaf, text, expected] of referenceWrites) {
const value = leaf.startsWith("float") ? (text === "-0" ? -0 : Number(text)) : text.endsWith("n") || Math.abs(Number(text)) > Number.MAX_SAFE_INTEGER ? BigInt(text.replace("n", "")) : Number(text);
const record = slots<{ v: unknown }>({ 0: ["v", numericLeaves[leaf]] });
assert.equal(hex(writeWire(record.encode({ v: value }))), expected, `${leaf} ${text}`);
}
const wide = slots<{ v: number | bigint }>({ 0: ["v", int64] });
for (const value of [2n ** 53n + 1n, 2n ** 63n - 1n, -(2n ** 63n), -(2n ** 53n) - 1n]) {
assert.equal(wide.decode(readWire(writeWire(wide.encode({ v: value })))).v, value);
}
assert.equal(wide.decode(readWire(writeWire(wide.encode({ v: 2n })))).v, 2, "a bigint that fits comes back a number");
});
function envelopeOf(payload: Uint8Array): CultCacheEnvelope {
return { key: "surface", type: "tests.union_surface", payload, storedAt: new Date().toISOString() };
}