~nch/glue

ref: ea3a590e496ffd97d17b67879f3072aae73bc1c3 glue/test_dataflow.py -rw-r--r-- 3.2 KiB
ea3a590e — nc WIP attempt with generators 1 year, 4 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
import unittest
from dataflow import *
import operator

class TestDataflow2(unittest.TestCase):
    def test_stream(self):
        @stream
        def test_f():
            yield 1
            yield 2
            yield 3

        it = test_f()

        self.assertEqual(next(it), 1)
        self.assertEqual(next(it), 2)
        self.assertEqual(next(it), 3)

    def test_int(self):
        it1 = int_(1)
        self.assertEqual(next(it1), 1)
        self.assertEqual(next(it1), 1)

        it2 = int_(2)
        self.assertEqual(next(it2), 2)
        self.assertEqual(next(it1), 1)

    def test_fby(self):
        s = fby(1, 2)
        self.assertEqual(next(s), 1)
        self.assertEqual(next(s), 2)
        self.assertEqual(next(s), 2)

    def test_fby2(self):
        s = fby(int_(1), int_(2))
        self.assertEqual(next(s), 1)
        self.assertEqual(next(s), 2)
        self.assertEqual(next(s), 2)

    def test_fby3(self):
        s = fby(int_(1), fby(int_(2), int_(3)))
        self.assertEqual(next(s), 1)
        self.assertEqual(next(s), 2)
        self.assertEqual(next(s), 3)
        self.assertEqual(next(s), 3)

    def test_fby4_with_duplicate_streams(self):
        s1 = fby(int_(1), fby(int_(2), fby(int_(3), int_(4))))
        self.assertEqual(next(s1), 1)
        s2 = s1.copy()
        self.assertEqual(next(s1), 2)
        self.assertEqual(next(s1), 3)
        self.assertEqual(next(s2), 2)
        self.assertEqual(next(s1), 4)
        self.assertEqual(next(s2), 3)
        self.assertEqual(next(s2), 4)


class TestDataflow:#(unittest.TestCase):
    def test_fby1(self):
        p = DataflowProgram()
        n = p.var('n')
        p(n).where(n = Int(0).fby(Int(1)))
        self.assertEqual(p.step(), 0)
        self.assertEqual(p.step(), 1)
        self.assertEqual(p.step(), 1)

    def test_fby2(self):
        p = DataflowProgram()
        n = p.var('n')
        p(n).where(n = Int(0).fby(Int(1).fby(Int(2))))
        self.assertEqual(p.step(), 0)
        self.assertEqual(p.step(), 1)
        self.assertEqual(p.step(), 2)

    def test_recursion(self):
        p = DataflowProgram()
        n = p.var('n')
        p(n).where(n = Int(0).fby(n + Int(1)))
        self.assertEqual(p.step(), 0)
        self.assertEqual(p.step(), 1)
        self.assertEqual(p.step(), 2)
        self.assertEqual(p.step(), 3)
        self.assertEqual(p.step(), 4)

    def test_fac(self):
        p = DataflowProgram()
        fac = p.var('fac')
        n = p.var('n')
        p(fac).where(
                n = Int(0).fby(n + Int(1)),
                fac = Int(1).fby(fac * (n + Int(1))))
        self.assertEqual(p.step(), 1)
        self.assertEqual(p.step(), 2)
        self.assertEqual(p.step(), 6)
        self.assertEqual(p.step(), 24)
        self.assertEqual(p.step(), 120)
        self.assertEqual(p.step(), 720)

    def test_fib(self): # FIXME
        p = DataflowProgram()
        fib = p.var('fib')
        p(fib).where(fib = Int(0).fby(Int(1).fby(fib + fib.next())))

        self.assertEqual(p.step(), 0)
        self.assertEqual(p.step(), 1)
        self.assertEqual(p.step(), 1)
        self.assertEqual(p.step(), 2)
        self.assertEqual(p.step(), 3)

if __name__ == '__main__':
    unittest.main()