stream_converter.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // Boost.Convert test and usage example
  2. // Copyright (c) 2009-2016 Vladimir Batov.
  3. // Use, modification and distribution are subject to the Boost Software License,
  4. // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
  5. #include "./test.hpp"
  6. #if defined(BOOST_CONVERT_IS_NOT_SUPPORTED)
  7. int main(int, char const* []) { return 0; }
  8. #else
  9. #include <boost/convert.hpp>
  10. #include <boost/convert/stream.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <cstdio>
  13. #include <cstdlib>
  14. #include <stdlib.h>
  15. //[stream_using
  16. using std::string;
  17. using std::wstring;
  18. using boost::convert;
  19. //]
  20. //[stream_cnv_namespace_shortcut
  21. namespace cnv = boost::cnv;
  22. namespace arg = boost::cnv::parameter;
  23. //]
  24. static
  25. void
  26. test_dbl_to_str()
  27. {
  28. boost::cnv::cstream cnv;
  29. cnv(std::fixed);
  30. BOOST_TEST(convert<string>( 99.999, cnv(arg::precision = 2)).value_or("bad") == "100.00");
  31. BOOST_TEST(convert<string>( 99.949, cnv(arg::precision = 2)).value_or("bad") == "99.95");
  32. BOOST_TEST(convert<string>(-99.949, cnv(arg::precision = 2)).value_or("bad") == "-99.95");
  33. BOOST_TEST(convert<string>( 99.949, cnv(arg::precision = 1)).value_or("bad") == "99.9");
  34. BOOST_TEST(convert<string>( 0.999, cnv(arg::precision = 2)).value_or("bad") == "1.00");
  35. BOOST_TEST(convert<string>( -0.999, cnv(arg::precision = 2)).value_or("bad") == "-1.00");
  36. BOOST_TEST(convert<string>( 0.949, cnv(arg::precision = 2)).value_or("bad") == "0.95");
  37. BOOST_TEST(convert<string>( -0.949, cnv(arg::precision = 2)).value_or("bad") == "-0.95");
  38. BOOST_TEST(convert<string>( 1.949, cnv(arg::precision = 1)).value_or("bad") == "1.9");
  39. BOOST_TEST(convert<string>( -1.949, cnv(arg::precision = 1)).value_or("bad") == "-1.9");
  40. }
  41. static
  42. void
  43. test_numbase()
  44. {
  45. //[stream_numbase_example1
  46. /*`The following example demonstrates the deployment of `std::dec`, `std::oct` `std::hex`
  47. manipulators:
  48. */
  49. boost::cnv::cstream ccnv;
  50. BOOST_TEST(convert<int>( "11", ccnv(std::hex)).value_or(0) == 17); // 11(16) = 17(10)
  51. BOOST_TEST(convert<int>( "11", ccnv(std::oct)).value_or(0) == 9); // 11(8) = 9(10)
  52. BOOST_TEST(convert<int>( "11", ccnv(std::dec)).value_or(0) == 11);
  53. BOOST_TEST(convert<string>( 18, ccnv(std::hex)).value_or("bad") == "12"); // 18(10) = 12(16)
  54. BOOST_TEST(convert<string>( 10, ccnv(std::oct)).value_or("bad") == "12"); // 10(10) = 12(8)
  55. BOOST_TEST(convert<string>( 12, ccnv(std::dec)).value_or("bad") == "12");
  56. BOOST_TEST(convert<string>(255, ccnv(arg::base = boost::cnv::base::oct)).value_or("bad") == "377");
  57. BOOST_TEST(convert<string>(255, ccnv(arg::base = boost::cnv::base::hex)).value_or("bad") == "ff");
  58. BOOST_TEST(convert<string>(255, ccnv(arg::base = boost::cnv::base::dec)).value_or("bad") == "255");
  59. ccnv(std::showbase);
  60. BOOST_TEST(convert<string>(18, ccnv(std::hex)).value_or("bad") == "0x12");
  61. BOOST_TEST(convert<string>(10, ccnv(std::oct)).value_or("bad") == "012");
  62. ccnv(std::uppercase);
  63. BOOST_TEST(convert<string>(18, ccnv(std::hex)).value_or("bad") == "0X12");
  64. //]
  65. //[stream_numbase_example2
  66. BOOST_TEST(convert<int>("11", ccnv(arg::base = cnv::base::hex)).value_or(0) == 17);
  67. BOOST_TEST(convert<int>("11", ccnv(arg::base = cnv::base::oct)).value_or(0) == 9);
  68. BOOST_TEST(convert<int>("11", ccnv(arg::base = cnv::base::dec)).value_or(0) == 11);
  69. //]
  70. //[wide_stream_numeric_base
  71. boost::cnv::wstream wcnv;
  72. BOOST_TEST(convert<int>(L"11", wcnv(std::hex)).value_or(0) == 17); // 11(16) = 17(10)
  73. BOOST_TEST(convert<int>(L"11", wcnv(std::oct)).value_or(0) == 9); // 11(8) = 9(10)
  74. BOOST_TEST(convert<int>(L"11", wcnv(std::dec)).value_or(0) == 11);
  75. BOOST_TEST(convert<wstring>(254, wcnv(arg::base = cnv::base::dec)).value_or(L"bad") == L"254");
  76. BOOST_TEST(convert<wstring>(254, wcnv(arg::base = cnv::base::hex)).value_or(L"bad") == L"fe");
  77. BOOST_TEST(convert<wstring>(254, wcnv(arg::base = cnv::base::oct)).value_or(L"bad") == L"376");
  78. //]
  79. }
  80. static
  81. void
  82. test_boolalpha()
  83. {
  84. boost::cnv::cstream cnv;
  85. //[stream_boolalpha_example
  86. BOOST_TEST(convert<string>( true, cnv(std::boolalpha)).value_or("bad") == "true");
  87. BOOST_TEST(convert<string>(false, cnv(std::boolalpha)).value_or("bad") == "false");
  88. BOOST_TEST(convert<bool>( "true", cnv(std::boolalpha)).value_or(false) == true);
  89. BOOST_TEST(convert<bool>("false", cnv(std::boolalpha)).value_or( true) == false);
  90. BOOST_TEST(convert<string>( true, cnv(std::noboolalpha)).value_or("bad") == "1");
  91. BOOST_TEST(convert<string>(false, cnv(std::noboolalpha)).value_or("bad") == "0");
  92. BOOST_TEST(convert<bool>("1", cnv(std::noboolalpha)).value_or(false) == true);
  93. BOOST_TEST(convert<bool>("0", cnv(std::noboolalpha)).value_or( true) == false);
  94. //]
  95. }
  96. static
  97. void
  98. test_skipws_char()
  99. {
  100. //[stream_skipws_example
  101. boost::cnv::cstream ccnv;
  102. char const* const cstr_good = " 123";
  103. char const* const cstr_bad = " 123 "; // std::skipws only affects leading spaces.
  104. ccnv(std::skipws); // Ignore leading whitespaces
  105. // ccnv(arg::skipws = true); // Ignore leading whitespaces. Alternative interface
  106. BOOST_TEST(convert<int>(cstr_good, ccnv).value_or(0) == 123);
  107. BOOST_TEST(convert<string>(" 123", ccnv).value_or("bad") == "123");
  108. BOOST_TEST(!convert<int>(cstr_bad, ccnv));
  109. ccnv(std::noskipws); // Do not ignore leading whitespaces
  110. // ccnv(arg::skipws = false); // Do not ignore leading whitespaces. Alternative interface
  111. // All conversions fail.
  112. BOOST_TEST(!convert<int>(cstr_good, ccnv));
  113. BOOST_TEST(!convert<int>( cstr_bad, ccnv));
  114. //]
  115. }
  116. static
  117. void
  118. test_skipws_wchar()
  119. {
  120. //[wide_stream_skipws
  121. boost::cnv::wstream wcnv;
  122. wcnv(std::noskipws); // Do not ignore leading whitespaces
  123. BOOST_TEST( convert<int>( L"123", wcnv).value_or(0) == 123);
  124. BOOST_TEST(!convert<int>( L" 123", wcnv));
  125. BOOST_TEST(!convert<int>(L" 123 ", wcnv));
  126. wcnv(std::skipws); // Ignore leading whitespaces
  127. // wcnv(arg::skipws = true); // Ignore leading whitespaces. Alternative interface
  128. BOOST_TEST( convert<int>( L" 123", wcnv).value_or(0) == 123);
  129. BOOST_TEST(!convert<int>(L" 123 ", wcnv));
  130. //]
  131. }
  132. static
  133. void
  134. test_width()
  135. {
  136. //[stream_width_example
  137. boost::cnv::cstream cnv;
  138. boost::optional<string> s01 = convert<string>(12, cnv(std::setw(4)));
  139. boost::optional<string> s02 = convert<string>(12, cnv(std::setw(5))(std::setfill('*')));
  140. boost::optional<string> s03 = convert<string>(12, cnv(std::setw(5))(std::setfill('*'))(std::left));
  141. BOOST_TEST(s01 && s01.value() == " 12"); // Field width = 4.
  142. BOOST_TEST(s02 && s02.value() == "***12"); // Field width = 5, filler = '*'.
  143. BOOST_TEST(s03 && s03.value() == "12***"); // Field width = 5, filler = '*', left adjustment
  144. /*`It needs to be remembered that `boost::cnv::stream` converter uses `std::stream` as its underlying
  145. conversion engine. Consequently, formatting-related behavior are driven by the `std::stream`. Namely,
  146. after every operation is performed, the ['default field width is restored]. The values of
  147. the fill character and the adjustment remain unchanged until they are modified explicitly.
  148. */
  149. // The fill and adjustment remain '*' and 'left'.
  150. boost::optional<string> s11 = convert<string>(12, cnv(arg::width = 4));
  151. boost::optional<string> s12 = convert<string>(12, cnv(arg::width = 5)
  152. (arg::fill = ' ')
  153. (arg::adjust = cnv::adjust::right));
  154. BOOST_TEST(s11 && s11.value() == "12**"); // Field width was set to 4.
  155. BOOST_TEST(s12 && s12.value() == " 12"); // Field width was set to 5 with the ' ' filler.
  156. //]
  157. }
  158. static
  159. void
  160. test_manipulators()
  161. {
  162. boost::cnv::cstream ccnv;
  163. boost::cnv::wstream wcnv;
  164. int const hex_v01 = boost::convert<int>("FF", ccnv(std::hex)).value_or(0);
  165. int const hex_v02 = boost::convert<int>(L"F", wcnv(std::hex)).value_or(0);
  166. int const hex_v03 = boost::convert<int>("FF", ccnv(std::dec)).value_or(-5);
  167. int const hex_v04 = boost::convert<int>(L"F", wcnv(std::dec)).value_or(-6);
  168. BOOST_TEST(hex_v01 == 255); // "FF"
  169. BOOST_TEST(hex_v02 == 15); // L"F"
  170. BOOST_TEST(hex_v03 == -5); // Failed conversion
  171. BOOST_TEST(hex_v04 == -6); // Failed conversion
  172. ccnv(std::noshowbase)(std::nouppercase)(std::oct);
  173. BOOST_TEST(boost::convert<string>(255, ccnv).value_or("bad") == "377");
  174. BOOST_TEST(boost::convert<string>( 15, ccnv).value_or("bad") == "17");
  175. ccnv(std::showbase);
  176. BOOST_TEST(boost::convert<string>(255, ccnv).value_or("bad") == "0377");
  177. BOOST_TEST(boost::convert<string>( 15, ccnv).value_or("bad") == "017");
  178. ccnv(std::uppercase)(std::hex);
  179. BOOST_TEST(boost::convert<string>(255, ccnv).value_or("bad") == "0XFF");
  180. BOOST_TEST(boost::convert<string>( 15, ccnv).value_or("bad") == "0XF");
  181. ccnv(std::noshowbase)(std::nouppercase)(std::oct);
  182. BOOST_TEST(boost::convert<string>(255, ccnv).value_or("bad") == "377");
  183. BOOST_TEST(boost::convert<string>( 15, ccnv).value_or("bad") == "17");
  184. ccnv(std::showbase)(arg::uppercase = true)(arg::base = cnv::base::hex);
  185. BOOST_TEST(boost::convert<string>(255, ccnv).value_or("bad") == "0XFF");
  186. BOOST_TEST(boost::convert<string>( 15, ccnv).value_or("bad") == "0XF");
  187. }
  188. void
  189. test_locale_example()
  190. {
  191. //[stream_locale_example1
  192. boost::cnv::cstream cnv;
  193. std::locale rus_locale;
  194. std::locale eng_locale;
  195. char const* eng_locale_name = test::cnv::is_msc ? "English_United States.1251" : "en_US.UTF-8";
  196. char const* rus_locale_name = test::cnv::is_msc ? "Russian_Russia.1251" : "ru_RU.UTF-8";
  197. char const* rus_expected = test::cnv::is_msc ? "1,235e-002" : "1,235e-02";
  198. char const* eng_expected = test::cnv::is_msc ? "1.235e-002" : "1.235e-02";
  199. char const* dbl_expected = test::cnv::is_msc ? "1.2345E-002" : "1.2345E-02";
  200. // cnv(std::setprecision(4))(std::uppercase)(std::scientific);
  201. cnv(arg::precision = 4)
  202. (arg::uppercase = true)
  203. (arg::notation = cnv::notation::scientific);
  204. double double_v01 = convert<double>(dbl_expected, cnv).value_or(0);
  205. string double_s02 = convert<string>(double_v01, cnv).value_or("bad");
  206. BOOST_TEST(dbl_expected == double_s02);
  207. try { rus_locale = std::locale(rus_locale_name); }
  208. catch (...) { printf("Bad locale %s.\n", rus_locale_name); exit(1); }
  209. try { eng_locale = std::locale(eng_locale_name); }
  210. catch (...) { printf("Bad locale %s.\n", eng_locale_name); exit(1); }
  211. // cnv(std::setprecision(3))(std::nouppercase);
  212. cnv(arg::precision = 3)(arg::uppercase = false);
  213. string double_rus = convert<string>(double_v01, cnv(rus_locale)).value_or("bad double_rus");
  214. string double_eng = convert<string>(double_v01, cnv(eng_locale)).value_or("bad double_eng");
  215. BOOST_TEST(double_rus == rus_expected);
  216. BOOST_TEST(double_eng == eng_expected);
  217. //]
  218. }
  219. void
  220. test_locale(double v, boost::cnv::cstream const& cnv, char const* expected)
  221. {
  222. boost::optional<string> res = convert<string>(v, cnv);
  223. std::string str = res ? *res : "conversion failed";
  224. BOOST_TEST(res);
  225. BOOST_TEST(str == expected);
  226. if (str != expected)
  227. printf("%s [%d]: result=<%s>, expected=<%s>\n", __FILE__, __LINE__, str.c_str(), expected);
  228. }
  229. static
  230. void
  231. test_locale()
  232. {
  233. boost::cnv::cstream cnv;
  234. std::locale rus_locale;
  235. std::locale eng_locale;
  236. bool eng_ignore = false;
  237. bool rus_ignore = false;
  238. char const* eng_locale_name = test::cnv::is_msc ? "English_United States.1251" : "en_US.UTF-8";
  239. char const* rus_locale_name = test::cnv::is_msc ? "Russian_Russia.1251" : "ru_RU.UTF-8";
  240. char const* eng_expected = test::cnv::is_old_msc ? "1.235e-002" : "1.235e-02";
  241. char const* rus_expected = test::cnv::is_old_msc ? "1,235e-002" : "1,235e-02";
  242. char const* dbl_expected = test::cnv::is_old_msc ? "1.2345E-002" : "1.2345E-02";
  243. cnv(arg::precision = 4)
  244. (arg::uppercase = true)
  245. (arg::notation = cnv::notation::scientific);
  246. double const double_v01 = convert<double>(dbl_expected, cnv).value_or(0);
  247. string const double_s02 = convert<string>(double_v01, cnv).value_or("bad");
  248. BOOST_TEST(double_v01 != 0);
  249. BOOST_TEST(dbl_expected == double_s02);
  250. if (dbl_expected != double_s02)
  251. printf("%s [%d]: <%s> != <%s>\n", __FILE__, __LINE__, dbl_expected, double_s02.c_str());
  252. try { eng_locale = std::locale(eng_locale_name); }
  253. catch (...) { printf("Bad locale %s. Ignored.\n", eng_locale_name); eng_ignore = true; }
  254. try { rus_locale = std::locale(rus_locale_name); }
  255. catch (...) { printf("Bad locale %s. Ignored.\n", rus_locale_name); rus_ignore = true; }
  256. // cnv(std::setprecision(3))(std::nouppercase);
  257. cnv(arg::precision = 3)(arg::uppercase = false);
  258. if (!eng_ignore) test_locale(double_v01, cnv(eng_locale), eng_expected);
  259. if (!rus_ignore) test_locale(double_v01, cnv(rus_locale), rus_expected);
  260. }
  261. static
  262. void
  263. test_user_str()
  264. {
  265. //[stream_my_string
  266. boost::cnv::cstream cnv;
  267. my_string my_str("123");
  268. cnv(std::setprecision(2))(std::fixed);
  269. BOOST_TEST(convert<int>(my_str, cnv).value_or(0) == 123);
  270. BOOST_TEST(convert<my_string>( 99.999, cnv).value_or("bad") == "100.00");
  271. BOOST_TEST(convert<my_string>( 99.949, cnv).value_or("bad") == "99.95");
  272. BOOST_TEST(convert<my_string>(-99.949, cnv).value_or("bad") == "-99.95");
  273. //]
  274. }
  275. int
  276. main(int, char const* [])
  277. {
  278. try
  279. {
  280. // QNX fails to handle std::skipws for wchat_t.
  281. // Excluding from tests so that I do not have to stare on the yellow box (regression failure)
  282. /*********************/ test_skipws_char();
  283. if (!test::cnv::is_qnx) test_skipws_wchar();
  284. test_numbase();
  285. test_boolalpha();
  286. test_width();
  287. test_manipulators();
  288. test_locale();
  289. test_dbl_to_str();
  290. test_user_str();
  291. }
  292. catch(boost::bad_optional_access const&)
  293. {
  294. BOOST_TEST(!"Caught boost::bad_optional_access exception");
  295. }
  296. catch(...)
  297. {
  298. BOOST_TEST(!"Caught an unknown exception");
  299. }
  300. return boost::report_errors();
  301. }
  302. #endif