Skip to content

Commit

Permalink
move remove Create
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLloyd committed Oct 13, 2023
1 parent b47d310 commit 4c4c2aa
Show file tree
Hide file tree
Showing 9 changed files with 751 additions and 787 deletions.
22 changes: 11 additions & 11 deletions CsCheck/Check.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1476,31 +1476,31 @@ public static void SampleMetamorphic<T>(this Gen<T> initial, GenMetamorphic<T> o
{
try
{
d.V1.Item2(d.V0.State1);
d.V1.Item3(d.V0.State2);
return equal(d.V0.State1, d.V0.State2);
d.Item2.Item2(d.Item1.State1);
d.Item2.Item3(d.Item1.State2);
return equal(d.Item1.State1, d.Item1.State2);
}
catch (Exception e)
{
d.V0.Exception = e;
d.Item1.Exception = e;
return false;
}
}, seed, iter, time, threads,
p =>
{
if (p.V0 == null) return "";
if (p.Item1 is null) return "";
var sb = new StringBuilder();
var initialState = initial.Generate(new PCG(p.V0.Stream, p.V0.Seed), null, out _);
var initialState = initial.Generate(new PCG(p.Item1.Stream, p.Item1.Seed), null, out _);
sb.Append("\nInitial State: ").Append(print(initialState));
sb.Append("\n Operations: ").Append(p.V1.Item1);
if (p.V0.Exception is null)
sb.Append("\n Operations: ").Append(p.Item2.Item1);
if (p.Item1.Exception is null)
{
sb.Append("\nFinal State 1: ").Append(print(p.V0.State1));
sb.Append("\nFinal State 2: ").Append(print(p.V0.State2));
sb.Append("\nFinal State 1: ").Append(print(p.Item1.State1));
sb.Append("\nFinal State 2: ").Append(print(p.Item1.State2));
}
else
{
sb.Append("\n Exception: ").Append(p.V0.Exception.ToString());
sb.Append("\n Exception: ").Append(p.Item1.Exception.ToString());
}
return sb.ToString();
});
Expand Down
1,378 changes: 671 additions & 707 deletions CsCheck/Gen.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Tests/CheckTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public void SampleConcurrent_ConcurrentDictionary()
Gen.Dictionary(Gen.Int[0, 100], Gen.Byte)[0, 10].Select(l => new ConcurrentDictionary<int, byte>(l))
.SampleConcurrent(
Gen.Int[0, 100].Select(Gen.Byte)
.Operation<ConcurrentDictionary<int, byte>>(t =>$"d[{t.V0}] = {t.V1}", (d, t) => d[t.V0] = t.V1),
.Operation<ConcurrentDictionary<int, byte>>(t =>$"d[{t.Item1}] = {t.Item2}", (d, t) => d[t.Item1] = t.Item2),

Gen.Int[0, 100]
.Operation<ConcurrentDictionary<int, byte>>(i => $"TryRemove({i})", (d, i) => d.TryRemove(i, out _))
Expand Down
88 changes: 44 additions & 44 deletions Tests/GenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public void Bool_Distribution()
public void SByte_Range()
{
(from t in Gen.Select(Gen.SByte, Gen.SByte)
let start = Math.Min(t.V0, t.V1)
let finish = Math.Max(t.V0, t.V1)
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.SByte[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
Expand All @@ -44,7 +44,7 @@ public void SByte_Distribution()
const int buckets = 70;
const int frequency = 10;
var expected = Enumerable.Repeat(frequency, buckets).ToArray();
Gen.SByte[0, (sbyte)(buckets - 1)]
Gen.SByte[0, buckets - 1]
.Convert<int>().Array[frequency * buckets]
.Select(sample => Tally(buckets, sample))
.Sample(actual => Check.ChiSquared(expected, actual), iter: 1, time: -2);
Expand All @@ -54,8 +54,8 @@ public void SByte_Distribution()
public void Byte_Range()
{
(from t in Gen.Byte.Select(Gen.Byte)
let start = Math.Min(t.V0, t.V1)
let finish = Math.Max(t.V0, t.V1)
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.Byte[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
Expand Down Expand Up @@ -83,8 +83,8 @@ public void Short_Zigzag_Roundtrip()
public void Short_Range()
{
(from t in Gen.Short.Select(Gen.Short)
let start = Math.Min(t.V0, t.V1)
let finish = Math.Max(t.V0, t.V1)
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.Short[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
Expand All @@ -106,8 +106,8 @@ public void Short_Distribution()
public void UShort_Range()
{
(from t in Gen.UShort.Select(Gen.UShort)
let start = Math.Min(t.V0, t.V1)
let finish = Math.Max(t.V0, t.V1)
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.UShort[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
Expand All @@ -129,8 +129,8 @@ public void UShort_Distribution()
public void Int_Range()
{
(from t in Gen.Int.Select(Gen.Int)
let start = Math.Min(t.V0, t.V1)
let finish = Math.Max(t.V0, t.V1)
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.Int[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
Expand Down Expand Up @@ -208,10 +208,10 @@ public void Int_Distribution()
public void Int_Skew()
{
(from t in Gen.Int.Select(Gen.Int, Gen.Double[-10.0, 10.0])
let start = Math.Min(t.V0, t.V1)
let finish = Math.Max(t.V0, t.V1)
from value in Gen.Int.Skew[start, finish, t.V2]
select (value, start, finish, t.V2))
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.Int.Skew[start, finish, t.Item3]
select (value, start, finish, t.Item3))
.Sample(i => i.value >= i.start && i.value <= i.finish);
}

Expand All @@ -235,8 +235,8 @@ public void Zigzag()
public void UInt_Range()
{
(from t in Gen.UInt.Select(Gen.UInt)
let start = Math.Min(t.V0, t.V1)
let finish = Math.Max(t.V0, t.V1)
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.UInt[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
Expand All @@ -258,9 +258,9 @@ public void UInt_Distribution()
public void UInt_Skew()
{
(from t in Gen.UInt.Select(Gen.UInt, Gen.Double[-10.0, 10.0])
let start = Math.Min(t.V0, t.V1)
let finish = Math.Max(t.V0, t.V1)
from value in Gen.UInt.Skew[start, finish, t.V2]
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.UInt.Skew[start, finish, t.Item3]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
}
Expand Down Expand Up @@ -291,8 +291,8 @@ public void Long_Gen_Method()
public void Long_Range()
{
(from t in Gen.Long.Select(Gen.Long)
let start = Math.Min(t.V0, t.V1)
let finish = Math.Max(t.V0, t.V1)
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.Long[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
Expand All @@ -314,8 +314,8 @@ public void Long_Distribution()
public void ULong_Range()
{
(from t in Gen.ULong.Select(Gen.ULong)
let start = Math.Min(t.V0, t.V1)
let finish = Math.Max(t.V0, t.V1)
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.ULong[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
Expand Down Expand Up @@ -349,8 +349,8 @@ public void Single_Unit_Range()
public void Single_Range()
{
(from t in Gen.Single.Unit.Select(Gen.Single.Unit)
let start = Math.Min(t.V0, t.V1)
let finish = Math.Max(t.V0, t.V1)
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.Single[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
Expand Down Expand Up @@ -428,8 +428,8 @@ public void Double_Unit_Range()
public void Double_Range()
{
(from t in Gen.Double.Unit.Select(Gen.Double.Unit)
let start = Math.Min(t.V0, t.V1)
let finish = Math.Max(t.V0, t.V1)
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.Double[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish, seed: "89rtRQWk16go", iter: 1);
Expand All @@ -452,9 +452,9 @@ public void Double_Distribution()
public void Double_Skew()
{
(from t in Gen.Double.Unit.Select(Gen.Double.Unit, Gen.Double[-10.0, 10.0])
let start = Math.Min(t.V0, t.V1)
let finish = Math.Max(t.V0, t.V1)
from value in Gen.Double.Skew[start, finish, t.V2]
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.Double.Skew[start, finish, t.Item3]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
}
Expand Down Expand Up @@ -527,8 +527,8 @@ public void Decimal_Unit_Range()
public void Decimal_Range()
{
(from t in Gen.Decimal.Unit.Select(Gen.Decimal.Unit)
let start = Math.Min(t.V0, t.V1)
let finish = Math.Max(t.V0, t.V1)
let start = Math.Min(t.Item1, t.Item2)
let finish = Math.Max(t.Item1, t.Item2)
from value in Gen.Decimal[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
Expand All @@ -551,8 +551,8 @@ public void Decimal_Distribution()
public void Date_Range()
{
(from t in Gen.Date.Select(Gen.Date)
let start = t.V0 < t.V1 ? t.V0 : t.V1
let finish = t.V0 < t.V1 ? t.V1 : t.V0
let start = t.Item1 < t.Item2 ? t.Item1 : t.Item2
let finish = t.Item1 < t.Item2 ? t.Item2 : t.Item1
from value in Gen.Date[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
Expand All @@ -562,8 +562,8 @@ public void Date_Range()
public void DateTime_Range()
{
(from t in Gen.DateTime.Select(Gen.DateTime)
let start = t.V0 < t.V1 ? t.V0 : t.V1
let finish = t.V0 < t.V1 ? t.V1 : t.V0
let start = t.Item1 < t.Item2 ? t.Item1 : t.Item2
let finish = t.Item1 < t.Item2 ? t.Item2 : t.Item1
from value in Gen.DateTime[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
Expand All @@ -573,8 +573,8 @@ public void DateTime_Range()
public void TimeSpan_Range()
{
(from t in Gen.TimeSpan.Select(Gen.TimeSpan)
let start = t.V0 < t.V1 ? t.V0 : t.V1
let finish = t.V0 < t.V1 ? t.V1 : t.V0
let start = t.Item1 < t.Item2 ? t.Item1 : t.Item2
let finish = t.Item1 < t.Item2 ? t.Item2 : t.Item1
from value in Gen.TimeSpan[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
Expand All @@ -596,8 +596,8 @@ public void Guid()
public void Char_Range()
{
(from t in Gen.Char.Select(Gen.Char)
let start = t.V0 > t.V1 ? t.V1 : t.V0
let finish = t.V0 > t.V1 ? t.V0 : t.V1
let start = t.Item1 > t.Item2 ? t.Item2 : t.Item1
let finish = t.Item1 > t.Item2 ? t.Item1 : t.Item2
from value in Gen.Char[start, finish]
select (value, start, finish))
.Sample(i => i.value >= i.start && i.value <= i.finish);
Expand Down Expand Up @@ -673,9 +673,9 @@ public void Frequency()
{
const int frequency = 10;
(from f in Gen.Select(Gen.Int[1, 5], Gen.Int[1, 5], Gen.Int[1, 5])
let expected = new[] { f.V0 * frequency, f.V1 * frequency, f.V2 * frequency }
from actual in Gen.FrequencyConst((f.V0, 0), (f.V1, 1), (f.V2, 2))
.Array[frequency * (f.V0 + f.V1 + f.V2)]
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)]
.Select(sample => Tally(3, sample))
select (expected, actual))
.Sample(t => Check.ChiSquared(t.expected, t.actual), iter: 1, time: -2);
Expand Down
8 changes: 4 additions & 4 deletions Tests/IMToolsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public void AddOrUpdate_Metamorphic()
.Select(i => new ImHolder<ImHashMap234<int, int>> { Im = i })
.SampleMetamorphic(
Gen.Select(Gen.Int[0, upperBound], Gen.Int, Gen.Int[0, upperBound], Gen.Int).Metamorphic<ImHolder<ImHashMap234<int, int>>>(
(d, t) => d.Im = d.Im.AddOrUpdate(t.V0, t.V1).AddOrUpdate(t.V2, t.V3),
(d, t) => d.Im = t.V0 == t.V2 ? d.Im.AddOrUpdate(t.V2, t.V3) : d.Im.AddOrUpdate(t.V2, t.V3).AddOrUpdate(t.V0, t.V1)
(d, t) => d.Im = d.Im.AddOrUpdate(t.Item1, t.Item2).AddOrUpdate(t.Item3, t.Item4),
(d, t) => d.Im = t.Item1 == t.Item3 ? d.Im.AddOrUpdate(t.Item3, t.Item4) : d.Im.AddOrUpdate(t.Item3, t.Item4).AddOrUpdate(t.Item1, t.Item2)
)
, equal: (a, b) => Check.Equal(a.Im.Enumerate().Select(j => (j.Key, j.Value)), b.Im.Enumerate().Select(j => (j.Key, j.Value)))
, print: i => Check.Print(i.Im.Enumerate().Select(j => (j.Key, j.Value)))
Expand All @@ -109,8 +109,8 @@ public void AddOrUpdate_ModelBased()
.SampleModelBased(
Gen.Int[0, upperBound].Select(Gen.Int).Operation<ImHolder<ImHashMap234<int, int>>, Dictionary<int, int>>((h, d, kv) =>
{
h.Im = h.Im.AddOrUpdate(kv.V0, kv.V1);
d[kv.V0] = kv.V1;
h.Im = h.Im.AddOrUpdate(kv.Item1, kv.Item2);
d[kv.Item1] = kv.Item2;
})
, equal: (h, d) =>
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/ModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static class ModelGen
public readonly static Gen<string> Name = Gen.String["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 "];
public readonly static Gen<Currency> Currency = Gen.Enum<Currency>();
public readonly static Gen<Country> Country = Gen.Enum<Country>();
public readonly static Gen<int> Quantity = Gen.Int[-99, 99].Select(Gen.Int[0, 5]).Select(t => t.V0 * (int)Math.Pow(10, t.V1));
public readonly static Gen<int> Quantity = Gen.Int[-99, 99].Select(Gen.Int[0, 5]).Select((m, e) => m * (int)Math.Pow(10, e));
public readonly static Gen<double> Coupon = Gen.Int[0, 100].Select(i => 0.125 * i);
public readonly static Gen<double> Price = Gen.Int[0001, 9999].Select(i => 0.01 * i);
public readonly static Gen<DateTime> Date = Gen.Date[new DateTime(2000, 1, 1), new DateTime(2040, 1, 1)];
Expand Down
14 changes: 7 additions & 7 deletions Tests/PCGTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,26 +160,26 @@ public void PCG_Next64()
public void PCG_Next_UInt()
{
Gen.UInt[1, uint.MaxValue].Select(genPCG)
.Select(t => (Max: t.V0, x: t.V1.Next(t.V0)))
.Sample(t => t.x <= t.Max);
.Select((max, pcg) => (max, pcg.Next(max)))
.Sample((max, x) => x <= max);
}

[Fact]
public void PCG_Next64_ULong()
{
Gen.ULong[1, ulong.MaxValue].Select(genPCG)
.Select(t => (Max: t.V0, x: t.V1.Next64(t.V0)))
.Sample(t => t.x <= t.Max);
.Select((max, pcg) => (max, pcg.Next64(max)))
.Sample((max, x) => x <= max);
}

[Fact]
public void SeedString_RoundTrip()
{
Gen.Select(Gen.ULong, Gen.UInt)
.Sample(t =>
.Sample((i0, i1) =>
{
var seed = PCG.ToSeedString(t.V0, t.V1);
Assert.Equal(t, PCG.ParseSeedString(seed));
var seed = PCG.ToSeedString(i0, i1);
Assert.Equal((i0, i1), PCG.ParseSeedString(seed));
});
}

Expand Down
12 changes: 6 additions & 6 deletions Tests/ShrinkingChallengeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class ShrinkingChallengeTests
{
[Fact(Skip="fails")]
[Fact(Skip = "fails")]
public void No1_Bound5()
{
static short Sum(short[] l)
Expand All @@ -19,9 +19,9 @@ static short Sum(short[] l)
}
var sGen = Gen.Short.Array[0, 10].Where(i => Sum(i) < 256);
Gen.Select(sGen, sGen, sGen, sGen, sGen)
.Sample(t =>
.Sample((a1, a2, a3, a4, a5) =>
{
var total = Sum(t.V0) + Sum(t.V1) + Sum(t.V2) + Sum(t.V3) + Sum(t.V4);
var total = Sum(a1) + Sum(a2) + Sum(a3) + Sum(a4) + Sum(a5);
return (short)total < 5 * 256;
});
}
Expand Down Expand Up @@ -105,21 +105,21 @@ public void No5_LengthList()
public void No6_Difference_MustNotBeZero()
{
Gen.Int.Positive.Select(Gen.Int.Positive)
.Sample(t => t.V0 < 10 || t.V0 != t.V1);
.Sample((i0, i1) => i0 < 10 || i0 != i1);
}

[Fact(Skip="fails")]
public void No6_Difference_MustNotBeSmall()
{
Gen.Int.Positive.Select(Gen.Int.Positive)
.Sample(t => t.V0 < 10 || Math.Abs(t.V0 - t.V1) > 4 || t.V0 == t.V1);
.Sample((i0, i1) => i0 < 10 || Math.Abs(i0 - i1) > 4 || i0 == i1);
}

[Fact(Skip="fails")]
public void No6_Difference_MustNotBeOne()
{
Gen.Int.Positive.Select(Gen.Int.Positive)
.Sample(t => t.V0 < 10 || Math.Abs(t.V0 - t.V1) != 1);
.Sample((i0, i1) => i0 < 10 || Math.Abs(i0 - i1) != 1);
}

class Heap { public int Head; public Heap Left; public Heap Right; }
Expand Down
Loading

0 comments on commit 4c4c2aa

Please sign in to comment.