00001 #include "config.h"
00002
00003 #include <iostream>
00004 #include <string>
00005 #include <cassert>
00006
00007 #include "asserts.h"
00008 #include "types.h"
00009 #include "error.h"
00010 #include "estring.h"
00011 #include "strfmt.h"
00012
00013
00014 #define ERR_OUT(e)
00015
00016 #define TEST(code) \
00017 try { \
00018 code; \
00019 } \
00020 catch(error e) { \
00021 ERR_OUT(e); \
00022 thrown = true; \
00023 } \
00024 catch(...) { \
00025 assert(0); \
00026 }
00027
00028
00029 void test1(void)
00030 {
00031 std::string str;
00032 bool thrown;
00033 uint64 ui64;
00034
00035 thrown = false;
00036 TEST(str = size_to_string(static_cast<signed char>(-127)));
00037 assert(!thrown);
00038 ERR_OUT(str << std::endl);
00039 assert(str == " -127 b");
00040
00041 thrown = false;
00042 ui64 = 1023;
00043 TEST(str = size_to_string(ui64));
00044 assert(!thrown);
00045 ERR_OUT(str << std::endl);
00046 assert(str == " 1023 b");
00047
00048 thrown = false;
00049 ui64 = 1024;
00050 TEST(str = size_to_string(ui64));
00051 assert(!thrown);
00052 ERR_OUT(str << std::endl);
00053 assert(str == " 1.0Kb");
00054
00055 thrown = false;
00056 ui64 = 1224;
00057 TEST(str = size_to_string(ui64));
00058 assert(!thrown);
00059 ERR_OUT(str << std::endl);
00060 assert(str == " 1.1Kb");
00061
00062 thrown = false;
00063 ui64 = 156789;
00064 ui64 *= 1000;
00065 ui64 *= 1000;
00066 ui64 *= 1000;
00067 ui64 *= 1000;
00068 TEST(str = size_to_string(ui64,"b",8,2,1000));
00069 assert(!thrown);
00070 ERR_OUT(str << std::endl);
00071 assert(str == "156.78Pb");
00072
00073 thrown = false;
00074 ui64 = 156;
00075 ui64 *= 1000;
00076 ui64 *= 1000;
00077 ui64 *= 1000;
00078 ui64 *= 1000;
00079 ui64 *= 1000;
00080 TEST(str = size_to_string(ui64,"b",8,1,1000));
00081 assert(!thrown);
00082 ERR_OUT(str << std::endl);
00083 assert(str == " 156.0Pb");
00084
00085 thrown = false;
00086 ui64 = 156;
00087 ui64 *= 1000;
00088 ui64 *= 1000;
00089 ui64 *= 1000;
00090 ui64 *= 1000;
00091 ui64 *= 1000;
00092 ui64 *= 1000;
00093 TEST(str = size_to_string(ui64,"b",8,1,1000));
00094 assert(thrown);
00095 ERR_OUT(str << std::endl);
00096 assert(str == " 156.0Pb");
00097 }
00098
00099 void test2(void)
00100 {
00101 std::string str;
00102 bool thrown = false;
00103
00104 thrown = false;
00105 TEST(str = num_to_string(0));
00106 assert(!thrown);
00107 ERR_OUT(str << std::endl);
00108 assert(str == "0");
00109
00110 thrown = false;
00111 TEST(str = num_to_string(10));
00112 assert(!thrown);
00113 ERR_OUT(str << std::endl);
00114 assert(str == "10");
00115
00116 thrown = false;
00117 TEST(str = num_to_string(100));
00118 assert(!thrown);
00119 ERR_OUT(str << std::endl);
00120 assert(str == "100");
00121
00122 thrown = false;
00123 TEST(str = num_to_string(1000));
00124 assert(!thrown);
00125 ERR_OUT(str << std::endl);
00126 assert(str == "1,000");
00127
00128 thrown = false;
00129 TEST(str = num_to_string(10000));
00130 assert(!thrown);
00131 ERR_OUT(str << std::endl);
00132 assert(str == "10,000");
00133
00134 thrown = false;
00135 TEST(str = num_to_string(100000));
00136 assert(!thrown);
00137 ERR_OUT(str << std::endl);
00138 assert(str == "100,000");
00139
00140 thrown = false;
00141 TEST(str = num_to_string(1000000));
00142 assert(!thrown);
00143 ERR_OUT(str << std::endl);
00144 assert(str == "1,000,000");
00145
00146 thrown = false;
00147 TEST(str = num_to_string(0,9));
00148 assert(!thrown);
00149 ERR_OUT(str << std::endl);
00150 assert(str == " 0");
00151
00152 thrown = false;
00153 TEST(str = num_to_string(10,9));
00154 assert(!thrown);
00155 ERR_OUT(str << std::endl);
00156 assert(str == " 10");
00157
00158 thrown = false;
00159 TEST(str = num_to_string(100,9));
00160 assert(!thrown);
00161 ERR_OUT(str << std::endl);
00162 assert(str == " 100");
00163
00164 thrown = false;
00165 TEST(str = num_to_string(1000,9));
00166 assert(!thrown);
00167 ERR_OUT(str << std::endl);
00168 assert(str == " 1,000");
00169
00170 thrown = false;
00171 TEST(str = num_to_string(10000,9));
00172 assert(!thrown);
00173 ERR_OUT(str << std::endl);
00174 assert(str == " 10,000");
00175
00176 thrown = false;
00177 TEST(str = num_to_string(100000,9));
00178 assert(!thrown);
00179 ERR_OUT(str << std::endl);
00180 assert(str == " 100,000");
00181
00182 thrown = false;
00183 TEST(str = num_to_string(1000000,9));
00184 assert(!thrown);
00185 ERR_OUT(str << std::endl);
00186 assert(str == "1,000,000");
00187 }
00188
00189 void test3(void)
00190 {
00191 std::string str;
00192 bool thrown;
00193 uint64 ui64;
00194
00195 thrown = false;
00196 TEST(str = throughput_to_string(static_cast<signed char>(-127)));
00197 assert(!thrown);
00198 ERR_OUT(str << std::endl);
00199 assert(str == " -127 b/s");
00200
00201 thrown = false;
00202 ui64 = 1023;
00203 TEST(str = throughput_to_string(ui64));
00204 assert(!thrown);
00205 ERR_OUT(str << std::endl);
00206 assert(str == " 1023 b/s");
00207
00208 thrown = false;
00209 ui64 = 1024;
00210 TEST(str = throughput_to_string(ui64));
00211 assert(!thrown);
00212 ERR_OUT(str << std::endl);
00213 assert(str == " 1.0Kb/s");
00214
00215 thrown = false;
00216 ui64 = 1224;
00217 TEST(str = throughput_to_string(ui64));
00218 assert(!thrown);
00219 ERR_OUT(str << std::endl);
00220 assert(str == " 1.1Kb/s");
00221
00222 thrown = false;
00223 ui64 = 156789;
00224 ui64 *= 1000;
00225 ui64 *= 1000;
00226 ui64 *= 1000;
00227 ui64 *= 1000;
00228 TEST(str = throughput_to_string(ui64,"b","s",10,2,1000));
00229 assert(!thrown);
00230 ERR_OUT(str << std::endl);
00231 assert(str == "156.78Pb/s");
00232
00233 thrown = false;
00234 ui64 = 156;
00235 ui64 *= 1000;
00236 ui64 *= 1000;
00237 ui64 *= 1000;
00238 ui64 *= 1000;
00239 ui64 *= 1000;
00240 TEST(str = throughput_to_string(ui64,"b","s",10,1,1000));
00241 assert(!thrown);
00242 ERR_OUT(str << std::endl);
00243 assert(str == " 156.0Pb/s");
00244
00245 thrown = false;
00246 ui64 = 156;
00247 ui64 *= 1000;
00248 ui64 *= 1000;
00249 ui64 *= 1000;
00250 ui64 *= 1000;
00251 ui64 *= 1000;
00252 ui64 *= 1000;
00253 TEST(str = throughput_to_string(ui64,"b","s",10,1,1000));
00254 assert(thrown);
00255 ERR_OUT(str << std::endl);
00256 assert(str == " 156.0Pb/s");
00257 }
00258
00259 void test4(void)
00260 {
00261 const uint8 max = max_limit<uint8>();
00262 uint8 x;
00263 std::string s;
00264 std::string::size_type s_len;
00265 std::string::size_type idx;
00266
00267 for (x = 0; x != max; ++x) {
00268 s = percent_string(x,max);
00269 idx = s.find(".");
00270 s_len = s.size();
00271
00272 if (s_len - idx > 3)
00273 std::cerr
00274 << "percent_string("
00275 << static_cast<unsigned int>(x)
00276 << ","
00277 << static_cast<unsigned int>(max)
00278 << ") = "
00279 << s
00280 << std::endl;
00281
00282 assert(s_len - idx < 4);
00283 }
00284
00285 }
00286
00287 int main(int argc, char const * argv[])
00288 {
00289 try {
00290 test1();
00291 test2();
00292 test3();
00293 test4();
00294 }
00295 catch(error e) {
00296 std::cerr << e;
00297 assert(0);
00298 }
00299 catch(...) {
00300 std::cerr << err_unknown;
00301 assert(0);
00302 }
00303 return(0);
00304 }
00305