Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
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"
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
32namespace lib {
33namespace meta {
34namespace test {
35
36 using std::move;
37 using std::tuple;
38 using std::string;
39 using lib::meta::dump;
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
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
89
92 }
93
94 uint
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");
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}")
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}")
143 .beforeEvent("got result"));
144 CHECK (log.verifyEvent("invoke lateBindInstance")
145 .beforeCall("ctor-move").arg("Track{23}")
146 .beforeCall("ctor-move").arg("Track{23}")
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
182
183
184
185}}} // namespace lib::meta::test
EventLog & event(string text)
log some text as event
EventLog & clear()
purge log contents while retaining just the original Header-ID
EventMatch verifyCall(string match) const
start a query to match especially a function call
EventMatch verifyEvent(string match) const
start a query to match for some event.
EventMatch ensureNot(string match) const
start a query to ensure the given expression does not match.
EventMatch & arg(ARGS const &...args)
refine filter to additionally require specific arguments
EventMatch & beforeEvent(string match)
find a match for an "event" after the current point of reference
EventMatch & afterEvent(string match)
EventMatch & beforeCall(string match)
find a match for some function invocation after the current point of reference
Automatically use custom string conversion in C++ stream output.
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
Metaprogramming tools for detecting and transforming function types.
unsigned int uint
Definition integral.hpp:29
enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition meta/util.hpp:87
std::string dump(std::tuple< TYPES... > const &tuple)
convenience function to dump a given tuple's contents.
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:387
Implementation namespace for support and library code.
int rani(uint bound=_iBOUND())
Definition random.hpp:135
Test runner and basic definitions for tests.
string join(COLL &&coll, string const &delim=", ")
enumerate a collection's contents, separated by delimiter.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
A tracking Dummy object for tests.
static lib::test::EventLog log
static constexpr int DEFUNCT
A collection of frequently used helper functions to support unit testing.
unittest helper code: test dummy objects to track instances.
Metaprogramming with tuples-of-types and the std::tuple record.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...