rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-estring.cc
Go to the documentation of this file.
1 #include "config.h"
2 
3 #ifdef HAVE_UNISTD_H
4 #include <unistd.h>
5 #endif
6 
7 #include <iostream>
8 #include <string>
9 #include <cassert>
10 
11 #include "asserts.h"
12 #include "types.h"
13 #include "error.h"
14 #include "estring.h"
15 
16 void test1(void)
17 {
18  estring s;
19 }
20 
21 void test2(void)
22 {
23  estring es;
24  std::string s;
25 
26  es = static_cast<char>('c');
27  s = es;
28  assert(s == "c");
29  assert(es == "c");
30 
31  es = "Hello World";
32  s = es;
33  assert(s == "Hello World");
34  assert(es == "Hello World");
35 
36  s = "The quick brown fox";
37  es = s;
38  assert(es == "The quick brown fox");
39 
40  es = static_cast<unsigned int>(6);
41  assert(es == "6");
42  es = static_cast<int>(-5);
43  assert(es == "-5");
44  es = static_cast<int>(5);
45  assert(es == "5");
46 
47  es = static_cast<unsigned short>(7);
48  assert(es == "7");
49  es = static_cast<short>(-7);
50  assert(es == "-7");
51  es = static_cast<short>(7);
52  assert(es == "7");
53 
54  es = static_cast<unsigned long>(8);
55  assert(es == "8");
56  es = static_cast<long>(-8);
57  assert(es == "-8");
58  es = static_cast<long>(8);
59  assert(es == "8");
60 
61  es = static_cast<unsigned long long>(9);
62  assert(es == "9");
63  es = static_cast<long long>(-9);
64  assert(es == "-9");
65  es = static_cast<long long>(9);
66  assert(es == "9");
67 
68  es.precision(1);
69  es = static_cast<float>(5.9);
70  assert(es == "5.9");
71  es = static_cast<float>(6.2);
72  es.assign(static_cast<float>(6.2),1);
73  assert(es == "6.2");
74  es = static_cast<float>(-5.9);
75  assert(es == "-5.9");
76  es = static_cast<float>(-6.2);
77  es.assign(static_cast<float>(-6.2),1);
78  assert(es == "-6.2");
79 
80 
81  es.assign(static_cast<float>(6.2),0);
82  assert(es == "6");
83  es.assign(static_cast<float>(6.2),1);
84  assert(es == "6.2");
85  es.assign(static_cast<float>(6.2),2);
86  assert(es == "6.20");
87  es.assign(static_cast<float>(-6.2),0);
88  assert(es == "-6");
89  es.assign(static_cast<float>(-6.2),1);
90  assert(es == "-6.2");
91  es.assign(static_cast<float>(-6.2),2);
92  assert(es == "-6.20");
93 
94 
95  es = "";
96 
97  assert(es.fmt_str(0,estring::left,'<','>') == "");
98  assert(es.fmt_str(1,estring::left,'<','>') == ">");
99  assert(es.fmt_str(2,estring::left,'<','>') == ">>");
100  assert(es.fmt_str(3,estring::left,'<','>') == ">>>");
101 
102  assert(es.fmt_str(0,estring::right,'<','>') == "");
103  assert(es.fmt_str(1,estring::right,'<','>') == "<");
104  assert(es.fmt_str(2,estring::right,'<','>') == "<<");
105  assert(es.fmt_str(3,estring::right,'<','>') == "<<<");
106 
107  assert(es.fmt_str(0,estring::center,'<','>') == "");
108  assert(es.fmt_str(1,estring::center,'<','>') == ">");
109  assert(es.fmt_str(2,estring::center,'<','>') == "<>");
110  assert(es.fmt_str(3,estring::center,'<','>') == "<>>");
111 
112  es = "Hello World";
113 
114  assert(es.fmt_str(0,estring::left,'<','>') == "");
115  assert(es.fmt_str(1,estring::left,'<','>') == ".");
116  assert(es.fmt_str(2,estring::left,'<','>') == "..");
117  assert(es.fmt_str(3,estring::left,'<','>') == "...");
118  assert(es.fmt_str(4,estring::left,'<','>') == "H...");
119  assert(es.fmt_str(5,estring::left,'<','>') == "He...");
120  assert(es.fmt_str(6,estring::left,'<','>') == "Hel...");
121  assert(es.fmt_str(7,estring::left,'<','>') == "Hell...");
122  assert(es.fmt_str(8,estring::left,'<','>') == "Hello...");
123  assert(es.fmt_str(9,estring::left,'<','>') == "Hello ...");
124  assert(es.fmt_str(10,estring::left,'<','>') == "Hello W...");
125  assert(es.fmt_str(11,estring::left,'<','>') == "Hello World");
126  assert(es.fmt_str(12,estring::left,'<','>') == "Hello World>");
127  assert(es.fmt_str(13,estring::left,'<','>') == "Hello World>>");
128 
129  assert(es.fmt_str(0,estring::right,'<','>') == "");
130  assert(es.fmt_str(1,estring::right,'<','>') == ".");
131  assert(es.fmt_str(2,estring::right,'<','>') == "..");
132  assert(es.fmt_str(3,estring::right,'<','>') == "...");
133  assert(es.fmt_str(4,estring::right,'<','>') == "...d");
134  assert(es.fmt_str(5,estring::right,'<','>') == "...ld");
135  assert(es.fmt_str(6,estring::right,'<','>') == "...rld");
136  assert(es.fmt_str(7,estring::right,'<','>') == "...orld");
137  assert(es.fmt_str(8,estring::right,'<','>') == "...World");
138  assert(es.fmt_str(9,estring::right,'<','>') == "... World");
139  assert(es.fmt_str(10,estring::right,'<','>') == "...o World");
140  assert(es.fmt_str(11,estring::right,'<','>') == "Hello World");
141  assert(es.fmt_str(12,estring::right,'<','>') == "<Hello World");
142  assert(es.fmt_str(13,estring::right,'<','>') == "<<Hello World");
143 
144  assert(es.fmt_str(0,estring::center,'<','>') == "");
145  assert(es.fmt_str(1,estring::center,'<','>') == ".");
146  assert(es.fmt_str(2,estring::center,'<','>') == "..");
147  assert(es.fmt_str(3,estring::center,'<','>') == "...");
148  assert(es.fmt_str(4,estring::center,'<','>') == "H...");
149  assert(es.fmt_str(5,estring::center,'<','>') == "H...d");
150  assert(es.fmt_str(6,estring::center,'<','>') == "He...d");
151  assert(es.fmt_str(7,estring::center,'<','>') == "He...ld");
152  assert(es.fmt_str(8,estring::center,'<','>') == "Hel...ld");
153  assert(es.fmt_str(9,estring::center,'<','>') == "Hel...rld");
154  assert(es.fmt_str(10,estring::center,'<','>') == "Hell...rld");
155  assert(es.fmt_str(11,estring::center,'<','>') == "Hello World");
156  assert(es.fmt_str(12,estring::center,'<','>') == "Hello World>");
157  assert(es.fmt_str(13,estring::center,'<','>') == "<Hello World>");
158  assert(es.fmt_str(14,estring::center,'<','>') == "<Hello World>>");
159  assert(es.fmt_str(15,estring::center,'<','>') == "<<Hello World>>");
160 
161  es.assign(static_cast<double>(6.25),2);
162  assert(es == "6.25");
163 
164  assert(es.fmt_str(0,estring::left,'<','>') == "");
165  assert(es.fmt_str(1,estring::left,'<','>') == ".");
166  assert(es.fmt_str(2,estring::left,'<','>') == "..");
167  assert(es.fmt_str(3,estring::left,'<','>') == "...");
168  assert(es.fmt_str(4,estring::left,'<','>') == "6.25");
169  assert(es.fmt_str(5,estring::left,'<','>') == "6.25>");
170  assert(es.fmt_str(6,estring::left,'<','>') == "6.25>>");
171  assert(es.fmt_str(7,estring::left,'<','>') == "6.25>>>");
172 
173  assert(es.fmt_str(0,estring::right,'<','>') == "");
174  assert(es.fmt_str(1,estring::right,'<','>') == ".");
175  assert(es.fmt_str(2,estring::right,'<','>') == "..");
176  assert(es.fmt_str(3,estring::right,'<','>') == "...");
177  assert(es.fmt_str(4,estring::right,'<','>') == "6.25");
178  assert(es.fmt_str(5,estring::right,'<','>') == "<6.25");
179  assert(es.fmt_str(6,estring::right,'<','>') == "<<6.25");
180  assert(es.fmt_str(7,estring::right,'<','>') == "<<<6.25");
181 
182  assert(es.fmt_str(0,estring::center,'<','>') == "");
183  assert(es.fmt_str(1,estring::center,'<','>') == ".");
184  assert(es.fmt_str(2,estring::center,'<','>') == "..");
185  assert(es.fmt_str(3,estring::center,'<','>') == "...");
186  assert(es.fmt_str(4,estring::center,'<','>') == "6.25");
187  assert(es.fmt_str(5,estring::center,'<','>') == "6.25>");
188  assert(es.fmt_str(6,estring::center,'<','>') == "<6.25>");
189  assert(es.fmt_str(7,estring::center,'<','>') == "<6.25>>");
190 }
191 
192 void test3(void)
193 {
194  estring es;
195  unsigned int uint;
196  int sint;
197  float fl;
198  double dbl;
199 
200  es = static_cast<unsigned int>(0);
201  assert(es == "0");
202  uint = es;
203  assert(uint == 0);
204 
205  es = static_cast<unsigned int>(1);
206  assert(es == "1");
207  uint = es;
208  assert(uint == 1);
209 
210  es = static_cast<unsigned int>(9);
211  assert(es == "9");
212  uint = es;
213  assert(uint == 9);
214 
215  es = static_cast<unsigned int>(10);
216  assert(es == "10");
217  uint = es;
218  assert(uint == 10);
219 
220  es = static_cast<unsigned int>(11);
221  assert(es == "11");
222  uint = es;
223  assert(uint == 11);
224 
225  es = static_cast<unsigned int>(99);
226  assert(es == "99");
227  uint = es;
228  assert(uint == 99);
229 
230  es = static_cast<unsigned int>(100);
231  assert(es == "100");
232  uint = es;
233  assert(uint == 100);
234 
235  es.reset();
236  es.base(16);
237 
238  es = static_cast<unsigned int>(0);
239  assert(es == "0");
240  uint = es;
241  assert(uint == 0);
242 
243  es = static_cast<unsigned int>(9);
244  assert(es == "9");
245  uint = es;
246  assert(uint == 9);
247 
248  es = static_cast<unsigned int>(10);
249  assert(es == "a");
250  uint = es;
251  assert(uint == 10);
252 
253  es = static_cast<unsigned int>(15);
254  assert(es == "f");
255  uint = es;
256  assert(uint == 15);
257 
258  es = static_cast<unsigned int>(16);
259  assert(es == "10");
260  uint = es;
261  assert(uint == 16);
262 
263  es = static_cast<unsigned int>(17);
264  assert(es == "11");
265  uint = es;
266  assert(uint == 17);
267 
268  es = static_cast<int>(0);
269  assert(es == "0");
270  sint = es;
271  assert(sint == 0);
272 
273  es = static_cast<int>(-0);
274  assert(es == "0");
275  sint = es;
276  assert(sint == 0);
277 
278  es = static_cast<int>(9);
279  assert(es == "9");
280  sint = es;
281  assert(sint == 9);
282 
283  es = static_cast<int>(-9);
284  assert(es == "-9");
285  sint = es;
286  assert(sint == -9);
287 
288  es = static_cast<int>(10);
289  assert(es == "a");
290  sint = es;
291  assert(sint == 10);
292 
293  es = static_cast<int>(-10);
294  assert(es == "-a");
295  sint = es;
296  assert(sint == -10);
297 
298  es = static_cast<int>(15);
299  assert(es == "f");
300  sint = es;
301  assert(sint == 15);
302 
303  es = static_cast<int>(-15);
304  assert(es == "-f");
305  sint = es;
306  assert(sint == -15);
307 
308  es = static_cast<int>(16);
309  assert(es == "10");
310  sint = es;
311  assert(sint == 16);
312 
313  es = static_cast<int>(-16);
314  assert(es == "-10");
315  sint = es;
316  assert(sint == -16);
317 
318  es = static_cast<int>(17);
319  assert(es == "11");
320  sint = es;
321  assert(sint == 17);
322 
323  es = static_cast<int>(-17);
324  assert(es == "-11");
325  sint = es;
326  assert(sint == -17);
327 
328  es.reset();
329 
330  es = static_cast<float>(6.2);
331  assert(es == "6.2");
332  fl = es;
333  assert(estring(fl,1) == "6.2");
334 
335  es.precision(4);
336  es = static_cast<double>(-5.9238);
337  assert(es == "-5.9238");
338  dbl = es;
339  assert(estring(dbl,4) == "-5.9238");
340 }
341 
342 void test4(void)
343 {
344  estring es;
345  estring es2;
346  union generic_value {
347  unsigned int ui;
348  int i;
349  unsigned short us;
350  short s;
351  unsigned long ul;
352  long l;
353  unsigned long long ull;
354  long long ll;
355  float f;
356  double d;
357  };
358  generic_value v;
359 
360  es.base(16);
361  es = static_cast<int>(16);
362  assert(es == "10");
363  v.i = es;
364  assert(v.i == 16);
365  es.base(10);
366  assert(es == "16");
367  v.i = es;
368  assert(v.i == 16);
369 
370  assert(es2.get_from_type() == estring::type_unknown);
371  es2 = es;
372  assert(es2.get_from_type() == estring::type_int);
373  assert(es2.base() == es.base());
374  assert(es2.get_from_value().value.i == 16);
375 }
376 
377 void test5(void)
378 {
379  estring es;
380 
381  es = "Hello World";
382  es.lower();
383  assert(es == "hello world");
384  es = "Hello World";
385  es.upper();
386  assert(es == "HELLO WORLD");
387 }
388 
389 int main(int argc, char const * argv[])
390 {
391  try {
392  test1();
393  test2();
394  test3();
395  test4();
396  test5();
397  }
398  catch(error e) {
399  std::cerr << e;
400  assert(0);
401  }
402  catch(...) {
403  std::cerr << err_unknown;
404  assert(0);
405  }
406  return(0);
407 }
408 
Left-justified.
Definition: estring.h:64
Basic types definitions and templates.
An extended string class.
Definition: estring.h:52
void test2(void)
Definition: test-estring.cc:21
void reset(void)
Erase and reinitialize.
Definition: estring.cc:421
void test1(void)
Definition: test-estring.cc:16
const set_from_type & get_from_type(void) const
Retrieve the type of value being held by this estring.
Definition: estring.cc:810
estring & assign(const estring &a_estr)
Assignment for estring objects.
Definition: estring.cc:853
Right-justified.
Definition: estring.h:66
#define err_unknown
Definition: error.h:114
void test5(void)
Centered.
Definition: estring.h:68
size_type precision(size_type a_p)
Set the precision used in converting to/from fractional types.
Definition: estring.cc:501
An error class.
Definition: error.h:72
value_type fmt_str(void)
Generate a formatted string.
Definition: estring.cc:688
int main(int argc, char const *argv[])
estring & lower(void)
Convert all characters to lowercase.
Definition: estring.cc:891
void test4(void)
estring & upper(void)
Convert all characters to uppercase.
Definition: estring.cc:908
void test3(void)
const unsigned int base(const unsigned int a_base)
Set the base used in numeric conversions.
Definition: estring.cc:540