tazel
A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.
Clonegit clone https://git.progmasoft.com/tazel
test(cadre-celeste): check red-green and change pruning on random graphs
- Commit
a0cf1eedd8ea2027c51bc8c9261da4b98b108351- Author
- Leitwolf11 ·
- Parents
da7332332b2e
Changes
.../CadreCelestePropertyTests.cs | 161 +++++++++++++++++++++ 1 file changed, 161 insertions(+) diff --git a/tests/Progmasoft.Tazel.Tests/CadreCelestePropertyTests.cs b/tests/Progmasoft.Tazel.Tests/CadreCelestePropertyTests.cs new file mode 100644 index 0000000..66244c0 --- /dev/null +++ b/tests/Progmasoft.Tazel.Tests/CadreCelestePropertyTests.cs @@ -0,0 +1,161 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: MIT + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading; +using CsCheck; +using Progmasoft.Tazel.CadreCeleste; +using Xunit; + +namespace Progmasoft.Tazel.Tests; + +/// <summary> +/// The two properties CadreCeleste.md asks of the engine, on random graphs: after any sequence of input changes and +/// queries, every node asked for equals a from-scratch evaluation, and no node's function runs unless a node it read +/// when it last ran took another value since: an input set to a different value, or a derived node whose function +/// returned a different value than the time before. That is red-green with change pruning; a node recomputed to the +/// same value changes nothing downstream. +/// </summary> +public sealed class CadreCelestePropertyTests +{ + /// <summary> + /// A derived node: it reads <see cref="Gate"/> first; when the gate's value is even it also reads every node of + /// <see cref="Reads"/>, otherwise only the first, so what it depends on changes with the values. + /// </summary> + private sealed record Derived(int Gate, int[] Reads, int Multiplier); + + /// <summary>A graph: inputs 0..Inputs-1, then derived nodes, each reading only nodes before it.</summary> + private sealed record Graph(int Inputs, Derived[] Nodes) + { + public int Count => Inputs + Nodes.Length; + } + + private static readonly Gen<Graph> Graphs = + from inputs in Gen.Int[1, 4] + from count in Gen.Int[1, 8] + from nodes in Gen.Int[0, 1_000_000].Array[3, 5].Array[count] + select new Graph(inputs, [.. nodes.Select((seed, index) => DerivedOf(inputs + index, seed))]); + + // Node `self` may read any node before it; the seed numbers pick which, and the multiplier. + private static Derived DerivedOf(int self, int[] seed) => + new(seed[0] % self, [.. seed.Skip(1).Take(seed.Length - 2).Select(value => value % self)], (seed[^1] % 3) + 1); + + private static NodeKey Key(Graph graph, int node) => node < graph.Inputs + ? new NodeKey("in", node.ToString(CultureInfo.InvariantCulture)) + : new NodeKey("derived", node.ToString(CultureInfo.InvariantCulture)); + + // Small values, so that different inputs often give equal values and change pruning is exercised. + private static int Combine(Derived node, int gate, IEnumerable<int> values) => ((gate + values.Sum()) * node.Multiplier) % 5; + + private static int Scratch(Graph graph, int[] inputs, int node) + { + if (node < graph.Inputs) + { + return inputs[node]; + } + + var derived = graph.Nodes[node - graph.Inputs]; + var gate = Scratch(graph, inputs, derived.Gate); + var reads = gate % 2 == 0 ? derived.Reads : derived.Reads.Take(1); + return Combine(derived, gate, reads.Select(read => Scratch(graph, inputs, read))); + } + + [Fact] + public void IncrementalEqualsFromScratchAndNoNodeRunsWithoutAnUpstreamChange() + { + var cancel = TestContext.Current.CancellationToken; + var steps = Gen.Select(Gen.Int[0, 1_000_000], Gen.Int[0, 9], Gen.Int[0, 1_000_000]).Array[1, 12]; + Gen.Select(Graphs, Gen.Int[0, 9].Array[4], steps).Sample((graph, initial, changes) => + { + var inputs = initial[..graph.Inputs]; + var step = 0; + // The test's own model of the engine: when each node last took a different value, what each derived node + // read and returned when it last ran, and when that was. + var changedAt = new int[graph.Count]; + var returned = new Dictionary<int, int>(); + var read = new Dictionary<int, List<int>>(); + var ranAt = new Dictionary<int, int>(); + var runs = 0; + var engine = new GraphEngine(); + engine.Register("derived", async (key, context, token) => + { + var node = int.Parse(key.Name, CultureInfo.InvariantCulture); + if (ranAt.TryGetValue(node, out var last)) + { + // Red-green: a node none of whose last reads took another value since it last ran is green, and + // green nodes are not run again. + Assert.True(read[node].Any(dependency => changedAt[dependency] > last), + $"node {node} ran at step {step} although nothing it read changed since step {last}"); + } + + ranAt[node] = step; + runs++; + var derived = graph.Nodes[node - graph.Inputs]; + var reads = new List<int> { derived.Gate }; + var gate = await context.GetAsync<int>(Key(graph, derived.Gate), token); + var values = new List<int>(); + foreach (var next in gate % 2 == 0 ? derived.Reads : derived.Reads.Take(1)) + { + reads.Add(next); + values.Add(await context.GetAsync<int>(Key(graph, next), token)); + } + + var result = Combine(derived, gate, values); + if (!returned.TryGetValue(node, out var before) || before != result) + { + changedAt[node] = step; + } + + returned[node] = result; + read[node] = reads; + return result; + }); + + for (var input = 0; input < graph.Inputs; input++) + { + engine.SetInput(Key(graph, input), inputs[input]); + } + + Check(graph, engine, inputs, Enumerable.Range(graph.Inputs, graph.Nodes.Length), cancel); + foreach (var (which, value, query) in changes) + { + step++; + var input = which % graph.Inputs; + if (inputs[input] != value) + { + changedAt[input] = step; + } + + inputs[input] = value; + engine.SetInput(Key(graph, input), value); + + // Ask for a random, non-empty subset of the derived nodes, in a random order: evaluation is on demand. + var asked = Enumerable.Range(graph.Inputs, graph.Nodes.Length) + .Where(node => ((query >> (node % 20)) & 1) == 1 || node == graph.Count - 1) + .OrderBy(node => (node * 7919) ^ query) + .ToList(); + Check(graph, engine, inputs, asked, cancel); + } + + // Everything asked for twice in a row: the second time, with nothing changed in between, runs nothing. + step++; + var all = Enumerable.Range(graph.Inputs, graph.Nodes.Length).ToList(); + Check(graph, engine, inputs, all, cancel); + var before = runs; + Check(graph, engine, inputs, all, cancel); + Assert.Equal(before, runs); + }, iter: 300); + } + + private static void Check(Graph graph, GraphEngine engine, int[] inputs, IEnumerable<int> asked, CancellationToken cancel) + { + foreach (var node in asked) + { + var incremental = engine.GetAsync<int>(Key(graph, node), cancel).AsTask().GetAwaiter().GetResult(); + Assert.Equal(Scratch(graph, inputs, node), incremental); + } + } +}