@@ -27,211 +27,4 @@ where
27
27
let ( vm_, res1) = expr1 ( vm) ;
28
28
let ( vm__, res2) = expr2 ( vm_) ;
29
29
( vm__, comb ( res1, res2) )
30
- }
31
-
32
- #[ cfg( test) ]
33
- mod test {
34
- use std:: any:: Any ;
35
- use std:: collections:: HashMap ;
36
- use crate :: core:: context:: context:: Context ;
37
- use crate :: core:: export:: export:: Export ;
38
- use crate :: core:: lang:: execution:: round;
39
- use crate :: core:: lang:: lang:: { branch, foldhood, mid, nbr, rep} ;
40
- use crate :: core:: path:: path:: path:: Path ;
41
- use crate :: core:: path:: slot:: slot:: Slot :: { FoldHood , Nbr , Rep } ;
42
- use crate :: core:: vm:: round_vm:: round_vm:: RoundVM ;
43
- use crate :: export;
44
- use crate :: path;
45
-
46
- fn init_vm ( ) -> RoundVM {
47
- let context = Context :: new ( 0 , Default :: default ( ) , Default :: default ( ) , Default :: default ( ) ) ;
48
- let mut vm = RoundVM :: new ( context) ;
49
- vm. export_stack . push ( Export :: new ( ) ) ;
50
- vm
51
- }
52
-
53
- fn init_with_ctx ( ctx : Context ) -> RoundVM {
54
- let mut vm = RoundVM :: new ( ctx) ;
55
- vm. export_stack . push ( Export :: new ( ) ) ;
56
- vm
57
- }
58
-
59
- fn push_to_ctx < A : Copy + ' static > ( mut ctx : Context , path : Path , val : A ) -> Context {
60
- let mut export = Export :: new ( ) ;
61
- export. put ( path, || val) ;
62
- ctx. put_export ( ctx. self_id , export) ;
63
- ctx
64
- }
65
-
66
- #[ test]
67
- fn test_multiple_rounds ( ) {
68
- //create the vm
69
- let vm = init_vm ( ) ;
70
- //write the aggregate program
71
- let program = |vm1| rep ( vm1, || 0 , |vm2, a| {
72
- let ( avm, res) = nbr ( vm2, |_vm| ( _vm, a) ) ;
73
- ( avm, res + 1 )
74
- } ) ;
75
- //first round
76
- let ( vm_, res) = round ( vm, program) ;
77
- assert_eq ! ( 1 , res) ;
78
- //add to the context the result of the previous round
79
- let ctx_ = push_to_ctx ( vm_. context , Path :: from ( vec ! [ Rep ( 0 ) ] ) , res) ;
80
- //second round
81
- let ( _vm__, res_) = round ( init_with_ctx ( ctx_) , program) ;
82
- assert_eq ! ( 2 , res_) ;
83
- }
84
-
85
- #[ test]
86
- fn test_local_value ( ) {
87
- let context = Context :: new ( 0 , Default :: default ( ) , Default :: default ( ) , Default :: default ( ) ) ;
88
- let result = round ( init_with_ctx ( context) , |vm| ( vm, 10 ) ) ;
89
- assert_eq ! ( 10 , result. 1 ) ;
90
- }
91
-
92
- #[ test]
93
- fn test_alignment ( ) {
94
- // No neighbor is aligned
95
- let context = Context :: new ( 0 , Default :: default ( ) , Default :: default ( ) , Default :: default ( ) ) ;
96
- // Program: rep(0, foldhood(0)(_ + _)(1))
97
- let program = |vm1| rep ( vm1,
98
- || 0 ,
99
- |vm2, _| { foldhood ( vm2,
100
- || 0 ,
101
- | a, b | ( a + b) ,
102
- |vm3| ( vm3, 1 ) ) } ) ;
103
- let result = round ( init_with_ctx ( context) , program) ;
104
- assert_eq ! ( 1 , result. 1 ) ;
105
-
106
- // One neighbor is aligned
107
- // Export: Map(1 -> Export(Rep(0) -> 1, Rep(0) / FoldHood(0) -> 1))
108
- let export_dev_1 = export ! ( ( path!( Rep ( 0 ) ) , 1 ) , ( path!( FoldHood ( 0 ) , Rep ( 0 ) ) , 1 ) ) ;
109
- let mut exports: HashMap < i32 , Export > = HashMap :: new ( ) ;
110
- exports. insert ( 1 , export_dev_1) ;
111
- let context = Context :: new ( 0 , Default :: default ( ) , Default :: default ( ) , exports) ;
112
- let vm = init_with_ctx ( context) ;
113
- let result = round ( vm, program) ;
114
- assert_eq ! ( 2 , result. 1 ) ;
115
- }
116
-
117
- #[ test]
118
- fn test_foldhood_basic ( ) {
119
- // Export of device 2: Export(/ -> 1, FoldHood(0) -> 1)
120
- let export_dev_2 = export ! ( ( path!( ) , 1 ) , ( path!( FoldHood ( 0 ) ) , 1 ) ) ;
121
- // Export of device 4: Export(/ -> 3, FoldHood(0) -> 3)
122
- let export_dev_4 = export ! ( ( path!( ) , 3 ) , ( path!( FoldHood ( 0 ) ) , 3 ) ) ;
123
- // Exports of the context: Map(2 -> Export(/ -> 1, FoldHood(0) -> 1), 4 -> Export(/ -> 3, FoldHood(0) -> 3))
124
- let mut exports: HashMap < i32 , Export > = HashMap :: new ( ) ;
125
- exports. insert ( 2 , export_dev_2) ;
126
- exports. insert ( 4 , export_dev_4) ;
127
- let context = Context :: new ( 0 , Default :: default ( ) , Default :: default ( ) , exports) ;
128
- // Program: foldhood(1)(_ + _)(2)
129
- let program = |vm| foldhood ( vm,
130
- || 1 ,
131
- | a, b| ( a + b) ,
132
- |vm1| ( vm1, 2 ) ) ;
133
- let result = round ( init_with_ctx ( context) , program) ;
134
- assert_eq ! ( 7 , result. 1 ) ;
135
- }
136
-
137
- #[ test]
138
- fn test_foldhood_advanced ( ) {
139
- // Export of device 2: Export(/ -> "1", FoldHood(0) -> "1", FoldHood(0) / Nbr(0) -> 4)
140
- let export_dev_2 = export ! ( ( path!( ) , 1 ) , ( path!( FoldHood ( 0 ) ) , 1 ) , ( path!( Nbr ( 0 ) , FoldHood ( 0 ) ) , 4 ) ) ;
141
- // Export of device 4: Export(/ -> "3", FoldHood(0) -> "3")
142
- let export_dev_4 = export ! ( ( path!( ) , 3 ) , ( path!( FoldHood ( 0 ) ) , 3 ) , ( path!( Nbr ( 0 ) , FoldHood ( 0 ) ) , 19 ) ) ;
143
- let mut exports: HashMap < i32 , Export > = HashMap :: new ( ) ;
144
- exports. insert ( 2 , export_dev_2) ;
145
- exports. insert ( 4 , export_dev_4) ;
146
- let context = Context :: new ( 0 , Default :: default ( ) , Default :: default ( ) , exports) ;
147
- // Program: foldhood(-5)(_ + _)(nbr(2))
148
- let program = |vm| foldhood ( vm,
149
- || -5 ,
150
- | a, b| ( a + b) ,
151
- |vm1| nbr ( vm1, |vm2| ( vm2, 2 ) ) ) ;
152
- let result = round ( init_with_ctx ( context) , program) ;
153
- assert_eq ! ( 20 , result. 1 ) ;
154
- }
155
-
156
- #[ test]
157
- fn test_nbr ( ) {
158
- // 1 - NBR needs not to be nested into fold
159
- let context = Context :: new ( 0 , Default :: default ( ) , Default :: default ( ) , Default :: default ( ) ) ;
160
- let result = round ( init_with_ctx ( context) , |vm| nbr ( vm, |vm1| ( vm1, 7 ) ) ) ;
161
- assert_eq ! ( 7 , result. 1 ) ;
162
-
163
- // 2 - NBR should support interaction between aligned devices
164
- let context = Context :: new ( 0 , Default :: default ( ) , Default :: default ( ) , create_exports_nbr_test ( ) ) ;
165
- // Program: foldhood(0)(_ + _)(if (nbr(mid()) == mid()) 0 else 1)
166
- let program = |vm| foldhood ( vm,
167
- || 0 ,
168
- | a, b| ( a + b) ,
169
- |vm1| {
170
- let ( vm2, res) = nbr ( vm1, |vm3| mid ( vm3) ) ;
171
- if res == vm2. self_id ( ) { ( vm2, 0 ) } else { ( vm2, 1 ) }
172
- } ) ;
173
- let result = round ( init_with_ctx ( context) , program) ;
174
- assert_eq ! ( 2 , result. 1 ) ;
175
- }
176
-
177
- fn create_exports_nbr_test ( ) -> HashMap < i32 , Export > {
178
- // Create this export: Map(
179
- // 1 -> Export(/ -> "any", FoldHood(0) -> 1, FoldHood(0) / Nbr(0) -> 1),
180
- // 2 -> Export(/ -> "any", FoldHood(0) -> 2, FoldHood(0) / Nbr(0) -> 2)
181
- // )
182
- let export_dev_1 = export ! ( ( path!( ) , "any" ) , ( path!( FoldHood ( 0 ) ) , 1 ) , ( path!( Nbr ( 0 ) , FoldHood ( 0 ) ) , 1 ) ) ;
183
- let export_dev_2 = export ! ( ( path!( ) , "any" ) , ( path!( FoldHood ( 0 ) ) , 2 ) , ( path!( Nbr ( 0 ) , FoldHood ( 0 ) ) , 2 ) ) ;
184
- let mut exports: HashMap < i32 , Export > = HashMap :: new ( ) ;
185
- exports. insert ( 1 , export_dev_1) ;
186
- exports. insert ( 2 , export_dev_2) ;
187
- exports
188
- }
189
-
190
- #[ test]
191
- // Rep should support dynamic evolution of fields
192
- fn test_rep ( ) {
193
- let context = Context :: new ( 0 , Default :: default ( ) , Default :: default ( ) , Default :: default ( ) ) ;
194
- // Program: rep(9)(_ * 2)
195
- let program = |vm| rep ( vm, || 9 , |vm1, a| ( vm1, a * 2 ) ) ;
196
- // Check if rep use the initial value
197
- let result = round ( init_with_ctx ( context) , program) ;
198
- assert_eq ! ( 18 , result. 1 ) ;
199
-
200
- // Export: Map(0 -> Export(Rep(0) -> 7))
201
- let export_dev_0= export ! ( ( path!( Rep ( 0 ) ) , 7 ) ) ;
202
- let mut exports: HashMap < i32 , Export > = HashMap :: new ( ) ;
203
- exports. insert ( 0 , export_dev_0) ;
204
- let context = Context :: new ( 0 , Default :: default ( ) , Default :: default ( ) , exports) ;
205
- // Rep should build upon previous state.
206
- let result = round ( init_with_ctx ( context) , program) ;
207
- assert_eq ! ( 14 , result. 1 ) ;
208
- }
209
-
210
- #[ test]
211
- // Branch should support domain restriction, thus affecting the structure of exports
212
- fn test_branch ( ) {
213
- // Program: rep(0) { x => branch(x % 2 == 0)(7)(rep(4)(_ => 4)); x + 1 }
214
- let program =
215
- |vm| rep ( vm,
216
- || 0 ,
217
- |vm1, x| { let res = branch ( vm1,
218
- || x % 2 == 0 ,
219
- |vm3| ( vm3, 7 ) ,
220
- |vm4| ( rep ( vm4,
221
- || 4 ,
222
- |vm5, _| ( vm5, 4 ) ) ) ) ;
223
- return ( res. 0 , x + 1 )
224
- } ) ;
225
- let context = Context :: new ( 0 , Default :: default ( ) , Default :: default ( ) , Default :: default ( ) ) ;
226
- let result = round ( init_with_ctx ( context) , program) ;
227
- assert_eq ! ( 1 , result. 1 ) ;
228
-
229
- // Export: Map(0 -> Export(Rep(0) -> 1))
230
- let export_dev_0= export ! ( ( path!( Rep ( 0 ) ) , 1 ) ) ;
231
- let mut exports: HashMap < i32 , Export > = HashMap :: new ( ) ;
232
- exports. insert ( 0 , export_dev_0) ;
233
- let context = Context :: new ( 0 , Default :: default ( ) , Default :: default ( ) , exports) ;
234
- let result = round ( init_with_ctx ( context) , program) ;
235
- assert_eq ! ( 2 , result. 1 ) ;
236
- }
237
30
}
0 commit comments