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)
5  2010, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
19 #include "lib/test/run.hpp"
20 #include "common/advice.hpp"
21 
22 
23 namespace lumiera {
24 namespace advice {
25 namespace test {
26 
27  namespace { // Some test classes using the advice system...
28 
29 
30  class TheAdvised
31  : private advice::Request<int>
32  {
33  public:
34  TheAdvised (Literal topic =0)
35  {
36  rebind (topic);
37  }
38 
39  void
40  rebind (Literal topic)
41  {
42  defineBinding (topic);
43  }
44 
45  bool
46  got(int val)
47  {
48  return val == getAdvice();
49  }
50  };
51 
52 
53  class TheAdvisor
54  {
56 
57  public:
58  TheAdvisor (Literal topic =0)
59  {
60  rebind (topic);
61  }
62 
63  void
64  rebind (Literal topic)
65  {
66  link_.defineBinding (topic);
67  }
68 
69  void
70  publish (int val)
71  {
72  link_.setAdvice (val);
73  }
74 
75  void
76  clear()
77  {
78  link_.retractAdvice();
79  }
80  };
81  }
82 
83 
84 
85  /***************************************************************************/
99  class AdviceBasics_test : public Test
100  {
101 
102  virtual void
103  run (Arg)
104  {
105  seedRand();
106 
107  simpleExchange();
108  createCollaboration();
109  overwriting_and_retracting();
110  }
111 
112 
115  void
117  {
118  TheAdvised client; // implicitly opens an request-for-advice
119  CHECK (client.got (0)); // no advice yet --> getting the default int()
120 
121  TheAdvisor server; // implicitly prepares an advice provision
122  CHECK (client.got (0)); // but as no advice was provided yet, nothing happens
123 
124  int rr{ 1 + rani(1000)};
125 
126  server.publish (rr); // now an match is detected, creating an advice channel
127  CHECK (client.got (rr)); // ..so the client can pick up the provided advice value
128  }
129 
130 
132  void
134  {
135  TheAdvised client1 ("topic1()");
136  TheAdvisor server2 ("topic2()");
137 
138  int r1{ 1 + rani(1000)};
139  int r2{ 1 + rani(1000)};
140 
141  server2.publish (r2);
142  CHECK (client1.got(0));
143 
144  TheAdvised client2 ("topic2()");
145  CHECK (client2.got(r2));
146 
147  TheAdvisor server1;
148  CHECK (client1.got(0));
149 
150  server1.publish (r1);
151  CHECK (client1.got(0));
152  CHECK (client2.got(r2));
153 
154  server1.rebind ("topic1()");
155  CHECK (client1.got(r1));
156  CHECK (client2.got(r2));
157  }
158 
159 
169  void
171  {
172  TheAdvised client1 ("slot1");
173  TheAdvised client2 ("slot2");
174  CHECK (client1.got(0));
175  CHECK (client2.got(0));
176 
177  int r1{ 1 + rani(1000)};
178  int r2{ 1 + rani(1000)};
179 
180  {
181  TheAdvisor server("slot1()");
182  CHECK (client1.got(0));
183  CHECK (client2.got(0));
184 
185  server.publish (r1);
186  CHECK (client1.got(r1));
187  CHECK (client2.got(0));
188 
189  server.publish (r2);
190  CHECK (client1.got(r2));
191  CHECK (client2.got(0));
192 
193  server.rebind("slot2()");
194  CHECK (client1.got(0));
195  CHECK (client2.got(r2));
196  }
197 
198  CHECK (client1.got(0));
199  CHECK (client2.got(r2));
200 
201  {
202  TheAdvisor anotherServer("slot1");
203  CHECK (client1.got(0));
204  CHECK (client2.got(r2));
205 
206  anotherServer.publish (r1);
207  CHECK (client1.got(r1));
208  CHECK (client2.got(r2));
209  }
210 
211  CHECK (client1.got(r1));
212  CHECK (client2.got(r2));
213 
214  {
215  TheAdvisor yetAnotherServer("slot2");
216  CHECK (client1.got(r1));
217  CHECK (client2.got(r2));
218 
219  yetAnotherServer.publish (r1);
220  CHECK (client1.got(r1));
221  CHECK (client2.got(r1));
222 
223  yetAnotherServer.rebind("slot1");
224  CHECK (client1.got(r1));
225  CHECK (client2.got(r2)); // ideally it should be 0, but actually we uncover the old provision
226  // the decision was to err for a simple implementation /////////TICKET #623
227  yetAnotherServer.clear();
228  CHECK (client1.got(r1)); // should be 0, but again the existing provision is uncovered
229  CHECK (client2.got(r2)); // should be 0
230 
231  yetAnotherServer.rebind("slot2"); // no effect, because it doesn't provide advice anymore
232  CHECK (client1.got(r1));
233  CHECK (client2.got(r2));
234 
235  yetAnotherServer.publish (5);
236  CHECK (client1.got(r1));
237  CHECK (client2.got(5));
238  }
239 
240  CHECK (client1.got(r1));
241  CHECK (client2.got(5));
242 
243  client1.rebind("slot2");
244  CHECK (client1.got(5));
245  CHECK (client2.got(5));
246 
247  client2.rebind("nonExistingSlot");
248  CHECK (client1.got(5));
249  CHECK (client2.got(0));
250  }
251  };
252 
253 
254 
256  LAUNCHER (AdviceBasics_test, "unit common");
257 
258 
259 }}} // namespace lumiera::advice::test
Access point for the advised entity (client).
Definition: advice.hpp:428
Definition: run.hpp:40
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:76
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
Expecting Advice and giving Advice: a cross-cutting collaboration of loosely coupled participants...
Simplistic test class runner.
Lumiera public interface.
Definition: advice.cpp:104