-
Notifications
You must be signed in to change notification settings - Fork 806
/
Copy pathPostInferenceChecksTests.fs
87 lines (72 loc) · 2.79 KB
/
PostInferenceChecksTests.fs
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
namespace FSharp.Compiler.ComponentTests.Checking
open System
open Xunit
open FSharp.Compiler.PostTypeCheckSemanticChecks.Limit
module PostInferenceChecksTests =
[<Literal>]
let internal NUM_TESTS = 10_000
let genNat (rng : Random) : int =
let i = rng.Next ()
if i = Int32.MinValue then
0
else
abs i
let internal possibleLimitFlags =
Enum.GetValues typeof<LimitFlags>
|> Seq.cast<LimitFlags>
|> Array.ofSeq
let internal genLimitFlags (rng : Random) : LimitFlags =
Seq.init (genNat rng % 10 + 1) (fun _ -> possibleLimitFlags[genNat rng % possibleLimitFlags.Length])
|> Seq.reduce (|||)
let internal generateLimit (rng : Random) : Limit =
{ scope = genNat rng
flags = genLimitFlags rng }
let createSeed () =
DateTime.UtcNow.Ticks % int64 Int32.MaxValue
|> int
[<Fact>]
let ``NoLimit is almost a zero for CombineTwoLimits`` () =
let seed = createSeed ()
let rng = Random seed
for count in 0..NUM_TESTS - 1 do
try
let original = generateLimit rng
let after = CombineTwoLimits original NoLimit
Assert.StrictEqual (after.flags, original.flags)
if original.scope <> after.scope then
// See docs on the `scope` field for the conditions under which
// these special values are used.
Assert.InRange (after.scope, 0, 1)
with
| exc ->
let exc = AggregateException ($"Failed with seed %i{seed}, count %i{count}", exc)
raise exc
[<Fact>]
let ``CombineTwoLimits is commutative`` () =
let seed = createSeed ()
let rng = Random seed
for count in 0..NUM_TESTS - 1 do
try
let x = generateLimit rng
let y = generateLimit rng
Assert.StrictEqual (CombineTwoLimits x y, CombineTwoLimits y x)
with
| exc ->
let exc = AggregateException ($"Failed with seed %i{seed}, count %i{count}", exc)
raise exc
[<Fact>]
let ``CombineTwoLimits is associative`` () =
let seed = createSeed ()
let rng = Random seed
for count in 0..NUM_TESTS - 1 do
try
let x = generateLimit rng
let y = generateLimit rng
let z = generateLimit rng
let lhs = CombineTwoLimits (CombineTwoLimits x y) z
let rhs = CombineTwoLimits x (CombineTwoLimits y z)
Assert.StrictEqual (lhs, rhs)
with
| exc ->
let exc = AggregateException ($"Failed with seed %i{seed}, %i{count}", exc)
raise exc