-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path_math_test.v
More file actions
65 lines (57 loc) · 1.96 KB
/
_math_test.v
File metadata and controls
65 lines (57 loc) · 1.96 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
module gui
// ------------------------------
// Tests for xtra_math.v helpers
// ------------------------------
// int_clamp returns x constrained between min and max
fn test_int_clamp_basic() {
// Below min
assert int_clamp(-10, 0, 5) == 0
// Above max
assert int_clamp(10, 0, 5) == 5
// Within range
assert int_clamp(3, 0, 5) == 3
}
fn test_int_clamp_boundaries() {
// On boundaries should return the value itself
assert int_clamp(0, 0, 5) == 0
assert int_clamp(5, 0, 5) == 5
// Negative ranges
assert int_clamp(-3, -5, -1) == -3
assert int_clamp(-10, -5, -1) == -5
assert int_clamp(0, -5, -1) == -1
}
// f32_clamp returns x constrained between min and max
fn test_f32_clamp_basic() {
// Below min
assert f32_clamp(f32(-1.5), f32(0.0), f32(2.5)) == f32(0.0)
// Above max
assert f32_clamp(f32(3.14), f32(0.0), f32(2.5)) == f32(2.5)
// Within range
assert f32_clamp(f32(1.25), f32(0.0), f32(2.5)) == f32(1.25)
}
fn test_f32_clamp_boundaries() {
// On boundaries should return the value itself
assert f32_clamp(f32(0.0), f32(0.0), f32(2.0)) == f32(0.0)
assert f32_clamp(f32(2.0), f32(0.0), f32(2.0)) == f32(2.0)
// Negative ranges
assert f32_clamp(f32(-3.0), f32(-5.0), f32(-1.0)) == f32(-3.0)
assert f32_clamp(f32(-10.0), f32(-5.0), f32(-1.0)) == f32(-5.0)
assert f32_clamp(f32(0.0), f32(-5.0), f32(-1.0)) == f32(-1.0)
}
// f32_are_close tests if |a - b| <= f32_tolerance (0.01)
fn test_f32_are_close_within_tolerance() {
// Within tolerance
assert f32_are_close(f32(1.00), f32(1.005))
assert f32_are_close(f32(-2.50), f32(-2.507))
}
fn test_f32_are_close_at_tolerance_boundary() {
// Almost at tolerance boundary should be considered close
// Round-off errors prevent exact boundary condition
assert f32_are_close(f32(10.00), f32(10.009))
assert f32_are_close(f32(-3.33), f32(-3.339))
}
fn test_f32_are_close_outside_tolerance() {
// Outside tolerance should be false
assert !f32_are_close(f32(0.0), f32(0.02))
assert !f32_are_close(f32(-1.0), f32(-1.02))
}