Lumiera  0.pre.03
»edit your freedom«
w-link-test.cpp
Go to the documentation of this file.
1 /*
2  WLink(Test) - verify managed link to sigc::trackable widget
3 
4  Copyright (C)
5  2018, 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 "lib/test/test-helper.hpp"
21 #include "stage/model/w-link.hpp"
22 #include "lib/util.hpp"
23 
24 #include <utility>
25 #include <memory>
26 
27 
28 using util::isSameObject;
29 using std::make_unique;
30 using std::move;
31 
32 
33 namespace stage {
34 namespace model{
35 namespace test {
36 
37 
38  namespace { // Test fixture...
39 
40  template<typename X>
41  struct DummyWidget
42  : public sigc::trackable
43  {
44  X val = 1 + rani(100);
45  };
46  }
47 
48 
49 
50 
51  /******************************************************************************/
55  class WLink_test : public Test
56  {
57 
58  virtual void
59  run (Arg)
60  {
61  seedRand();
62  verify_standardUsage();
63  verify_reconnect();
64  verify_copy();
65  }
66 
67 
71  void
73  {
74  using Wint = DummyWidget<int>;
75  auto widget = make_unique<Wint>();
76  int r = widget->val;
77 
78  WLink<Wint> link{*widget};
79  CHECK (link);
80  link->val += 23;
81  CHECK (r+23 == widget->val);
82 
83  // kill widget
84  widget.reset();
85  CHECK (not link);
86  VERIFY_ERROR (BOTTOM_VALUE, link->val );
87  }
88 
89 
91  void
93  {
94  using Wint = DummyWidget<int>;
95 
96  auto w1 = make_unique<Wint>();
97  auto w2 = make_unique<Wint>();
98  int r1 = w1->val;
99  int r2 = w2->val;
100 
101  WLink<Wint> l1;
102  WLink<Wint> l2{*w1};
103  CHECK (not l1);
104  CHECK ( l2);
105 
106  l2->val = r1;
107  l1.connect(*l2);
108  ++l1->val;
109  CHECK (w1->val == r1+1);
110  CHECK (isSameObject (*l1, *l2));
111 
112  l2.connect(*w2);
113  CHECK (not isSameObject (*l1, *l2));
114  w2->val = r2;
115  CHECK (r1+1 == l1->val);
116  CHECK (r2 == l2->val);
117 
118  w1.reset(); // kill first widget
119  CHECK (not l1);
120  CHECK ( l2);
121  l2->val *= -10;
122  l2.clear();
123  CHECK (not l1);
124  CHECK (not l2);
125  CHECK (-10*r2 == w2->val);
126  l1.connect(*w2);
127  l2.connect(*l1);
128  CHECK (-10*r2 == l2->val);
129  CHECK (isSameObject (*l1, *l2));
130  CHECK (isSameObject (*l1, *w2));
131 
132  // implicitly kill second widget
133  *w2 = Wint{};
134  CHECK (not l1);
135  CHECK (not l2);
136  }
137 
138 
140  void
142  {
143  using Wint = DummyWidget<int>;
144  auto w1 = make_unique<Wint>();
145  auto w2 = make_unique<Wint>();
146 
147  WLink<Wint> l1;
148  WLink<Wint> l2{l1};
149  CHECK (not l2);
150  l2.connect(*w1);
151 
152  WLink<Wint> l3{l2};
153  CHECK (l3);
154  CHECK (w1->val == l3->val);
155 
156  CHECK (not l1); // they are statefull and independent
157  l1 = move(l2);
158  CHECK (not l2);
159  CHECK (l1);
160  CHECK (isSameObject (*l1, *l3));
161 
162  l2 = WLink<Wint>{WLink<Wint>{*w2}};
163  CHECK (w2->val == l2->val);
164 
165  l1 = l3;
166  CHECK (w1->val == l1->val);
167  WLink<Wint>& ref{l1};
168  l1 = move(ref);
169  CHECK (w1->val == l1->val);
170  CHECK (w1->val == l3->val);
171 
172  swap (l2, l3);
173  CHECK (w1->val == l1->val);
174  CHECK (w1->val == l2->val);
175  CHECK (w2->val == l3->val);
176 
177  w1.reset();
178  CHECK (not l1);
179  CHECK (not l2);
180  CHECK (w2->val == l3->val);
181 
182  using Wuint = DummyWidget<uint>;
183  auto uu = make_unique<Wuint>();
184  WLink<Wuint> lu{*uu};
185 
187 // l1 = uu;
188 // l1.connect(*uu);
189 
190  // But it is a compile time check...
191  // At runtime, only the bare pointer is managed
192  l1 = reinterpret_cast<WLink<Wint>&&> (lu);
193  CHECK ((int)uu->val == l1->val);
194  CHECK (not lu); // assignment was actually a move
195 
196  // even the subversively attached link is managed properly
197  uu.reset();
198  CHECK (not l1);
199 
200  // others unaffected...
201  CHECK (not l2);
202  CHECK (l3);
203  }
204  };
205 
206 
208  LAUNCHER (WLink_test, "unit stage");
209 
210 
211 }}} // namespace stage::model::test
Definition: run.hpp:40
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Simplistic test class runner.
Lumiera GTK UI implementation root.
Definition: guifacade.cpp:37
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
A collection of frequently used helper functions to support unit testing.
bool isSameObject(A const &a, B const &b)
compare plain object identity, based directly on the referee&#39;s memory identities. ...
Definition: util.hpp:421