-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathExamples.lean
More file actions
219 lines (163 loc) · 5.76 KB
/
Examples.lean
File metadata and controls
219 lines (163 loc) · 5.76 KB
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
import LSpec
/-!
# LSpec Examples
This file provides comprehensive examples of using LSpec for testing in Lean 4.
## Quick Reference
| Function | Use Case | Output |
|----------|----------|--------|
| `test` | Unit tests | `✓ ∃: name` |
| `check` | Property tests (simple) | `✓ ∃: name` |
| `check'` | Property tests (with syntax) | `✓ ∃₁₀₀: "name" (property)` |
| `checkIO` | Runtime property tests | Same as check |
| `checkIO'` | Runtime tests (with syntax) | Same as check' |
-/
section LSpec
open LSpec
/-! ## Basic Usage
The simplest way to run tests is with the `#lspec` command.
-/
#lspec test "Nat equality" (4 ≠ 5)
-- ✓ ∃: Nat equality
/-!
Tests can be composed into sequences using `$` or `++`.
The output shows `✓ ∃:` for each passing unit test.
-/
#lspec test "bool equality" (42 == 42) $
test "list length" ([42].length = 2) $ -- This fails
test "list nonempty" ¬ [42].isEmpty
-- ✓ ∃: bool equality
-- × ∃: list length
-- Expected to be equal: '1' and '2'
-- ✓ ∃: list nonempty
/-!
## Bounded Universal Quantification
Tests like `∀ n, n < 10 → P n` are automatically iterated.
-/
#lspec test "all lt" $ ∀ n, n < 10 → n - 5 < 5
-- ✓ ∃: all lt
/-!
## IO-based Tests
For runtime tests, use `lspecIO` with a HashMap of test suites.
-/
def fourIO : IO Nat := return 4
def fiveIO : IO Nat := return 5
def main := do
let four ← fourIO
let five ← fiveIO
lspecIO (.ofList [("IO tests", [
test "fourIO equals 4" (four = 4) $
test "fiveIO equals 5" (five = 5)])]) []
#eval main
-- IO tests
-- ✓ ∃: fourIO equals 4
-- ✓ ∃: fiveIO equals 5
-- 0
end LSpec
section SlimCheck
/-!
## Property-Based Testing with SlimCheck
SlimCheck generates random test cases to verify properties.
The output shows how many samples were tested.
### Basic Property Tests
Use `check` for simple output or `check'` to see the property syntax.
-/
open LSpec SlimCheck
-- Nat has a SampleableExt instance for random generation
example : SampleableExt Nat := by infer_instance
/-!
### Using `check'` (Recommended)
The `check'` macro captures the property syntax for display:
- Success shows: `✓ ∃₁₀₀: "name" (∀ n m : Nat, n + m = m + n)`
- Failure shows: `× ∃²/₁₀₀: "name" (property)` with counterexample
The superscript indicates which sample failed, subscript shows total samples.
-/
#lspec check' "add_comm" (∀ n m : Nat, n + m = m + n)
-- ✓ ∃₁₀₀: "add_comm" (∀ n m : Nat, n + m = m + n)
#lspec check' "mul_comm_wrong" (∀ n m : Nat, n * m = m * m)
-- × ∃⁶/₁₀₀: "mul_comm_wrong" (∀ n m : Nat, n * m = m * m)
-- ===================
-- Found problems!
-- n := 1
-- m := 2
-- issue: 2 = 4 does not hold
-- (2 shrinks)
-- -------------------
/-!
### Custom Types with SlimCheck
To use SlimCheck with custom types, define `Shrinkable` and `SampleableExt` instances.
-/
structure Pairs where
left : Nat
right : Nat
deriving Repr
private def mkPairs (as : List α) (bs : List β) : List (α × β) :=
let mkPairsAux (a : α) (bs : List β) : List (α × β) := bs.map fun b => (a, b)
as.foldl (fun abs a => mkPairsAux a bs ++ abs) []
-- Shrinkable instance for reducing counterexamples
open Shrinkable in
instance : Shrinkable Pairs where
shrink := fun p =>
let shrinkl := shrink p.left
let shrinkr := shrink p.right
mkPairs shrinkl shrinkr |>.map fun (a, b) => ⟨a, b⟩
-- SampleableExt instance for random generation
open SampleableExt
def pairsGen : Gen Pairs := return ⟨← Gen.chooseAny Nat, ← Gen.chooseAny Nat⟩
instance : SampleableExt Pairs := mkSelfContained pairsGen
-- Now we can test properties over Pairs
#lspec check' "left + 2 ≤ right" (∀ pair : Pairs, pair.left + 2 ≤ pair.right)
-- × ∃¹/₁₀₀: "left + 2 ≤ right" (∀ pair : Pairs, pair.left + 2 ≤ pair.right)
-- ===================
-- Found problems!
-- pair := { left := 0, right := 1 }
-- issue: 2 ≤ 1 does not hold
-- ...
-- This passes (but note: ⟨0, 0⟩ would fail, showing shrink limitations)
#lspec check' "left + right > right" (∀ pair : Pairs, pair.left + pair.right > pair.right)
-- ✓ ∃₁₀₀: "left + right > right" (∀ pair : Pairs, pair.left + pair.right > pair.right)
/-!
### Weighted Random Generation with Gen.frequency
`Gen.frequency` creates weighted random generators for more realistic test data.
-/
inductive Command where
| noop
| read
| write
| delete
deriving Repr, DecidableEq
/--
Weighted command generator:
- noop: 10%, read: 50%, write: 30%, delete: 10%
-/
def commandGen : Gen Command :=
Gen.frequency #[
(1, pure Command.noop),
(5, pure Command.read),
(3, pure Command.write),
(1, pure Command.delete)
] (pure Command.noop)
instance : Shrinkable Command where
shrink := fun _ => []
instance : SampleableExt Command := mkSelfContained commandGen
#lspec check' "commands are valid" (∀ cmd : Command,
cmd = Command.noop ∨ cmd = Command.read ∨ cmd = Command.write ∨ cmd = Command.delete)
-- ✓ ∃₁₀₀: "commands are valid" (∀ cmd : Command, ...)
/-!
### Biased Number Generation
Generate numbers with custom distributions.
-/
def biasedSmallGen : Gen Nat :=
Gen.frequency #[
(5, Gen.choose Nat 0 10), -- 50%: small (0-10)
(3, Gen.choose Nat 11 100), -- 30%: medium (11-100)
(2, Gen.choose Nat 101 1000) -- 20%: larger (101-1000)
] (pure 0)
structure BiasedNat where
val : Nat
deriving Repr
instance : Shrinkable BiasedNat where
shrink := fun n => (Shrinkable.shrink n.val).map BiasedNat.mk
instance : SampleableExt BiasedNat := mkSelfContained (BiasedNat.mk <$> biasedSmallGen)
#lspec check' "biased numbers bounded" (∀ n : BiasedNat, n.val ≤ 1000)
-- ✓ ∃₁₀₀: "biased numbers bounded" (∀ n : BiasedNat, n.val ≤ 1000)
end SlimCheck