summaryrefslogtreecommitdiff
path: root/test/unheard/cycles_test.clj
blob: 0f55a2cf8c27c5692fc8788495392701903d3a9d (plain)
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
(ns unheard.cycles-test
  (:require [clojure.test :refer [deftest is testing]]
            [unheard.cycles :refer [l f p rate unfold]]))

(deftest unfold-tests
  (testing "single scalar"
    (is (= [[0 1 :a]]
           (unfold 1 :a))))

  (testing "simple list - time subdivision"
    (is (= [[0 1/3 :a] [1/3 2/3 :b] [2/3 1 :c]]
           (unfold 1 (l :a :b :c)))))

  (testing "simple fork - cycles through children"
    (is (= [[0 1 :a]
            [1 2 :b]
            [2 3 :c]]
           (unfold 1 (f :a :b :c)))))

  (testing "list with fork - fork gets time slice"
    (is (= [[0 1/2 :1] [1/2 1 :2]
            [1 3/2 :1] [3/2 2 :3]]
           (unfold 1 (l :1 (f :2 :3))))))

  (testing "fork with list - each child subdivides its cycle"
    (is (= [[0 1/2 :a] [1/2 1 :b]
            [1 2 :c]]
           (unfold 1 (f (l :a :b) :c)))))

  (testing "nested lists"
    (is (= [[0 1/2 :a] [1/2 3/4 :b] [3/4 1 :c]]
           (unfold 1 (l :a (l :b :c))))))

  (testing "nested forks"
    (is (= [[0 1 :a]
            [1 2 :b]
            [2 3 :c]
            [3 4 :d]
            [4 5 :a]
            [5 6 :e]
            [6 7 :c]
            [7 8 :d]]
           (unfold 1 (f :a (f :b :e) :c :d)))))

  (testing "complex nested structure from example"
    (is (= [[0 1/2 :1] [1/2 3/4 :2] [3/4 1 :9] [1 3/2 :6] [3/2 2 :7]
            [2 5/2 :1] [5/2 3 :3] [3 7/2 :6] [7/2 4 :8]
            [4 9/2 :1] [9/2 5 :4] [5 11/2 :6] [11/2 6 :7]
            [6 13/2 :1] [13/2 27/4 :2] [27/4 7 :9] [7 15/2 :6] [15/2 8 :8]
            [8 17/2 :1] [17/2 9 :3] [9 19/2 :6] [19/2 10 :7]
            [10 21/2 :1] [21/2 11 :5] [11 23/2 :6] [23/2 12 :8]]
           (unfold 2 (l :1 (f (l :2 :9) :3 (f :4 :5)) :6 (f :7 :8))))))

  (testing "different cycle lengths"
    (is (= [[0 1/2 :a] [1/2 1 :b]]
           (unfold 1 (l :a :b))))
    (is (= [[0 1 :a] [1 2 :b]]
           (unfold 2 (l :a :b))))
    (is (= [[0 5/2 :a] [5/2 5 :b]]
           (unfold 5 (l :a :b)))))

  (testing "fork with nested list subdivides correctly"
    (is (= [[0 1/3 :a] [1/3 2/3 :b] [2/3 1 :c]
            [1 2 :x]]
           (unfold 1 (f (l :a :b :c) :x)))))

  (testing "simple parallel - all children at same time"
    (is (= [[0 1 :a] [0 1 :b] [0 1 :c]]
           (unfold 1 (p :a :b :c)))))

  (testing "parallel with list - children subdivide in parallel"
    (is (= [[0 1/2 :a] [1/2 1 :b]
            [0 1/2 :c] [1/2 1 :d]]
           (unfold 1 (p (l :a :b) (l :c :d))))))

  (testing "parallel with fork - forks extend together"
    (is (= [[0 1 :a] [0 1 :c]
            [1 2 :b] [1 2 :d]]
           (unfold 1 (p (f :a :b) (f :c :d))))))

  (testing "list containing parallel"
    (is (= [[0 1/2 :x] [0 1/2 :y]
            [1/2 1 :z]]
           (unfold 1 (l (p :x :y) :z)))))

  (testing "parallel with different cycle lengths"
    (is (= [[0 1 :a] [0 1 :b]
            [1 2 :a] [1 2 :c]]
           (unfold 1 (p :a (f :b :c))))))

  (testing "rate 2 - doubles speed of scalar"
    (is (= [[0 1/2 :a] [1/2 1 :a]]
           (unfold 1 (rate 2 :a)))))

  (testing "rate 1/2 - halves speed of scalar"
    (is (= [[0 2 :a]]
           (unfold 1 (rate 1/2 :a)))))

  (testing "rate 2 - doubles speed of list"
    (is (= [[0 1/6 :a] [1/6 1/3 :b] [1/3 1/2 :c]
            [1/2 2/3 :a] [2/3 5/6 :b] [5/6 1 :c]]
           (unfold 1 (rate 2 (l :a :b :c))))))

  (testing "rate 1/2 - halves speed of list"
    (is (= [[0 1 :a] [1 2 :b]]
           (unfold 1 (rate 1/2 (l :a :b))))))

  (testing "rate 2 - doubles speed of fork"
    ;; Fork has 3 children, cycle count is 3
    ;; rate 2 makes it repeat 2x, so 6 total cycles
    (is (= [[0 1/6 :a] [1/6 1/3 :b] [1/3 1/2 :c]
            [1/2 2/3 :a] [2/3 5/6 :b] [5/6 1 :c]
            [1 7/6 :a] [7/6 4/3 :b] [4/3 3/2 :c]
            [3/2 5/3 :a] [5/3 11/6 :b] [11/6 2 :c]
            [2 13/6 :a] [13/6 7/3 :b] [7/3 5/2 :c]
            [5/2 8/3 :a] [8/3 17/6 :b] [17/6 3 :c]]
           (unfold 1 (rate 2 (f :a :b :c))))))

  (testing "rate with parallel"
    ;; Parallel has cycle 1, rate 2 repeats it twice
    (is (= [[0 1/2 :a] [0 1/2 :b]
            [1/2 1 :a] [1/2 1 :b]]
           (unfold 1 (rate 2 (p :a :b))))))

  (testing "rate inside list"
    (is (= [[0 1/2 :x] [1/2 3/4 :y] [3/4 1 :y]]
           (unfold 1 (l :x (rate 2 :y))))))

  (testing "nested rates"
    (is (= [[0 1/4 :a] [1/4 1/2 :a] [1/2 3/4 :a] [3/4 1 :a]]
           (unfold 1 (rate 2 (rate 2 :a)))))))