Lumiera  0.pre.03
»edit your freedom«
late-bind-instance-test.cpp
Go to the documentation of this file.
1 /*
2  LateBindInstance(Test) - verify rewriting of member function invocation
3 
4  Copyright (C)
5  2023, 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/meta/function.hpp"
22 #include "lib/test/test-helper.hpp"
24 #include "lib/format-cout.hpp"
25 #include "lib/format-util.hpp"
26 #include "lib/util.hpp"
27 
28 #include <tuple>
29 #include <string>
30 
31 
32 namespace lib {
33 namespace meta {
34 namespace test {
35 
36  using std::move;
37  using std::tuple;
38  using std::string;
39  using lib::meta::dump;
40  using lib::test::Tracker;
41 
42 
43 
44 
45 
46 
47  /**********************************************************************************/
55  class LateBindInstance_test : public Test
56  {
57  void
58  run (Arg)
59  {
60  seedRand();
64  }
65 
66 
67 
74  void
76  {
77  uint randomLimit = 2 + rani(98);
78 
79  auto plannedArgs
81  ,randomLimit
82  };
83 
84  // the actual instance may be available only later...
85  Test& instanceRef = *this;
86 
87  // now rewrite the argument tuple to inject the instance-ptr
88  auto preparedArgs = lateBindInstance (instanceRef, plannedArgs);
89 
90  uint res = std::apply (&LateBindInstance_test::theMember, preparedArgs);
91  CHECK (res < randomLimit);
92  }
93 
94  uint
95  theMember (uint limit)
96  {
97  return rani (limit);
98  }
99 
100 
101 
104  void
106  {
107  auto& log = Tracker::log;
108  log.clear (this);
109 
111  Tracker t1(11);
112 
113  log.event("construct tuple");
114  tuple tup(t1, marker, Tracker{23}, 55);
115 
116  Test& instanceRef = *this;
117 
118  log.event("invoke lateBindInstance");
119  auto tup1r = lateBindInstance (instanceRef, tup);
120  log.event("got result");
121 
122  // before invocation the Tracker instances are created (obviously..)
123  CHECK (log.verifyCall("ctor").arg(11)
124  .beforeCall("ctor-copy").arg("Track{11}")
125  .beforeEvent("invoke lateBindInstance"));
126  CHECK (log.verifyCall("ctor").arg(23)
127  .beforeCall("ctor-move").arg("Track{23}")
128  .beforeCall("dtor").arg(Tracker::DEFUNCT)
129  .beforeEvent("invoke lateBindInstance"));
130 
131  // but there is no copy operation after the invocation
132  CHECK (log.ensureNot("ctor-copy")
133  .afterEvent("invoke lateBindInstance"));
134 
135  // both Tracker instances are moved two times
136  // - once to create the maybeInject-invocation and
137  // - once to consolidate the result
138  // for each instance one moved-away temporary is destroyed
139  CHECK (log.verifyEvent("invoke lateBindInstance")
140  .beforeCall("ctor-move").arg("Track{11}")
141  .beforeCall("ctor-move").arg("Track{11}")
142  .beforeCall("dtor").arg(Tracker::DEFUNCT)
143  .beforeEvent("got result"));
144  CHECK (log.verifyEvent("invoke lateBindInstance")
145  .beforeCall("ctor-move").arg("Track{23}")
146  .beforeCall("ctor-move").arg("Track{23}")
147  .beforeCall("dtor").arg(Tracker::DEFUNCT)
148  .beforeEvent("got result"));
149 
150  cout << "____Tracker-Log_______________\n"
151  << util::join(Tracker::log, "\n")
152  << "\n───╼━━━━━━━━━━━╾──────────────"<<endl;
153  }
154 
155 
158  void
160  {
161  long dummy{555};
163  CHECK (dump(lateBindInstance (dummy, tuple<>{})) == "()"_expect);
164  CHECK (dump(lateBindInstance (dummy, tuple{42})) == "(42)"_expect);
165  CHECK (dump(lateBindInstance (dummy, tuple{1,2,3})) == "(1,2,3)"_expect);
166  CHECK (dump(lateBindInstance (dummy, tuple{marker,2,3})) == "(↗555,2,3)"_expect);
167  CHECK (dump(lateBindInstance (dummy, tuple{1,marker,3})) == "(1,↗555,3)"_expect);
168  CHECK (dump(lateBindInstance (dummy, tuple{1,2,marker})) == "(1,2,↗555)"_expect);
169  CHECK (dump(lateBindInstance (dummy, tuple{marker})) == "(↗555)"_expect);
170  CHECK (dump(lateBindInstance (dummy, tuple{string{"1"}
171  ,"2"
172  ,marker
173  ,tuple(2,3)
174  , 5.5
175  })) == "(1,2,↗555,«tuple<int, int>»──(2,3),5.5)"_expect);
176  }
177  };
178 
179 
181  LAUNCHER (LateBindInstance_test, "unit meta");
182 
183 
184 
185 }}} // namespace lib::meta::test
EventLog & event(string text)
log some text as event
Definition: event-log.cpp:676
constexpr auto lateBindInstance(W &instance, TUP &&invocation)
Fix-up the arguments for a member-function invocation, allowing to inject the actual this instance in...
Definition: function.hpp:318
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:40
EventMatch & beforeCall(string match)
find a match for some function invocation after the current point of reference
Definition: event-log.cpp:438
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
EventMatch & arg(ARGS const &...args)
refine filter to additionally require specific arguments
Definition: event-log.hpp:198
EventMatch verifyEvent(string match) const
start a query to match for some event.
Definition: event-log.cpp:770
Implementation namespace for support and library code.
Metaprogramming with tuples-of-types and the std::tuple record.
EventMatch verifyCall(string match) const
start a query to match especially a function call
Definition: event-log.cpp:788
Placeholder marker for a special argument position to be supplied later.
Definition: function.hpp:284
EventMatch ensureNot(string match) const
start a query to ensure the given expression does not match.
Definition: event-log.cpp:805
Metaprogramming tools for transforming functor types.
A tracking Dummy object for tests.
EventMatch & beforeEvent(string match)
find a match for an "event" after the current point of reference
Definition: event-log.cpp:417
EventLog & clear()
purge log contents while retaining just the original Header-ID
Definition: event-log.cpp:643
Simplistic test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
unittest helper code: test dummy objects to track instances.
A collection of frequently used helper functions to support unit testing.
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
std::string dump(std::tuple< TYPES... > const &tuple)
convenience function to dump a given tuple&#39;s contents.