test_posix_formatting.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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_POSIX_BACKEND
  9. #include <iostream>
  10. int main()
  11. {
  12. std::cout << "POSIX 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 <boost/locale/info.hpp>
  20. #include <iomanip>
  21. #include "test_locale.hpp"
  22. #include "test_locale_tools.hpp"
  23. #include "test_posix_tools.hpp"
  24. #include <iostream>
  25. #include <time.h>
  26. #include <monetary.h>
  27. #include <assert.h>
  28. #include <langinfo.h>
  29. //#define DEBUG_FMT
  30. bool equal(std::string const &s1,std::string const &s2,locale_t /*lc*/)
  31. {
  32. return s1 == s2;
  33. }
  34. bool equal(std::wstring const &s1,std::string const &s2,locale_t lc)
  35. {
  36. return s1 == boost::locale::conv::to_utf<wchar_t>(s2,nl_langinfo_l(CODESET,lc));
  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,locale_t lreal)
  48. {
  49. typedef std::basic_stringstream<CharType> ss_type;
  50. using namespace boost::locale;
  51. {
  52. std::cout << "- Testing as::posix" << std::endl;
  53. ss_type ss;
  54. ss.imbue(l);
  55. ss << 1045.45;
  56. TEST(ss);
  57. double n;
  58. ss >> n;
  59. TEST(ss);
  60. TEST(n == 1045.45);
  61. TEST(ss.str()==to_correct_string<CharType>("1045.45",l));
  62. #ifdef DEBUG_FMT
  63. std::cout << "[" << boost::locale::conv::from_utf(ss.str(),"UTF-8") << "]=\n" ;
  64. #endif
  65. }
  66. {
  67. std::cout << "- Testing as::number" << std::endl;
  68. ss_type ss;
  69. ss.imbue(l);
  70. ss << as::number;
  71. ss << 1045.45;
  72. TEST(ss);
  73. double n;
  74. ss >> n;
  75. TEST(ss);
  76. TEST(n == 1045.45);
  77. if(std::use_facet<boost::locale::info>(l).country()=="US")
  78. TEST(equal(ss.str(),"1,045.45",lreal));
  79. }
  80. {
  81. std::cout << "- Testing as::currency national " << std::endl;
  82. char buf[256];
  83. strfmon_l(buf,256,lreal,"%n",1043.34);
  84. ss_type ss;
  85. ss.imbue(l);
  86. ss << as::currency;
  87. ss << 1043.34;
  88. TEST(ss);
  89. TEST(equal(ss.str(),buf,lreal));
  90. #ifdef DEBUG_FMT
  91. std::cout << "[" << boost::locale::conv::from_utf(ss.str(),"UTF-8") << "]=\n" ;
  92. std::cout << "[" << boost::locale::conv::from_utf(buf,"UTF-8") << "]\n" ;
  93. #endif
  94. }
  95. {
  96. std::cout << "- Testing as::currency iso" << std::endl;
  97. char buf[256];
  98. strfmon_l(buf,256,lreal,"%i",1043.34);
  99. ss_type ss;
  100. ss.imbue(l);
  101. ss << as::currency << as::currency_iso;
  102. ss << 1043.34;
  103. TEST(ss);
  104. TEST(equal(ss.str(),buf,lreal));
  105. #ifdef DEBUG_FMT
  106. std::cout << "[" << boost::locale::conv::from_utf(ss.str(),"UTF-8") << "]=\n" ;
  107. std::cout << "[" << boost::locale::conv::from_utf(buf,"UTF-8") << "]\n" ;
  108. #endif
  109. }
  110. {
  111. std::cout << "- Testing as::date/time" << std::endl;
  112. ss_type ss;
  113. ss.imbue(l);
  114. time_t a_date = 3600*24*(31+4); // Feb 5th
  115. time_t a_time = 3600*15+60*33; // 15:33:05
  116. time_t a_timesec = 13;
  117. time_t a_datetime = a_date + a_time + a_timesec;
  118. ss << as::time_zone("GMT");
  119. ss << as::date << a_datetime << CharType('\n');
  120. ss << as::time << a_datetime << CharType('\n');
  121. ss << as::datetime << a_datetime << CharType('\n');
  122. ss << as::time_zone("GMT+01:00");
  123. ss << as::ftime(conv_to_char<CharType>("%H")) << a_datetime << CharType('\n');
  124. ss << as::time_zone("GMT+00:15");
  125. ss << as::ftime(conv_to_char<CharType>("%M")) << a_datetime << CharType('\n');
  126. std::tm tm=*gmtime(&a_datetime);
  127. char buf[256];
  128. strftime_l(buf,sizeof(buf),"%x\n%X\n%c\n16\n48\n",&tm,lreal);
  129. TEST(equal(ss.str(),buf,lreal));
  130. #ifdef DEBUG_FMT
  131. std::cout << "[" << boost::locale::conv::from_utf(ss.str(),"UTF-8") << "]=\n" ;
  132. std::cout << "[" << boost::locale::conv::from_utf(buf,"UTF-8") << "]\n" ;
  133. #endif
  134. }
  135. }
  136. int main()
  137. {
  138. locale_t lreal = 0;
  139. try {
  140. boost::locale::localization_backend_manager mgr = boost::locale::localization_backend_manager::global();
  141. mgr.select("posix");
  142. boost::locale::localization_backend_manager::global(mgr);
  143. boost::locale::generator gen;
  144. std::string name;
  145. {
  146. std::cout << "en_US.UTF locale" << std::endl;
  147. name="en_US.UTF-8";
  148. if(!have_locale(name)) {
  149. std::cout << "en_US.UTF-8 not supported" << std::endl;
  150. }
  151. else {
  152. std::locale l1=gen(name);
  153. lreal=newlocale(LC_ALL_MASK,name.c_str(),0);
  154. assert(lreal);
  155. std::cout << "UTF-8" << std::endl;
  156. test_by_char<char,char>(l1,lreal);
  157. std::cout << "Wide UTF-" << sizeof(wchar_t) * 8 << std::endl;
  158. test_by_char<wchar_t,char>(l1,lreal);
  159. freelocale(lreal);
  160. lreal = 0;
  161. }
  162. }
  163. {
  164. std::cout << "en_US.Latin-1 locale" << std::endl;
  165. std::string name = "en_US.ISO8859-1";
  166. if(!have_locale(name)) {
  167. std::cout << "en_US.ISO8859-8 not supported" << std::endl;
  168. }
  169. else {
  170. std::locale l1=gen(name);
  171. lreal=newlocale(LC_ALL_MASK,name.c_str(),0);
  172. assert(lreal);
  173. test_by_char<char,char>(l1,lreal);
  174. std::cout << "Wide UTF-" << sizeof(wchar_t) * 8 << std::endl;
  175. test_by_char<wchar_t,char>(l1,lreal);
  176. freelocale(lreal);
  177. lreal = 0;
  178. }
  179. }
  180. {
  181. std::cout << "he_IL.UTF locale" << std::endl;
  182. name="he_IL.UTF-8";
  183. if(!have_locale(name)) {
  184. std::cout << name << " not supported" << std::endl;
  185. }
  186. else {
  187. std::locale l1=gen(name);
  188. lreal=newlocale(LC_ALL_MASK,name.c_str(),0);
  189. assert(lreal);
  190. std::cout << "UTF-8" << std::endl;
  191. test_by_char<char,char>(l1,lreal);
  192. std::cout << "Wide UTF-" << sizeof(wchar_t) * 8 << std::endl;
  193. test_by_char<wchar_t,char>(l1,lreal);
  194. freelocale(lreal);
  195. lreal = 0;
  196. }
  197. }
  198. {
  199. std::cout << "he_IL.ISO locale" << std::endl;
  200. std::string name = "he_IL.ISO8859-8";
  201. if(!have_locale(name)) {
  202. std::cout << name <<" not supported" << std::endl;
  203. }
  204. else {
  205. std::locale l1=gen(name);
  206. lreal=newlocale(LC_ALL_MASK,name.c_str(),0);
  207. assert(lreal);
  208. test_by_char<char,char>(l1,lreal);
  209. std::cout << "Wide UTF-" << sizeof(wchar_t) * 8 << std::endl;
  210. test_by_char<wchar_t,char>(l1,lreal);
  211. freelocale(lreal);
  212. lreal = 0;
  213. }
  214. }
  215. {
  216. std::cout << "Testing UTF-8 punct issues" << std::endl;
  217. std::string name = "ru_RU.UTF-8";
  218. if(!have_locale(name)) {
  219. std::cout << "- No russian locale" << std::endl;
  220. }
  221. else {
  222. std::locale l1=gen(name);
  223. std::ostringstream ss;
  224. ss.imbue(l1);
  225. ss << std::setprecision(10) ;
  226. ss << boost::locale::as::number << 12345.45;
  227. std::string v=ss.str();
  228. TEST(v == "12345,45" || v == "12 345,45" || v=="12.345,45");
  229. }
  230. }
  231. }
  232. catch(std::exception const &e) {
  233. std::cerr << "Failed " << e.what() << std::endl;
  234. if(lreal)
  235. freelocale(lreal);
  236. return EXIT_FAILURE;
  237. }
  238. FINALIZE();
  239. }
  240. #endif // posix
  241. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
  242. // boostinspect:noascii