ci / test (postgres) (push) Successful in 1m24s
ci / test (sqlite) (push) Successful in 1m35s
ci / image (push) Successful in 16s
ci / test (postgres) (pull_request) Successful in 1m11s
ci / test (sqlite) (pull_request) Successful in 1m24s
ci / image (pull_request) Successful in 14s
The independent review found that the policy tests could be bypassed: Microsoft-named direct packages and GlobalPackageReference slipped past the dependency check, the live-money scan missed the JSON, Dockerfile and Unraid template forms, the secret scan missed short project secrets but would have failed on the user's own ignored .env. The scans now read only the files git would commit and cover those cases. Also adds the fixed-seed generator for property tests, the REGISTRY variables reserved for phase 9 in the CI, a git pre-commit hook in place of the pre-commit tool (tasks hooks), the missing SPEC 4 facts in API-NOTES (Fed/ECB/BLS times, USDC depth on OKX) and the full list of today's indirect packages. refs #3 refs #5 refs #6 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
3.5 KiB
C#
108 lines
3.5 KiB
C#
using System.Globalization;
|
|
|
|
namespace Anubis.Tests;
|
|
|
|
/// <summary>
|
|
/// Property tests without external packages (ADR 0003, 0004): many generated cases from a fixed
|
|
/// seed, so a failure always repeats, and a message that names the seed and the failing case.
|
|
/// </summary>
|
|
internal static class Property
|
|
{
|
|
public const int DefaultSeed = 20261110;
|
|
public const int DefaultCases = 200;
|
|
|
|
public static void ForAll<T>(Func<Gen, T> generate, Action<T> property, int cases = DefaultCases, int seed = DefaultSeed)
|
|
{
|
|
var gen = new Gen(seed);
|
|
for (var i = 0; i < cases; i++)
|
|
{
|
|
var value = generate(gen);
|
|
try
|
|
{
|
|
property(value);
|
|
}
|
|
catch (Exception ex) when (ex is not AssertInconclusiveException)
|
|
{
|
|
throw new AssertFailedException(
|
|
string.Create(CultureInfo.InvariantCulture, $"Proprietà violata al caso {i} (seme {seed}): {value}"), ex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fixed-seed generator based on SplitMix64. Unlike a seeded <see cref="Random"/>, its sequence is
|
|
/// fixed by this code, so it stays the same on every platform and .NET version.
|
|
/// </summary>
|
|
internal sealed class Gen(int seed)
|
|
{
|
|
private ulong _state = unchecked((ulong)(long)seed);
|
|
|
|
public ulong NextUInt64()
|
|
{
|
|
var z = _state += 0x9E3779B97F4A7C15UL;
|
|
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9UL;
|
|
z = (z ^ (z >> 27)) * 0x94D049BB133111EBUL;
|
|
return z ^ (z >> 31);
|
|
}
|
|
|
|
/// <summary>Uniform integer in [<paramref name="minInclusive"/>, <paramref name="maxExclusive"/>).</summary>
|
|
public long Long(long minInclusive, long maxExclusive)
|
|
{
|
|
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(minInclusive, maxExclusive);
|
|
var range = unchecked((ulong)(maxExclusive - minInclusive));
|
|
var limit = ulong.MaxValue - (ulong.MaxValue % range);
|
|
ulong x;
|
|
do
|
|
{
|
|
x = NextUInt64();
|
|
}
|
|
while (x >= limit);
|
|
|
|
return unchecked(minInclusive + (long)(x % range));
|
|
}
|
|
|
|
public int Int(int minInclusive, int maxExclusive) => (int)Long(minInclusive, maxExclusive);
|
|
|
|
public bool Bool() => (NextUInt64() & 1UL) == 1UL;
|
|
|
|
/// <summary>Uniform value in [0, 1).</summary>
|
|
public double Double() => (NextUInt64() >> 11) * (1.0 / (1UL << 53));
|
|
|
|
/// <summary>A decimal in [<paramref name="min"/>, <paramref name="max"/>] with at most <paramref name="scale"/> decimals.</summary>
|
|
public decimal Decimal(decimal min, decimal max, int scale)
|
|
{
|
|
ArgumentOutOfRangeException.ThrowIfGreaterThan(min, max);
|
|
ArgumentOutOfRangeException.ThrowIfNegative(scale);
|
|
ArgumentOutOfRangeException.ThrowIfGreaterThan(scale, 18);
|
|
|
|
var unit = 1m;
|
|
for (var i = 0; i < scale; i++)
|
|
{
|
|
unit /= 10m;
|
|
}
|
|
|
|
var low = decimal.Ceiling(min / unit);
|
|
var high = decimal.Floor(max / unit);
|
|
if (low > high)
|
|
{
|
|
throw new ArgumentException("Nessun valore con questa scala nell'intervallo.", nameof(scale));
|
|
}
|
|
|
|
var steps = high - low;
|
|
if (steps >= long.MaxValue)
|
|
{
|
|
throw new ArgumentException("Intervallo troppo ampio per questa scala.", nameof(scale));
|
|
}
|
|
|
|
var k = Long(0, (long)steps + 1);
|
|
return (low + k) * unit;
|
|
}
|
|
|
|
public T Pick<T>(IReadOnlyList<T> items)
|
|
{
|
|
ArgumentOutOfRangeException.ThrowIfZero(items.Count);
|
|
return items[Int(0, items.Count)];
|
|
}
|
|
}
|