8bfe24ac1d
**Task 5 — golden-fixture parity for the C-ABI bindings.** Lifts the C#/Go/Java/R tests from *one indicator per archetype* toward reference-value parity, catching FFI wiring bugs (swapped params, wrong multi-output field) the math-only core tests cannot see. ## What's here - **`examples/rust/src/bin/gen_golden.rs`** + **`testdata/golden/*.csv`** — a Rust generator computing a deterministic OHLCV series plus the core's reference outputs for a curated archetype-spanning set: scalar (`Sma`/`Ema`/`Rsi`), candle (`Atr`), scalar multi-output (`MACD`), candle multi-output (`ADX`), pairwise (`Beta`). `nan` marks warmup. Regenerate with `cargo run -p wickra-examples --bin gen_golden`. - **Parity runners** replaying the identical fixtures through each FFI (rel-tol 1e-6), each a standard test in the binding's existing suite (no `ci.yml` change — rides `dotnet test` / `go test` / `mvn install` / `R CMD`+testthat). A walk-up search locates `testdata/golden` regardless of run dir. - **C#** (`bindings/csharp/.../GoldenTests.cs`) — ✅ validated locally, 7/7 pass. - **Go** (`bindings/go/golden_test.go`) — ✅ validated locally, pass. - **Java** (`bindings/java/.../GoldenTests.java`) — modeled on the archetype API; validated by CI (no local mvn). - **R** (`bindings/r/tests/testthat/test-golden.R`) — modeled on the archetype API; validated by CI (no local Rscript). ## Notes - The curated set spans every marshalling archetype; extending the indicator list is mechanical (add to the generator + regenerate). Bars/profile archetypes can be added next. - No new CI jobs.
163 lines
5.3 KiB
C#
163 lines
5.3 KiB
C#
using System.Globalization;
|
|
using System.Runtime.CompilerServices;
|
|
using Wickra;
|
|
using Xunit;
|
|
|
|
namespace Wickra.Tests;
|
|
|
|
/// <summary>
|
|
/// Golden-fixture parity: replay the shared <c>testdata/golden</c> input series
|
|
/// through the C# FFI and assert every value matches the Rust reference output.
|
|
/// Where the archetype tests only check finiteness, this pins exact values, so a
|
|
/// wiring bug (swapped parameter, wrong multi-output field) is caught.
|
|
/// Fixtures are generated by <c>cargo run -p wickra-examples --bin gen_golden</c>.
|
|
/// </summary>
|
|
public class GoldenTests
|
|
{
|
|
private const double Tol = 1e-6;
|
|
|
|
private static string GoldenDir([CallerFilePath] string file = "") =>
|
|
Path.GetFullPath(Path.Combine(Path.GetDirectoryName(file)!, "..", "..", "..", "testdata", "golden"));
|
|
|
|
private static List<string[]> ReadCsv(string name)
|
|
{
|
|
var path = Path.Combine(GoldenDir(), name + ".csv");
|
|
return File.ReadAllLines(path)
|
|
.Skip(1) // header
|
|
.Where(l => l.Length > 0)
|
|
.Select(l => l.Split(','))
|
|
.ToList();
|
|
}
|
|
|
|
private static double[][] Input()
|
|
{
|
|
return ReadCsv("input")
|
|
.Select(r => r.Select(c => double.Parse(c, CultureInfo.InvariantCulture)).ToArray())
|
|
.ToArray();
|
|
}
|
|
|
|
private static double Cell(string s) =>
|
|
s == "nan" ? double.NaN : double.Parse(s, CultureInfo.InvariantCulture);
|
|
|
|
private static void AssertClose(double got, double want, int row, string field)
|
|
{
|
|
if (double.IsNaN(want))
|
|
{
|
|
Assert.True(double.IsNaN(got), $"row {row} {field}: expected warmup/NaN, got {got}");
|
|
return;
|
|
}
|
|
var tol = Tol * Math.Max(1.0, Math.Abs(want));
|
|
Assert.True(Math.Abs(got - want) <= tol, $"row {row} {field}: got {got}, want {want}");
|
|
}
|
|
|
|
// --- scalar (close-driven) ------------------------------------------------
|
|
|
|
[Theory]
|
|
[InlineData("sma")]
|
|
[InlineData("ema")]
|
|
[InlineData("rsi")]
|
|
public void Scalar_MatchesGolden(string name)
|
|
{
|
|
var input = Input();
|
|
var expected = ReadCsv(name);
|
|
using var ind = (IDisposable)(name switch
|
|
{
|
|
"sma" => new Sma(14),
|
|
"ema" => new Ema(14),
|
|
"rsi" => new Rsi(14),
|
|
_ => throw new ArgumentOutOfRangeException(nameof(name)),
|
|
});
|
|
for (var i = 0; i < input.Length; i++)
|
|
{
|
|
var close = input[i][3];
|
|
double got = ind switch
|
|
{
|
|
Sma s => s.Update(close),
|
|
Ema e => e.Update(close),
|
|
Rsi r => r.Update(close),
|
|
_ => double.NaN,
|
|
};
|
|
AssertClose(got, Cell(expected[i][0]), i, name);
|
|
}
|
|
}
|
|
|
|
// --- candle, single output ------------------------------------------------
|
|
|
|
[Fact]
|
|
public void Candle_Atr_MatchesGolden()
|
|
{
|
|
var input = Input();
|
|
var expected = ReadCsv("atr");
|
|
using var atr = new Atr(14);
|
|
for (var i = 0; i < input.Length; i++)
|
|
{
|
|
var (o, h, l, c, v) = (input[i][0], input[i][1], input[i][2], input[i][3], input[i][4]);
|
|
AssertClose(atr.Update(o, h, l, c, v, i), Cell(expected[i][0]), i, "atr");
|
|
}
|
|
}
|
|
|
|
// --- pairwise -------------------------------------------------------------
|
|
|
|
[Fact]
|
|
public void Pairwise_Beta_MatchesGolden()
|
|
{
|
|
var input = Input();
|
|
var expected = ReadCsv("beta");
|
|
using var beta = new Beta(20);
|
|
for (var i = 0; i < input.Length; i++)
|
|
{
|
|
// generator fed (close, open)
|
|
AssertClose(beta.Update(input[i][3], input[i][0]), Cell(expected[i][0]), i, "beta");
|
|
}
|
|
}
|
|
|
|
// --- scalar multi-output: MACD -------------------------------------------
|
|
|
|
[Fact]
|
|
public void MultiOutput_Macd_MatchesGolden()
|
|
{
|
|
var input = Input();
|
|
var expected = ReadCsv("macd");
|
|
using var macd = new MacdIndicator(12, 26, 9);
|
|
for (var i = 0; i < input.Length; i++)
|
|
{
|
|
MacdOutput? got = macd.Update(input[i][3]);
|
|
var e = expected[i];
|
|
if (e[0] == "nan")
|
|
{
|
|
Assert.Null(got);
|
|
continue;
|
|
}
|
|
Assert.NotNull(got);
|
|
AssertClose(got!.Value.Macd, Cell(e[0]), i, "macd.macd");
|
|
AssertClose(got.Value.Signal, Cell(e[1]), i, "macd.signal");
|
|
AssertClose(got.Value.Histogram, Cell(e[2]), i, "macd.histogram");
|
|
}
|
|
}
|
|
|
|
// --- candle multi-output: ADX --------------------------------------------
|
|
|
|
[Fact]
|
|
public void MultiOutput_Adx_MatchesGolden()
|
|
{
|
|
var input = Input();
|
|
var expected = ReadCsv("adx");
|
|
using var adx = new Adx(14);
|
|
for (var i = 0; i < input.Length; i++)
|
|
{
|
|
var (o, h, l, c, v) = (input[i][0], input[i][1], input[i][2], input[i][3], input[i][4]);
|
|
AdxOutput? got = adx.Update(o, h, l, c, v, i);
|
|
var e = expected[i];
|
|
if (e[0] == "nan")
|
|
{
|
|
Assert.Null(got);
|
|
continue;
|
|
}
|
|
Assert.NotNull(got);
|
|
AssertClose(got!.Value.PlusDi, Cell(e[0]), i, "adx.plus_di");
|
|
AssertClose(got.Value.MinusDi, Cell(e[1]), i, "adx.minus_di");
|
|
AssertClose(got.Value.Adx, Cell(e[2]), i, "adx.adx");
|
|
}
|
|
}
|
|
}
|