rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-strfmt-004.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 "types.h"
8 #include "error.h"
9 #include "estring.h"
10 #include "strfmt.h"
11 #include "rmath.h"
12 #include "timer.h"
13 
14 // #define ERR_OUT(e) std::cerr << e
15 #define ERR_OUT(e)
16 
17 #define TEST(code) \
18  try { \
19  code; \
20  } \
21  catch(error e) { \
22  ERR_OUT(e); \
23  thrown = true; \
24  } \
25  catch(...) { \
26  assert(0); \
27  }
28 
29 
30 void test(void)
31 {
32  const uint8 max = max_limit<uint8>();
33  uint8 x;
34  std::string s;
35  std::string last_s;
36  std::string::size_type s_len;
37  std::string::size_type idx;
38  timer t;
39  int ip;
40  double dp;
41 
42  t.start();
43  for (x = 0; x != max; ++x) {
44  s = percent_string(x,max);
45  idx = s.find(".");
46  s_len = s.size();
47  if (last_s != s) {
48  t.stop();
49  dp = static_cast<double>(x) / static_cast<double>(max);
50  ip = static_cast<unsigned int>(dp);
51  std::cerr
52  << "Test is "
53  << s
54  << " complete. ETA: "
55  << t.eta(ip)
56  << std::endl;
57  last_s = s;
58  }
59  if (s_len - idx > 3)
60  std::cerr
61  << "percent_string("
62  << static_cast<unsigned int>(x)
63  << ","
64  << static_cast<unsigned int>(max)
65  << ") = "
66  << s
67  << std::endl;
68  }
69 }
70 
71 int main(int argc, char const * argv[])
72 {
73  try {
74  test();
75  }
76  catch(error e) {
77  std::cerr << e;
78  assert(0);
79  }
80  catch(...) {
81  std::cerr << err_unknown;
82  assert(0);
83  }
84  return(0);
85 }
86 
Basic types definitions and templates.
void test(void)
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
#define err_unknown
Definition: error.h:114
int main(int argc, char const *argv[])
void start(void)
Start (or restart) the timer.
Definition: timer.cc:137
An error class.
Definition: error.h:72
const std::string eta(unsigned int a_percent_complete) const
Given a percent-complete for some unknown task, estimate a time to completion.
Definition: timer.cc:463
void stop(void)
Stop the timer.
Definition: timer.cc:143
Used as a stopwatch.
Definition: timer.h:29