Lumiera  0.pre.03
»edit your freedom«
format-helper-test.cpp
Go to the documentation of this file.
1 /*
2  FormatHelper(Test) - validate formatting and diagnostics helpers
3 
4  Copyright (C) Lumiera.org
5  2009, 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 
28 #include "lib/test/run.hpp"
29 #include "lib/format-util.hpp"
30 #include "lib/format-string.hpp"
31 #include "lib/iter-adapter-stl.hpp"
32 #include "lib/test/test-helper.hpp"
33 #include "lib/error.hpp"
34 
35 #include <vector>
36 #include <string>
37 
38 
42 using lib::eachNum;
43 using util::_Fmt;
44 
45 using std::vector;
46 using std::string;
47 using std::to_string;
48 
49 
50 namespace util {
51 namespace test {
52 
53  namespace { // test fixture...
54 
55  class Reticent
56  { };
57 
58  class UnReticent
59  : public Reticent
60  {
61  public:
62  operator string() const { return "hey Joe!"; }
63  };
64 
65 
66 
68  {
69  static uint cnt;
70 
71  uint id_;
72  double d_;
73 
74  public:
75  AutoCounter(double d)
76  : id_(++cnt)
77  , d_(d*2)
78  { }
79 
80  operator string() const
81  {
82  return _Fmt("Nr.%02d(%3.1f)") % id_ % d_;
83  }
84  };
85  uint AutoCounter::cnt = 0;
86  }
87 
88 
89 
90  /***************************************************************************/
99  : public Test
100  {
101  void
102  run (Arg)
103  {
104  check2String();
105  checkStringify();
106  checkStringJoin();
107  checkPrefixSuffix();
108  }
109 
110 
112  void
114  {
115  Reticent closeLipped;
116  UnReticent chatterer;
117 
118  CHECK (toString (closeLipped) == "«Reticent»"_expect);
119  CHECK (toString (chatterer) == "hey Joe!"_expect);
120 
121  CHECK (toString (&chatterer) == "↗hey Joe!"_expect); // pointer indicated
122  CHECK (toString (nullptr) == "↯"_expect); // runtime exception, caught
123 
124  CHECK (toString (true) == "true"_expect); // special handling for bool
125  CHECK (toString (2+2 == 5) == "false"_expect);
126  CHECK (toString (12.34e55) == "1.234e+56"_expect);
127 
128  CHECK (toString (short(12))
129  +toString (345L)
130  +toString ("67")
131  +toString ('8') == "12345678"_expect); // these go through lexical_cast<string>
132  }
133 
134 
139  void
141  {
142  // use as transformer within an (iterator) pipeline
143  auto ss = stringify (eachNum (1.11, 10.2));
144 
145  CHECK (ss);
146  CHECK ("1.11" == *ss);
147  ++ss;
148  CHECK ("2.11" == *ss);
149 
150  string res{".."};
151  for (auto s : ss)
152  res += s;
153 
154  CHECK (res == "..2.113.114.115.116.117.118.119.1110.11"_expect);
155 
156 
157  using VecS = vector<string>;
158 
159  // another variant: collect arbitrary heterogeneous arguments
160  VecS vals = stringify (short(12), 345L, "67", '8');
161  CHECK (vals == VecS({"12", "345", "67", "8"}));
162 
163 
164  // stringify can both consume (RValue-Ref) or take a copy from its source
165  auto nn = snapshot (eachNum (5, 10));
166  CHECK (5 == *nn);
167  ++nn;
168  CHECK (6 == *nn);
169 
170  auto sn = stringify (nn);
171  CHECK ("6" == *sn);
172  ++sn;
173  CHECK ("7" == *sn);
174  CHECK (6 == *nn);
175  ++++nn;
176  CHECK (8 == *nn);
177  CHECK ("7" == *sn);
178 
179  sn = stringify (std::move(nn));
180  CHECK ("8" == *sn);
181  CHECK (isnil (nn)); // was consumed by moving it into sn
182  ++sn;
183  CHECK ("9" == *sn);
184  ++sn;
185  CHECK (isnil (sn));
186  }
187 
188 
199  void
201  {
202  vector<double> dubious;
203  for (uint i=0; i<10; ++i)
204  dubious.push_back(1.1*i);
205 
206  std::function<AutoCounter(double)> justCount = [](double d){ return AutoCounter(d); };
207 
208 
209  CHECK (join (dubious, "--+--")
210  == "0--+--"
211  "1.1--+--"
212  "2.2--+--"
213  "3.3--+--"
214  "4.4--+--"
215  "5.5--+--"
216  "6.6--+--"
217  "7.7--+--"
218  "8.8--+--"
219  "9.9"_expect);
220  CHECK (join (transformIterator(eachElm(dubious), justCount))
221  == "Nr.01(0.0), "
222  "Nr.02(2.2), "
223  "Nr.03(4.4), "
224  "Nr.04(6.6), "
225  "Nr.05(8.8), "
226  "Nr.06(11.0), "
227  "Nr.07(13.2), "
228  "Nr.08(15.4), "
229  "Nr.09(17.6), "
230  "Nr.10(19.8)"_expect);
231  }
232 
233 
235  void
237  {
238  string abcdef{"abcdef"};
239  CHECK (startsWith (abcdef, "abcdef"));
240  CHECK (startsWith (abcdef, "abcde"));
241  CHECK (startsWith (abcdef, "abcd"));
242  CHECK (startsWith (abcdef, "abc"));
243  CHECK (startsWith (abcdef, "ab"));
244  CHECK (startsWith (abcdef, "a"));
245  CHECK (startsWith (abcdef, ""));
246 
247  CHECK (endsWith (abcdef, "abcdef"));
248  CHECK (endsWith (abcdef, "bcdef"));
249  CHECK (endsWith (abcdef, "cdef"));
250  CHECK (endsWith (abcdef, "def"));
251  CHECK (endsWith (abcdef, "ef"));
252  CHECK (endsWith (abcdef, "f"));
253  CHECK (endsWith (abcdef, ""));
254 
255  CHECK (startsWith (string{}, ""));
256  CHECK (endsWith (string{}, ""));
257 
258  CHECK (not startsWith (string{"abc"}, "abcd"));
259  CHECK (not startsWith (string{"a"}, "ä"));
260  CHECK (not startsWith (string{"ä"}, "a"));
261 
262  CHECK (not endsWith (string{"abc"}, " abc"));
263  CHECK (not endsWith (string{"a"}, "ä"));
264  CHECK (not endsWith (string{"ä"}, "a"));
265 
266  string abc{"abcdef"};
267  removePrefix(abc, "ab");
268  CHECK ("cdef" == abc);
269  removeSuffix(abc, "ef");
270  CHECK ("cd" == abc);
271 
272  abc = "bcdef";
273  removePrefix(abc, "ab");
274  CHECK ("bcdef" == abc);
275  removeSuffix(abc, "abcdef");
276  CHECK ("bcdef" == abc);
277  removeSuffix(abc, "bcdef");
278  CHECK (isnil (abc));
279  }
280  };
281 
282  LAUNCHER (FormatHelper_test, "unit common");
283 
284 
285 }} // namespace util::test
286 
vector< string > stringify(ELMS const &...elms)
standard setup: convert to string into a vector
NumIter< INT > eachNum(INT start, INT end)
convenience function to iterate "each number"
bool startsWith(string const &str, string const &prefix)
check if string starts with a given prefix
Definition: util.hpp:185
Definition: run.hpp:49
Front-end for printf-style string template interpolation.
iter_stl::IterSnapshot< VAL > snapshot(std::initializer_list< VAL > const &&ili)
Take a snapshot of the given std::initializer_list.
A front-end for using printf-style formatting.
Simple test class runner.
std::string toString(TY const &val) noexcept
get some string representation of any object, reliably.
Definition: format-obj.hpp:200
A collection of frequently used helper functions to support unit testing.
_SeqT< CON >::Range eachElm(CON &coll)
Lumiera error handling (C++ interface).
string join(CON &&coll, string const &delim=", ")
enumerate a collection&#39;s contents, separated by delimiter.
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
bool endsWith(string const &str, string const &suffix)
check if string ends with the given suffix
Definition: util.hpp:198
Preconfigured adapters for some STL container standard usage situations.
auto transformIterator(IT const &src, FUN processingFunc)
Build a TransformIter: convenience free function shortcut, picking up the involved types automaticall...
Definition: itertools.hpp:797