Lumiera  0.pre.03
»edit your freedom«
variadic-argument-picker-test.cpp
Go to the documentation of this file.
1 /*
2  VariadicArgumentPicker(Test) - access individual arguments from a variadic template
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
46 #include "lib/test/run.hpp"
48 #include "lib/format-string.hpp"
49 #include "lib/format-cout.hpp"
50 #include "lib/format-util.hpp"
51 
52 #include <utility>
53 
54 using util::_Fmt;
55 using util::join;
56 using util::toString;
57 using std::forward;
58 using std::string;
59 
60 
61 namespace lib {
62 namespace meta {
63 namespace test {
64 
65 
66  namespace { // test data
67 
68  template<int n>
69  struct N
70  {
71  uint i_;
72  static int instanceCnt;
73 
74  N (uint x = rand() % (abs(n)+1))
75  : i_{x}
76  {
77  ++instanceCnt;
78  }
79  ~N()
80  {
81  --instanceCnt;
82  }
83  N (N const& r)
84  : i_{r.i_}
85  {
86  ++instanceCnt;
87  }
88  N (N&& rr)
89  {
90  std::swap (i_, rr.i_);
91  ++instanceCnt;
92  }
93 
94  // instanceCnt remains same...
95  N& operator= (N const&) = default;
96  N& operator= (N&&) = default;
97 
98  operator string() const
99  {
100  static _Fmt format{"%s──%s─"};
101  return format % typeStr(*this) % i_;
102  }
103  friend bool operator== (N const& l, N const& r) { return l.i_ == r.i_; }
104  friend bool operator!= (N const& l, N const& r) { return l.i_ != r.i_; }
105  };
106 
107  template<int n>
108  int N<n>::instanceCnt = 0;
109 
110 
114  template<class...ARGS>
115  string
116  fun (ARGS&& ...args)
117  {
118  static _Fmt format{"%2d╎%s┤"};
119  return format % sizeof...(ARGS) % join({toString(args)...}, "┼");
120  }
121  } // (End) test data
122 
123 
124 
125 
126 
127 
128  /****************************************************************************/
134  class VariadicArgumentPicker_test : public Test
135  {
136  virtual void
137  run (Arg)
138  {
139  verify_fixture();
140  check_pickArg ();
141  check_pickInit();
142  check_reorderedArguments();
143 
144  CHECK (0 == N<0>::instanceCnt);
145  CHECK (0 == N<1>::instanceCnt);
146  CHECK (0 == N<2>::instanceCnt);
147  CHECK (0 == N<3>::instanceCnt);
148  }
149 
150 
151  void
152  verify_fixture ()
153  {
154  CHECK (0 == N<0>::instanceCnt);
155  CHECK (0 == N<1>::instanceCnt);
156  CHECK (0 == N<2>::instanceCnt);
157  CHECK (0 == N<3>::instanceCnt);
158  {
159  N<1> n1;
160  N<2> n2;
161  N<3> n3;
162  N<3> nn{n3};
163  cout << fun (n1,n2,n3,nn) << endl;
164 
165  CHECK (0 == N<0>::instanceCnt);
166  CHECK (1 == N<1>::instanceCnt);
167  CHECK (1 == N<2>::instanceCnt);
168  CHECK (2 == N<3>::instanceCnt);
169 
170  }
171  CHECK (0 == N<0>::instanceCnt);
172  CHECK (0 == N<1>::instanceCnt);
173  CHECK (0 == N<2>::instanceCnt);
174  CHECK (0 == N<3>::instanceCnt);
175  }
176 
177 
178  void
179  check_pickArg ()
180  {
181  N<1> n1;
182  N<2> n2;
183  N<3> n3;
184 
185  CHECK (n1 == pickArg<0> (n1,n2,n3));
186  CHECK (n2 == pickArg<1> (n1,n2,n3));
187  CHECK (n3 == pickArg<2> (n1,n2,n3));
188 
189  // does not compile...
190 // pickArg<3> (n1,n2,n3);
191 
192  N<0> n0{42};
193  CHECK (n0 != pickArg<0> (N<0>{23}));
194  CHECK (n0 == pickArg<0> (N<0>{n0}));
195  }
196 
197 
198  void
199  check_pickInit ()
200  {
201  N<1> n1;
202  N<2> n2;
203  using N0 = N<0>;
204 
205  CHECK (1 == (pickInit<0,int> (1,2) ));
206  CHECK (2 == (pickInit<1,int> (1,2) ));
207  CHECK (0 == (pickInit<2,int> (1,2) ));
208 
209  CHECK (n1 == (pickInit<0,N0> (n1,n2) ));
210  CHECK (n2 == (pickInit<1,N0> (n1,n2) ));
211 
212  CHECK ("N<0>" == typeStr(pickInit<3,N0> (n1,n2)));
213  CHECK ("N<0>" == typeStr(pickInit<3,N0> (1,"2")));
214  CHECK ("N<0>" == typeStr(pickInit<3,N0> ()));
215  }
216 
217 
218 
236  template<class...ARGS, size_t...idx>
237  static auto
239  {
240  return fun (pickArg<idx> (forward<ARGS>(args)...) ...);
241  }
242 
244  void
246  {
247  N<0> n0;
248  N<1> n1;
249  N<2> n2;
250  N<3> n3;
251 
252  cout << fun (n0,n1,n2,n3) << endl;
253 
254  using Backwards = typename BuildIndexSeq<4>::Descending; // 3,2,1,0
255  using Back2 = typename BuildIndexSeq<2>::Descending; // 1,0
256  using After2 = typename BuildIndexSeq<4>::After<2>; // 2,3
257 
258  cout << call_with_reversed_arguments (Backwards(), n0,n1,n2,n3) <<endl;
259  cout << call_with_reversed_arguments (Back2() , n0,n1,n2,n3) <<endl;
260  cout << call_with_reversed_arguments (After2() , n0,n1,n2,n3) <<endl;
261  }
262  };
263 
264 
266  LAUNCHER (VariadicArgumentPicker_test, "unit common");
267 
268 
269 
270 }}} // namespace lib::meta::test
Hold a sequence of index numbers as template parameters.
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:49
Front-end for printf-style string template interpolation.
bool operator==(PtrDerefIter< I1 > const &il, PtrDerefIter< I2 > const &ir)
Supporting equality comparisons...
A front-end for using printf-style formatting.
Implementation namespace for support and library code.
Simple test class runner.
std::string toString(TY const &val) noexcept
get some string representation of any object, reliably.
Definition: format-obj.hpp:200
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
static auto call_with_reversed_arguments(IndexSeq< idx... >, ARGS &&...args)
Demonstration of argument manipulation through metaprogramming.
Metaprogramming with type sequences based on variadic template parameters.
std::string typeStr(TY const *obj=nullptr) noexcept
failsafe human readable type display
Definition: meta/util.hpp:325