aboutsummaryrefslogtreecommitdiffstats
path: root/src/test.py
blob: 253868085a4c645ad54f01e0dae1a493edd8fd79 (plain) (blame)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python3
'''
join python – Join-calculus for Python
Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

from join import *

import time


@fragment
def f1():
    pass

@fragment
def f2():
    pass

@fragment
def f3():
    pass


def unordered_f123():
    (case, (jargs, jkwargs, jrc)) = unordered_join((f1,), f2, (f3,))
    return case

def unordered():
    f1()
    f2()
    f3()
    return unordered_f123()


print('Expecting 0,1,2 uniformally random')
print([unordered() for _ in range(100)])
print()


def ordered_f123():
    (case, (jargs, jkwargs, jrc)) = ordered_join((f1,), f2, (f3,))
    return case

def ordered():
    f1()
    f2()
    f3()
    return ordered_f123()


print('Expecting 0 exclusively')
print([ordered() for _ in range(100)])
print()


@puresignal
def sig():
    time.sleep(0.25)
    print('  first')
    return 'correct signal return'

print('Testing signals')
s = sig()
print('  last')
print('  signal returned: ' + s.join())
print()



@fragment
@puresignal
def fsig1(value):
    pass

@fragment
@puresignal
def fsig2(value):
    pass

@fragment
@puresignal
def fsig3(value):
    pass

def unjoining(index):
    if index == 0:  fsig1(1)
    if index == 1:  fsig2(2)
    if index == 2:  fsig3(3)
    (case, (jargs, jkwargs, jrc)) = ordered_join((fsig1,), (fsig2,), (fsig3,))
    if index != 0:  fsig1(1)
    if index != 1:  fsig2(2)
    if index != 2:  fsig3(3)
    print(' ', *jargs)
    time.sleep(0.25)

print('Testing internal unjoining and signal fragments, expecting 1,2,1')
unjoining(0)
unjoining(1)
unjoining(2)
print()


def c(value):
    print('  Not last (but often ordered): %i' % value)
    time.sleep(1)

print('Testing connurrently')
concurrently(lambda : c(0), lambda : c(1), lambda : c(2), lambda : c(3))
print('  Last (delayed c:a 1 s)')
print()


print('Testing connurrently with @signal and @puresignal')
concurrently(signal(lambda : c(0)), signal(lambda : c(1)), puresignal(lambda : c(2)), puresignal(lambda : c(3)))
print('  Last (delayed c:a 1 s)')
print()


@signal
def fsig1(value):
    pass

@signal
def fsig2(value):
    pass

@signal
def fsig3(value):
    pass

def unjoining(index):
    if index == 0:  fsig1(1)
    if index == 1:  fsig2(2)
    if index == 2:  fsig3(3)
    (case, (jargs, jkwargs, jrc)) = ordered_join((fsig1,), (fsig2,), (fsig3,))
    if index != 0:  fsig1(1)
    if index != 1:  fsig2(2)
    if index != 2:  fsig3(3)
    print(' ', *jargs)
    time.sleep(0.25)

print('Testing @signal, expecting 1,2,1')
unjoining(0)
unjoining(1)
unjoining(2)
print()


class C:
    def __init__(self, value):
        self.value = value
    
    @joinmethod
    @fragment
    def f(self, v):
        print('  %i' % (self.value + v))

f1 = C(1).f
f2 = C(2).f

print('Testing @joinmethod, expecting 11,22')
f1(10)
f2(20)
print()