Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
tuple-closure-test.cpp
Go to the documentation of this file.
1/*
2 TupleClosure(Test) - appending, mixing and filtering typelists
3
4 Copyright (C)
5 2025, 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
14
24#include "lib/test/run.hpp"
27#include "lib/format-util.hpp"
28
29
30namespace lib {
31namespace meta {
32namespace test {
33
34 using std::string;
35 using std::tuple;
36 using std::array;
37 using std::make_tuple;
38 using lib::meta::_Fun;
40
41
42
43
44 /*********************************************************************/
50 class TupleClosure_test : public Test
51 {
52 virtual void
62
63
65 void
67 {
68 using Tup = tuple<int,double,string>;
69 using Builder = TupleClosureBuilder<Tup>;
70
71 auto cons = Builder::closeFront (1,2.3);
72 using FunType = _Fun<decltype(cons)>;
73 CHECK (FunType() == true); // indeed a function
74 CHECK (showType<FunType::Sig>() == "tuple<int, double, string> (tuple<string>)"_expect);
75
76 Tup tup = cons("five");
77 CHECK (tup == "«tuple<int, double, string>»──(1,2.3,five)"_expect);
78 }
79
80
82 void
84 {
85 using Tup = tuple<int,double,string>;
86 using Builder = TupleClosureBuilder<Tup>;
87
88 auto c1 = Builder::closeBack("π");
89 CHECK (showType<_Fun<decltype(c1)>::Sig>() == "tuple<int, double, string> (tuple<int, double>)"_expect);
90
91 Tup t1 = c1(make_tuple (2,3.1415));
92 CHECK (t1 == "«tuple<int, double, string>»──(2,3.1415,π)"_expect);
93 auto c2 = Builder::closeBack(3.14159265,"pi");
94 CHECK (showType<_Fun<decltype(c2)>::Sig>() == "tuple<int, double, string> (tuple<int>)"_expect);
95
96 Tup t2 = c2(make_tuple (-1));
97 CHECK (t2 == "«tuple<int, double, string>»──(-1,3.1415927,pi)"_expect);
98 }
99
100
102 void
104 {
105 using Tup = tuple<int,double,string>;
106 using Builder = TupleClosureBuilder<Tup>;
107
108 auto c1 = Builder::close<1>(3.1415927);
109 CHECK (showType<_Fun<decltype(c1)>::Sig>() == "tuple<int, double, string> (tuple<int, string>)"_expect);
110
111 Tup t1 = c1({2,"π"});
112 CHECK (t1 == "«tuple<int, double, string>»──(2,3.1415927,π)"_expect);
113
114 auto c2 = Builder::close<3>("fantastic");
115 // Binding to out-of-scope arguments is ignored: the result is the identity-function
116 CHECK (showType<_Fun<decltype(c2)>::Sig>() == "tuple<int, double, string> (tuple<int, double, string>)"_expect);
117 }
118
119
121 void
123 {
124 using Arr = array<int,5>;
125 using Builder = TupleClosureBuilder<Arr>;
126
127 auto cons = Builder::closeFront (1u,2.3);
128 CHECK (showType<_Fun<decltype(cons)>::Sig>() == "ArrayAdapt<int, int, int, int, int> (ArrayAdapt<int, int, int>)"_expect);
129
130 Arr arr = cons({3,4,5});
131 CHECK (arr == "[1, 2, 3, 4, 5]"_expect);
132 }
133
134
136 void
138 {
139 using Arr = array<int,5>;
140 using Builder = TupleClosureBuilder<Arr>;
141
142 auto cons = Builder::close<3>(55);
143 CHECK (showType<_Fun<decltype(cons)>::Sig>() == "ArrayAdapt<int, int, int, int, int> (ArrayAdapt<int, int, int, int>)"_expect);
144
145 Arr arr = cons(array{1,2,3,4});
146 CHECK (arr == "[1, 2, 3, 55, 4]"_expect);
147 }
148
149
150
155 void
157 {
158 // can be constructed from aggregate
159 ArrayAdapt arr{1,2,3,4,5};
160 CHECK (arr.size() == 5);
161
162 // picks up a tuple-like type signature
163 using AA = decltype(arr);
164 CHECK (showType<AA>() == "ArrayAdapt<int, int, int, int, int>"_expect );
166
167 // can use subscript operator from underlying array
168 CHECK (arr[0] == 1);
169 CHECK (arr[2] == 3);
170 CHECK (arr[4] == 5);
171 // can use the tuple-like binding defined for array
172 CHECK (std::get<0>(arr) == 1);
173 CHECK (std::get<2>(arr) == 3);
174 CHECK (std::get<4>(arr) == 5);
175
176 // supports structured bindings
177 auto& [v1,v2,v3,v4,v5] = arr;
178 CHECK (v3 == 3);
179 v3 = 33;
180 CHECK (arr[2] == 33);
181
182 // can copy-assign from std::array
183 arr = array{5,4,3,2,1};
184 CHECK (arr[0] == 5);
185 CHECK (arr[4] == 1);
186
187 // can copy/move-construct from std::array
188 AA axx{array{-1,-2,-3,-4,-5}};
189 CHECK (axx[0] == -1);
190 CHECK (axx[2] == -3);
191 CHECK (axx[4] == -5);
192 }
193 };
194
195
197 LAUNCHER (TupleClosure_test, "unit common");
198
199
200
201}}} // namespace lib::meta::test
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition meta/util.hpp:87
Metaprogramming adapter to overlay a tuple-like signature on top of std::array, with N times the same...
string showType()
diagnostic type output, including const and similar adornments
Implementation namespace for support and library code.
Test runner and basic definitions for tests.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
Trait template for uniform access to function signature types.
Definition function.hpp:144
A collection of frequently used helper functions to support unit testing.
Partial binding for construction of tuple-like records.