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