diff --git a/CsCheck/Check.cs b/CsCheck/Check.cs index 545e600..58cc392 100644 --- a/CsCheck/Check.cs +++ b/CsCheck/Check.cs @@ -1,4 +1,4 @@ -// Copyright 2026 Anthony Lloyd +// Copyright 2026 Anthony Lloyd // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -40,7 +40,6 @@ public static partial class Check public static int Ulps = ParseEnvironmentVariableToInt("CsCheck_Ulps", 4); /// The number of Where Gne iterations before throwing an exception. public static int WhereLimit = ParseEnvironmentVariableToInt("CsCheck_WhereLimit", 100); - internal static bool IsDebug = Assembly.GetCallingAssembly().GetCustomAttribute()?.IsJITTrackingEnabled ?? false; sealed class SampleActionWorker(Gen gen, Action assert, CountdownEvent cde, string? seed, long target, bool isIter) : IThreadPoolWorkItem { @@ -1408,7 +1407,7 @@ public static void SampleModelBased(this Gen<(Actual, Model)> ini } new GenInitial(initial) - .Select(Gen.OneOf(opNameActions).Array, (a, b) => new ModelBasedData(a.Actual, a.Model, a.Stream, a.Seed, b)) + .Select(Gen.OneOf(opNameActions).Array(), (a, b) => new ModelBasedData(a.Actual, a.Model, a.Stream, a.Seed, b)) .Sample(d => { try @@ -1754,7 +1753,7 @@ public static void SampleParallel(this Gen initial, GenOperation[] oper Gen.Int[2, maxParallelOperations] .SelectMany(np => Gen.Int[2, Math.Min(threads, np)].Select(nt => (nt, np))) .SelectMany((nt, np) => Gen.Int[0, maxSequentialOperations].Select(ns => (ns, nt, np))) - .SelectMany((ns, nt, np) => new GenSampleParallel(initial).Select(genOps.Array[ns], genOps.Array[np]) + .SelectMany((ns, nt, np) => new GenSampleParallel(initial).Select(genOps.Array()[ns], genOps.Array()[np]) .Select((initial, sequential, parallel) => (initial, sequential, nt, parallel))) .Select((initial, sequential, threads, parallel) => new SampleParallelData(initial.Value, initial.Stream, initial.Seed, sequential, parallel, threads)) .Sample(spd => @@ -2018,7 +2017,7 @@ public static void SampleParallel(this Gen<(Actual, Model)> initi Gen.Int[2, maxParallelOperations] .SelectMany(np => Gen.Int[2, Math.Min(threads, np)].Select(nt => (nt, np))) .SelectMany((nt, np) => Gen.Int[0, maxSequentialOperations].Select(ns => (ns, nt, np))) - .SelectMany((ns, nt, np) => new GenSampleParallel(initial).Select(genOps.Array[ns], genOps.Array[np]) + .SelectMany((ns, nt, np) => new GenSampleParallel(initial).Select(genOps.Array()[ns], genOps.Array()[np]) .Select((initial, sequential, parallel) => (initial, sequential, nt, parallel))) .Select((initial, sequential, threads, parallel) => new SampleParallelData(initial.Actual, initial.Model, initial.Stream, initial.Seed, sequential, parallel, threads)) .Sample(spd => @@ -2268,7 +2267,7 @@ public void Execute() { if (result.Add(fasterTimer.Time(), slowerTimer.Time())) { - if (raiseexception && result.NotFaster && !IsDebug) + if (raiseexception && result.NotFaster) result.Exception ??= new CsCheckException(result.ToString()); running = false; return; @@ -2354,7 +2353,7 @@ public void Execute() { if (result.Add(fasterTimer.Time(out var fasterValue), slowerTimer.Time(out var slowerValue))) { - if (raiseexception && result.NotFaster && !IsDebug) + if (raiseexception && result.NotFaster) result.Exception ??= new CsCheckException(result.ToString()); running = false; return; @@ -2410,7 +2409,7 @@ public static void Faster(Func faster, Func slower, Func? e while (--threads > 0) ThreadPool.UnsafeQueueUserWorkItem(worker, false); worker.Execute(); - if (raiseexception && result.NotFaster && !IsDebug) + if (raiseexception && result.NotFaster) throw new CsCheckException(result.ToString()); if (result.Exception is not null) throw result.Exception; if (writeLine is not null) result.Output(writeLine); @@ -2441,7 +2440,7 @@ async Task Worker() { if (result.Add(await fasterTimer.Time().ConfigureAwait(false), await slowerTimer.Time().ConfigureAwait(false))) { - if (raiseexception && result.NotFaster && !IsDebug) + if (raiseexception && result.NotFaster) result.Exception ??= new CsCheckException(result.ToString()); running = false; return; @@ -2498,7 +2497,7 @@ async Task Worker() var (slowerTime, slowerValue) = await slowerTimer.Time().ConfigureAwait(false); if (result.Add(fasterTime, slowerTime)) { - if (raiseexception && result.NotFaster && !IsDebug) + if (raiseexception && result.NotFaster) result.Exception ??= new CsCheckException(result.ToString()); running = false; return; @@ -2552,7 +2551,7 @@ public void Execute() t = gen.Generate(pcg, null, out _); if (running && result.Add(fasterTimer.Time(t), slowerTimer.Time(t))) { - if (raiseexception && result.NotFaster && !IsDebug) + if (raiseexception && result.NotFaster) result.Exception ??= new CsCheckException(result.ToString()); running = false; return; @@ -2757,7 +2756,7 @@ async Task Worker() if (!running) return; if (result.Add(await fasterTimer.Time(t).ConfigureAwait(false), await slowerTimer.Time(t).ConfigureAwait(false))) { - if (raiseexception && result.NotFaster && !IsDebug) + if (raiseexception && result.NotFaster) result.Exception ??= new CsCheckException(result.ToString()); running = false; return; @@ -2918,7 +2917,7 @@ public void Execute() t = gen.Generate(pcg, null, out _); if (result.Add(fasterTimer.Time(t, out var fasterValue), slowerTimer.Time(t, out var slowerValue))) { - if (raiseexception && result.NotFaster && !IsDebug) + if (raiseexception && result.NotFaster) result.Exception ??= new CsCheckException(result.ToString()); running = false; return; @@ -3180,7 +3179,7 @@ async Task Worker() var (slowerTime, slowerValue) = await slowerTimer.Time(t).ConfigureAwait(false); if (result.Add(fasterTime, slowerTime)) { - if (raiseexception && result.NotFaster && !IsDebug) + if (raiseexception && result.NotFaster) result.Exception ??= new CsCheckException(result.ToString()); running = false; return; @@ -3555,9 +3554,7 @@ public override string ToString() var result = $"{Median.Median:P2}[{Median.Q1:P2}..{Median.Q3:P2}] {times:#0.00}x[{q1Times:#0.00}x..{q3Times:#0.00}x] {faster}"; if (double.IsNaN(Median.Median)) result = $"Time resolution too small try using repeat.\n{result}"; else if ((Median.Median >= 0.0) != (Faster > Slower)) result = $"Inconsistent result try using repeat or increasing sigma.\n{result}"; - result = $"{result}, sigma = {Math.Sqrt(SigmaSquared):#0.0} ({Faster:#,0} vs {Slower:#,0}), min = {timeString((double)FasterMin / repeat)}{timeUnit} vs {timeString((double)SlowerMin / repeat)}{timeUnit}"; - if (Check.IsDebug) result += " - DEBUG MODE - DO NOT TRUST THESE RESULTS"; - return result; + return $"{result}, sigma = {Math.Sqrt(SigmaSquared):#0.0} ({Faster:#,0} vs {Slower:#,0}), min = {timeString((double)FasterMin / repeat)}{timeUnit} vs {timeString((double)SlowerMin / repeat)}{timeUnit}"; } private static (Func, string) TimeFormat(double maxValue) => diff --git a/CsCheck/CsCheck.csproj b/CsCheck/CsCheck.csproj index 5cdaab2..79aec1d 100644 --- a/CsCheck/CsCheck.csproj +++ b/CsCheck/CsCheck.csproj @@ -44,10 +44,11 @@ Fixed Faster statistics locking. CS1591,MA0143 README.md All + true - + diff --git a/CsCheck/Dbg.cs b/CsCheck/Dbg.cs index 79cae17..37796b9 100644 --- a/CsCheck/Dbg.cs +++ b/CsCheck/Dbg.cs @@ -91,7 +91,7 @@ public static IEnumerable Output() lock (stats) { var now = Stopwatch.GetTimestamp(); - return stats.Select(i => + return [.. stats.Select(i => { var (completed, starts) = i.Value; lock (starts) @@ -101,7 +101,7 @@ public static IEnumerable Output() running.Add(now - start); return KeyValuePair.Create(i.Key, (completed, running)); } - }).ToArray(); + })]; } } @@ -120,11 +120,8 @@ public static void Clear() objects = new(); functions = new(); times = new(); - if (regressionStream is not null) - { - regressionStream.Dispose(); - regressionStream = null; - } + regressionStream?.Dispose(); + regressionStream = null; } /// Save object by name. @@ -363,20 +360,14 @@ public IEnumerator GetEnumerator() _cache.Add(current); yield return current; } - if (_enumerator is not null) - { - _enumerator.Dispose(); - _enumerator = null; - } + _enumerator?.Dispose(); + _enumerator = null; for (; index < _cache.Count; index++) yield return _cache[index]; } public void Dispose() { - if (_enumerator is not null) - { - _enumerator.Dispose(); - _enumerator = null; - } + _enumerator?.Dispose(); + _enumerator = null; } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } @@ -841,7 +832,7 @@ public static class RegressionExtensions public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -849,7 +840,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -857,7 +848,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -865,7 +856,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -873,7 +864,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -881,7 +872,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -889,7 +880,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -897,7 +888,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -905,7 +896,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -913,7 +904,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -921,7 +912,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -929,7 +920,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -937,7 +928,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -945,7 +936,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -953,7 +944,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -961,7 +952,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -969,7 +960,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } @@ -977,7 +968,7 @@ public static void Add(this IRegression r, IEnumerable val) public static void Add(this IRegression r, IEnumerable val) { if (val is null) { r.Add(NULL); return; } - var col = val as ICollection ?? val.ToArray(); + var col = val as IReadOnlyCollection ?? [.. val]; r.Add((uint)col.Count); foreach (var v in col) r.Add(v); } diff --git a/CsCheck/Gen.cs b/CsCheck/Gen.cs index 6d372a9..ac8f22e 100644 --- a/CsCheck/Gen.cs +++ b/CsCheck/Gen.cs @@ -1,4 +1,4 @@ -// Copyright 2026 Anthony Lloyd +// Copyright 2026 Anthony Lloyd // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -91,17 +91,6 @@ public abstract class Gen : IGen public GenOperation Operation(Action actual, Action model) => GenOperation.Create(this, actual, model); public GenMetamorphic Metamorphic(Func name, Action action1, Action action2) => GenMetamorphic.Create(this, name, action1, action2); public GenMetamorphic Metamorphic(Action action1, Action action2) => GenMetamorphic.Create(this, Check.Print, action1, action2); - - /// Generator for an array of - public GenArray Array => new(this); - /// Generator for a two dimensional array of - public GenArray2D Array2D => new(this); - /// Generator for a List of - public GenList List => new(this); - /// Generator for a HashSet of - public GenHashSet HashSet => new(this); - /// Generator for a unique array of - public GenArrayUnique ArrayUnique => new(this); } public delegate T GenMap(T v, ref Size size); @@ -109,6 +98,17 @@ public abstract class Gen : IGen /// Provides a set of static methods for composing generators. public static class Gen { + /// Generator for an array of + public static GenArray Array(this Gen gen) => new(gen); + /// Generator for a two dimensional array of + public static GenArray2D Array2D(this Gen gen) => new(gen); + /// Generator for a List of + public static GenList List(this Gen gen) => new(gen); + /// Generator for a HashSet of + public static GenHashSet HashSet(this Gen gen) => new(gen); + /// Generator for a unique array of + public static GenArrayUnique ArrayUnique(this Gen gen) => new(gen); + sealed class GenConst(T value) : Gen { public override T Generate(PCG pcg, Size? min, out Size size) @@ -2683,7 +2683,7 @@ public override char Generate(PCG pcg, Size? min, out Size size) public sealed class GenString : Gen { - static readonly Gen d = Gen.Char.Array.Select(i => new string(i)); + static readonly Gen d = Gen.Char.Array().Select(i => new string(i)); public override string Generate(PCG pcg, Size? min, out Size size) => d.Generate(pcg, min, out size); /// Generate string with length in the range to both inclusive. @@ -2692,7 +2692,7 @@ public override string Generate(PCG pcg, Size? min, out Size size) get { if (finish < start) ThrowHelper.ThrowFinishLessThanStart(start, finish); - return Gen.Char.Array[start, finish].Select(i => new string(i)); + return Gen.Char.Array()[start, finish].Select(i => new string(i)); } } @@ -2701,16 +2701,16 @@ public override string Generate(PCG pcg, Size? min, out Size size) get { if (finish < start) ThrowHelper.ThrowFinishLessThanStart(start, finish); - return gen.Array[start, finish].Select(i => new string(i)); + return gen.Array()[start, finish].Select(i => new string(i)); } } public Gen this[Gen gen] => - gen.Array.Select(i => new string(i)); + gen.Array().Select(i => new string(i)); /// Generate string from chars in the string. public Gen this[string chars] => - Gen.Char[chars].Array.Select(i => new string(i)); - public readonly Gen AlphaNumeric = Gen.Char.AlphaNumeric.Array.Select(i => new string(i)); + Gen.Char[chars].Array().Select(i => new string(i)); + public readonly Gen AlphaNumeric = Gen.Char.AlphaNumeric.Array().Select(i => new string(i)); } public sealed class GenSeed : Gen diff --git a/README.md b/README.md index bf437f5..3bfa310 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ The following tests are in ~~xUnit~~ TUnit but could equally be used in any test More to see in the [Tests](https://github.com/AnthonyLloyd/CsCheck/tree/master/Tests). There are also 1,000+ F# tests using CsCheck in [MKL.NET](https://github.com/MKL-NET/MKL.NET/tree/master/Tests). -No Reflection was used in the making of this product. CsCheck is close to being AOT compatible but 'generic recursion is AOT kryptonite'. +No Reflection was used in the making of this product. CsCheck is AOT compatible. ## Generator Creation Example @@ -67,7 +67,7 @@ static readonly Gen genJsonNode = Gen.Recursive((depth, genJ { if (depth == 5) return genJsonValue; var genJsonObject = Gen.Dictionary(genString, genJsonNode.Null())[0, 5].Select(d => new JsonObject(d)); - var genJsonArray = genJsonNode.Null().Array[0, 5].Select(i => new JsonArray(i)); + var genJsonArray = genJsonNode.Null().Array()[0, 5].Select(i => new JsonArray(i)); return Gen.OneOf(genJsonObject, genJsonArray, genJsonValue); }); ``` @@ -110,7 +110,7 @@ public void Int_Distribution() int buckets = 70; int frequency = 10; int[] expected = Enumerable.Repeat(frequency, buckets).ToArray(); - Gen.Int[0, buckets - 1].Array[frequency * buckets] + Gen.Int[0, buckets - 1].Array()[frequency * buckets] .Select(sample => Tally(buckets, sample)) .Sample(actual => Check.ChiSquared(expected, actual)); } @@ -150,7 +150,7 @@ public void DateTime() [Test] public void No2_LargeUnionList() { - Gen.Int.Array.Array + Gen.Int.Array().Array() .Sample(aa => { var hs = new HashSet(); @@ -173,7 +173,7 @@ public void RecursiveDepth() { int maxDepth = 4; Gen.Recursive((i, my) => - Gen.Select(Gen.Int, my.Array[0, i < maxDepth ? 6 : 0], (i, a) => new MyObj(i, a)) + Gen.Select(Gen.Int, my.Array()[0, i < maxDepth ? 6 : 0], (i, a) => new MyObj(i, a)) ) .Sample(i => { @@ -194,8 +194,8 @@ public void AllocatorMany_Classify() { Gen.Select(Gen.Int[3, 30], Gen.Int[3, 15]).SelectMany((rows, cols) => Gen.Select( - Gen.Int[0, 5].Array[cols].Where(a => a.Sum() > 0).Array[rows], - Gen.Int[900, 1000].Array[rows], + Gen.Int[0, 5].Array()[cols].Where(a => a.Sum() > 0).Array()[rows], + Gen.Int[900, 1000].Array()[rows], Gen.Int.Uniform)) .Sample((solution, rowPrice, @@ -234,7 +234,7 @@ SampleModelBased generates an initial actual and model and then applies a random public void SetSlim_ModelBased() { - Gen.Int.Array.Select(a => (new SetSlim(a), new HashSet(a))) + Gen.Int.Array().Select(a => (new SetSlim(a), new HashSet(a))) .SampleModelBased( Gen.Int.Operation, HashSet>( (ss, i) => ss.Add(i), @@ -358,7 +358,7 @@ public void Portfolio_Small_Mixed_Example() && p.Positions.Any(p => p.Instrument is Equity) , "0N0XIzNsQ0O2"); var currencies = portfolio.Positions.Select(p => p.Instrument.Currency).Distinct().ToArray(); - var fxRates = ModelGen.Price.Array[currencies.Length].Single(a => + var fxRates = ModelGen.Price.Array()[currencies.Length].Single(a => a.All(p => pp is > 0.75 and < 1.5) , "ftXKwKhS6ec4"); double fxRate(Currency c) => fxRates[Array.IndexOf(currencies, c)]; @@ -382,7 +382,7 @@ It's just what you need to iteratively improve performance while making sure it [Test] public void Faster_Linq_Random() { - Gen.Byte.Array[100, 1000] + Gen.Byte.Array()[100, 1000] .Faster( data => data.Aggregate(0.0, (t, b) => t + b), data => data.Select(i => (double)i).Sum(), @@ -412,7 +412,7 @@ Standard Output Messages: public void Faster_Matrix_Multiply_Range() { var genDim = Gen.Int[5, 30]; - var genArray = Gen.Double.Unit.Array2D; + var genArray = Gen.Double.Unit.Array2D(); Gen.SelectMany(genDim, genDim, genDim, (i, j, k) => Gen.Select(genArray[i, j], genArray[j, k])) .Faster( MulIKJ, @@ -427,7 +427,7 @@ public void Faster_Matrix_Multiply_Range() [Test] public void MapSlim_Performance_Increment() { - Gen.Byte.Array + Gen.Byte.Array() .Select(a => (a, new MapSlim(), new Dictionary())) .Faster( (items, mapslim, _) => diff --git a/Tests/AllocatorMany_Tests.cs b/Tests/AllocatorMany_Tests.cs index 64b559e..2dd2764 100644 --- a/Tests/AllocatorMany_Tests.cs +++ b/Tests/AllocatorMany_Tests.cs @@ -1,4 +1,4 @@ -namespace Tests; +namespace Tests; using System; using System.Linq; @@ -10,8 +10,8 @@ public class AllocatorMany_Tests public void RoundingSolutionTest() { Gen.Select( - Gen.Int[1, 1000].Array[2, 200], - Gen.Int[1, 1000].Select(i => (double)i).Array[2, 20]) + Gen.Int[1, 1000].Array()[2, 200], + Gen.Int[1, 1000].Select(i => (double)i).Array()[2, 20]) .Sample((rowTotal, colWeight) => { var colTotal = Allocator.Allocate(rowTotal.Sum(), colWeight); @@ -25,8 +25,8 @@ public void GroupUngroup() { Gen.Select(Gen.Int[2, 10], Gen.Int[2, 10]).SelectMany((I, J) => Gen.Select( - Gen.Int[0, 5].Array[J].Where(a => a.Sum() > 0).Array[I], - Gen.Int[0, 10].Array[I])) + Gen.Int[0, 5].Array()[J].Where(a => a.Sum() > 0).Array()[I], + Gen.Int[0, 10].Array()[I])) .Sample((solution, rowPrice) => { @@ -81,8 +81,8 @@ public async Task AllocatorMany_Classify() { Gen.Select(Gen.Int[3, 30], Gen.Int[3, 15]).SelectMany((rows, cols) => Gen.Select( - Gen.Int[0, 5].Array[cols].Where(a => a.Sum() > 0).Array[rows], - Gen.Int[900, 1000].Array[rows], + Gen.Int[0, 5].Array()[cols].Where(a => a.Sum() > 0).Array()[rows], + Gen.Int[900, 1000].Array()[rows], Gen.Int.Uniform)) .Sample((solution, rowPrice, @@ -222,9 +222,9 @@ public async Task Example10() { Gen.Int[2, 20].SelectMany(rows => Gen.Select( - Gen.Int[1, 300].Array[rows], - Gen.Int[1, 100].Array[rows], - Gen.Int[1, 1000].Select(i => (double)i).Array[2, 10], + Gen.Int[1, 300].Array()[rows], + Gen.Int[1, 100].Array()[rows], + Gen.Int[1, 1000].Select(i => (double)i).Array()[2, 10], Gen.Int.Uniform)) .Sample((rowPrice, rowTotal, weight, seed) => { @@ -274,9 +274,9 @@ public async Task AllocateTest() { Gen.Int[2, 20].SelectMany(rows => Gen.Select( - Gen.Int[1, 300].Array[rows], - Gen.Int[1, 100].Array[rows], - Gen.Int[1, 1000].Select(i => (double)i).Array[2, 10], + Gen.Int[1, 300].Array()[rows], + Gen.Int[1, 100].Array()[rows], + Gen.Int[1, 1000].Select(i => (double)i).Array()[2, 10], Gen.Int.Uniform)) .Sample((rowPrice, rowTotal, weight, seed) => { diff --git a/Tests/Allocator_Tests.cs b/Tests/Allocator_Tests.cs index e9a61c0..d993ca9 100644 --- a/Tests/Allocator_Tests.cs +++ b/Tests/Allocator_Tests.cs @@ -1,4 +1,4 @@ -namespace Tests; +namespace Tests; using System; using System.Collections.Generic; @@ -11,16 +11,16 @@ public class Allocator_Tests { readonly static Gen<(long Quantity, double[] Weights)> genAllSigns = - Gen.Select(Gen.Long[-10_000, 10_000], Gen.Double[-10_000, 10_000].Array[2, 50].Where(ws => ws.Sum() > 1e-9)); + Gen.Select(Gen.Long[-10_000, 10_000], Gen.Double[-10_000, 10_000].Array()[2, 50].Where(ws => ws.Sum() > 1e-9)); readonly static Gen<(long Quantity, long[] Weights)> genAllSignsLong = - Gen.Select(Gen.Long[-10_000, 10_000], Gen.Long[-100_000, 100_000].Array[2, 50].Where(ws => ws.Sum() != 0)); + Gen.Select(Gen.Long[-10_000, 10_000], Gen.Long[-100_000, 100_000].Array()[2, 50].Where(ws => ws.Sum() != 0)); readonly static Gen<(long Quantity, double[] Weights)> genPositive = - Gen.Select(Gen.Long[1, 10_000], Gen.Double[0, 10_000].Array[2, 50].Where(ws => Math.Abs(ws.Sum()) > 1e-9)); + Gen.Select(Gen.Long[1, 10_000], Gen.Double[0, 10_000].Array()[2, 50].Where(ws => Math.Abs(ws.Sum()) > 1e-9)); readonly static Gen<(long Quantity, long[] Weights)> genPositiveLong = - Gen.Select(Gen.Long[1, 10_000], Gen.Long[0, 100_000].Array[2, 50].Where(ws => ws.Sum() != 0)); + Gen.Select(Gen.Long[1, 10_000], Gen.Long[0, 100_000].Array()[2, 50].Where(ws => ws.Sum() != 0)); static bool TotalCorrectly(long quantity, W[] weights, Func allocator) => allocator(quantity, weights).Sum() == quantity; @@ -276,7 +276,7 @@ public void Allocate_HasSmallestAllocationError() var genInt = Gen.Int[0, i - 1]; return Gen.Select(genInt, genInt) .Where((i, j) => i != j) - .HashSet[1, i]; + .HashSet()[1, i]; } genAllSigns.Where((_, ws) => ws.Length >= 2) .SelectMany((quantity, weights) => GenChanges(weights.Length).Select(i => (quantity, weights, i))) @@ -318,7 +318,7 @@ public void Allocate_Long_HasSmallestAllocationError() var genInt = Gen.Int[0, i - 1]; return Gen.Select(genInt, genInt) .Where((i, j) => i != j) - .HashSet[1, i]; + .HashSet()[1, i]; } genAllSignsLong.Where((_, ws) => ws.Length >= 2) .SelectMany((quantity, weights) => GenChanges(weights.Length).Select(i => (quantity, weights, i))) diff --git a/Tests/CacheTests.cs b/Tests/CacheTests.cs index c3b202d..6c915b5 100644 --- a/Tests/CacheTests.cs +++ b/Tests/CacheTests.cs @@ -1,4 +1,4 @@ -namespace Tests; +namespace Tests; using CsCheck; using System.Collections.Concurrent; @@ -11,7 +11,7 @@ public void Cache_GetOrAdd_ModelBased() static void CacheTryAdd(Cache cache, int key, byte value) => cache.GetOrAdd(key, _ => Task.FromResult(value)).AsTask().GetAwaiter().GetResult(); - Gen.Select(Gen.Int, Gen.Byte).Array + Gen.Select(Gen.Int, Gen.Byte).Array() .Select(kvs => { var cache = new Cache(); @@ -33,7 +33,7 @@ static void CacheTryAdd(Cache cache, int key, byte value) [Test] public async Task Cache_GetOrAdd_StampedeFree() { - await Gen.Int.HashSet[1, 10].SampleAsync(async ks => + await Gen.Int.HashSet()[1, 10].SampleAsync(async ks => { var alreadyRun = 0; var ks0 = ks.First(); @@ -147,7 +147,7 @@ async Task Factory(int _) [Test] public async Task Cache_Update_StampedeFree() { - await Gen.Int.HashSet[1, 10].SampleAsync(async ks => + await Gen.Int.HashSet()[1, 10].SampleAsync(async ks => { var alreadyRun = 0; var callCount = 0; diff --git a/Tests/CheckTests.cs b/Tests/CheckTests.cs index bb910c1..d304bfb 100644 --- a/Tests/CheckTests.cs +++ b/Tests/CheckTests.cs @@ -1,4 +1,4 @@ -namespace Tests; +namespace Tests; using System; using System.Collections.Concurrent; @@ -132,7 +132,7 @@ public void Faster_Matrix_Multiply_Fixed() public void Faster_Matrix_Multiply_Range() { var genDim = Gen.Int[5, 30]; - var genArray = Gen.Double.Unit.Array2D; + var genArray = Gen.Double.Unit.Array2D(); Gen.SelectMany(genDim, genDim, genDim, (i, j, k) => Gen.Select(genArray[i, j], genArray[j, k])) .Faster( MulIKJ, @@ -143,7 +143,7 @@ public void Faster_Matrix_Multiply_Range() [Test] public void Faster_Linq_Random() { - Gen.Byte.Array[100, 1000] + Gen.Byte.Array()[100, 1000] .Faster( data => { @@ -235,7 +235,7 @@ await Assert.That(Check.ModelEqual( [Test] public void SampleModelBased_ConcurrentBag() { - Gen.Int[0, 5].List.Select(l => (new ConcurrentBag(l), l)) + Gen.Int[0, 5].List().Select(l => (new ConcurrentBag(l), l)) .SampleModelBased( Gen.Int.Operation, List>((bag, i) => bag.Add(i), (list, i) => list.Add(i)), Gen.Operation, List>(bag => bag.TryTake(out _), list => { if (list.Count > 0) list.RemoveAt(0); }), @@ -317,7 +317,7 @@ public void Equality_String() [Test] public void Enqueue_Faster_Than_Median() { - Gen.Double.OneTwo.Array[10].Select(Gen.Double.OneTwo, (a, s) => + Gen.Double.OneTwo.Array()[10].Select(Gen.Double.OneTwo, (a, s) => { var median = new MedianEstimator(); foreach (var d in a) median.Add(d); diff --git a/Tests/FloatingPointTests.cs b/Tests/FloatingPointTests.cs index 1f0c155..8c3ab6e 100644 --- a/Tests/FloatingPointTests.cs +++ b/Tests/FloatingPointTests.cs @@ -1,4 +1,4 @@ -namespace Tests; +namespace Tests; using CsCheck; using System; @@ -13,7 +13,7 @@ public void DoubleSupportsRoundtripPrecisionTo15() var genDigit = Gen.Char['0', '9']; var genPriceIO = Gen.Int[1, 15] .SelectMany(p => - genDigit.Array[p + 1].Select(Gen.Int[0, p], (cs, d) => + genDigit.Array()[p + 1].Select(Gen.Int[0, p], (cs, d) => { cs[d] = '.'; return new string(cs); @@ -30,7 +30,7 @@ private static void DoubleSumPrecision(int significantFigures, int maxLength) const double scaling = 0.01; var lower = (long)Math.Pow(10, significantFigures - 1); var upper = (long)Math.Pow(10, significantFigures) - 1; - Gen.Long[lower, upper].Array[100, maxLength] + Gen.Long[lower, upper].Array()[100, maxLength] .Sample(longs => { var longSum = longs.Sum(); @@ -44,7 +44,7 @@ private static void DoubleNSumPrecision(int significantFigures, int maxLength) const double scaling = 0.01; var lower = (long)Math.Pow(10, significantFigures - 1); var upper = (long)Math.Pow(10, significantFigures) - 1; - Gen.Long[lower, upper].Array[100, maxLength] + Gen.Long[lower, upper].Array()[100, maxLength] .Sample(longs => { var longSum = longs.Sum(); diff --git a/Tests/GenTests.cs b/Tests/GenTests.cs index 5303019..5b80e21 100644 --- a/Tests/GenTests.cs +++ b/Tests/GenTests.cs @@ -1,4 +1,4 @@ -namespace Tests; +namespace Tests; using System; using System.Runtime.InteropServices; @@ -18,7 +18,7 @@ public void Bool_Distribution() { const int frequency = 10; var expected = new int[] { frequency, frequency}; - Gen.Bool.Select(i => i ? 1 : 0).Array[2 * frequency] + Gen.Bool.Select(i => i ? 1 : 0).Array()[2 * frequency] .Select(sample => Tally(2, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2); } @@ -48,7 +48,7 @@ public void SByte_Distribution() var expected = new int[buckets]; Array.Fill(expected, frequency); Gen.SByte[0, buckets - 1] - .Select(i => (int)i).Array[frequency * buckets] + .Select(i => (int)i).Array()[frequency * buckets] .Select(sample => Tally(buckets, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2); } @@ -78,7 +78,7 @@ public void Byte_Distribution() var expected = new int[buckets]; Array.Fill(expected, frequency); Gen.Byte[0, buckets - 1] - .Select(i => (int)i).Array[frequency * buckets] + .Select(i => (int)i).Array()[frequency * buckets] .Select(sample => Tally(buckets, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2); } @@ -114,7 +114,7 @@ public void Short_Distribution() var expected = new int[buckets]; Array.Fill(expected, frequency); Gen.Short[0, buckets - 1] - .Select(i => (int)i).Array[frequency * buckets] + .Select(i => (int)i).Array()[frequency * buckets] .Select(sample => Tally(buckets, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2); } @@ -144,7 +144,7 @@ public void UShort_Distribution() var expected = new int[buckets]; Array.Fill(expected, frequency); Gen.UShort[0, buckets - 1] - .Select(i => (int)i).Array[frequency * buckets] + .Select(i => (int)i).Array()[frequency * buckets] .Select(sample => Tally(buckets, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2); } @@ -230,7 +230,7 @@ public void Int_Distribution() const int frequency = 10; var expected = new int[buckets]; Array.Fill(expected, frequency); - Gen.Int[0, buckets - 1].Array[frequency * buckets] + Gen.Int[0, buckets - 1].Array()[frequency * buckets] .Select(sample => Tally(buckets, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2); } @@ -276,7 +276,7 @@ public void UInt_Distribution() var expected = new int[buckets]; Array.Fill(expected, frequency); Gen.UInt[0, buckets - 1] - .Select(i => (int)i).Array[frequency * buckets] + .Select(i => (int)i).Array()[frequency * buckets] .Select(sample => Tally(buckets, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2); } @@ -328,7 +328,7 @@ public void Long_Distribution() var expected = new int[buckets]; Array.Fill(expected, frequency); Gen.Long[0, buckets - 1] - .Select(i => (int)i).Array[frequency * buckets] + .Select(i => (int)i).Array()[frequency * buckets] .Select(sample => Tally(buckets, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2); } @@ -358,7 +358,7 @@ public void ULong_Distribution() var expected = new int[buckets]; Array.Fill(expected, frequency); Gen.ULong[0, buckets - 1] - .Select(i => (int)i).Array[frequency * buckets] + .Select(i => (int)i).Array()[frequency * buckets] .Select(sample => Tally(buckets, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2); } @@ -407,7 +407,7 @@ public void Single_Distribution() Array.Fill(expected, frequency); Gen.Single.Unit .Select(i => (int)(i * buckets)) - .Array[frequency * buckets] + .Array()[frequency * buckets] .Select(sample => Tally(buckets, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2); } @@ -450,7 +450,7 @@ public void Double_Distribution() Array.Fill(expected, frequency); Gen.Double.Unit .Select(i => (int)(i * buckets)) - .Array[frequency * buckets] + .Array()[frequency * buckets] .Select(sample => Tally(buckets, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2); } @@ -501,7 +501,7 @@ public void Decimal_Distribution() var expected = Enumerable.Repeat(frequency, buckets).ToArray(); Gen.Decimal.Unit .Select(i => (int)(i * buckets)) - .Array[frequency * buckets] + .Array()[frequency * buckets] .Select(sample => Tally(buckets, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2); } @@ -622,7 +622,7 @@ public void Char_Distribution() var expected = new int[buckets]; Array.Fill(expected, frequency); Gen.Char[(char)0, (char)(buckets - 1)] - .Select(i => (int)i).Array[frequency * buckets] + .Select(i => (int)i).Array()[frequency * buckets] .Select(sample => Tally(buckets, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2); } @@ -638,7 +638,7 @@ public void Char_Array() public void List() { Gen.UShort[1, 1000] - .List[10, 100] + .List()[10, 100] .Sample(l => l.Count >= 10 && l.Count <= 100 && l.All(i => i is >= 1 and <= 1000)); } @@ -647,7 +647,7 @@ public void List() public void HashSet() { Gen.ULong[1, 1000] - .HashSet[10, 100] + .HashSet()[10, 100] .Sample(i => i.Count >= 10 && i.Count <= 100 && i.All(j => j is >= 1 and <= 1000)); } @@ -687,7 +687,7 @@ public void Frequency() (from f in Gen.Select(Gen.Int[1, 5], Gen.Int[1, 5], Gen.Int[1, 5]) let expected = new[] { f.Item1 * frequency, f.Item2 * frequency, f.Item3 * frequency } from actual in Gen.FrequencyConst((f.Item1, 0), (f.Item2, 1), (f.Item3, 2)) - .Array[frequency * (f.Item1 + f.Item2 + f.Item3)] + .Array()[frequency * (f.Item1 + f.Item2 + f.Item3)] .Select(sample => Tally(3, sample)) select (expected, actual)) .Sample(t => Check.ChiSquared(t.expected, t.actual, 10), iter: 1, time: -2); @@ -696,7 +696,7 @@ from actual in Gen.FrequencyConst((f.Item1, 0), (f.Item2, 1), (f.Item3, 2)) [Test] public void Shuffle() { - Gen.Int.Array.SelectMany(a1 => Gen.Shuffle(a1).Select(a2 => (a1, a2))) + Gen.Int.Array().SelectMany(a1 => Gen.Shuffle(a1).Select(a2 => (a1, a2))) .Sample((a1, a2) => { Array.Sort(a1); @@ -712,7 +712,7 @@ public void RecursiveDepth() { const int maxDepth = 4; Gen.Recursive((i, my) => - Gen.Select(Gen.Int, my.Array[0, i < maxDepth ? 6 : 0], (i, a) => new MyObj(i, a)) + Gen.Select(Gen.Int, my.Array()[0, i < maxDepth ? 6 : 0], (i, a) => new MyObj(i, a)) ) .Sample(i => { diff --git a/Tests/HashTests.cs b/Tests/HashTests.cs index 40d48df..ae412d0 100644 --- a/Tests/HashTests.cs +++ b/Tests/HashTests.cs @@ -1,4 +1,4 @@ -namespace Tests; +namespace Tests; using System; using System.IO; @@ -145,7 +145,7 @@ public void Hash_Example() [Test] public void HashStream_Parts() { - Gen.Byte.Array[0, 31].Array[3, 10] + Gen.Byte.Array()[0, 31].Array()[3, 10] .Sample(bs => { var actual = new HashStream(); diff --git a/Tests/IMToolsTests.cs b/Tests/IMToolsTests.cs index 27cc506..9238cf3 100644 --- a/Tests/IMToolsTests.cs +++ b/Tests/IMToolsTests.cs @@ -1,4 +1,4 @@ -#if !NET35 && !PCL +#if !NET35 && !PCL #define SUPPORTS_SPIN_WAIT #endif @@ -54,7 +54,7 @@ public async Task AddOrUpdate_random_items_and_randomly_checking() public async Task AddOrUpdate_random_items_and_randomly_checking_CsCheck() { const int upperBound = 11966; - Gen.Int[0, upperBound].Array[1, 12].Sample(ints => + Gen.Int[0, upperBound].Array()[1, 12].Sample(ints => { var m = ImHashMap234.Empty; foreach (var n in ints) @@ -72,8 +72,8 @@ public async Task AddOrUpdate_random_items_and_randomly_checking_CsCheck() } static Gen> GenMap(int upperBound) => - Gen.Int[0, upperBound].ArrayUnique.SelectMany(ks => - Gen.Int.Array[ks.Length].Select(vs => + Gen.Int[0, upperBound].ArrayUnique().SelectMany(ks => + Gen.Int.Array()[ks.Length].Select(vs => { var m = ImHashMap234.Empty; for (int i = 0; i < ks.Length; i++) diff --git a/Tests/LoggingTests.cs b/Tests/LoggingTests.cs index 2175dcb..3492abc 100644 --- a/Tests/LoggingTests.cs +++ b/Tests/LoggingTests.cs @@ -1,4 +1,4 @@ -namespace Tests; +namespace Tests; using System.Text; using System.Text.Json; using CsCheck; @@ -17,8 +17,8 @@ static int[] Tally(int n, int[] ia) [Arguments(0)] public async Task Bool_Distribution_WithTycheLogs(int generatedIntUponTrue) { - using var memoryStream = new MemoryStream(); - using var writer = new StreamWriter(memoryStream); + await using var memoryStream = new MemoryStream(); + await using var writer = new StreamWriter(memoryStream); writer.AutoFlush = true; var logger = Logging.CreateTycheLogger(writer: writer); @@ -29,7 +29,7 @@ public async Task Bool_Distribution_WithTycheLogs(int generatedIntUponTrue) //Try catch to suppress failing original test logic try { - Gen.Bool.Select(i => i ? generatedIntUponTrue : 0).Array[2 * frequency] + Gen.Bool.Select(i => i ? generatedIntUponTrue : 0).Array()[2 * frequency] .Select(sample => Tally(2, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2, logger: logger); } @@ -74,7 +74,7 @@ public void Bool_Distribution_WithTycheLogs_ToFile(int generatedIntUponTrue) //Try catch to suppress failing original test logic try { - Gen.Bool.Select(i => i ? generatedIntUponTrue : 0).Array[2 * frequency] + Gen.Bool.Select(i => i ? generatedIntUponTrue : 0).Array()[2 * frequency] .Select(sample => Tally(2, sample)) .Sample(actual => Check.ChiSquared(expected, actual, 10), iter: 1, time: -2, logger: logger); } diff --git a/Tests/MathXTests.cs b/Tests/MathXTests.cs index 485e8d9..af6ad02 100644 --- a/Tests/MathXTests.cs +++ b/Tests/MathXTests.cs @@ -1,4 +1,4 @@ -namespace Tests; +namespace Tests; using System; using System.Linq; @@ -105,7 +105,7 @@ public async Task FSum_Examples() [Test] public void NSum_Shuffle_Check() { - genDouble.Array[3, 100] + genDouble.Array()[3, 100] .SelectMany(a => Gen.Shuffle(a).Select(s => (a, s))) .Sample((original, shuffled) => { @@ -118,7 +118,7 @@ public void NSum_Shuffle_Check() [Test] public void FSum_Shuffle_Check() { - genDouble.Array[3, 100] + genDouble.Array()[3, 100] .SelectMany(a => Gen.Shuffle(a).Select(s => (a, s))) .Sample((original, shuffled) => { @@ -131,7 +131,7 @@ public void FSum_Shuffle_Check() [Test] public void KSum_Shuffle_Error_Distribution() { - genDouble.Array[3, 100] + genDouble.Array()[3, 100] .SelectMany(a => Gen.Shuffle(a).Select(s => (a, s))) .Sample((original, shuffled) => { @@ -144,7 +144,7 @@ public void KSum_Shuffle_Error_Distribution() [Test] public void NSum_Shuffle_Error_Distribution() { - genDouble.Array[3, 100] + genDouble.Array()[3, 100] .SelectMany(a => Gen.Shuffle(a).Select(s => (a, s))) .Sample((original, shuffled) => { @@ -157,7 +157,7 @@ public void NSum_Shuffle_Error_Distribution() [Test] public void FSum_Shuffle_Error_Distribution() { - genDouble.Array[3, 100] + genDouble.Array()[3, 100] .SelectMany(a => Gen.Shuffle(a).Select(s => (a, s))) .Sample((original, shuffled) => { @@ -170,7 +170,7 @@ public void FSum_Shuffle_Error_Distribution() [Test] public void FSum_Shuffle_Error_Distribution_Compress() { - genDouble.Array[3, 100] + genDouble.Array()[3, 100] .SelectMany(a => Gen.Shuffle(a).Select(s => (a, s))) .Sample((original, shuffled) => { @@ -196,7 +196,7 @@ public async Task FSum_Compress_Needed_Example() [Test] public void NSum_FSum_Error_Distribution() { - genDouble.Array[3, 100] + genDouble.Array()[3, 100] .Sample(values => { var fsumSum = MathX.FSum(values); @@ -223,7 +223,7 @@ public async Task SSum_Examples() [Test] public void SSum_Shuffle_Check() { - genDouble.Array[2, 10] + genDouble.Array()[2, 10] .SelectMany(a => Gen.Shuffle(a).Select(s => (a, s))) .Sample((original, shuffled) => { @@ -236,7 +236,7 @@ public void SSum_Shuffle_Check() [Test] public void SSum_Negative_Check() { - genDouble.Array[2, 10] + genDouble.Array()[2, 10] .Sample(original => { var originalSum = MathX.SSum(original); @@ -250,7 +250,7 @@ public void SSum_Negative_Check() [Test] public void SSum_Shuffle_Negative_Check() { - genDouble.Array[2, 10] + genDouble.Array()[2, 10] .SelectMany(a => Gen.Shuffle(a).Select(s => (a, s))) .Sample((original, shuffled) => { @@ -265,7 +265,7 @@ public void SSum_Shuffle_Negative_Check() [Test] public void FSum_Vs_SSum_Perf() { - genDouble.Array[2, 100] + genDouble.Array()[2, 100] .Faster( values => values.FSum(), values => values.SSum(), diff --git a/Tests/ModelTests.cs b/Tests/ModelTests.cs index 54fd123..48e0c65 100644 --- a/Tests/ModelTests.cs +++ b/Tests/ModelTests.cs @@ -1,4 +1,4 @@ -namespace Tests; +namespace Tests; using System; using System.Collections.Generic; @@ -53,14 +53,14 @@ public static class ModelGen public readonly static Gen Coupon = Gen.Int[0, 100].Select(i => 0.125 * i); public readonly static Gen Price = Gen.Int[0001, 9999].Select(i => 0.01 * i); public readonly static Gen Date = Gen.Date[new DateTime(2000, 1, 1), new DateTime(2040, 1, 1)]; - public readonly static Gen Equity = Gen.Select(Name, Country, Currency, Gen.Enum().HashSet[1, 3], + public readonly static Gen Equity = Gen.Select(Name, Country, Currency, Gen.Enum().HashSet()[1, 3], (n, co, cu, e) => new Equity(n, co, cu, e)); public readonly static Gen Bond = Gen.Select(Name, Country, Currency, Gen.SortedDictionary(Date, Coupon), (n, co, cu, c) => new Bond(n, co, cu, c)); public readonly static Gen Instrument = Gen.OneOf(Equity, Bond); public readonly static Gen Trade = Gen.Select(Date, Quantity, Price, (dt, q, p) => new Trade(dt, q, q * p)); - public readonly static Gen Position = Gen.Select(Instrument, Trade.List, Price, (i, t, p) => new Position(i, t, p)); - public readonly static Gen Portfolio = Gen.Select(Name, Currency, Position.Array, (n, c, p) => new Portfolio(n, c, p)); + public readonly static Gen Position = Gen.Select(Instrument, Trade.List(), Price, (i, t, p) => new Position(i, t, p)); + public readonly static Gen Portfolio = Gen.Select(Name, Currency, Position.Array(), (n, c, p) => new Portfolio(n, c, p)); } [Test] @@ -72,7 +72,7 @@ public void Portfolio_Small_Mixed_Example() && p.Positions.Any(p => p.Instrument is Equity) , "e2v0jI554Uya"); var currencies = portfolio.Positions.Select(p => p.Instrument.Currency).Distinct().ToArray(); - var fxRates = ModelGen.Price.Array[currencies.Length].Single(a => + var fxRates = ModelGen.Price.Array()[currencies.Length].Single(a => a.All(p => p is > 0.75 and < 1.5) , "ftXKwKhS6ec4"); double fxRate(Currency c) => fxRates[Array.IndexOf(currencies, c)]; diff --git a/Tests/PCGTests.cs b/Tests/PCGTests.cs index db4e6d5..33faeac 100644 --- a/Tests/PCGTests.cs +++ b/Tests/PCGTests.cs @@ -1,4 +1,4 @@ -namespace Tests; +namespace Tests; using System; using System.Diagnostics; @@ -117,7 +117,7 @@ public void PCG_Next() { genPCG .Select(i => i.Next()) - .Array[20] + .Array()[20] .Sample(t => { var expected = Enumerable.Repeat(10, 32).ToArray(); @@ -140,7 +140,7 @@ public void PCG_Next64() { genPCG .Select(i => i.Next64()) - .Array[20] + .Array()[20] .Sample(t => { var expected = Enumerable.Repeat(10, 64).ToArray(); diff --git a/Tests/ShrinkingChallengeTests.cs b/Tests/ShrinkingChallengeTests.cs index f205fdf..ab92d24 100644 --- a/Tests/ShrinkingChallengeTests.cs +++ b/Tests/ShrinkingChallengeTests.cs @@ -1,4 +1,4 @@ -namespace Tests; +namespace Tests; using System; using System.Linq; @@ -16,7 +16,7 @@ static short Sum(short[] l) for (int i = 0; i < l.Length; i++) s += l[i]; return s; } - var sGen = Gen.Short.Array[0, 10].Where(i => Sum(i) < 256); + var sGen = Gen.Short.Array()[0, 10].Where(i => Sum(i) < 256); Gen.Select(sGen, sGen, sGen, sGen, sGen) .Sample((a1, a2, a3, a4, a5) => { @@ -28,7 +28,7 @@ static short Sum(short[] l) [Test, Skip("fails")] public void No2_LargeUnionList() { - Gen.Int.Array.Array + Gen.Int.Array().Array() .Sample(aa => { var hs = new HashSet(); @@ -44,7 +44,7 @@ public void No2_LargeUnionList() [Test, Skip("fails")] public void No3_Reverse() { - Gen.Int.Array + Gen.Int.Array() .Sample(a => { var c = new int[a.Length]; @@ -91,7 +91,7 @@ public void No4_Calculator() [Test, Skip("fails")] public void No5_LengthList() { - Gen.Int.Array + Gen.Int.Array() .Sample(a => { foreach (var i in a) @@ -188,7 +188,7 @@ static List Sorted(List l) [Test, Skip("fails")] public void No8_Coupling() { - Gen.Int[0, 100].SelectMany(l => Gen.Int[0, l - 1].Array[l]) + Gen.Int[0, 100].SelectMany(l => Gen.Int[0, l - 1].Array()[l]) .Sample(a => { for (int i = 0; i < a.Length; i++) @@ -203,7 +203,7 @@ public void No8_Coupling() [Test, Skip("fails")] public void No9_Deletion() { - Gen.Int.List[1, 100].SelectMany(l => Gen.Int[0, l.Count - 1].Select(i => (l, i))) + Gen.Int.List()[1, 100].SelectMany(l => Gen.Int[0, l.Count - 1].Select(i => (l, i))) .Sample((list, i) => { var l = new List(list); @@ -216,14 +216,14 @@ public void No9_Deletion() [Test, Skip("fails")] public void No10_Distinct() { - Gen.Int.Array + Gen.Int.Array() .Sample(a => a.ToHashSet().Count < 3); } [Test, Skip("fails")] public void No11_NestedLists() { - Gen.Int.Array.Array + Gen.Int.Array().Array() .Sample(aa => { int l = 0; diff --git a/Tests/SlimCollectionsTests.cs b/Tests/SlimCollectionsTests.cs index 3565a90..6c35579 100644 --- a/Tests/SlimCollectionsTests.cs +++ b/Tests/SlimCollectionsTests.cs @@ -1,4 +1,4 @@ -namespace Tests; +namespace Tests; using System; using System.Linq; @@ -12,7 +12,7 @@ public class SlimCollectionsTests() [Test] public void ListSlim_ModelBased() { - Gen.Int.Array.Select(a => (new ListSlim(a), new List(a))) + Gen.Int.Array().Select(a => (new ListSlim(a), new List(a))) .SampleModelBased( Gen.Int.Operation, List>( (ls, i) => ls.Add(i), @@ -23,7 +23,7 @@ public void ListSlim_ModelBased() [Test] public void ListSlim_Parallel() { - Gen.Byte.Array.Select(a => new ListSlim(a)) + Gen.Byte.Array().Select(a => new ListSlim(a)) .SampleParallel( Gen.Byte.Operation>(i => $"Add {i}", (l, i) => { lock (l) l.Add(i); }), Gen.Int.NonNegative.Operation>(i => $"Get {i}", (l, i) => { if (i < l.Count) { var _ = l[i]; } }), @@ -35,7 +35,7 @@ public void ListSlim_Parallel() [Test] public void SetSlim_ModelBased() { - Gen.Int.Array.Select(a => (new SetSlim(a), new HashSet(a))) + Gen.Int.Array().Select(a => (new SetSlim(a), new HashSet(a))) .SampleModelBased( Gen.Int.Operation, HashSet>( (ss, i) => ss.Add(i), @@ -47,7 +47,7 @@ public void SetSlim_ModelBased() [Test] public void SetSlim_Parallel() { - Gen.Byte.Array.Select(a => new SetSlim(a)) + Gen.Byte.Array().Select(a => new SetSlim(a)) .SampleParallel( Gen.Byte.Operation>((l, i) => { lock (l) l.Add(i); }), Gen.Int.NonNegative.Operation>((l, i) => { if (i < l.Count) { var _ = l[i]; } }), @@ -59,7 +59,7 @@ public void SetSlim_Parallel() [Test] public void SetSlim_Performance_Add() { - Gen.Int.Array + Gen.Int.Array() .Faster( a => { @@ -77,7 +77,7 @@ public void SetSlim_Performance_Add() [Test, Skip("fails")] public void SetSlim_Performance_Contains() { - Gen.Int.Array.Select(a => (a, new SetSlim(a), new HashSet(a))) + Gen.Int.Array().Select(a => (a, new SetSlim(a), new HashSet(a))) .Faster( (items, setslim, _) => { @@ -129,7 +129,7 @@ public void MapSlim_Parallel() [Test] public void MapSlim_Performance_Add() { - Gen.Int.Select(Gen.Byte).Array + Gen.Int.Select(Gen.Byte).Array() .Faster( items => { @@ -164,7 +164,7 @@ public void MapSlim_Performance_IndexOf() [Test, Skip("fails")] public void MapSlim_Performance_Increment() { - Gen.Int[0, 255].Array + Gen.Int[0, 255].Array() .Select(a => (a, new MapSlim(), new Dictionary())) .Faster( (items, mapslim, _) => diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index fd18ad6..27db246 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -14,9 +14,10 @@ true false ../SigningKey.snk + true - + diff --git a/Tests/UtilsTests.cs b/Tests/UtilsTests.cs index 0ab68a8..ab02975 100644 --- a/Tests/UtilsTests.cs +++ b/Tests/UtilsTests.cs @@ -1,4 +1,4 @@ -namespace Tests; +namespace Tests; using System; using System.Collections.Generic; @@ -179,7 +179,7 @@ await Test([1, 2, 3, 2], [ [Test] public void Permutations_Should_Be_Unique() { - Gen.Int[0, 5].Array[0, 10] + Gen.Int[0, 5].Array()[0, 10] .Sample(a => { var a2 = new int[a.Length]; diff --git a/Why.md b/Why.md index f70088c..844796f 100644 --- a/Why.md +++ b/Why.md @@ -29,7 +29,7 @@ Instead of coming up with examples we need to create a generator `Gen` for th This may sound like a pain, but actually it's really simple with the composable fluent `Gen` classes in CsCheck, and can be done in one or two lines of code. The generators created for domain types can be composed and reused across a number of tests e.g. serialization and domain logic. -We start with a highly defaulted generator `Gen.Double.Array.List` say but may want to be more specific `Gen.Double[0.0, 100.0].Array[5].List[1, 10]` about the range of values. +We start with a highly defaulted generator `Gen.Double.Array().List` say but may want to be more specific `Gen.Double[0.0, 100.0].Array()[5].List()[1, 10]` about the range of values. Some testing libraries can create the generator for you automatically using reflection but this can lead to a number of bugs for the library author and a lack of control and insight for the library user. Fluent style composition similar to LINQ is a much more robust and extensible option.