aboutsummaryrefslogtreecommitdiffstats
path: root/src/test.py
blob: 0dd65dc0010bcdfbf42fb3f66e00cb5031f184ba (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
#!/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()


@signal
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
@signal
def fsig1(value):
    pass

@fragment
@signal
def fsig2(value):
    pass

@fragment
@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 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)')