~subsetpark/ec

ref: 23027903beabdba42f04dc75e4e6b628b66dbd55 ec/test/calc.janet -rw-r--r-- 3.0 KiB
23027903 — Zach Smith Factor out logic into surrounding modules 3 months ago
                                                                                
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
(import /src/calc)

(import /src/operations :prefix "o/")
(import /src/adverbs :prefix "a/")
(import /src/compose :prefix "c/")

(use testament)
(use /test-support)

(deftest vectors
  (def v (wrap 1 2 3))
  (def v2 (wrap 2 5 10))

  (vec= [4 5 6] (o/apply-operation o/add v (:new calc/Int 3)))
  (vec= [3 7 13] (o/apply-operation o/add v v2))

  (vec= [-2 -1 0] (o/apply-operation o/sub v (:new calc/Int 3)))
  (vec= [2 1 0] (o/apply-operation o/sub (:new calc/Int 3) v))

  (is (vec= 6 (o/apply-operation o/add (:new calc/Int 3) (:new calc/Int 3)))))

(deftest matrices
  (is (thrown? (wrap (wrap 1) (wrap 1 2)))
      "Inner quotations are of differing sizes")

  (def m (wrap (wrap 1 2 3)
               (wrap 4 5 6)))
  (is (= [2 3] (calc/get-shape m)))

  (def filled (calc/fill m [2 2 3]))
  (is (= [2 2 3] (calc/get-shape filled)))

  (vec= @[@[@[1 2 3]
            @[4 5 6]]
          @[@[1 2 3]
            @[4 5 6]]]
        filled)

  (is (thrown? (calc/fill m [2 3 3]))
      "Vectors can be filled into shapes that share a suffix")

  (vec= @[@[@[2 4 6]
            @[8 10 12]]
          @[@[2 4 6]
            @[8 10 12]]]
        (o/apply-operation o/add m filled)))

(deftest dist-add
  (def s (:new calc/Stack))
  # Operands
  (calc/push s (wrap 1 2 3))
  (calc/push s (:new calc/Int 0))
  # Operator
  (calc/push s (quote-wrap o/add))
  (calc/push s a/dist)
  (pop-and-compare 6 s))

(deftest dist-multiple-operations
  (def s (:new calc/Stack))
  (calc/push s (wrap 1 2 3))
  (calc/push s (:new calc/Int 0))
  (calc/push s (quote-wrap 4 o/mul o/add))
  # 0 [1 2 3]
  # [2 3] 0 1 4 * +
  # [2 3] 4
  # [3] 4 2 4 * +
  # [3] 12
  # 12 3 4 * +
  # 24
  (calc/push s a/dist)
  (pop-and-compare 24 s))

(deftest apply-quotation
  (def s (:new calc/Stack))
  (calc/push s (quote-wrap 1 2 3))
  (calc/push s a/apply-quotation)
  (pop-and-compare 3 s)
  (pop-and-compare 2 s)
  (pop-and-compare 1 s)
  (is (empty? (s :data))))

(deftest apply-vector
  (def s (:new calc/Stack))
  (calc/push s (wrap 1 2 3))
  (calc/push s a/apply-quotation)
  (pop-and-compare 3 s)
  (pop-and-compare 2 s)
  (pop-and-compare 1 s)
  (is (empty? (s :data))))

(deftest apply-operation
  (def s (:new calc/Stack))
  (calc/push s (:new calc/Int 3))
  (calc/push s (:new calc/Int 5))
  (calc/push s (quote-wrap o/add))
  (calc/push s a/apply-quotation)
  (pop-and-compare 8 s)
  (is (empty? (s :data))))

(deftest wrap-stack
  (def s (:new calc/Stack))
  (calc/push s (:new calc/Int 3))
  (calc/push s (:new calc/Int 4))
  (calc/push s c/wrap-all)

  (def vec (calc/pop s))
  (is (= [2] (calc/shape vec)))
  (is (empty? (s :data))))

(deftest wrap-quotations
  (def s (:new calc/Stack))
  (calc/push s (wrap 1 2))
  (calc/push s (wrap 2 4))
  (calc/push s c/wrap-all)

  (def vec (calc/pop s))
  (is (= [2 2] (calc/shape vec)))
  (is (empty? (s :data))))

(deftest wrap-failure
  (def s (:new calc/Stack))
  (calc/push s (wrap 1))
  (calc/push s (wrap 2 4))
  (is (thrown?
        (calc/push s c/wrap-all))
      "Wrapping a stack requires homogeneous data"))

(run-tests!)