rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-strfmt.cc
Go to the documentation of this file.
1 #include "config.h"
2 
3 #include <iostream>
4 #include <string>
5 #include <cassert>
6 
7 #include "asserts.h"
8 #include "types.h"
9 #include "error.h"
10 #include "estring.h"
11 #include "strfmt.h"
12 
13 // #define ERR_OUT(e) std::cerr << e
14 #define ERR_OUT(e)
15 
16 #define TEST(code) \
17  try { \
18  code; \
19  } \
20  catch(error e) { \
21  ERR_OUT(e); \
22  thrown = true; \
23  } \
24  catch(...) { \
25  assert(0); \
26  }
27 
28 
29 void test1(void)
30 {
31  std::string str;
32  bool thrown;
33  uint64 ui64;
34 
35  thrown = false;
36  TEST(str = size_to_string(static_cast<signed char>(-127)));
37  assert(!thrown);
38  ERR_OUT(str << std::endl);
39  assert(str == " -127 b");
40 
41  thrown = false;
42  ui64 = 1023;
43  TEST(str = size_to_string(ui64));
44  assert(!thrown);
45  ERR_OUT(str << std::endl);
46  assert(str == " 1023 b");
47 
48  thrown = false;
49  ui64 = 1024;
50  TEST(str = size_to_string(ui64));
51  assert(!thrown);
52  ERR_OUT(str << std::endl);
53  assert(str == " 1.0Kb");
54 
55  thrown = false;
56  ui64 = 1224;
57  TEST(str = size_to_string(ui64));
58  assert(!thrown);
59  ERR_OUT(str << std::endl);
60  assert(str == " 1.1Kb");
61 
62  thrown = false;
63  ui64 = 156789;
64  ui64 *= 1000;
65  ui64 *= 1000;
66  ui64 *= 1000;
67  ui64 *= 1000;
68  TEST(str = size_to_string(ui64,"b",8,2,1000));
69  assert(!thrown);
70  ERR_OUT(str << std::endl);
71  assert(str == "156.78Pb");
72 
73  thrown = false;
74  ui64 = 156;
75  ui64 *= 1000;
76  ui64 *= 1000;
77  ui64 *= 1000;
78  ui64 *= 1000;
79  ui64 *= 1000;
80  TEST(str = size_to_string(ui64,"b",8,1,1000));
81  assert(!thrown);
82  ERR_OUT(str << std::endl);
83  assert(str == " 156.0Pb");
84 
85  thrown = false;
86  ui64 = 156;
87  ui64 *= 1000;
88  ui64 *= 1000;
89  ui64 *= 1000;
90  ui64 *= 1000;
91  ui64 *= 1000;
92  ui64 *= 1000;
93  TEST(str = size_to_string(ui64,"b",8,1,1000));
94  assert(thrown);
95  ERR_OUT(str << std::endl);
96  assert(str == " 156.0Pb");
97 }
98 
99 void test2(void)
100 {
101  std::string str;
102  bool thrown = false;
103 
104  thrown = false;
105  TEST(str = num_to_string(0));
106  assert(!thrown);
107  ERR_OUT(str << std::endl);
108  assert(str == "0");
109 
110  thrown = false;
111  TEST(str = num_to_string(10));
112  assert(!thrown);
113  ERR_OUT(str << std::endl);
114  assert(str == "10");
115 
116  thrown = false;
117  TEST(str = num_to_string(100));
118  assert(!thrown);
119  ERR_OUT(str << std::endl);
120  assert(str == "100");
121 
122  thrown = false;
123  TEST(str = num_to_string(1000));
124  assert(!thrown);
125  ERR_OUT(str << std::endl);
126  assert(str == "1,000");
127 
128  thrown = false;
129  TEST(str = num_to_string(10000));
130  assert(!thrown);
131  ERR_OUT(str << std::endl);
132  assert(str == "10,000");
133 
134  thrown = false;
135  TEST(str = num_to_string(100000));
136  assert(!thrown);
137  ERR_OUT(str << std::endl);
138  assert(str == "100,000");
139 
140  thrown = false;
141  TEST(str = num_to_string(1000000));
142  assert(!thrown);
143  ERR_OUT(str << std::endl);
144  assert(str == "1,000,000");
145 
146  thrown = false;
147  TEST(str = num_to_string(0,9));
148  assert(!thrown);
149  ERR_OUT(str << std::endl);
150  assert(str == " 0");
151 
152  thrown = false;
153  TEST(str = num_to_string(10,9));
154  assert(!thrown);
155  ERR_OUT(str << std::endl);
156  assert(str == " 10");
157 
158  thrown = false;
159  TEST(str = num_to_string(100,9));
160  assert(!thrown);
161  ERR_OUT(str << std::endl);
162  assert(str == " 100");
163 
164  thrown = false;
165  TEST(str = num_to_string(1000,9));
166  assert(!thrown);
167  ERR_OUT(str << std::endl);
168  assert(str == " 1,000");
169 
170  thrown = false;
171  TEST(str = num_to_string(10000,9));
172  assert(!thrown);
173  ERR_OUT(str << std::endl);
174  assert(str == " 10,000");
175 
176  thrown = false;
177  TEST(str = num_to_string(100000,9));
178  assert(!thrown);
179  ERR_OUT(str << std::endl);
180  assert(str == " 100,000");
181 
182  thrown = false;
183  TEST(str = num_to_string(1000000,9));
184  assert(!thrown);
185  ERR_OUT(str << std::endl);
186  assert(str == "1,000,000");
187 }
188 
189 void test3(void)
190 {
191  std::string str;
192  bool thrown;
193  uint64 ui64;
194 
195  thrown = false;
196  TEST(str = throughput_to_string(static_cast<signed char>(-127)));
197  assert(!thrown);
198  ERR_OUT(str << std::endl);
199  assert(str == " -127 b/s");
200 
201  thrown = false;
202  ui64 = 1023;
203  TEST(str = throughput_to_string(ui64));
204  assert(!thrown);
205  ERR_OUT(str << std::endl);
206  assert(str == " 1023 b/s");
207 
208  thrown = false;
209  ui64 = 1024;
210  TEST(str = throughput_to_string(ui64));
211  assert(!thrown);
212  ERR_OUT(str << std::endl);
213  assert(str == " 1.0Kb/s");
214 
215  thrown = false;
216  ui64 = 1224;
217  TEST(str = throughput_to_string(ui64));
218  assert(!thrown);
219  ERR_OUT(str << std::endl);
220  assert(str == " 1.1Kb/s");
221 
222  thrown = false;
223  ui64 = 156789;
224  ui64 *= 1000;
225  ui64 *= 1000;
226  ui64 *= 1000;
227  ui64 *= 1000;
228  TEST(str = throughput_to_string(ui64,"b","s",10,2,1000));
229  assert(!thrown);
230  ERR_OUT(str << std::endl);
231  assert(str == "156.78Pb/s");
232 
233  thrown = false;
234  ui64 = 156;
235  ui64 *= 1000;
236  ui64 *= 1000;
237  ui64 *= 1000;
238  ui64 *= 1000;
239  ui64 *= 1000;
240  TEST(str = throughput_to_string(ui64,"b","s",10,1,1000));
241  assert(!thrown);
242  ERR_OUT(str << std::endl);
243  assert(str == " 156.0Pb/s");
244 
245  thrown = false;
246  ui64 = 156;
247  ui64 *= 1000;
248  ui64 *= 1000;
249  ui64 *= 1000;
250  ui64 *= 1000;
251  ui64 *= 1000;
252  ui64 *= 1000;
253  TEST(str = throughput_to_string(ui64,"b","s",10,1,1000));
254  assert(thrown);
255  ERR_OUT(str << std::endl);
256  assert(str == " 156.0Pb/s");
257 }
258 
259 void test4(void)
260 {
261  const uint8 max = max_limit<uint8>();
262  uint8 x;
263  std::string s;
264  std::string::size_type s_len;
265  std::string::size_type idx;
266 
267  for (x = 0; x != max; ++x) {
268  s = percent_string(x,max);
269  idx = s.find(".");
270  s_len = s.size();
271  /**/
272  if (s_len - idx > 3)
273  std::cerr
274  << "percent_string("
275  << static_cast<unsigned int>(x)
276  << ","
277  << static_cast<unsigned int>(max)
278  << ") = "
279  << s
280  << std::endl;
281  /**/
282  assert(s_len - idx < 4);
283  }
284 
285 }
286 
287 int main(int argc, char const * argv[])
288 {
289  try {
290  test1();
291  test2();
292  test3();
293  test4();
294  }
295  catch(error e) {
296  std::cerr << e;
297  assert(0);
298  }
299  catch(...) {
300  std::cerr << err_unknown;
301  assert(0);
302  }
303  return(0);
304 }
305 
Basic types definitions and templates.
#define TEST(code)
Definition: test-strfmt.cc:16
void test1(void)
Definition: test-strfmt.cc:29
void test2(void)
Definition: test-strfmt.cc:99
int main(int argc, char const *argv[])
Definition: test-strfmt.cc:287
std::string percent_string(const T &a_complete, const T &a_total)
Given a count complete and a count total, calculate a percentage.
Definition: strfmt.h:157
std::string num_to_string(T a_t, uint16 a_width=0)
Given an integral number, return a string with the number formatted in a generic, human-readable form...
Definition: strfmt.h:96
#define ERR_OUT(e)
Definition: test-strfmt.cc:14
#define err_unknown
Definition: error.h:114
std::string throughput_to_string(T a_t, const std::string &a_base_size_unit="b", const std::string &a_base_time_unit="s", uint16 a_width=10, uint16 a_precision=1, uint16 a_kilo=1024)
Given an integral number, return a string with the number formated as a number of machine-type size p...
Definition: strfmt.h:135
An error class.
Definition: error.h:72
std::string size_to_string(T a_t, const std::string &a_base_size_unit="b", uint16 a_width=8, uint16 a_precision=1, uint16 a_kilo=1024)
Given an integral number, return a string with the number formatted as a human-readable machine-type ...
Definition: strfmt.h:12
void test3(void)
Definition: test-strfmt.cc:189
void test4(void)
Definition: test-strfmt.cc:259