00001
00011 #include <stdlib.h>
00012
00013 #include <algorithm>
00014 #include <iterator>
00015 #include <iostream>
00016 #include <utility>
00017
00018 #include "string_manip.h"
00019
00020 using namespace std;
00021
00022 template <typename Input, typename Output>
00023 struct input_output {
00024 Input input;
00025 Output output;
00026 };
00027
00028
00029 template <typename Input, typename Output, typename Result>
00030 static void check_result(char const * fct_name, Input const & input,
00031 Output const & output, Result const & result)
00032 {
00033 if (result != output) {
00034 cerr << fct_name << ": \n"
00035 << "for:\n\"" << input << "\"\n"
00036 << "expect:\n\"" << output << "\"\n"
00037 << "found:\n\"" << result << "\"\n";
00038 exit(EXIT_FAILURE);
00039 }
00040 }
00041
00042
00043 static input_output<char const*, char const*> expect_erase[] =
00044 {
00045 { "", "" },
00046 { ";;;", "" },
00047 { "ab;;;cd", "cd" },
00048 { ";;;cd", "cd" },
00049 { "ab;;;", "" },
00050 { 0, 0 }
00051 };
00052
00053 static void erase_to_last_of_tests()
00054 {
00055 input_output<char const *, char const*> const * cur;
00056 for (cur = expect_erase; cur->input; ++cur) {
00057 string result = erase_to_last_of(cur->input, ';');
00058 check_result("erase_to_last_of()", cur->input, cur->output,
00059 result);
00060 }
00061 }
00062
00063
00064 static input_output<char const *, pair<string, string> > expect_split[] =
00065 {
00066 #define MAKE_PAIR(a, b) make_pair(string(a), string(b))
00067 { "ab;cd", MAKE_PAIR("ab", "cd") },
00068 { ";cd", MAKE_PAIR("", "cd") },
00069 { "ab;", MAKE_PAIR("ab", "") },
00070 { "b;d", MAKE_PAIR("b", "d") },
00071 { ";d", MAKE_PAIR("", "d") },
00072 { "a;", MAKE_PAIR("a", "") },
00073 { ";", MAKE_PAIR("", "") },
00074 { "", MAKE_PAIR("", "") },
00075 { 0, MAKE_PAIR("", "") }
00076 #undef MAKE_PAIR
00077 };
00078
00079 static void split_tests()
00080 {
00081 input_output<char const *, pair<string, string> > const * cur;
00082 for (cur = expect_split; cur->input; ++cur) {
00083 string temp = cur->input;
00084 string result = split(temp, ';');
00085 check_result("split()", cur->input, cur->output.first, temp);
00086 check_result("split()", cur->input, cur->output.second, result);
00087 }
00088 }
00089
00090 static input_output<char const *, pair<string, bool> > expect_is_prefix[] =
00091 {
00092 #define MAKE_PAIR(a, b) make_pair(string(a), b)
00093 { "abcd", MAKE_PAIR("abc", true) },
00094 { "abcd", MAKE_PAIR("ac", false) },
00095 { "babcd", MAKE_PAIR("abc", false) },
00096
00097
00098
00099
00100 { 0, MAKE_PAIR("", true) }
00101 #undef MAKE_PAIR
00102 };
00103
00104 static void is_prefix_tests()
00105 {
00106 input_output<char const *, pair<string, bool> > const * cur;
00107 for (cur = expect_is_prefix; cur->input; ++cur) {
00108 bool result = is_prefix(cur->input, cur->output.first);
00109 if (result != cur->output.second) {
00110 cerr << "is_prefix(" << cur->input << ", "
00111 << cur->output.first << ") "
00112 << "return " << result << endl;
00113 exit(EXIT_FAILURE);
00114 }
00115 }
00116 }
00117
00118
00119 static const size_t max_token = 8;
00120 static input_output<char const *, char const *[max_token]> expect_separate_token[] =
00121 {
00122 { "aa", { "aa" } },
00123 { "a\\c", { "a\\c" } },
00124 { "a\\\\c", { "a\\\\c" } },
00125 { "a\\\\c\\", { "a\\\\c\\" } },
00126 { "ab;cd;ef;gh", { "ab", "cd", "ef", "gh" } },
00127 { "ab\\;cd", { "ab;cd" } },
00128 { "a;a", { "a", "a" } },
00129 { ";a", { "", "a" } },
00130 { ";", { "", "" } },
00131 { ";;", { "", "", "" } },
00132 { 0, { 0, } }
00133 };
00134
00135
00136 static void separate_token_tests()
00137 {
00138 input_output<char const *, char const *[max_token]> const * cur;
00139 for (cur = expect_separate_token; cur->input; ++cur) {
00140 vector<string> result = separate_token(cur->input, ';');
00141 if (result.size() > max_token) {
00142 cerr << "separate_token(): too many token\n"
00143 << "input:\n"
00144 << '"' << cur->input << "\"\n"
00145 << "output\n";
00146 copy(result.begin(), result.end(),
00147 ostream_iterator<string>(cerr, "\n"));
00148 exit(EXIT_FAILURE);
00149 }
00150 for (size_t i = 0; i < result.size(); ++i) {
00151 if (result[i] != cur->output[i]) {
00152 cerr << "separate_token():\n"
00153 << "input:\n"
00154 << cur->input << endl;
00155 cerr << "expect:\n";
00156 for (size_t i = 0; i < max_token; ++i) {
00157 if (!cur->output[i])
00158 break;
00159 cerr << cur->output[i] << endl;
00160 }
00161 cerr << "output:\n";
00162 copy(result.begin(), result.end(),
00163 ostream_iterator<string>(cerr, "\n"));
00164 exit(EXIT_FAILURE);
00165 }
00166 }
00167 }
00168 }
00169
00170
00171 static input_output<char const *, char const *> expect_rtrim[] =
00172 {
00173 { "abc", "abc" },
00174 { "abc ", "abc" },
00175 { " abc ", " abc" },
00176 { " abc \t \t", " abc" },
00177 { " ", "" },
00178 { "\t \t", "" },
00179 { "", "" },
00180 { 0, 0 }
00181 };
00182
00183 static void rtrim_tests()
00184 {
00185 input_output<char const *, char const*> const * cur;
00186 for (cur = expect_rtrim; cur->input; ++cur) {
00187 string result = rtrim(cur->input);
00188 check_result("rtrim()", cur->input, cur->output, result);
00189 }
00190 }
00191
00192
00193 static input_output<char const *, char const *> expect_ltrim[] =
00194 {
00195 { "abc", "abc" },
00196 { "abc ", "abc " },
00197 { " abc ", "abc " },
00198 { "\t \tabc ", "abc " },
00199 { " ", "" },
00200 { "\t \t", "" },
00201 { "", "" },
00202 { 0, 0 }
00203 };
00204
00205 static void ltrim_tests()
00206 {
00207 input_output<char const *, char const*> const * cur;
00208 for (cur = expect_ltrim; cur->input; ++cur) {
00209 string result = ltrim(cur->input);
00210 check_result("ltrim()", cur->input, cur->output, result);
00211 }
00212 }
00213
00214
00215 static input_output<char const *, char const *> expect_trim[] =
00216 {
00217 { "abc", "abc" },
00218 { "abc ", "abc" },
00219 { " abc ", "abc" },
00220 { "\t \tabc \t", "abc" },
00221 { " ", "" },
00222 { "\t \t", "" },
00223 { "", "" },
00224 { 0, 0 }
00225 };
00226
00227 static void trim_tests()
00228 {
00229 input_output<char const *, char const*> const * cur;
00230 for (cur = expect_trim; cur->input; ++cur) {
00231 string result = trim(cur->input);
00232 check_result("trim()", cur->input, cur->output, result);
00233 }
00234 }
00235
00236
00237 static input_output<double, char const *> expect_format_percent[] =
00238 {
00239 { 2.2, " 2.2000" },
00240 { 0, " 0" },
00241 { 100.00, "100.000" },
00242 { 99.99999, "100.000" },
00243 { 0.00000344, "3.4e-06" },
00244
00245
00246 { -1.0, 0 }
00247 };
00248
00249 static void format_percent_tests()
00250 {
00251 input_output<double, char const*> const * cur;
00252 for (cur = expect_format_percent; cur->input != -1.0; ++cur) {
00253 string result = format_percent(cur->input, percent_int_width,
00254 percent_fract_width);
00255 check_result("format_percent()", cur->input, cur->output,
00256 result);
00257 }
00258 }
00259
00260
00261 static input_output<unsigned int, char const *> expect_from_str_to_uint[] =
00262 {
00263 { 123, "123" },
00264 { 33, "33" },
00265 { 0, "0" },
00266 { 0, 0 }
00267 };
00268
00269 static void tostr_tests()
00270 {
00271 input_output<unsigned int, char const *> const * cur;
00272 for (cur = expect_from_str_to_uint; cur->output; ++cur) {
00273 string result = op_lexical_cast<string>(cur->input);
00274 check_result("op_lexical_cast()", cur->input,
00275 cur->output, result);
00276 }
00277 }
00278
00279 static void touint_tests()
00280 {
00281
00282 input_output<unsigned int, char const *> const * cur;
00283 for (cur = expect_from_str_to_uint; cur->output; ++cur) {
00284 unsigned int result =
00285 op_lexical_cast<unsigned int>(cur->output);
00286 check_result("op_lexical_cast()", cur->output, cur->input,
00287 result);
00288 }
00289 }
00290
00291
00292 static input_output<char const*, bool> expect_from_str_to_bool[] =
00293 {
00294 { "0", false },
00295 { "1", true },
00296 { 0, 0 }
00297 };
00298
00299 static void tobool_tests()
00300 {
00301 input_output<char const *, bool> const * cur;
00302 for (cur = expect_from_str_to_bool; cur->input; ++cur) {
00303 bool result = op_lexical_cast<bool>(cur->input);
00304 check_result("op_lexical_cast()", cur->input, cur->output,
00305 result);
00306 }
00307 }
00308
00309
00310
00311 int main()
00312 {
00313 erase_to_last_of_tests();
00314 tostr_tests();
00315 touint_tests();
00316 tobool_tests();
00317 split_tests();
00318 is_prefix_tests();
00319 separate_token_tests();
00320 rtrim_tests();
00321 ltrim_tests();
00322 trim_tests();
00323 format_percent_tests();
00324 return EXIT_SUCCESS;
00325 }