test_std_formatting.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #ifdef BOOST_LOCALE_NO_STD_BACKEND
  9. #include <iostream>
  10. int main()
  11. {
  12. std::cout << "STD Backend is not build... Skipping" << std::endl;
  13. }
  14. #else
  15. #include <boost/locale/formatting.hpp>
  16. #include <boost/locale/localization_backend.hpp>
  17. #include <boost/locale/generator.hpp>
  18. #include <boost/locale/encoding.hpp>
  19. #include <iomanip>
  20. #include "test_locale.hpp"
  21. #include "test_locale_tools.hpp"
  22. #include <iostream>
  23. //#define DEBUG_FMT
  24. #include <boost/config.hpp>
  25. #ifdef BOOST_MSVC
  26. # pragma warning(disable : 4996)
  27. #endif
  28. template<typename C1,typename C2>
  29. bool equal(std::basic_string<C1> const &s1,std::basic_string<C2> const &s2)
  30. {
  31. return boost::locale::conv::from_utf(s1,"UTF-8") == boost::locale::conv::from_utf(s2,"UTF-8");
  32. }
  33. template<>
  34. bool equal(std::string const &s1,std::string const &s2)
  35. {
  36. return s1==s2;
  37. }
  38. template<typename CharType>
  39. std::basic_string<CharType> conv_to_char(char const *p)
  40. {
  41. std::basic_string<CharType> r;
  42. while(*p)
  43. r+=CharType(*p++);
  44. return r;
  45. }
  46. template<typename CharType,typename RefCharType>
  47. void test_by_char(std::locale const &l,std::locale const &lreal)
  48. {
  49. typedef std::basic_stringstream<CharType> ss_type;
  50. typedef std::basic_stringstream<RefCharType> ss_ref_type;
  51. using namespace boost::locale;
  52. {
  53. std::cout << "- Testing as::posix" << std::endl;
  54. ss_type ss;
  55. ss.imbue(l);
  56. ss << 1045.45;
  57. TEST(ss);
  58. double n;
  59. ss >> n;
  60. TEST(ss);
  61. TEST(n == 1045.45);
  62. TEST(ss.str()==to_correct_string<CharType>("1045.45",l));
  63. #ifdef DEBUG_FMT
  64. std::cout << "[" << boost::locale::conv::from_utf(ss.str(),"UTF-8") << "]=\n" ;
  65. #endif
  66. }
  67. {
  68. std::cout << "- Testing as::number" << std::endl;
  69. ss_type ss;
  70. ss.imbue(l);
  71. ss << as::number;
  72. ss << 1045.45;
  73. TEST(ss);
  74. double n;
  75. ss >> n;
  76. TEST(ss);
  77. TEST(n == 1045.45);
  78. ss_ref_type ss_ref;
  79. ss_ref.imbue(lreal);
  80. ss_ref << 1045.45;
  81. TEST(equal(ss.str(),ss_ref.str()));
  82. #ifdef DEBUG_FMT
  83. std::cout << "[" << boost::locale::conv::from_utf(ss.str(),"UTF-8") << "]=\n" ;
  84. std::cout << "[" << boost::locale::conv::from_utf(ss_ref.str(),"UTF-8") << "]\n" ;
  85. #endif
  86. }
  87. {
  88. std::cout << "- Testing as::currency national " << std::endl;
  89. bool bad_parsing = false;
  90. ss_ref_type ss_ref;
  91. ss_ref.imbue(lreal);
  92. ss_ref << std::showbase;
  93. std::use_facet<std::money_put<RefCharType> >(lreal).put(ss_ref,false,ss_ref,RefCharType(' '),104334);
  94. { // workaround MSVC library issues
  95. std::ios_base::iostate err=std::ios_base::iostate();
  96. typename std::money_get<RefCharType>::iter_type end;
  97. long double tmp;
  98. std::use_facet<std::money_get<RefCharType> >(lreal).get(ss_ref,end,false,ss_ref,err,tmp);
  99. if(err & std::ios_base::failbit) {
  100. std::cout << "-- Looks like standard library does not support parsing well" << std::endl;
  101. bad_parsing=true;
  102. }
  103. }
  104. ss_type ss;
  105. ss.imbue(l);
  106. ss << as::currency;
  107. ss << 1043.34;
  108. if(!bad_parsing) {
  109. TEST(ss);
  110. double v1;
  111. ss >> v1;
  112. }
  113. TEST(equal(ss.str(),ss_ref.str()));
  114. #ifdef DEBUG_FMT
  115. std::cout << "[" << boost::locale::conv::from_utf(ss.str(),"UTF-8") << "]=\n" ;
  116. std::cout << "[" << boost::locale::conv::from_utf(ss_ref.str(),"UTF-8") << "]\n" ;
  117. #endif
  118. }
  119. {
  120. std::cout << "- Testing as::currency iso" << std::endl;
  121. ss_type ss;
  122. ss.imbue(l);
  123. ss << as::currency << as::currency_iso;
  124. ss << 1043.34;
  125. TEST(ss);
  126. double v1;
  127. ss >> v1;
  128. TEST(ss);
  129. TEST(v1==1043.34);
  130. ss_ref_type ss_ref;
  131. ss_ref.imbue(lreal);
  132. ss_ref << std::showbase;
  133. std::use_facet<std::money_put<RefCharType> >(lreal).put(ss_ref,true,ss_ref,RefCharType(' '),104334);
  134. TEST(equal(ss.str(),ss_ref.str()));
  135. #ifdef DEBUG_FMT
  136. std::cout << "[" << boost::locale::conv::from_utf(ss.str(),"UTF-8") << "]=\n" ;
  137. std::cout << "[" << boost::locale::conv::from_utf(ss_ref.str(),"UTF-8") << "]\n" ;
  138. #endif
  139. }
  140. {
  141. std::cout << "- Testing as::date/time" << std::endl;
  142. ss_type ss;
  143. ss.imbue(l);
  144. time_t a_date = 3600*24*(31+4); // Feb 5th
  145. time_t a_time = 3600*15+60*33; // 15:33:05
  146. time_t a_timesec = 13;
  147. time_t a_datetime = a_date + a_time + a_timesec;
  148. ss << as::time_zone("GMT");
  149. ss << as::date << a_datetime << CharType('\n');
  150. ss << as::time << a_datetime << CharType('\n');
  151. ss << as::datetime << a_datetime << CharType('\n');
  152. ss << as::time_zone("GMT+01:00");
  153. ss << as::ftime(conv_to_char<CharType>("%H")) << a_datetime << CharType('\n');
  154. ss << as::time_zone("GMT+00:15");
  155. ss << as::ftime(conv_to_char<CharType>("%M")) << a_datetime << CharType('\n');
  156. ss_ref_type ss_ref;
  157. ss_ref.imbue(lreal);
  158. std::basic_string<RefCharType> rfmt(conv_to_char<RefCharType>("%x\n%X\n%c\n16\n48\n"));
  159. std::tm tm=*gmtime(&a_datetime);
  160. std::use_facet<std::time_put<RefCharType> >(lreal).put(ss_ref,ss_ref,RefCharType(' '),&tm,rfmt.c_str(),rfmt.c_str()+rfmt.size());
  161. TEST(equal(ss.str(),ss_ref.str()));
  162. #ifdef DEBUG_FMT
  163. std::cout << "[" << boost::locale::conv::from_utf(ss.str(),"UTF-8") << "]=\n" ;
  164. std::cout << "[" << boost::locale::conv::from_utf(ss_ref.str(),"UTF-8") << "]\n" ;
  165. #endif
  166. }
  167. }
  168. int main()
  169. {
  170. try {
  171. boost::locale::localization_backend_manager mgr = boost::locale::localization_backend_manager::global();
  172. mgr.select("std");
  173. boost::locale::localization_backend_manager::global(mgr);
  174. boost::locale::generator gen;
  175. {
  176. std::cout << "en_US.UTF locale" << std::endl;
  177. std::string real_name;
  178. std::string name = get_std_name("en_US.UTF-8",&real_name);
  179. if(name.empty()) {
  180. std::cout << "en_US.UTF-8 not supported" << std::endl;
  181. }
  182. else {
  183. std::locale l1=gen(name),l2(real_name.c_str());
  184. std::cout << "UTF-8" << std::endl;
  185. if(name==real_name)
  186. test_by_char<char,char>(l1,l2);
  187. else
  188. test_by_char<char,wchar_t>(l1,l2);
  189. std::cout << "Wide UTF-" << sizeof(wchar_t) * 8 << std::endl;
  190. test_by_char<wchar_t,wchar_t>(l1,l2);
  191. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  192. std::cout << "char16 UTF-16" << std::endl;
  193. test_by_char<char16_t,char16_t>(l1,l2);
  194. #endif
  195. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  196. std::cout << "char32 UTF-32" << std::endl;
  197. test_by_char<char32_t,char32_t>(l1,l2);
  198. #endif
  199. }
  200. }
  201. {
  202. std::cout << "en_US.Latin-1 locale" << std::endl;
  203. std::string real_name;
  204. std::string name = get_std_name("en_US.ISO8859-1",&real_name);
  205. if(name.empty()) {
  206. std::cout << "en_US.ISO8859-8 not supported" << std::endl;
  207. }
  208. else {
  209. std::locale l1=gen(name),l2(real_name.c_str());
  210. test_by_char<char,char>(l1,l2);
  211. std::cout << "Wide UTF-" << sizeof(wchar_t) * 8 << std::endl;
  212. test_by_char<wchar_t,wchar_t>(l1,l2);
  213. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  214. std::cout << "char16 UTF-16" << std::endl;
  215. test_by_char<char16_t,char16_t>(l1,l2);
  216. #endif
  217. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  218. std::cout << "char32 UTF-32" << std::endl;
  219. test_by_char<char32_t,char32_t>(l1,l2);
  220. #endif
  221. }
  222. }
  223. {
  224. std::cout << "he_IL.UTF locale" << std::endl;
  225. std::string real_name;
  226. std::string name = get_std_name("he_IL.UTF-8",&real_name);
  227. if(name.empty()) {
  228. std::cout << "he_IL.UTF-8 not supported" << std::endl;
  229. }
  230. else {
  231. std::locale l1=gen(name),l2(real_name.c_str());
  232. std::cout << "UTF-8" << std::endl;
  233. if(name==real_name)
  234. test_by_char<char,char>(l1,l2);
  235. else
  236. test_by_char<char,wchar_t>(l1,l2);
  237. std::cout << "Wide UTF-" << sizeof(wchar_t) * 8 << std::endl;
  238. test_by_char<wchar_t,wchar_t>(l1,l2);
  239. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  240. std::cout << "char16 UTF-16" << std::endl;
  241. test_by_char<char16_t,char16_t>(l1,l2);
  242. #endif
  243. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  244. std::cout << "char32 UTF-32" << std::endl;
  245. test_by_char<char32_t,char32_t>(l1,l2);
  246. #endif
  247. }
  248. }
  249. {
  250. std::cout << "he_IL.ISO8859-8 locale" << std::endl;
  251. std::string real_name;
  252. std::string name = get_std_name("he_IL.ISO8859-8",&real_name);
  253. if(name.empty()) {
  254. std::cout << "he_IL.ISO8859-8 not supported" << std::endl;
  255. }
  256. else {
  257. std::locale l1=gen(name),l2(real_name.c_str());
  258. test_by_char<char,char>(l1,l2);
  259. std::cout << "Wide UTF-" << sizeof(wchar_t) * 8 << std::endl;
  260. test_by_char<wchar_t,wchar_t>(l1,l2);
  261. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  262. std::cout << "char16 UTF-16" << std::endl;
  263. test_by_char<char16_t,char16_t>(l1,l2);
  264. #endif
  265. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  266. std::cout << "char32 UTF-32" << std::endl;
  267. test_by_char<char32_t,char32_t>(l1,l2);
  268. #endif
  269. }
  270. }
  271. {
  272. std::cout << "Testing UTF-8 punct workaround" << std::endl;
  273. std::string real_name;
  274. std::string name = get_std_name("ru_RU.UTF-8",&real_name);
  275. if(name.empty()) {
  276. std::cout << "- No russian locale" << std::endl;
  277. }
  278. else if(name != real_name) {
  279. std::cout << "- Not having UTF-8 locale, no need for workaround" << std::endl;
  280. }
  281. else {
  282. std::locale l1=gen(name),l2(real_name.c_str());
  283. bool fails = false;
  284. try {
  285. std::ostringstream ss;
  286. ss.imbue(l2);
  287. ss << 12345.45;
  288. boost::locale::conv::from_utf<char>(ss.str(),"windows-1251",boost::locale::conv::stop);
  289. fails = false;
  290. }
  291. catch(...) {
  292. fails = true;
  293. }
  294. if(!fails) {
  295. std::cout << "- No invalid UTF. No need to check"<<std::endl;
  296. }
  297. else {
  298. std::ostringstream ss;
  299. ss.imbue(l1);
  300. ss << std::setprecision(10) ;
  301. ss << boost::locale::as::number << 12345.45;
  302. TEST(ss.str() == "12 345,45" || ss.str()=="12345,45");
  303. }
  304. }
  305. }
  306. }
  307. catch(std::exception const &e) {
  308. std::cerr << "Failed " << e.what() << std::endl;
  309. return EXIT_FAILURE;
  310. }
  311. FINALIZE();
  312. }
  313. #endif // no std
  314. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
  315. // boostinspect:noascii