-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathShrinkingChallengeTests.cs
237 lines (215 loc) · 6.79 KB
/
ShrinkingChallengeTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Linq;
using System.Collections.Generic;
using CsCheck;
using Xunit;
namespace Tests
{
public class ShrinkingChallengeTests
{
[Fact]
public void No1_Bound5()
{
static short Sum(short[] l)
{
short s = 0;
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);
Gen.Select(sGen, sGen, sGen, sGen, sGen)
.Sample(t =>
{
var total = Sum(t.V0) + Sum(t.V1) + Sum(t.V2) + Sum(t.V3) + Sum(t.V4);
return (short)total < 5 * 256;
});
}
[Fact]
public void No2_LargeUnionList()
{
Gen.Int.Array.Array
.Sample(aa =>
{
var hs = new HashSet<int>();
foreach (var a in aa)
{
foreach (var i in a) hs.Add(i);
if (hs.Count >= 5) return false;
}
return true;
});
}
[Fact]
public void No3_Reverse()
{
Gen.Int.Array
.Sample(a =>
{
var c = new int[a.Length];
for (int i = 0; i < a.Length; i++)
c[i] = a[i];
Array.Reverse(c);
return Check.Equal(a, c);
});
}
[Fact]
public void No4_Calculator()
{
static bool DivSubTerms(object o) => o switch
{
int i => true,
('/', object x, int y) => y != 0,
(_, object x, object y) => DivSubTerms(x) && DivSubTerms(y),
_ => throw new Exception(o.ToString())
};
static int Evaluate(object o) => o switch
{
int i => i,
('/', object x, object y) => Evaluate(x) / Evaluate(y),
('+', object x, object y) => Evaluate(x) + Evaluate(y),
_ => throw new Exception(o.ToString())
};
Gen.Recursive<object>(g =>
Gen.Frequency(
(3, Gen.Int[-10, 10].Cast<object>()),
(1, Gen.Select(Gen.Const('+'), g, g).Cast<object>()),
(1, Gen.Select(Gen.Const('/'), g, g).Cast<object>())
)
)
.Where(DivSubTerms)
.Sample(o =>
{
int i = Evaluate(o);
});
}
[Fact]
public void No5_LengthList()
{
Gen.Int.Array
.Sample(a =>
{
foreach (var i in a)
if (i >= 900) return false;
return true;
});
}
[Fact]
public void No6_Difference_MustNotBeZero()
{
Gen.Int.Positive.Select(Gen.Int.Positive)
.Sample(t => t.V0 < 10 || t.V0 != t.V1);
}
[Fact]
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);
}
[Fact]
public void No6_Difference_MustNotBeOne()
{
Gen.Int.Positive.Select(Gen.Int.Positive)
.Sample(t => t.V0 < 10 || Math.Abs(t.V0 - t.V1) != 1);
}
class Heap { public int Head; public Heap Left; public Heap Right; }
[Fact]
public void No7_BinHeap()
{
static uint Count(Heap h) => h is null ? 0 : 1 + Count(h.Left) + Count(h.Right);
static Heap MergeHeaps(Heap h1, Heap h2) =>
h1 is null ? h2
: h2 is null ? h1
: h1.Head <= h2.Head ? new Heap { Head = h1.Head, Left = MergeHeaps(h1.Right, h2), Right = h1.Left }
: new Heap { Head = h2.Head, Left = MergeHeaps(h2.Right, h1), Right = h2.Left };
static List<int> ToList(Heap heap)
{
var r = new List<int>();
var s = new Stack<Heap>();
s.Push(heap);
while (s.Count != 0)
{
var h = s.Pop();
if (h == null) continue;
r.Add(h.Head);
s.Push(h.Left);
s.Push(h.Right);
}
return r;
}
static List<int> WrongToSortedList(Heap heap)
{
var r = new List<int>();
if (heap is not null)
{
r.Add(heap.Head);
r.AddRange(ToList(MergeHeaps(heap.Left, heap.Right)));
}
return r;
}
static List<int> Sorted(List<int> l)
{
l = new(l);
l.Sort();
return l;
}
static string Print(Heap h) => h is null ? "None" : $"({h.Head}, {Print(h.Left)}, {Print(h.Right)})";
Gen.Recursive(g =>
Gen.Frequency(
(3, Gen.Const((Heap)null)),
(1, Gen.Select(Gen.Int, g, g, (h, l, r) => new Heap { Head = h, Left = l, Right = r }))
),
(Heap h, ref Size size) => { size = new Size(Count(h), size); return h; }
)
.Sample(h =>
{
var l1 = ToList(h);
var l2 = WrongToSortedList(h);
return Check.Equal(l2, Sorted(l2)) && Check.Equal(Sorted(l1), l2);
}, print: Print);
}
[Fact]
public void No8_Coupling()
{
Gen.Int[0, 100].SelectMany(l => Gen.Int[0, l - 1].Array[l])
.Sample(a =>
{
for (int i = 0; i < a.Length; i++)
{
int j = a[i];
if (i != j && a[j] == i) return false;
}
return true;
});
}
[Fact]
public void No9_Deletion()
{
Gen.Int.List[1, 100].SelectTuple(l => Gen.Int[0, l.Count - 1])
.Sample(t =>
{
var l = new List<int>(t.V0);
var x = l[t.V1];
l.Remove(x);
return !l.Contains(x);
});
}
[Fact]
public void No10_Distinct()
{
Gen.Int.Array
.Sample(a => a.ToHashSet().Count < 3);
}
[Fact]
public void No11_NestedLists()
{
Gen.Int.Array.Array
.Sample(aa =>
{
int l = 0;
foreach (var a in aa)
l += a.Length;
return l <= 10;
});
}
}
}