diff --git a/quest/m2/README.md b/quest/m2/README.md index 94f3bc8c9..02702f43e 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -27,7 +27,6 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export/README ## Required - [Byte-slice locals that crash the translator](/quest/m2/ts-scalar-ice.md) - `take_apart` never reaches the arm `find_scalars` promised it wouldn't -- [Fixture cases by name](/quest/m2/ts-harness-names.md) - adding a harness case never renumbers another - [Byte slices without objects](/quest/m2/ts-byte-views/README.md) - reading through a translated cursor allocates no object per read - [Async in the TypeScript translator](/quest/m2/ts-async.md) - moq-net's and kio's async code translates with Rust's lazy futures - [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values diff --git a/quest/m2/ts-harness-names.md b/quest/m2/ts-harness-names.md deleted file mode 100644 index 75468bddb..000000000 --- a/quest/m2/ts-harness-names.md +++ /dev/null @@ -1,24 +0,0 @@ -# [S] Fixture cases by name - -## Goal - -Adding a module to a TS fixture's differential harness never renumbers another's, so two -PRs that each add one merge without editing the dispatch, and a targeted run in -`caller.ts` can't land on the wrong module. - -## Plan - -Found on 2026-10-27: the lang fixture dispatches by hand-numbered `match op % N` in -`ts/fixtures/lang/src/harness.rs`, which `caller.ts` targets by number. Every concurrent -ts PR that day collided on the numbers and on `N`, and one collision routed -`durations`' targeted cases to another module, which only review caught. - -Decided with the maintainer on 2026-10-27: m2, after the swap fix, since it slows every -ts PR now. - -- Name each case, or derive the dispatch from one list, so the count follows it. -- Keep each case's share of the random run and the harness's coverage; a dispatch change - must not shrink what the differential run reaches. -- Apply it to every fixture with the same pattern. - -Public API: none. diff --git a/ts/fixtures/codec/expected/harness.ts b/ts/fixtures/codec/expected/harness.ts index 65ef06ba3..8694c6cb2 100644 --- a/ts/fixtures/codec/expected/harness.ts +++ b/ts/fixtures/codec/expected/harness.ts @@ -15,30 +15,21 @@ import * as Group from "./lite/group.Group.ts"; import * as Probe from "./lite/probe.Probe.ts"; import type * as Version from "./lite/version.Version.ts"; +/** + * Each case, which the first byte picks modulo their count, so a case added at the end + * moves no other. + */ +export function cases(): Array<(a0: rt.ByteSlice, a1: rt.ByteVec) => void> { + return [decodeScript, encodeScript, zigzag, sizes, roundtrip$Versions, roundtripLite$Group, roundtripLite$Probe, encodeMessages, errors]; +} + /** Run one case. */ export function run(input: rt.ByteSlice): rt.ByteVec { const out: rt.ByteVec = new rt.ByteVec(); if (input.length !== 0) { - const _8: rt.u8 = rt.u8Rem(input.data[input.start], 9); - if (_8 === 0) { - decodeScript(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out); - } else if (_8 === 1) { - encodeScript(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out); - } else if (_8 === 2) { - zigzag(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out); - } else if (_8 === 3) { - sizes(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out); - } else if (_8 === 4) { - roundtrip$Versions(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out); - } else if (_8 === 5) { - roundtripLite$Group(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out); - } else if (_8 === 6) { - roundtripLite$Probe(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out); - } else if (_8 === 7) { - encodeMessages(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out); - } else { - errors(new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out); - } + const op: rt.u8 = input.data[input.start]; + const cases_: Array<(a0: rt.ByteSlice, a1: rt.ByteVec) => void> = cases(); + cases_[rt.idx(cases_, rt.usizeRem(op as rt.usize, cases_.length))](new rt.ByteSlice(input.data, input.start + 1, input.length - 1), out); return out; } return out; diff --git a/ts/fixtures/codec/src/harness.rs b/ts/fixtures/codec/src/harness.rs index 73f8502d0..71d3fe991 100644 --- a/ts/fixtures/codec/src/harness.rs +++ b/ts/fixtures/codec/src/harness.rs @@ -8,24 +8,33 @@ use crate::coding::{ }; use crate::{SessionError, lite}; +/// What a case runs on the rest of the input. +type Case = fn(&[u8], &mut Vec); + +/// Each case, which the first byte picks modulo their count, so a case added at the end +/// moves no other. +fn cases() -> Vec { + vec![ + decode_script, + encode_script, + zigzag, + sizes, + roundtrip::, + roundtrip_lite::, + roundtrip_lite::, + encode_messages, + errors, + ] +} + /// Run one case. pub fn run(input: &[u8]) -> Vec { let mut out = Vec::new(); let Some((&op, data)) = input.split_first() else { return out; }; - - match op % 9 { - 0 => decode_script(data, &mut out), - 1 => encode_script(data, &mut out), - 2 => zigzag(data, &mut out), - 3 => sizes(data, &mut out), - 4 => roundtrip::(data, &mut out), - 5 => roundtrip_lite::(data, &mut out), - 6 => roundtrip_lite::(data, &mut out), - 7 => encode_messages(data, &mut out), - _ => errors(data, &mut out), - } + let cases = cases(); + cases[usize::from(op) % cases.len()](data, &mut out); out } diff --git a/ts/fixtures/lang/caller.ts b/ts/fixtures/lang/caller.ts index 8916fd4a8..06bfceeff 100644 --- a/ts/fixtures/lang/caller.ts +++ b/ts/fixtures/lang/caller.ts @@ -4,7 +4,7 @@ // `harness::entry`, from the output directory. import * as Fault from "./fmt.Fault.ts"; -import { run as harness } from "./harness.ts"; +import { cases as named, run as harness, op } from "./harness.ts"; import * as rt from "./rt.ts"; export function run(input: rt.ByteSlice): rt.ByteVec { @@ -25,7 +25,7 @@ export function run(input: rt.ByteSlice): rt.ByteVec { } /** `shapes::loops` with each first byte that one of its cases of `!` takes, with one more byte and without. */ -const loops = [0xf1, 0xf3, 0xf4, 0xf5, 0xf6, 0xf9, 0xfa].flatMap((b) => [Uint8Array.of(16, 0, b), Uint8Array.of(16, 0, b, 1)]); +const loops = [0xf1, 0xf3, 0xf4, 0xf5, 0xf6, 0xf9, 0xfa].flatMap((b) => [Uint8Array.of(op("shapes::loops"), 0, b), Uint8Array.of(op("shapes::loops"), 0, b, 1)]); /** A `Duration`'s bytes as `durations` takes them: its seconds, then its nanoseconds. */ function duration(secs: bigint, nanos: number): number[] { @@ -43,11 +43,12 @@ function duration(secs: bigint, nanos: number): number[] { */ const edges = [0n, 1n, (1n << 53n) - 1n, 1n << 53n, (1n << 64n) - 1n].flatMap((secs) => [0, 1, 999_999_999].map((nanos) => duration(secs, nanos))); const factors = [0, 1, 3, 1000, 9_007_199, 1_000_000_000, 0xffffffff].map((n) => [n >>> 24, (n >>> 16) & 0xff, (n >>> 8) & 0xff, n & 0xff]); +const durationsOp = op("durations::durations"); const durations = [ ...[0n, 1n, 1n << 32n, (1n << 53n) - 1n, 1n << 53n, (1n << 64n) - 2n, (1n << 64n) - 1n].flatMap((secs) => - [0, 1, 999_999_999, 1_000_000_000, 0xffffffff].flatMap((nanos) => [0, 1].map((kind) => Uint8Array.of(47, kind, ...duration(secs, nanos)))), + [0, 1, 999_999_999, 1_000_000_000, 0xffffffff].flatMap((nanos) => [0, 1].map((kind) => Uint8Array.of(durationsOp, kind, ...duration(secs, nanos)))), ), - ...edges.flatMap((a) => edges.flatMap((b) => factors.flatMap((n) => [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13].map((kind) => Uint8Array.of(47, kind, ...a, ...b, ...n))))), + ...edges.flatMap((a) => edges.flatMap((b) => factors.flatMap((n) => [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13].map((kind) => Uint8Array.of(durationsOp, kind, ...a, ...b, ...n))))), // Each side of where minutes, hours, and a `u128` of nanoseconds pass `u64::MAX` seconds. ...[2n ** 64n / 60n, 2n ** 64n / 3600n].flatMap((units) => [units, units + 1n].map((n) => wide(n << 64n))), ...[(7n << 64n) + 999_999_999n, 2n ** 64n * 1_000_000_000n - 1n, 2n ** 64n * 1_000_000_000n, 2n ** 128n - 1n].map((n) => wide(n)), @@ -56,7 +57,7 @@ const durations = [ /** `durations`' larger units, of a `u128` whose high half is also its `u64`. */ function wide(n: bigint): Uint8Array { const bytes = new Uint8Array(18); - bytes.set([47, 12]); + bytes.set([durationsOp, 12]); const view = new DataView(bytes.buffer); view.setBigUint64(2, n >> 64n); view.setBigUint64(10, n & 0xffff_ffff_ffff_ffffn); @@ -86,15 +87,18 @@ const doubles: (number | bigint)[] = [ ]; /** Integers whose `f64` rounds, as `floats`' kind 2 takes them: 16 bytes of a `u128`. */ const wides = [0n, 1n, (1n << 53n) + 1n, (1n << 53n) + 3n, (1n << 54n) + 2n, (1n << 63n) + 1025n, (1n << 64n) - 1n, (1n << 64n) - 1025n, (1n << 64n) + (1n << 11n) + 1n, (1n << 127n) + 1n, (1n << 128n) - 1n, (1n << 128n) - (1n << 74n), -(1n << 100n) - 1n]; +const floatsOp = op("floats::floats"); +/** How many cases the first byte picks among, modulo. */ +const count = named().length; const floats = [ - ...Array.from({ length: 10 }, (_, kind) => doubles.flatMap((a) => doubles.map((b) => Uint8Array.of(58, kind, ...double(a), ...double(b), 1)))).flat(), + ...Array.from({ length: 10 }, (_, kind) => doubles.flatMap((a) => doubles.map((b) => Uint8Array.of(floatsOp, kind, ...double(a), ...double(b), 1)))).flat(), // `from_secs_f64`, `mul_f64`, and `div_f64` panicking, and `Duration::MAX.mul_f64`. - ...[18, 28, 38].flatMap((kind) => doubles.map((a) => Uint8Array.of(58, kind, ...double(a), ...double(a), 3))), + ...[18, 28, 38].flatMap((kind) => doubles.map((a) => Uint8Array.of(floatsOp, kind, ...double(a), ...double(a), 3))), ...wides.map((v) => { const bytes = new Uint8Array(16); new DataView(bytes.buffer).setBigUint64(0, BigInt.asUintN(64, v >> 64n)); new DataView(bytes.buffer).setBigUint64(8, BigInt.asUintN(64, v)); - return Uint8Array.of(58, 2, ...bytes); + return Uint8Array.of(floatsOp, 2, ...bytes); }), ]; @@ -102,7 +106,7 @@ const floats = [ const ULPS = 2n; export function within(input: Uint8Array, native: Uint8Array, translated: Uint8Array): boolean { - if (input[0] % 59 !== 58 || input[1] % 10 !== 7 || native.length !== translated.length) return false; + if (input[0] % count !== floatsOp || input[1] % 10 !== 7 || native.length !== translated.length) return false; const [a, b] = [native, translated].map((v) => new DataView(v.buffer, v.byteOffset, v.length)); // Bits as a line through zero, so the distance between two is how many doubles apart they are. const line = (v: bigint) => (v < 0n ? -(v & 0x7fffffffffffffffn) : v); @@ -113,6 +117,86 @@ export function within(input: Uint8Array, native: Uint8Array, translated: Uint8A return true; } +/** + * The 64- and 128-bit cases over operands that random bytes rarely make: every pair of + * values next to a power of two. + */ +function ints(): Uint8Array[] { + const [u64, u128, i64, i128] = ["ints::u64_op", "ints::u128_op", "ints::i64_op", "ints::i128_op"].map(op); + const [neg, cast, convert, fmtInts] = ["ints::neg", "ints::cast", "ints::convert", "fmt::ints"].map(op); + const cases: Uint8Array[] = []; + // 2^k - 1, 2^k, and 2^k + 1 for each k where a limb, a safe number, or a sign ends, + // and values without that structure, whose quotients and remainders are not round. + const edges = new Set([ + 1_000_000n, + 0x9e3779b97f4a7c15n, + 0x0123456789abcdefn, + 0xfedcba9876543210n, + // Safe integers whose halves carry when multiplied, and a negative one. + 0x1d3c5b7a9f8e6dn, + -0x1b2a3c4d5e6f71n & 0xffffffffffffffffn, + ]); + for (const k of [0n, 1n, 16n, 21n, 31n, 32n, 33n, 52n, 53n, 54n, 63n, 64n]) { + for (const delta of [-1n, 0n, 1n]) edges.add(((1n << k) + delta) & 0xffffffffffffffffn); + } + // Small negative numbers, -2^32, and where negative safe numbers end, as signed values. + for (const k of [1n, 32n, 53n]) { + for (const delta of [-1n, 0n, 1n]) edges.add((-(1n << k) + delta) & 0xffffffffffffffffn); + } + const values = [...edges]; + const be = (v: bigint) => { + const bytes = new Uint8Array(8); + new DataView(bytes.buffer).setBigUint64(0, v); + return [...bytes]; + }; + // Shift amounts in and past each limb, and past the width, which panics. + const shifts = [0, 1, 7, 31, 32, 33, 63, 64, 65, 95, 96, 127, 128, 200]; + let i = 0; + for (const x of values) { + for (const y of values) { + // A 64-bit case's operands are `x` and `y`. A 128-bit case's are `x:y` and `u:v`, + // whose high halves are equal a third of the time, so the low half decides. + const u = i % 3 === 0 ? x : values[(i * 7) % values.length]; + const v = values[(i * 13 + 5) % values.length]; + const shift = shifts[i++ % shifts.length]; + for (let kind = 0; kind < 30; kind++) { + for (const o of [u64, i64]) cases.push(Uint8Array.of(o, kind, ...be(x), ...be(y), shift)); + for (const o of [u128, i128]) cases.push(Uint8Array.of(o, kind, ...be(x), ...be(y), ...be(u), ...be(v), shift)); + } + // `clamp` between two values of the sweep, whose third operand follows the shift's place. + for (const o of [u64, i64]) cases.push(Uint8Array.of(o, 26, ...be(x), ...be(y), ...be(u))); + for (const o of [u128, i128]) cases.push(Uint8Array.of(o, 26, ...be(x), ...be(y), ...be(u), ...be(v), ...be(y), ...be(x))); + // Negating an `i64` and an `i128`. + cases.push(Uint8Array.of(neg, 3, ...be(x)), Uint8Array.of(neg, 4, ...be(x), ...be(y))); + // `x * y`, `checked_mul`, `saturating_mul`, and `wrapping_mul` at 128 bits, extended in + // each type's sign: a product of two numbers, past 2^53, multiplies their halves. + const signs = (v: bigint) => (v >> 63n === 1n ? 0xffffffffffffffffn : 0n); + for (const kind of [2, 13, 24, 25]) { + cases.push(Uint8Array.of(u128, kind, ...be(0n), ...be(x), ...be(0n), ...be(y))); + cases.push(Uint8Array.of(i128, kind, ...be(signs(x)), ...be(x), ...be(signs(y)), ...be(y))); + } + // `x:y / d`, `x:y % d`, `div_ceil`, and `is_multiple_of` at 128 bits by a divisor + // below 2^32 in magnitude, which divides limbs by a number, extended in each type's + // sign: -1 overflows `i128::MIN`. + for (const d of [3n, 1_000_000n, 0xffffffffn, -1n, -0xffffffffn]) { + for (const kind of [3, 4, 27, 28]) { + for (const o of [u128, i128]) cases.push(Uint8Array.of(o, kind, ...be(x), ...be(y), ...be(signs(d & 0xffffffffffffffffn)), ...be(d & 0xffffffffffffffffn))); + } + } + } + // Conversions, and halves joined back. + cases.push(Uint8Array.of(convert, 0, ...be(x), ...be(x))); + for (const y of [0n, 1n, 1n << 32n, 1n << 63n, 0xffffffffffffffffn]) { + for (const from of [3, 4, 9, 10]) { + // A cast to every type, and every format spec. + for (let to = 0; to < 12; to++) cases.push(Uint8Array.of(cast, from, to, ...be(x), ...be(y))); + cases.push(Uint8Array.of(fmtInts, from, ...be(x), ...be(y))); + } + } + } + return cases; +} + /** * `utf8::decodes` of raw bytes: a byte order mark, which std keeps, and the sequences at * each edge of what it takes: surrogates, overlongs, past U+10FFFF, and cut short at the @@ -129,6 +213,6 @@ const utf8 = [ [0xe2, 0x82], [0xc2], [0x80, 0xf8, 0xff], -].map((bytes) => Uint8Array.of(57, 0, ...bytes)); +].map((bytes) => Uint8Array.of(op("utf8::decodes"), 0, ...bytes)); -export const cases: Uint8Array[] = [...loops, ...durations, ...utf8, ...floats]; +export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats]; diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 3658e9650..297f168b8 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -11,7 +11,7 @@ import { edits } from "./edits.ts"; import { floats } from "./floats.ts"; import { ints, strings, derived, written, counts, rounding, panics } from "./fmt.ts"; import { methods } from "./generics.ts"; -import { u8Op, u16Op, u32Op, u64Op, u128Op, usizeOp, i8Op, i16Op, i32Op, i64Op, i128Op, isizeOp, i8Neg, i16Neg, i32Neg, i64Neg, i128Neg, isizeNeg, cast, convert } from "./ints.ts"; +import { u8Op, u16Op, u32Op, u64Op, u128Op, usizeOp, i8Op, i16Op, i32Op, i64Op, i128Op, isizeOp, i8Neg, i16Neg, i32Neg, i64Neg, i128Neg, isizeNeg, cast as ints_cast, convert } from "./ints.ts"; import { adaptors, consumers, skips, ranges, options, own, values } from "./iters.ts"; import { keys } from "./keys.ts"; import { nested } from "./nested.ts"; @@ -22,153 +22,112 @@ import { strs, slices } from "./strs.ts"; import { decodes } from "./utf8.ts"; import { wrappers } from "./wrappers.ts"; +/** + * Each case's name and what it runs. The first byte picks one modulo their count, so a + * case added at the end moves no other. + */ +export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void][] { + return [["ints::u8_op", u8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u16_op", u16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u32_op", u32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u64_op", u64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::u128_op", u128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::usize_op", usizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i8_op", i8Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i16_op", i16Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i32_op", i32Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i64_op", i64Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::i128_op", i128Op] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::isize_op", isizeOp] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::neg", neg] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::cast", cast] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["ints::convert", (_210: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void => { + convert(data, out); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::copies", (_211: rt.u8, data2: rt.ByteSlice, out2: rt.ByteVec): void => { + copies(data2, out2); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::loops", (_212: rt.u8, data3: rt.ByteSlice, out3: rt.ByteVec): void => { + loops(data3, out3); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::patterns", (_213: rt.u8, data4: rt.ByteSlice, out4: rt.ByteVec): void => { + patterns(data4, out4); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::closures", (_214: rt.u8, data5: rt.ByteSlice, out5: rt.ByteVec): void => { + closures(data5, out5); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::discriminants", (_215: rt.u8, data6: rt.ByteSlice, out6: rt.ByteVec): void => { + discriminants(data6, out6); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::library", (_216: rt.u8, data7: rt.ByteSlice, out7: rt.ByteVec): void => { + library(data7, out7); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::ints", ints] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::strings", (_217: rt.u8, data8: rt.ByteSlice, out8: rt.ByteVec): void => { + strings(data8, out8); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::derived", (_218: rt.u8, data9: rt.ByteSlice, out9: rt.ByteVec): void => { + derived(data9, out9); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::written", (_219: rt.u8, data10: rt.ByteSlice, out10: rt.ByteVec): void => { + written(data10, out10); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::panics", panics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["generics::methods", (_220: rt.u8, data11: rt.ByteSlice, out11: rt.ByteVec): void => { + methods(data11, out11); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::adaptors", adaptors] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::consumers", consumers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::ranges", (_221: rt.u8, data12: rt.ByteSlice, out12: rt.ByteVec): void => { + ranges(data12, out12); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::options", (_222: rt.u8, data13: rt.ByteSlice, out13: rt.ByteVec): void => { + options(data13, out13); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::own", (_223: rt.u8, data14: rt.ByteSlice, out14: rt.ByteVec): void => { + own(data14, out14); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::values", (_224: rt.u8, data15: rt.ByteSlice, out15: rt.ByteVec): void => { + values(data15, out15); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::order", (_225: rt.u8, data16: rt.ByteSlice, out16: rt.ByteVec): void => { + order(data16, out16); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::hash_maps", hashMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::sets", sets] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::btree_maps", btreeMaps] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::deques", (_226: rt.u8, data17: rt.ByteSlice, out17: rt.ByteVec): void => { + deques(data17, out17); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["shapes::flows", (_227: rt.u8, data18: rt.ByteSlice, out18: rt.ByteVec): void => { + flows(data18, out18); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::keys", keys] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chars::chars", chars] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::strs", strs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::vecs", (_228: rt.u8, data19: rt.ByteSlice, out19: rt.ByteVec): void => { + vecs(data19, out19); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::slices", slices] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["collections::stores", (_229: rt.u8, data20: rt.ByteSlice, out20: rt.ByteVec): void => { + stores(data20, out20); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["chunks::chunks", chunks] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["cows::cows", cows] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["durations::durations", durations] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["combinators::combinators", combinators] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["splits::splits", splits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["nested::nested", nested] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["refs::refs", (_230: rt.u8, data21: rt.ByteSlice, out21: rt.ByteVec): void => { + refs(data21, out21); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_231: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { + counts(data22, out22); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_232: rt.u8, _310: rt.ByteSlice, out23: rt.ByteVec): void => { + rounding(out23); + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]; +} + /** Run one case. */ export function run(input: rt.ByteSlice): rt.ByteVec { const out: rt.ByteVec = new rt.ByteVec(); if (!(input.length >= 2)) { return out; } - const _10: [rt.u8, rt.u8] = [input.data[input.start], input.data[input.start + 1]]; - const op: rt.u8 = _10[0]; - const kind: rt.u8 = _10[1]; - const _14: rt.u8 = rt.u8Rem(op, 59); - if (_14 === 0) { - u8Op(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 1) { - u16Op(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 2) { - u32Op(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 3) { - u64Op(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 4) { - u128Op(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 5) { - usizeOp(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 6) { - i8Op(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 7) { - i16Op(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 8) { - i32Op(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 9) { - i64Op(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 10) { - i128Op(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 11) { - isizeOp(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 12) { - const _28: rt.u8 = rt.u8Rem(kind, 6); - if (_28 === 0) { - i8Neg(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_28 === 1) { - i16Neg(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_28 === 2) { - i32Neg(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_28 === 3) { - i64Neg(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_28 === 4) { - i128Neg(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else { - isizeNeg(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } - } else if (_14 === 13) { - if (input.length - 2 === 0) { - cast(kind, rt.u8Div(kind, 12), new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else { - cast(kind, input.data[input.start + 2], new rt.ByteSlice(input.data, input.start + 3, input.length - 3), out); - } - } else if (_14 === 14) { - convert(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 15) { - copies(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 16) { - loops(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 17) { - patterns(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 18) { - closures(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 19) { - discriminants(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 20) { - library(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 21) { - ints(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 22) { - strings(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 23) { - derived(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 24) { - written(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 25) { - panics(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 26) { - methods(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 27) { - adaptors(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 28) { - consumers(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 29) { - ranges(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 30) { - options(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 31) { - own(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 32) { - values(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 33) { - order(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 34) { - hashMaps(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 35) { - sets(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 36) { - btreeMaps(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 37) { - deques(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 38) { - flows(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 39) { - keys(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 40) { - chars(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 41) { - strs(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 42) { - vecs(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 43) { - slices(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 44) { - stores(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 45) { - chunks(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 46) { - cows(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 47) { - durations(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 48) { - combinators(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 49) { - splits(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 50) { - nested(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 51) { - refs(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 52) { - counts(new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 53) { - rounding(out); - } else if (_14 === 54) { - skips(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 55) { - wrappers(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 56) { - edits(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); - } else if (_14 === 57) { - decodes(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); + const cases_: [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void][] = cases(); + cases_[rt.idx(cases_, rt.usizeRem(input.data[input.start] as rt.usize, cases_.length))][1](input.data[input.start + 1], new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); + return out; +} + +/** The first byte that picks the case named `name`, which `caller.ts` targets by. */ +export function op(name: string): rt.u8 { + const _2: rt.usize | undefined = rt.iterPosition(rt.iterSlice(cases()), (_22: [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]): boolean => { + return _22[0] === name; + }); + if (_2 === undefined) { + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtPad(f, name)]; + rt.panicFmt(["no case ", args2[0]]); } else { - floats(kind, new rt.ByteSlice(input.data, input.start + 2, input.length - 2), out); + return _2 & 0xff; + } +} + +/** Negate the signed type `kind` picks. */ +export function neg(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const _4: rt.u8 = rt.u8Rem(kind, 6); + if (_4 === 0) { + i8Neg(data, out); + } else if (_4 === 1) { + i16Neg(data, out); + } else if (_4 === 2) { + i32Neg(data, out); + } else if (_4 === 3) { + i64Neg(data, out); + } else if (_4 === 4) { + i128Neg(data, out); + } else { + isizeNeg(data, out); + } +} + +/** + * Cast from the type `kind` picks to the one the next byte does, or without one, to + * a type `kind` also picks. + */ +export function cast(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + if (data.length === 0) { + ints_cast(kind, rt.u8Div(kind, 12), data, out); + } else { + ints_cast(kind, data.data[data.start], new rt.ByteSlice(data.data, data.start + 1, data.length - 1), out); } - return out; } //# sourceMappingURL=harness.ts.map diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 0f2a6f0ea..3da9d59e8 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -15,83 +15,112 @@ pub fn entry(input: &[u8]) -> Vec { } } +/// What a case runs on the second byte and the rest. +type Case = fn(u8, &[u8], &mut Vec); + +/// Each case's name and what it runs. The first byte picks one modulo their count, so a +/// case added at the end moves no other. +fn cases() -> Vec<(&'static str, Case)> { + vec![ + ("ints::u8_op", ints::u8_op), + ("ints::u16_op", ints::u16_op), + ("ints::u32_op", ints::u32_op), + ("ints::u64_op", ints::u64_op), + ("ints::u128_op", ints::u128_op), + ("ints::usize_op", ints::usize_op), + ("ints::i8_op", ints::i8_op), + ("ints::i16_op", ints::i16_op), + ("ints::i32_op", ints::i32_op), + ("ints::i64_op", ints::i64_op), + ("ints::i128_op", ints::i128_op), + ("ints::isize_op", ints::isize_op), + ("ints::neg", neg), + ("ints::cast", cast), + ("ints::convert", |_, data, out| ints::convert(data, out)), + ("shapes::copies", |_, data, out| shapes::copies(data, out)), + ("shapes::loops", |_, data, out| shapes::loops(data, out)), + ("shapes::patterns", |_, data, out| shapes::patterns(data, out)), + ("shapes::closures", |_, data, out| shapes::closures(data, out)), + ("shapes::discriminants", |_, data, out| shapes::discriminants(data, out)), + ("shapes::library", |_, data, out| shapes::library(data, out)), + ("fmt::ints", fmt::ints), + ("fmt::strings", |_, data, out| fmt::strings(data, out)), + ("fmt::derived", |_, data, out| fmt::derived(data, out)), + ("fmt::written", |_, data, out| fmt::written(data, out)), + ("fmt::panics", fmt::panics), + ("generics::methods", |_, data, out| generics::methods(data, out)), + ("iters::adaptors", iters::adaptors), + ("iters::consumers", iters::consumers), + ("iters::ranges", |_, data, out| iters::ranges(data, out)), + ("iters::options", |_, data, out| iters::options(data, out)), + ("iters::own", |_, data, out| iters::own(data, out)), + ("iters::values", |_, data, out| iters::values(data, out)), + ("shapes::order", |_, data, out| shapes::order(data, out)), + ("collections::hash_maps", collections::hash_maps), + ("collections::sets", collections::sets), + ("collections::btree_maps", collections::btree_maps), + ("collections::deques", |_, data, out| collections::deques(data, out)), + ("shapes::flows", |_, data, out| shapes::flows(data, out)), + ("keys::keys", keys::keys), + ("chars::chars", chars::chars), + ("strs::strs", strs::strs), + ("collections::vecs", |_, data, out| collections::vecs(data, out)), + ("strs::slices", strs::slices), + ("collections::stores", |_, data, out| collections::stores(data, out)), + ("chunks::chunks", chunks::chunks), + ("cows::cows", cows::cows), + ("durations::durations", durations::durations), + ("combinators::combinators", combinators::combinators), + ("splits::splits", splits::splits), + ("nested::nested", nested::nested), + ("refs::refs", |_, data, out| refs::refs(data, out)), + ("fmt::counts", |_, data, out| fmt::counts(data, out)), + ("fmt::rounding", |_, _, out| fmt::rounding(out)), + ("iters::skips", iters::skips), + ("wrappers::wrappers", wrappers::wrappers), + ("edits::edits", edits::edits), + ("utf8::decodes", utf8::decodes), + ("floats::floats", floats::floats), + ] +} + /// Run one case. pub fn run(input: &[u8]) -> Vec { let mut out = Vec::new(); let [op, kind, data @ ..] = input else { return out; }; - let (op, kind) = (*op, *kind); - match op % 59 { - 0 => ints::u8_op(kind, data, &mut out), - 1 => ints::u16_op(kind, data, &mut out), - 2 => ints::u32_op(kind, data, &mut out), - 3 => ints::u64_op(kind, data, &mut out), - 4 => ints::u128_op(kind, data, &mut out), - 5 => ints::usize_op(kind, data, &mut out), - 6 => ints::i8_op(kind, data, &mut out), - 7 => ints::i16_op(kind, data, &mut out), - 8 => ints::i32_op(kind, data, &mut out), - 9 => ints::i64_op(kind, data, &mut out), - 10 => ints::i128_op(kind, data, &mut out), - 11 => ints::isize_op(kind, data, &mut out), - 12 => match kind % 6 { - 0 => ints::i8_neg(data, &mut out), - 1 => ints::i16_neg(data, &mut out), - 2 => ints::i32_neg(data, &mut out), - 3 => ints::i64_neg(data, &mut out), - 4 => ints::i128_neg(data, &mut out), - _ => ints::isize_neg(data, &mut out), - }, - 13 => match data.split_first() { - Some((&to, data)) => ints::cast(kind, to, data, &mut out), - None => ints::cast(kind, kind / 12, data, &mut out), - }, - 14 => ints::convert(data, &mut out), - 15 => shapes::copies(data, &mut out), - 16 => shapes::loops(data, &mut out), - 17 => shapes::patterns(data, &mut out), - 18 => shapes::closures(data, &mut out), - 19 => shapes::discriminants(data, &mut out), - 20 => shapes::library(data, &mut out), - 21 => fmt::ints(kind, data, &mut out), - 22 => fmt::strings(data, &mut out), - 23 => fmt::derived(data, &mut out), - 24 => fmt::written(data, &mut out), - 25 => fmt::panics(kind, data, &mut out), - 26 => generics::methods(data, &mut out), - 27 => iters::adaptors(kind, data, &mut out), - 28 => iters::consumers(kind, data, &mut out), - 29 => iters::ranges(data, &mut out), - 30 => iters::options(data, &mut out), - 31 => iters::own(data, &mut out), - 32 => iters::values(data, &mut out), - 33 => shapes::order(data, &mut out), - 34 => collections::hash_maps(kind, data, &mut out), - 35 => collections::sets(kind, data, &mut out), - 36 => collections::btree_maps(kind, data, &mut out), - 37 => collections::deques(data, &mut out), - 38 => shapes::flows(data, &mut out), - 39 => keys::keys(kind, data, &mut out), - 40 => chars::chars(kind, data, &mut out), - 41 => strs::strs(kind, data, &mut out), - 42 => collections::vecs(data, &mut out), - 43 => strs::slices(kind, data, &mut out), - 44 => collections::stores(data, &mut out), - 45 => chunks::chunks(kind, data, &mut out), - 46 => cows::cows(kind, data, &mut out), - 47 => durations::durations(kind, data, &mut out), - 48 => combinators::combinators(kind, data, &mut out), - 49 => splits::splits(kind, data, &mut out), - 50 => nested::nested(kind, data, &mut out), - 51 => refs::refs(data, &mut out), - 52 => fmt::counts(data, &mut out), - 53 => fmt::rounding(&mut out), - 54 => iters::skips(kind, data, &mut out), - 55 => wrappers::wrappers(kind, data, &mut out), - 56 => edits::edits(kind, data, &mut out), - 57 => utf8::decodes(kind, data, &mut out), - _ => floats::floats(kind, data, &mut out), - } + let cases = cases(); + let (_, case) = cases[usize::from(*op) % cases.len()]; + case(*kind, data, &mut out); out } + +/// The first byte that picks the case named `name`, which `caller.ts` targets by. +pub fn op(name: &str) -> u8 { + match cases().iter().position(|&(n, _)| n == name) { + Some(i) => i as u8, + None => panic!("no case {name}"), + } +} + +/// Negate the signed type `kind` picks. +fn neg(kind: u8, data: &[u8], out: &mut Vec) { + match kind % 6 { + 0 => ints::i8_neg(data, out), + 1 => ints::i16_neg(data, out), + 2 => ints::i32_neg(data, out), + 3 => ints::i64_neg(data, out), + 4 => ints::i128_neg(data, out), + _ => ints::isize_neg(data, out), + } +} + +/// Cast from the type `kind` picks to the one the next byte does, or without one, to +/// a type `kind` also picks. +fn cast(kind: u8, data: &[u8], out: &mut Vec) { + match data.split_first() { + Some((&to, data)) => ints::cast(kind, to, data, out), + None => ints::cast(kind, kind / 12, data, out), + } +} diff --git a/ts/fixtures/lang/ts.toml b/ts/fixtures/lang/ts.toml index 17acfad71..575fe039e 100644 --- a/ts/fixtures/lang/ts.toml +++ b/ts/fixtures/lang/ts.toml @@ -1,3 +1,3 @@ package = "lang-fixture" -roots = ["harness::run", "fmt::Fault"] +roots = ["harness::run", "harness::op", "fmt::Fault"] printable = ["fmt::Fault"] diff --git a/ts/fixtures/task/src/harness.rs b/ts/fixtures/task/src/harness.rs index 0ad135688..46ec6efca 100644 --- a/ts/fixtures/task/src/harness.rs +++ b/ts/fixtures/task/src/harness.rs @@ -19,11 +19,13 @@ pub fn run(input: &[u8]) -> Vec { let log = Log::new(); let mut exec = Executor::new(log.clone()); let mut channels: Vec = Vec::new(); + let steps = steps(); for step in input.chunks(2) { let (op, arg) = (step[0], step.get(1).copied().unwrap_or(0)); + let (_, run_step) = steps[usize::from(op) % steps.len()]; #[cfg(panic = "unwind")] { - let run = std::panic::AssertUnwindSafe(|| run_step(&log, &mut exec, &mut channels, op, arg)); + let run = std::panic::AssertUnwindSafe(|| run_step(&log, &mut exec, &mut channels, arg)); if let Err(panic) = std::panic::catch_unwind(run) { let message = match (panic.downcast_ref::(), panic.downcast_ref::<&str>()) { (Some(s), _) => s.clone(), @@ -34,7 +36,7 @@ pub fn run(input: &[u8]) -> Vec { } } #[cfg(not(panic = "unwind"))] - run_step(&log, &mut exec, &mut channels, op, arg); + run_step(&log, &mut exec, &mut channels, arg); } for t in 0..exec.len() { exec.drop(t); @@ -46,77 +48,89 @@ pub fn run(input: &[u8]) -> Vec { log.text() } -/// Run one step of a script. -fn run_step(log: &Log, exec: &mut Executor, channels: &mut Vec, op: u8, arg: u8) { - let pick = |len: usize| (len > 0).then(|| usize::from(arg) % len); - match op % 15 { - 0 => { +/// What a step does with its argument. +type Step = fn(&Log, &mut Executor, &mut Vec, u8); + +/// Each step and its name, which a script's op picks modulo their count: `tests/task.ts`'s +/// `steps`, in the same order. A step added at the end moves no other. +fn steps() -> Vec<(&'static str, Step)> { + vec![ + ("open", |log, _, channels, _| { log.note(format!("c{} open", channels.len())); channels.push(Channel::open()); - } - 1 => { - if let Some(c) = pick(channels.len()) { + }), + ("sum", |log, exec, channels, arg| { + if let Some(c) = pick(channels.len(), arg) { let guard = Guard::new(exec.len() as u32, log.clone()); exec.spawn(model::sum(channels[c].consumer(), guard)); } - } - 2 => { - if let Some(c) = pick(channels.len()) { + }), + ("drain", |_, exec, channels, arg| { + if let Some(c) = pick(channels.len(), arg) { let depth = [1, 4, 16][usize::from(arg / 16) % 3]; exec.spawn(model::drain(channels[c].consumer(), depth)); } - } - 3 => { - if let Some(c) = pick(channels.len()) { + }), + ("send", |log, _, channels, arg| { + if let Some(c) = pick(channels.len(), arg) { log.note(format!("c{c} send {arg}")); if !channels[c].send(u64::from(arg)) { log.note(format!("c{c} closed")); } } - } - 4 => { - if let Some(c) = pick(channels.len()) { + }), + ("close", |log, _, channels, arg| { + if let Some(c) = pick(channels.len(), arg) { log.note(format!("c{c} close")); channels[c].close(); } - } - 5 => { - if let Some(c) = pick(channels.len()) { + }), + ("clone", |log, _, channels, arg| { + if let Some(c) = pick(channels.len(), arg) { log.note(format!("c{c} clone")); channels[c].clone_producer(); } - } - 6 => exec.run(), - 7 => _ = exec.step(), - 8 => { - if let Some(t) = pick(exec.len()) { + }), + ("run", |_, exec, _, _| exec.run()), + ("step", |_, exec, _, _| _ = exec.step()), + ("poll", |_, exec, _, arg| { + if let Some(t) = pick(exec.len(), arg) { exec.poll(t); } - } - 9 => { - if let Some(t) = pick(exec.len()) { + }), + ("drop", |_, exec, _, arg| { + if let Some(t) = pick(exec.len(), arg) { exec.drop(t); } - } - 10 => { - if let Some(c) = pick(channels.len()) { + }), + ("blocks", |log, exec, channels, arg| { + if let Some(c) = pick(channels.len(), arg) { let guard = Guard::new(exec.len() as u32, log.clone()); exec.spawn(model::blocks(channels[c].consumer(), guard, arg >= 128)); } - } - 11 => drops::run(log, 1000 * exec.len() as u32, arg), - 12 => { - if let Some(c) = pick(channels.len()) { + }), + ("drops", |log, exec, _, arg| { + drops::run(log, 1000 * exec.len() as u32, arg) + }), + ("held", |log, exec, channels, arg| { + if let Some(c) = pick(channels.len(), arg) { let guard = Guard::new(exec.len() as u32, log.clone()); exec.spawn(drops::held(channels[c].consumer(), guard)); } - } - 13 => drops::edited(log, 1000 * exec.len() as u32, arg % 8), - _ => { - if let Some(c) = pick(channels.len()) { + }), + ("edited", |log, exec, _, arg| { + drops::edited(log, 1000 * exec.len() as u32, arg % 8) + }), + ("helpers", |log, exec, channels, arg| { + if let Some(c) = pick(channels.len(), arg) { let guard = Guard::new(exec.len() as u32, log.clone()); exec.spawn(futures::helpers(channels[c].consumer(), guard)); } - } - } + }), + ] +} + +/// The one of `len` channels or tasks that `arg` picks, if there are any. +fn pick(len: usize, arg: u8) -> Option { + (len > 0).then(|| usize::from(arg) % len) } diff --git a/ts/tests/differential.ts b/ts/tests/differential.ts index b632370ab..2b87c55df 100644 --- a/ts/tests/differential.ts +++ b/ts/tests/differential.ts @@ -1,10 +1,7 @@ // Runs a fixture's harness both ways on the same inputs and fails on any difference: // natively, as the fixture's wasm32 build, and as its TypeScript translation. // -// usage: node differential.ts [cases] [wide] -// -// `wide` adds a sweep of the `lang` fixture's 64- and 128-bit cases over operands that -// random bytes rarely make: every pair of values next to a power of two. +// usage: node differential.ts [cases] // // A harness, or a hand-written caller wrapping one, exports `run(input: rt.ByteSlice): // Vec`, and may export `cases`, inputs it names, which run with the generated ones, @@ -22,8 +19,8 @@ import { Wasm } from "./wasm.ts"; type Outcome = Uint8Array | { panic: string }; -const [wasmPath, harnessPath, countArg, wideArg] = process.argv.slice(2); -if (!wasmPath || !harnessPath) throw new Error("usage: differential.ts [cases] [wide]"); +const [wasmPath, harnessPath, countArg] = process.argv.slice(2); +if (!wasmPath || !harnessPath) throw new Error("usage: differential.ts [cases]"); const count = Number(countArg ?? "20000"); const harness = await import(pathToFileURL(harnessPath).href); @@ -99,79 +96,8 @@ for (let op = 0; op < 8; op++) { } } } -if (wideArg === "wide") { - // 2^k - 1, 2^k, and 2^k + 1 for each k where a limb, a safe number, or a sign ends, - // and values without that structure, whose quotients and remainders are not round. - const edges = new Set([ - 1_000_000n, - 0x9e3779b97f4a7c15n, - 0x0123456789abcdefn, - 0xfedcba9876543210n, - // Safe integers whose halves carry when multiplied, and a negative one. - 0x1d3c5b7a9f8e6dn, - -0x1b2a3c4d5e6f71n & 0xffffffffffffffffn, - ]); - for (const k of [0n, 1n, 16n, 21n, 31n, 32n, 33n, 52n, 53n, 54n, 63n, 64n]) { - for (const delta of [-1n, 0n, 1n]) edges.add(((1n << k) + delta) & 0xffffffffffffffffn); - } - // Small negative numbers, -2^32, and where negative safe numbers end, as signed values. - for (const k of [1n, 32n, 53n]) { - for (const delta of [-1n, 0n, 1n]) edges.add((-(1n << k) + delta) & 0xffffffffffffffffn); - } - const values = [...edges]; - const be = (v: bigint) => { - const bytes = new Uint8Array(8); - new DataView(bytes.buffer).setBigUint64(0, v); - return [...bytes]; - }; - // Shift amounts in and past each limb, and past the width, which panics. - const shifts = [0, 1, 7, 31, 32, 33, 63, 64, 65, 95, 96, 127, 128, 200]; - let i = 0; - for (const x of values) { - for (const y of values) { - // A 64-bit case's operands are `x` and `y`. A 128-bit case's are `x:y` and `u:v`, - // whose high halves are equal a third of the time, so the low half decides. - const u = i % 3 === 0 ? x : values[(i * 7) % values.length]; - const v = values[(i * 13 + 5) % values.length]; - const shift = shifts[i++ % shifts.length]; - for (let kind = 0; kind < 30; kind++) { - for (const op of [3, 9]) cases.push(Uint8Array.of(op, kind, ...be(x), ...be(y), shift)); - for (const op of [4, 10]) cases.push(Uint8Array.of(op, kind, ...be(x), ...be(y), ...be(u), ...be(v), shift)); - } - // `clamp` between two values of the sweep, whose third operand follows the shift's place. - for (const op of [3, 9]) cases.push(Uint8Array.of(op, 26, ...be(x), ...be(y), ...be(u))); - for (const op of [4, 10]) cases.push(Uint8Array.of(op, 26, ...be(x), ...be(y), ...be(u), ...be(v), ...be(y), ...be(x))); - // Negating an `i64` and an `i128`. - cases.push(Uint8Array.of(12, 3, ...be(x)), Uint8Array.of(12, 4, ...be(x), ...be(y))); - // `x * y`, `checked_mul`, `saturating_mul`, and `wrapping_mul` at 128 bits, extended in - // each type's sign: a product of two numbers, past 2^53, multiplies their halves. - const signs = (v: bigint) => (v >> 63n === 1n ? 0xffffffffffffffffn : 0n); - for (const kind of [2, 13, 24, 25]) { - cases.push(Uint8Array.of(4, kind, ...be(0n), ...be(x), ...be(0n), ...be(y))); - cases.push(Uint8Array.of(10, kind, ...be(signs(x)), ...be(x), ...be(signs(y)), ...be(y))); - } - // `x:y / d`, `x:y % d`, `div_ceil`, and `is_multiple_of` at 128 bits by a divisor - // below 2^32 in magnitude, which divides limbs by a number, extended in each type's - // sign: -1 overflows `i128::MIN`. - for (const d of [3n, 1_000_000n, 0xffffffffn, -1n, -0xffffffffn]) { - for (const kind of [3, 4, 27, 28]) { - for (const op of [4, 10]) cases.push(Uint8Array.of(op, kind, ...be(x), ...be(y), ...be(signs(d & 0xffffffffffffffffn)), ...be(d & 0xffffffffffffffffn))); - } - } - } - // Conversions, and halves joined back. - cases.push(Uint8Array.of(14, 0, ...be(x), ...be(x))); - for (const y of [0n, 1n, 1n << 32n, 1n << 63n, 0xffffffffffffffffn]) { - for (const from of [3, 4, 9, 10]) { - // A cast to every type, and every format spec. - for (let to = 0; to < 12; to++) cases.push(Uint8Array.of(13, from, to, ...be(x), ...be(y))); - cases.push(Uint8Array.of(21, from, ...be(x), ...be(y))); - } - } - } -} // A harness's own cases, which name what they cover. -if (Array.isArray(harness.cases)) cases.push(...harness.cases); +if (Array.isArray(harness.cases)) for (const input of harness.cases) cases.push(input); for (let i = 0; i < count; i++) { const len = Number(next() % 48n); const input = new Uint8Array(len + 1); diff --git a/ts/tests/fixtures.rs b/ts/tests/fixtures.rs index 7f13efeab..23ee864a2 100644 --- a/ts/tests/fixtures.rs +++ b/ts/tests/fixtures.rs @@ -149,9 +149,8 @@ fn typecheck(out: &Path) { } /// Run the harness natively, as wasm32, and translated, on the same inputs: the -/// translated side through `entry`, a module in `out`. `features` are the package's, and -/// `extra` are `differential.ts`'s arguments after the case count. -fn differential(package: &str, features: &[&str], out: &Path, entry: &str, cases: u32, extra: &[&str]) { +/// translated side through `entry`, a module in `out`. `features` are the package's. +fn differential(package: &str, features: &[&str], out: &Path, entry: &str, cases: u32) { let target_dir = scratch().join("wasm"); let status = cargo() .args([ @@ -180,7 +179,6 @@ fn differential(package: &str, features: &[&str], out: &Path, entry: &str, cases .arg(&wasm) .arg(out.join(entry)) .arg(cases.to_string()) - .args(extra) .output() .expect("running node, which the dev shell provides"); let stdout = String::from_utf8_lossy(&output.stdout); @@ -198,7 +196,7 @@ fn codec() { snapshot("codec", &out); std::fs::write(out.join("shake.ts"), SHAKE).unwrap(); typecheck(&out); - differential("codec-fixture", &[], &out, "harness.ts", 20_000, &[]); + differential("codec-fixture", &[], &out, "harness.ts", 20_000); shake(&out); } @@ -298,12 +296,12 @@ fn source_order() { /// impl of a method, renames nothing the old roots reach. #[test] fn stable_names() { - let roots = r#"roots = ["harness::run", "fmt::Fault"]"#; + let roots = r#"roots = ["harness::run", "harness::op", "fmt::Fault"]"#; added_root( "lang", roots, roots, - r#"roots = ["fmt::added", "harness::run", "fmt::Fault"]"#, + r#"roots = ["fmt::added", "harness::run", "harness::op", "fmt::Fault"]"#, ); } @@ -371,7 +369,7 @@ fn lang_differential(out: &Path) { std::fs::copy(manifest_dir().join("fixtures/lang").join(file), out.join(file)).unwrap(); } typecheck(out); - differential("lang-fixture", &[], out, "caller.ts", 50_000, &["wide"]); + differential("lang-fixture", &[], out, "caller.ts", 50_000); } /// A copy that only one write in the crate keeps, of each kind: one per feature. @@ -380,7 +378,7 @@ fn share() { let out = translated("share"); snapshot("share", &out); typecheck(&out); - differential("share-fixture", &[], &out, "harness.ts", 1_000, &[]); + differential("share-fixture", &[], &out, "harness.ts", 1_000); let config = fixture("share").join("ts.toml"); for feature in ["call", "pointer"] { let out = scratch().join("share").join(feature); @@ -398,7 +396,7 @@ fn share() { String::from_utf8_lossy(&output.stderr) ); typecheck(&out); - differential("share-fixture", &[feature], &out, "harness.ts", 1_000, &[]); + differential("share-fixture", &[feature], &out, "harness.ts", 1_000); } } @@ -423,7 +421,7 @@ import * as drops from "./drops.ts"; import * as futures from "./futures.ts"; import {{ harness }} from {harness:?}; -export {{ cases }} from {harness:?}; +export {{ cases, steps }} from {harness:?}; const fixture = {{ Pending: rt.Pending, u64: {{ of: rt.u64Of }}, Log, Channel, Guard, sum, drain, blocks, drops, futures }}; const script = harness(fixture); export const run = (input: rt.ByteSlice) => script(rt.byteSliceView(input)); @@ -432,7 +430,7 @@ export const unwind = harness(fixture, true); ); std::fs::write(out.join("caller.ts"), text).unwrap(); typecheck(out); - differential("task-fixture", &[], out, "caller.ts", 20_000, &[]); + differential("task-fixture", &[], out, "caller.ts", 20_000); unwinding(out, "caller.ts", 5_000); } @@ -488,7 +486,7 @@ fn bind() { let out = translated("bind"); snapshot("bind", &out); typecheck(&out); - differential("bind-fixture", &[], &out, "harness.ts", 1_000, &[]); + differential("bind-fixture", &[], &out, "harness.ts", 1_000); for config in ["wrong", "refused"] { let out = scratch().join("bind").join(config); let toml = fixture("bind").join(format!("{config}.toml")); diff --git a/ts/tests/task.ts b/ts/tests/task.ts index 3a3f76dbd..b18e5d6b6 100644 --- a/ts/tests/task.ts +++ b/ts/tests/task.ts @@ -168,6 +168,115 @@ class Executor { } } +/** What a step acts on: one script's fixture, log, executor, and channels. */ +interface Run { + f: Fixture; + log: Log; + note: (event: string) => void; + exec: Executor; + channels: Channel[]; +} + +/** The one of `len` channels or tasks that `arg` picks, if there are any. */ +const pick = (len: number, arg: number) => (len > 0 ? arg % len : undefined); + +/** A new `Guard` for the next task spawned. */ +const guard = (r: Run) => r.f.Guard.new(r.exec.tasks.length, r.f.Log.clone$Clone(r.log)); + +/** + * Each step and its name, which a script's op picks modulo their count: `harness.rs`'s + * `steps`, in the same order. + */ +export const steps: [string, (r: Run, arg: number) => void][] = [ + [ + "open", + (r) => { + r.note(`c${r.channels.length} open`); + r.channels.push(r.f.Channel.open()); + }, + ], + [ + "sum", + (r, arg) => { + const c = pick(r.channels.length, arg); + if (c !== undefined) r.exec.spawn(r.f.sum(r.f.Channel.consumer(r.channels[c]), guard(r))); + }, + ], + [ + "drain", + (r, arg) => { + const c = pick(r.channels.length, arg); + if (c !== undefined) r.exec.spawn(r.f.drain(r.f.Channel.consumer(r.channels[c]), [1, 4, 16][(arg >> 4) % 3])); + }, + ], + [ + "send", + (r, arg) => { + const c = pick(r.channels.length, arg); + if (c === undefined) return; + r.note(`c${c} send ${arg}`); + if (!r.f.Channel.send(r.channels[c], r.f.u64.of(arg))) r.note(`c${c} closed`); + }, + ], + [ + "close", + (r, arg) => { + const c = pick(r.channels.length, arg); + if (c === undefined) return; + r.note(`c${c} close`); + r.f.Channel.close(r.channels[c]); + }, + ], + [ + "clone", + (r, arg) => { + const c = pick(r.channels.length, arg); + if (c === undefined) return; + r.note(`c${c} clone`); + r.f.Channel.cloneProducer(r.channels[c]); + }, + ], + ["run", (r) => r.exec.run()], + ["step", (r) => r.exec.step()], + [ + "poll", + (r, arg) => { + const t = pick(r.exec.tasks.length, arg); + if (t !== undefined) r.exec.poll(t); + }, + ], + [ + "drop", + (r, arg) => { + const t = pick(r.exec.tasks.length, arg); + if (t !== undefined) r.exec.drop(t); + }, + ], + [ + "blocks", + (r, arg) => { + const c = pick(r.channels.length, arg); + if (c !== undefined) r.exec.spawn(r.f.blocks(r.f.Channel.consumer(r.channels[c]), guard(r), arg >= 128)); + }, + ], + ["drops", (r, arg) => r.f.drops.run(r.log, 1000 * r.exec.tasks.length, arg)], + [ + "held", + (r, arg) => { + const c = pick(r.channels.length, arg); + if (c !== undefined) r.exec.spawn(r.f.drops.held(r.f.Channel.consumer(r.channels[c]), guard(r))); + }, + ], + ["edited", (r, arg) => r.f.drops.edited(r.log, 1000 * r.exec.tasks.length, arg % 8)], + [ + "helpers", + (r, arg) => { + const c = pick(r.channels.length, arg); + if (c !== undefined) r.exec.spawn(r.f.futures.helpers(r.f.Channel.consumer(r.channels[c]), guard(r))); + }, + ], +]; + /** * The script runner, as `harness.rs` runs it natively. Where a panic unwinds, as on * the host, `unwind` logs a step's panic and goes on, as the native harness does. @@ -177,127 +286,71 @@ export function harness(f: Fixture, unwind = false): (input: Uint8Array) = const log = f.Log.new(); const note = (event: string) => f.Log.note(log, event); const exec = new Executor(note, f.Pending); - const channels: Channel[] = []; + const r: Run = { f, log, note, exec, channels: [] }; for (let i = 0; i < input.length; i += 2) { - const op = input[i]; + const [, step] = steps[input[i] % steps.length]; const arg = input[i + 1] ?? 0; if (!unwind) { - step(op, arg); + step(r, arg); continue; } try { - step(op, arg); + step(r, arg); } catch (err) { if (!(err instanceof Error && err.name === "Panic")) throw err; note(`panic ${err.message}`); } } for (let t = 0; t < exec.tasks.length; t++) exec.drop(t); - channels.forEach((channel, c) => { + r.channels.forEach((channel, c) => { note(`c${c} drop`); channel[Symbol.dispose](); }); return f.Log.text(log); - - function step(op: number, arg: number): void { - const pick = (len: number) => (len > 0 ? arg % len : undefined); - const c = pick(channels.length); - switch (op % 15) { - case 0: - note(`c${channels.length} open`); - channels.push(f.Channel.open()); - break; - case 1: - if (c !== undefined) exec.spawn(f.sum(f.Channel.consumer(channels[c]), f.Guard.new(exec.tasks.length, f.Log.clone$Clone(log)))); - break; - case 2: - if (c !== undefined) exec.spawn(f.drain(f.Channel.consumer(channels[c]), [1, 4, 16][(arg >> 4) % 3])); - break; - case 3: - if (c !== undefined) { - note(`c${c} send ${arg}`); - if (!f.Channel.send(channels[c], f.u64.of(arg))) note(`c${c} closed`); - } - break; - case 4: - if (c !== undefined) { - note(`c${c} close`); - f.Channel.close(channels[c]); - } - break; - case 5: - if (c !== undefined) { - note(`c${c} clone`); - f.Channel.cloneProducer(channels[c]); - } - break; - case 6: - exec.run(); - break; - case 7: - exec.step(); - break; - case 8: { - const t = pick(exec.tasks.length); - if (t !== undefined) exec.poll(t); - break; - } - case 9: { - const t = pick(exec.tasks.length); - if (t !== undefined) exec.drop(t); - break; - } - case 10: - if (c !== undefined) { - exec.spawn(f.blocks(f.Channel.consumer(channels[c]), f.Guard.new(exec.tasks.length, f.Log.clone$Clone(log)), arg >= 128)); - } - break; - case 11: - f.drops.run(log, 1000 * exec.tasks.length, arg); - break; - case 12: - if (c !== undefined) exec.spawn(f.drops.held(f.Channel.consumer(channels[c]), f.Guard.new(exec.tasks.length, f.Log.clone$Clone(log)))); - break; - case 13: - f.drops.edited(log, 1000 * exec.tasks.length, arg % 8); - break; - default: - if (c !== undefined) exec.spawn(f.futures.helpers(f.Channel.consumer(channels[c]), f.Guard.new(exec.tasks.length, f.Log.clone$Clone(log)))); - } - } }; } +/** A script of steps, each by name and with its argument. */ +function script(...script: [string, number][]): Uint8Array { + return Uint8Array.from( + script.flatMap(([name, arg]) => { + const op = steps.findIndex(([n]) => n === name); + if (op < 0) throw new Error(`no step ${name}`); + return [op, arg]; + }), + ); +} + /** Scripts for the cases async must get right, run before the random ones. */ export const cases: Uint8Array[] = [ // A future dropped before its first poll runs nothing, and drops its arguments. - [0, 0, 1, 0, 2, 0, 9, 0, 9, 1], + script(["open", 0], ["sum", 0], ["drain", 0], ["drop", 0], ["drop", 1]), // One dropped mid-await drops its awaited future, then its locals, then its arguments. - [0, 0, 1, 0, 6, 0, 3, 5, 6, 0, 9, 0], + script(["open", 0], ["sum", 0], ["run", 0], ["send", 5], ["run", 0], ["drop", 0]), // Polling a future after it returned `Ready` panics. - [0, 0, 1, 0, 6, 0, 4, 0, 6, 0, 8, 0], + script(["open", 0], ["sum", 0], ["run", 0], ["close", 0], ["run", 0], ["poll", 0]), // Every await depth, woken by sends and the channel closing. - [0, 0, 2, 0, 2, 16, 2, 32, 6, 0, 3, 7, 6, 0, 3, 9, 7, 0, 4, 0, 6, 0], + script(["open", 0], ["drain", 0], ["drain", 16], ["drain", 32], ["run", 0], ["send", 7], ["run", 0], ["send", 9], ["step", 0], ["close", 0], ["run", 0]), // The channel stays open until its last producer drops. - [0, 0, 1, 0, 5, 0, 4, 0, 6, 0, 3, 2, 4, 0, 6, 0], + script(["open", 0], ["sum", 0], ["clone", 0], ["close", 0], ["run", 0], ["send", 2], ["close", 0], ["run", 0]), // A task woken twice before its poll is queued once. - [0, 0, 0, 1, 1, 0, 1, 1, 6, 0, 3, 1, 5, 0, 3, 2, 6, 0], + script(["open", 0], ["open", 1], ["sum", 0], ["sum", 1], ["run", 0], ["send", 1], ["clone", 0], ["send", 2], ["run", 0]), // An async block's captures: one borrowed, one moved on one path only, and one moved // but only borrowed, dropped when its block returns or is cancelled mid-await. - [0, 0, 10, 0, 10, 128, 10, 0, 6, 0, 3, 5, 7, 0, 7, 0, 9, 2, 6, 0], + script(["open", 0], ["blocks", 0], ["blocks", 128], ["blocks", 0], ["run", 0], ["send", 5], ["step", 0], ["step", 0], ["drop", 2], ["run", 0]), // A future that panics mid-loop holding guards: unwinding drops them, and a poll after // panics again. - [0, 0, 1, 0, 1, 0, 6, 0, 3, 99, 7, 0, 3, 4, 6, 0, 8, 0, 9, 1], + script(["open", 0], ["sum", 0], ["sum", 0], ["run", 0], ["send", 99], ["step", 0], ["send", 4], ["run", 0], ["poll", 0], ["drop", 1]), // Values std holds that drop, held across an await: returned, and cancelled. - [0, 0, 12, 0, 6, 0, 3, 5, 6, 0], - [0, 0, 12, 0, 12, 1, 6, 0, 9, 1, 9, 0], + script(["open", 0], ["held", 0], ["run", 0], ["send", 5], ["run", 0]), + script(["open", 0], ["held", 0], ["held", 1], ["run", 0], ["drop", 1], ["drop", 0]), // std's helpers: a `poll_fn` over a pinned `recv` and `future::pending`, returning, // cancelled with the guard it owns, and panicking inside it. - [0, 0, 14, 0, 6, 0, 3, 5, 6, 0, 3, 77, 6, 0], - [0, 0, 14, 0, 14, 1, 6, 0, 3, 4, 7, 0, 9, 1, 9, 0], - [0, 0, 14, 0, 6, 0, 3, 99, 6, 0, 8, 0], + script(["open", 0], ["helpers", 0], ["run", 0], ["send", 5], ["run", 0], ["send", 77], ["run", 0]), + script(["open", 0], ["helpers", 0], ["helpers", 1], ["run", 0], ["send", 4], ["step", 0], ["drop", 1], ["drop", 0]), + script(["open", 0], ["helpers", 0], ["run", 0], ["send", 99], ["run", 0], ["poll", 0]), // Each scenario of values std holds that drop, panicking at each point or none. - ...Array.from({ length: 253 }, (_, arg) => [11, arg]), + ...Array.from({ length: 253 }, (_, arg) => script(["drops", arg])), // Each of a `Vec`'s edits, its and a tuple's `clone`, and `Ord`'s defaults, panicking at each point or none. - ...Array.from({ length: 8 }, (_, at) => [13, at]), -].map((script) => Uint8Array.from(script)); + ...Array.from({ length: 8 }, (_, at) => script(["edited", at])), +]; diff --git a/ts/tests/unwind.ts b/ts/tests/unwind.ts index 5312334df..e6871d926 100644 --- a/ts/tests/unwind.ts +++ b/ts/tests/unwind.ts @@ -6,7 +6,7 @@ // usage: node unwind.ts [scripts] // // The caller exports `unwind`, the translation's harness with panics caught per step, -// and `cases`, the scripts it names. The rest are random, with sends of the value that +// `steps`, what a script's ops pick from, and `cases`, the scripts it names. The rest are random, with sends of the value that // panics mixed in. import { spawnSync } from "node:child_process"; @@ -31,7 +31,7 @@ for (let i = 0; i < count; i++) { const steps = 4 + (next() % 20); const script = new Uint8Array(steps * 2); for (let s = 0; s < steps; s++) { - script[2 * s] = next() % 15; + script[2 * s] = next() % caller.steps.length; // A send of 99 panics the `sum` reading it. script[2 * s + 1] = next() % 4 === 0 ? 99 : next() % 256; }