Lumiera  0.pre.03
»edit your freedom«
advice-basics-test.cpp
Go to the documentation of this file.
1 /*
2  AdviceBasics(Test) - basic behaviour of the Advice collaboration
3 
4  Copyright (C) Lumiera.org
5  2010, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "common/advice.hpp"
30 
31 #include <cstdlib>
32 
33 using std::rand;
34 
35 
36 
37 namespace lumiera {
38 namespace advice {
39 namespace test {
40 
41  namespace { // Some test classes using the advice system...
42 
43 
44  class TheAdvised
45  : private advice::Request<int>
46  {
47  public:
48  TheAdvised (Literal topic =0)
49  {
50  rebind (topic);
51  }
52 
53  void
54  rebind (Literal topic)
55  {
56  defineBinding (topic);
57  }
58 
59  bool
60  got(int val)
61  {
62  return val == getAdvice();
63  }
64  };
65 
66 
67  class TheAdvisor
68  {
70 
71  public:
72  TheAdvisor (Literal topic =0)
73  {
74  rebind (topic);
75  }
76 
77  void
78  rebind (Literal topic)
79  {
80  link_.defineBinding (topic);
81  }
82 
83  void
84  publish (int val)
85  {
86  link_.setAdvice (val);
87  }
88 
89  void
90  clear()
91  {
92  link_.retractAdvice();
93  }
94  };
95  }
96 
97 
98 
99  /***************************************************************************/
113  class AdviceBasics_test : public Test
114  {
115 
116  virtual void
117  run (Arg)
118  {
119  simpleExchange();
120  createCollaboration();
121  overwriting_and_retracting();
122  }
123 
124 
127  void
129  {
130  TheAdvised client; // implicitly opens an request-for-advice
131  CHECK (client.got (0)); // no advice yet --> getting the default int()
132 
133  TheAdvisor server; // implicitly prepares an advice provision
134  CHECK (client.got (0)); // but as no advice was provided yet, nothing happens
135 
136  int rr (1 + (rand() % 1000));
137 
138  server.publish (rr); // now an match is detected, creating an advice channel
139  CHECK (client.got (rr)); // ..so the client can pick up the provided advice value
140  }
141 
142 
144  void
146  {
147  TheAdvised client1 ("topic1()");
148  TheAdvisor server2 ("topic2()");
149 
150  int r1 (1 + (rand() % 1000));
151  int r2 (1 + (rand() % 1000));
152 
153  server2.publish (r2);
154  CHECK (client1.got(0));
155 
156  TheAdvised client2 ("topic2()");
157  CHECK (client2.got(r2));
158 
159  TheAdvisor server1;
160  CHECK (client1.got(0));
161 
162  server1.publish (r1);
163  CHECK (client1.got(0));
164  CHECK (client2.got(r2));
165 
166  server1.rebind ("topic1()");
167  CHECK (client1.got(r1));
168  CHECK (client2.got(r2));
169  }
170 
171 
181  void
183  {
184  TheAdvised client1 ("slot1");
185  TheAdvised client2 ("slot2");
186  CHECK (client1.got(0));
187  CHECK (client2.got(0));
188 
189  int r1 (1 + (rand() % 1000));
190  int r2 (1 + (rand() % 1000));
191 
192  {
193  TheAdvisor server("slot1()");
194  CHECK (client1.got(0));
195  CHECK (client2.got(0));
196 
197  server.publish (r1);
198  CHECK (client1.got(r1));
199  CHECK (client2.got(0));
200 
201  server.publish (r2);
202  CHECK (client1.got(r2));
203  CHECK (client2.got(0));
204 
205  server.rebind("slot2()");
206  CHECK (client1.got(0));
207  CHECK (client2.got(r2));
208  }
209 
210  CHECK (client1.got(0));
211  CHECK (client2.got(r2));
212 
213  {
214  TheAdvisor anotherServer("slot1");
215  CHECK (client1.got(0));
216  CHECK (client2.got(r2));
217 
218  anotherServer.publish (r1);
219  CHECK (client1.got(r1));
220  CHECK (client2.got(r2));
221  }
222 
223  CHECK (client1.got(r1));
224  CHECK (client2.got(r2));
225 
226  {
227  TheAdvisor yetAnotherServer("slot2");
228  CHECK (client1.got(r1));
229  CHECK (client2.got(r2));
230 
231  yetAnotherServer.publish (r1);
232  CHECK (client1.got(r1));
233  CHECK (client2.got(r1));
234 
235  yetAnotherServer.rebind("slot1");
236  CHECK (client1.got(r1));
237  CHECK (client2.got(r2)); // ideally it should be 0, but actually we uncover the old provision
238  // the decision was to err for a simple implementation /////////TICKET #623
239  yetAnotherServer.clear();
240  CHECK (client1.got(r1)); // should be 0, but again the existing provision is uncovered
241  CHECK (client2.got(r2)); // should be 0
242 
243  yetAnotherServer.rebind("slot2"); // no effect, because it doesn't provide advice anymore
244  CHECK (client1.got(r1));
245  CHECK (client2.got(r2));
246 
247  yetAnotherServer.publish (5);
248  CHECK (client1.got(r1));
249  CHECK (client2.got(5));
250  }
251 
252  CHECK (client1.got(r1));
253  CHECK (client2.got(5));
254 
255  client1.rebind("slot2");
256  CHECK (client1.got(5));
257  CHECK (client2.got(5));
258 
259  client2.rebind("nonExistingSlot");
260  CHECK (client1.got(5));
261  CHECK (client2.got(0));
262  }
263  };
264 
265 
266 
268  LAUNCHER (AdviceBasics_test, "unit common");
269 
270 
271 }}} // namespace lumiera::advice::test
Access point for the advised entity (client).
Definition: advice.hpp:437
Definition: run.hpp:49
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:85
Expecting Advice and giving Advice: a cross-cutting collaboration of loosely coupled participants...
Simple test class runner.
Lumiera public interface.
Definition: advice.cpp:113