-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.affine
More file actions
321 lines (276 loc) · 9.57 KB
/
Copy pathtesting.affine
File metadata and controls
321 lines (276 loc) · 9.57 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// SPDX-License-Identifier: PMPL-1.0-or-later
// SPDX-FileCopyrightText: 2025 hyperpolymath
//
// AffineScript Standard Library - Testing
//
// Provides assertion functions, test organisation, property-based helpers,
// and lightweight benchmarking.
//
// Depends on builtins: show, panic, time_now
// ============================================================================
// Assertions
// ============================================================================
/// Assert that a condition is true; panic with message on failure
fn assert(condition: Bool, message: String) -> () {
if !condition {
panic("Assertion failed: " ++ message);
}
}
/// Assert two values are equal
fn assert_eq<T>(actual: T, expected: T, message: String) -> () {
if actual != expected {
panic("Assertion failed: " ++ message ++ "\n Expected: " ++ show(expected) ++ "\n Actual: " ++ show(actual));
}
}
/// Assert two values are not equal
fn assert_ne<T>(actual: T, expected: T, message: String) -> () {
if actual == expected {
panic("Assertion failed: values should not be equal: " ++ message ++ "\n Both: " ++ show(actual));
}
}
/// Assert a Result is Ok and return the inner value
fn assert_ok<T, E>(result: Result<T, E>, message: String) -> T {
match result {
Ok(value) => value,
Err(e) => panic("Assertion failed: expected Ok, got Err(" ++ show(e) ++ "): " ++ message)
}
}
/// Assert a Result is Err and return the error value
fn assert_err<T, E>(result: Result<T, E>, message: String) -> E {
match result {
Ok(v) => panic("Assertion failed: expected Err, got Ok(" ++ show(v) ++ "): " ++ message),
Err(e) => e
}
}
/// Assert an Option is Some and return the inner value
fn assert_some<T>(opt: Option<T>, message: String) -> T {
match opt {
Some(value) => value,
None => panic("Assertion failed: expected Some, got None: " ++ message)
}
}
/// Assert an Option is None
fn assert_none<T>(opt: Option<T>, message: String) -> () {
match opt {
Some(v) => panic("Assertion failed: expected None, got Some(" ++ show(v) ++ "): " ++ message),
None => {}
}
}
// ============================================================================
// Numeric Assertions
// ============================================================================
/// Assert actual < bound
fn assert_lt<T>(actual: T, bound: T, message: String) -> () {
if !(actual < bound) {
panic("Assertion failed: " ++ show(actual) ++ " should be < " ++ show(bound) ++ ": " ++ message);
}
}
/// Assert actual <= bound
fn assert_le<T>(actual: T, bound: T, message: String) -> () {
if !(actual <= bound) {
panic("Assertion failed: " ++ show(actual) ++ " should be <= " ++ show(bound) ++ ": " ++ message);
}
}
/// Assert actual > bound
fn assert_gt<T>(actual: T, bound: T, message: String) -> () {
if !(actual > bound) {
panic("Assertion failed: " ++ show(actual) ++ " should be > " ++ show(bound) ++ ": " ++ message);
}
}
/// Assert actual >= bound
fn assert_ge<T>(actual: T, bound: T, message: String) -> () {
if !(actual >= bound) {
panic("Assertion failed: " ++ show(actual) ++ " should be >= " ++ show(bound) ++ ": " ++ message);
}
}
/// Assert two floats are equal within an epsilon tolerance
fn assert_float_eq(actual: Float, expected: Float, epsilon: Float, message: String) -> () {
let diff = if actual > expected { actual - expected } else { expected - actual };
if diff > epsilon {
panic("Assertion failed: " ++ show(actual) ++ " != " ++ show(expected) ++ " (epsilon " ++ show(epsilon) ++ "): " ++ message);
}
}
// ============================================================================
// Collection Assertions
// ============================================================================
/// Assert a list is empty
fn assert_empty<T>(list: [T], message: String) -> () {
if len(list) != 0 {
panic("Assertion failed: list should be empty (length " ++ show(len(list)) ++ "): " ++ message);
}
}
/// Assert a list is not empty
fn assert_not_empty<T>(list: [T], message: String) -> () {
if len(list) == 0 {
panic("Assertion failed: list should not be empty: " ++ message);
}
}
/// Assert list has expected length
fn assert_length<T>(list: [T], expected_len: Int, message: String) -> () {
let actual_len = len(list);
if actual_len != expected_len {
panic("Assertion failed: expected length " ++ show(expected_len) ++ ", got " ++ show(actual_len) ++ ": " ++ message);
}
}
/// Assert list contains a specific element
fn assert_contains<T>(list: [T], element: T, message: String) -> () {
let mut found = false;
for x in list {
if x == element {
found = true;
}
}
if !found {
panic("Assertion failed: list should contain " ++ show(element) ++ ": " ++ message);
}
}
/// Assert a string contains a substring
fn assert_str_contains(haystack: String, needle: String, message: String) -> () {
if string_find(haystack, needle) < 0 {
panic("Assertion failed: \"" ++ haystack ++ "\" should contain \"" ++ needle ++ "\": " ++ message);
}
}
// ============================================================================
// Test Organisation
// ============================================================================
/// Result of a single test execution
type TestResult = Pass | Fail(String)
/// A named test case
type TestCase = {
name: String,
test: () -> TestResult
}
/// A named collection of test cases
type TestSuite = {
name: String,
tests: [TestCase]
}
/// Run a single test case, catching panics
fn run_test(test: TestCase) -> TestResult {
println(" Running: " ++ test.name);
try {
let result = test.test();
match result {
Pass => {
println(" PASS");
Pass
},
Fail(msg) => {
println(" FAIL: " ++ msg);
Fail(msg)
}
}
} catch {
RuntimeError(msg) => {
println(" ERROR: " ++ msg);
Fail(msg)
},
_ => {
println(" PANIC");
Fail("Test panicked")
}
}
}
/// Run all tests in a suite and return (passed, failed) counts
fn run_suite(suite: TestSuite) -> (Int, Int) {
println("Running suite: " ++ suite.name);
let mut passed = 0;
let mut failed = 0;
for test in suite.tests {
match run_test(test) {
Pass => passed = passed + 1,
Fail(_) => failed = failed + 1
}
}
println("\nResults: " ++ show(passed) ++ " passed, " ++ show(failed) ++ " failed");
(passed, failed)
}
/// Run multiple test suites and return aggregate (passed, failed) counts
fn run_suites(suites: [TestSuite]) -> (Int, Int) {
let mut total_passed = 0;
let mut total_failed = 0;
for suite in suites {
let (passed, failed) = run_suite(suite);
total_passed = total_passed + passed;
total_failed = total_failed + failed;
println("");
}
println("========================================");
println("Total: " ++ show(total_passed) ++ " passed, " ++ show(total_failed) ++ " failed");
(total_passed, total_failed)
}
// ============================================================================
// Property-Based Testing Utilities
// ============================================================================
/// Check that a property holds for all elements in a list
fn for_all<T>(values: [T], prop: T -> Bool) -> TestResult {
for value in values {
if !prop(value) {
return Fail("Property failed for value: " ++ show(value));
}
}
Pass
}
/// Check that a property holds for at least one element
fn exists<T>(values: [T], prop: T -> Bool) -> TestResult {
for value in values {
if prop(value) {
return Pass;
}
}
Fail("Property failed for all values")
}
/// Check that a property is an involution (applying twice yields the original)
fn assert_involution<T>(f: T -> T, values: [T], message: String) -> () {
for v in values {
let round_tripped = f(f(v));
if round_tripped != v {
panic("Involution failed for " ++ show(v) ++ ": f(f(x)) = " ++ show(round_tripped) ++ ": " ++ message);
}
}
}
/// Check that a binary operation is commutative over given pairs
fn assert_commutative<T, R>(op: (T, T) -> R, pairs: [(T, T)], message: String) -> () {
for (a, b) in pairs {
let lhs = op(a, b);
let rhs = op(b, a);
if lhs != rhs {
panic("Commutativity failed: op(" ++ show(a) ++ ", " ++ show(b) ++ ") = " ++ show(lhs) ++
" but op(" ++ show(b) ++ ", " ++ show(a) ++ ") = " ++ show(rhs) ++ ": " ++ message);
}
}
}
// ============================================================================
// Benchmarking
// ============================================================================
/// Result of a benchmark run
type BenchResult = {
iterations: Int,
total_time: Float,
avg_time: Float
}
/// Benchmark a function by running it for the given number of iterations.
/// Returns timing statistics using the time_now() builtin.
fn bench(f: () -> (), iterations: Int) -> BenchResult {
let start = time_now();
let mut i = 0;
while i < iterations {
f();
i = i + 1;
}
let tot = time_now() - start;
{
iterations: iterations,
total_time: tot,
avg_time: tot / float(iterations)
}
}
/// Compare performance of two functions side-by-side
fn bench_compare(name1: String, f1: () -> (), name2: String, f2: () -> (), iterations: Int) -> (BenchResult, BenchResult) {
println("Benchmarking " ++ name1 ++ " vs " ++ name2 ++ " (" ++ show(iterations) ++ " iterations)");
let r1 = bench(f1, iterations);
let r2 = bench(f2, iterations);
println(" " ++ name1 ++ ": " ++ show(r1.total_time) ++ "s total, " ++ show(r1.avg_time) ++ "s/iter");
println(" " ++ name2 ++ ": " ++ show(r2.total_time) ++ "s total, " ++ show(r2.avg_time) ++ "s/iter");
(r1, r2)
}