35 #include <boost/lexical_cast.hpp> 39 using boost::lexical_cast;
69 check_simpleInvocation();
70 check_delayedInvocation();
71 verify_typeHandling();
73 verify_errorHandling();
74 verify_pointerHandling();
80 check_simpleInvocation ()
82 string formatted =
_Fmt(
"--format-template--int=%04d--double=%+5.2f--string=%-9s--")
86 cout << formatted << endl;
87 CHECK (formatted ==
"--format-template--int=0012--double=+1.23--string=Lumiera --");
92 check_delayedInvocation ()
94 string formatString =
"%p %|20T_| %u";
95 _Fmt formatter (formatString);
97 uint val = rand() % 100;
103 cout << formatter << endl;
108 verify_typeHandling ()
111 CHECK (
_Fmt(
"%d") % i ==
"-12" );
112 CHECK (
_Fmt(
"%6d") % i ==
" -12" );
113 CHECK (
_Fmt(
"%-6d") % i ==
"-12 " );
114 CHECK (
_Fmt(
"%+-6d") % -i ==
"+12 " );
115 CHECK (
_Fmt(
"%+06d") % -i ==
"+00012" );
116 CHECK (
_Fmt(
"%06X") % -i ==
"00000C" );
117 CHECK (
_Fmt(
"%#x") % -i ==
"0xc" );
120 CHECK (
_Fmt(
"%d") % u ==
"12" );
121 CHECK (
_Fmt(
"%6d") % u ==
" 12" );
122 CHECK (
_Fmt(
"%-6d") % u ==
"12 " );
123 CHECK (
_Fmt(
"%-+6d") % u ==
"12 " );
124 CHECK (
_Fmt(
"%+06d") % u ==
"000012" );
127 CHECK (
_Fmt(
"%6d") % sh ==
" -123" );
130 CHECK (
_Fmt(
"%6d") % ush ==
" 123" );
133 CHECK (
_Fmt(
"%6d") % l ==
" -123" );
136 CHECK (
_Fmt(
"%6d") % ul ==
" 123" );
139 CHECK (
_Fmt(
"%d") % ll ==
"5000000000" );
140 CHECK (
_Fmt(
"%5.3d") %ll ==
"5000000000" );
141 CHECK (
_Fmt(
"%10.3e") %ll ==
"5000000000" );
144 CHECK (
_Fmt(
"%d") % ull ==
"5000000000" );
147 CHECK (
_Fmt(
"%g") % f ==
"12.34" );
148 CHECK (
_Fmt(
"%d") % f ==
"12.34" );
149 CHECK (
_Fmt(
"%.3f") % f ==
"12.340" );
150 CHECK (
_Fmt(
"%.1e") % f ==
"1.2e+01");
151 CHECK (
_Fmt(
"%.0f") % f ==
"12" );
154 CHECK (
_Fmt(
"%g") % d ==
"-12.34" );
157 CHECK (
_Fmt(
"%d") % c ==
"@" );
158 CHECK (
_Fmt(
"%x") % c ==
"@" );
159 CHECK (
_Fmt(
"%o") % c ==
"@" );
160 CHECK (
_Fmt(
"%c") % c ==
"@" );
161 CHECK (
_Fmt(
"%s") % c ==
"@" );
162 CHECK (
_Fmt(
"%d") %
short(c) ==
"64" );
163 CHECK (
_Fmt(
"%x") %
short(c) ==
"40" );
164 CHECK (
_Fmt(
"%o") %
short(c) ==
"100" );
165 CHECK (
_Fmt(
"%c") %
short(c) ==
"6" );
166 CHECK (
_Fmt(
"%s") %
short(c) ==
"64" );
169 CHECK (
_Fmt(
"%d") % uint(uc) ==
"255" );
170 CHECK (
_Fmt(
"%x") % uint(uc) ==
"ff" );
171 CHECK (
_Fmt(
"%X") % uint(uc) ==
"FF" );
172 CHECK (
_Fmt(
"%c") % uint(uc) ==
"2" );
173 CHECK (
_Fmt(
"%s") % uint(uc) ==
"255" );
174 CHECK (
_Fmt(
"%d") % uc ==
"\xFF");
175 CHECK (
_Fmt(
"%x") % uc ==
"\xFF");
176 CHECK (
_Fmt(
"%X") % uc ==
"\xFF");
177 CHECK (
_Fmt(
"%c") % uc ==
"\xFF");
178 CHECK (
_Fmt(
"%s") % uc ==
"\xFF");
181 CHECK (
_Fmt(
"%c") % c ==
"\xFF");
183 string str(
"Lumiera");
184 CHECK (
_Fmt(
"%s") % str ==
"Lumiera" );
185 CHECK (
_Fmt(
"%10s") % str ==
" Lumiera" );
186 CHECK (
_Fmt(
"%7.4s") %str ==
" Lumi" );
187 CHECK (
_Fmt(
"%10c") % str ==
" L" );
189 const char* pch(
"edit");
190 CHECK (
_Fmt(
"%s") % pch ==
"edit" );
191 CHECK (
_Fmt(
"%10s") % pch ==
" edit" );
192 CHECK (
_Fmt(
"%7.3s") %pch ==
" edi" );
193 CHECK (
_Fmt(
"%10c") % pch ==
" e" );
196 CHECK (
_Fmt(
"%s") % lit ==
"your" );
197 CHECK (
_Fmt(
"%10s") % lit ==
" your" );
198 CHECK (
_Fmt(
"%7.2s") %lit ==
" yo" );
199 CHECK (
_Fmt(
"%10c") % lit ==
" y" );
202 CHECK (
_Fmt(
"%s") % sym ==
"freedom" );
203 CHECK (
_Fmt(
"%10s") % sym ==
" freedom" );
204 CHECK (
_Fmt(
"%7.5s") %sym ==
" freed" );
205 CHECK (
_Fmt(
"%10c") % sym ==
" f" );
221 using Silent::Silent;
225 operator string()
const 227 return _Fmt(
"Number-%03d") % i_;
234 using Verbose::Verbose;
236 operator string()
const 241 return Verbose::operator string();
258 string typeDisplay =
_Fmt(
"%s") % s;
259 cout << typeDisplay << endl;
260 CHECK (
contains (typeDisplay,
"test"));
261 CHECK (
contains (typeDisplay,
"FormatString_test"));
262 CHECK (
contains (typeDisplay,
"Silent"));
264 CHECK (
_Fmt(
"!!%s!!") % v ==
"!!Number-013!!");
265 CHECK (
_Fmt(
"!!%s!!") % x ==
"!!<string conversion failed: LUMIERA_ERROR_STATE:unforeseen state (encountered Fantomas).>!!");
270 CHECK (
_Fmt(
"!!%s!!") % rv ==
"!!<string conversion failed: LUMIERA_ERROR_STATE:unforeseen state (encountered Fantomas).>!!");
273 CHECK (
_Fmt(
"!!%s!!") % rv ==
"!!Number-042!!");
274 CHECK (
_Fmt(
"!!%s!!") % x ==
"!!Number-042!!");
277 CHECK (
_Fmt(
"!!%s!!") % sym ==
"!!42!!");
282 verify_errorHandling ()
284 cout <<
_Fmt(
"__nix_") % 1 % 2 << endl;
285 cout <<
_Fmt(
"__%d__") << endl;
286 cout <<
_Fmt(
"__%d__") % 1 << endl;
287 cout <<
_Fmt(
"__%d__") % 1 % 2 << endl;
289 const char* evil = NULL;
290 cout <<
_Fmt(
"__%d__") % evil << endl;
291 cout <<
_Fmt(
"__%d__") %
"dirt" << endl;
292 cout <<
_Fmt(
"__%d__") %
"1234" << endl;
293 cout <<
_Fmt(
"__%d__") %
"0xff" << endl;
300 verify_pointerHandling ()
302 int i(-12);
int * pi = & i;
303 uint u(12); uint * pu = & u;
304 short sh(-123);
short * psh = & sh;
305 ushort ush(123); ushort * push = &ush;
306 long l(-123);
long * pl = & l;
307 ulong ul(123); ulong * pul = & ul;
308 int64_t ll(5e+9); int64_t * pll = & ll;
309 uint64_t ull(ll); uint64_t * pull = &ull;
310 float f(12.34);
float * pf = & f;
311 double d(-12.34);
double * pd = & d;
312 uchar uc(0xff); uchar * puc = & uc;
313 string str(
"Lumiera");
string * pstr = &str;
315 CHECK (
_Fmt(
"%d") % i ==
_Fmt(
"%d") % pi);
316 CHECK (
_Fmt(
"%d") % u ==
_Fmt(
"%d") % pu);
317 CHECK (
_Fmt(
"%d") % sh ==
_Fmt(
"%d") % psh);
318 CHECK (
_Fmt(
"%d") % ush ==
_Fmt(
"%d") % push);
319 CHECK (
_Fmt(
"%d") % l ==
_Fmt(
"%d") % pl);
320 CHECK (
_Fmt(
"%d") % ul ==
_Fmt(
"%d") % pul);
321 CHECK (
_Fmt(
"%d") % ll ==
_Fmt(
"%d") % pll);
322 CHECK (
_Fmt(
"%d") % ull ==
_Fmt(
"%d") % pull);
323 CHECK (
_Fmt(
"%e") % f ==
_Fmt(
"%e") % pf);
324 CHECK (
_Fmt(
"%e") % d ==
_Fmt(
"%e") % pd);
325 CHECK (
_Fmt(
"%g") % uc ==
_Fmt(
"%g") % puc);
326 CHECK (
_Fmt(
"%s") % str ==
_Fmt(
"%s") % pstr);
329 char pc[2] = {
'a',
'\0'};
330 CHECK (
_Fmt(
"%g") % c ==
_Fmt(
"%g") % pc);
335 CHECK (
_Fmt(
"__%s__") % v ==
"__Number-042__");
336 CHECK (
_Fmt(
"__%s__") % pv ==
"__Number-042__");
337 CHECK (
_Fmt(
"__%s__") % vv !=
"__Number-042__");
339 string address = lexical_cast<
string>(pv);
340 CHECK (
_Fmt(
"__%s__") % vv ==
"__"+address+
"__");
344 CHECK (
_Fmt(
"__%s__") % pv ==
"__"+BOTTOM_INDICATOR+
"__");
345 CHECK (
_Fmt(
"__%s__") % vv ==
"__0__");
inline string literal This is a marker type to indicate that
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
bool contains(MAP &map, typename MAP::key_type const &key)
shortcut for containment test on a map
A front-end for using printf-style formatting.
Derived specific exceptions within Lumiera's exception hierarchy.
Token or Atom with distinct identity.
Marker types to indicate a literal string and a Symbol.
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
A collection of frequently used helper functions to support unit testing.
Lumiera error handling (C++ interface).