-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmath-tests.ark
More file actions
97 lines (79 loc) · 4.78 KB
/
Copy pathmath-tests.ark
File metadata and controls
97 lines (79 loc) · 4.78 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
(import "tests-tools.ark")
(import "Math.ark")
(let math-tests (fun () {
(mut tests 0)
(let start-time (time))
(set tests (assert-eq (math:abs -1) 1 "math:abs" tests))
(set tests (assert-eq (math:abs 1) 1 "math:abs" tests))
(set tests (assert-val (math:even 2) "math:even" tests))
(set tests (assert-val (math:even -2) "math:even" tests))
(set tests (assert-val (math:odd 1) "math:odd" tests))
(set tests (assert-val (math:odd -1) "math:odd" tests))
(set tests (assert-eq (math:min 1 2) 1 "math:min" tests))
(set tests (assert-eq (math:min 1 -2) -2 "math:min" tests))
(set tests (assert-eq (math:min 0.5 0.2) 0.2 "math:min" tests))
(set tests (assert-eq (math:max 1 2) 2 "math:max" tests))
(set tests (assert-eq (math:max 1 -2) 1 "math:max" tests))
(set tests (assert-eq (math:max 0.5 0.2) 0.5 "math:max" tests))
(set tests (assert-eq (math:pow 2 2) 4 "math:pow" tests))
(set tests (assert-eq (math:pow 4 0.5) 2 "math:pow" tests))
# small trick with toNumber because we have number's approximation because of the underlying double
(set tests (assert-eq (math:fibo 31) (toNumber "1346269") "math:fibo" tests))
# small trick with toNumber because we have number's approximation because of the underlying double
(set tests (assert-eq (math:fibo 32) (toNumber "2178309") "math:fibo" tests))
(set tests (assert-eq (math:divs 6) [1 2 3 6] "math:divs" tests))
(set tests (assert-eq (math:divs 2) [1 2] "math:divs" tests))
(set tests (assert-eq (math:divs 931) [1 7 19 49 133 931] "math:divs" tests))
(set tests (assert-eq (math:log 27 3) 3 "math:log" tests))
(set tests (assert-eq (math:log 1953125 5) 9 "math:log" tests))
(set tests (assert-eq (math:log2 128) 7 "math:log2" tests))
(set tests (assert-eq (math:log2 2048) 11 "math:log2" tests))
(set tests (assert-eq (math:log10 1000) 3 "math:log10" tests))
(set tests (assert-eq (math:floordiv 14 6) 2 "math:floordiv" tests))
(set tests (assert-eq (math:floordiv 14 14) 1 "math:floordiv" tests))
(set tests (assert-eq (math:floordiv 14 15) 0 "math:floordiv" tests))
(let c0 (math:complex 1 2))
(let c1 (math:complex 0 -1))
(let c2 (math:complex -43 24))
(let c3 (math:complex -67 -89))
(let c4 (math:complex 4 12))
(mut c_add (math:complex-add c0 c1))
(set tests (assert-eq c_add.real 1 "math:complex-add" tests))
(set tests (assert-eq c_add.imag 1 "math:complex-add" tests))
(set c_add (math:complex-add c2 c3))
(set tests (assert-eq c_add.real -110 "math:complex-add" tests))
(set tests (assert-eq c_add.imag -65 "math:complex-add" tests))
(mut c_sub (math:complex-sub c0 c1))
(set tests (assert-eq c_sub.real 1 "math:complex-sub" tests))
(set tests (assert-eq c_sub.imag 3 "math:complex-sub" tests))
(set c_sub (math:complex-sub c2 c3))
(set tests (assert-eq c_sub.real 24 "math:complex-sub" tests))
(set tests (assert-eq c_sub.imag 113 "math:complex-sub" tests))
(mut c_mul (math:complex-mul c0 c1))
(set tests (assert-eq c_mul.real 2 "math:complex-mul" tests))
(set tests (assert-eq c_mul.imag -1 "math:complex-mul" tests))
(set c_mul (math:complex-mul c2 c3))
(set tests (assert-eq c_mul.real 5017 "math:complex-mul" tests))
(set tests (assert-eq c_mul.imag 2219 "math:complex-mul" tests))
(mut c_div (math:complex-div c0 c1))
(set tests (assert-eq c_div.real -2 "math:complex-div" tests))
(set tests (assert-eq c_div.imag 1 "math:complex-div" tests))
(set c_div (math:complex-div c4 c0))
(set tests (assert-eq c_div.real 5.6 "math:complex-div" tests))
(set tests (assert-eq c_div.imag 0.8 "math:complex-div" tests))
(mut c_conj (math:complex-conjugate c0))
(set tests (assert-eq c_conj.real 1 "math:complex-conjugate" tests))
(set tests (assert-eq c_conj.imag -2 "math:complex-conjugate" tests))
(set c_conj (math:complex-conjugate c3))
(set tests (assert-eq c_conj.real -67 "math:complex-conjugate" tests))
(set tests (assert-eq c_conj.imag 89 "math:complex-conjugate" tests))
(set tests (assert-lt (math:abs (- (math:complex-module c0) 2.236067977499789)) 0.0001 "math:complex-module" tests))
(set tests (assert-lt (math:abs (- (math:complex-module c1) 1)) 0.0001 "math:complex-module" tests))
(set tests (assert-lt (math:abs (- (math:complex-module c2) 49.244289008980523)) 0.0001 "math:complex-module" tests))
# FIXME computing c3 is crashing the vm, I suspect an integer/double overflow here
# (set tests (assert-lt (math:abs (- (math:complex-module c3) 111.400179533068976)) 0.0001 "math:complex-module" tests))
# (set tests (assert-lt (math:abs (- (math:complex-module c4) 12.649110640673517)) 0.0001 "math:complex-module" tests))
(recap "Math tests passed" tests (- (time) start-time))
tests
}))
(let passed-math (math-tests))