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