test_convert.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef BOOST_LOCALE_WITH_ICU
  9. #include <iostream>
  10. int main()
  11. {
  12. std::cout << "ICU is not build... Skipping" << std::endl;
  13. }
  14. #else
  15. #include <boost/locale/conversion.hpp>
  16. #include <boost/locale/generator.hpp>
  17. #include <boost/locale/info.hpp>
  18. #include <iomanip>
  19. #include "test_locale.hpp"
  20. template<typename Char>
  21. void test_normc(std::basic_string<Char> orig,std::basic_string<Char> normal,boost::locale::norm_type type)
  22. {
  23. std::locale l = boost::locale::generator().generate("en_US.UTF-8");
  24. TEST(normalize(orig,type,l)==normal);
  25. TEST(normalize(orig.c_str(),type,l)==normal);
  26. TEST(normalize(orig.c_str(),orig.c_str()+orig.size(),type,l)==normal);
  27. }
  28. void test_norm(std::string orig,std::string normal,boost::locale::norm_type type)
  29. {
  30. test_normc<char>(orig,normal,type);
  31. test_normc<wchar_t>(to<wchar_t>(orig),to<wchar_t>(normal),type);
  32. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  33. test_normc<char16_t>(to<char16_t>(orig),to<char16_t>(normal),type);
  34. #endif
  35. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  36. test_normc<char32_t>(to<char32_t>(orig),to<char32_t>(normal),type);
  37. #endif
  38. }
  39. #define TEST_A(Chr,how,source,dest) \
  40. do { \
  41. boost::locale::info const &inf=std::use_facet<boost::locale::info>(std::locale()); \
  42. std::cout <<"Testing " #how " for " #Chr ", lang="<<inf.language(); \
  43. if(std::string("char")==#Chr) std::cout <<" charset="<< inf.encoding(); \
  44. std::cout << std::endl; \
  45. std::basic_string<Chr> source_s=(source),dest_s=(dest); \
  46. TEST(boost::locale::how(source_s)==dest_s); \
  47. TEST(boost::locale::how(source_s.c_str())==dest_s); \
  48. TEST(boost::locale::how(source_s.c_str(),source_s.c_str()+source_s.size())==dest_s);\
  49. }while(0)
  50. #define TEST_ALL_CASES \
  51. do { \
  52. eight_bit=true; \
  53. std::locale::global(gen("en_US.UTF-8")); \
  54. TEST_V(to_upper,"grüßen i","GRÜSSEN I"); \
  55. TEST_V(to_lower,"Façade","façade"); \
  56. TEST_V(to_title,"façadE world","Façade World"); \
  57. TEST_V(fold_case,"Hello World","hello world"); \
  58. std::locale::global(gen("tr_TR.UTF-8")); \
  59. eight_bit=false; \
  60. TEST_V(to_upper,"i","İ"); \
  61. TEST_V(to_lower,"İ","i"); \
  62. }while(0)
  63. int main()
  64. {
  65. try {
  66. {
  67. using namespace boost::locale;
  68. std::cout << "Testing Unicode normalization" << std::endl;
  69. test_norm("\xEF\xAC\x81","\xEF\xAC\x81",norm_nfd); /// ligature fi
  70. test_norm("\xEF\xAC\x81","\xEF\xAC\x81",norm_nfc);
  71. test_norm("\xEF\xAC\x81","fi",norm_nfkd);
  72. test_norm("\xEF\xAC\x81","fi",norm_nfkc);
  73. test_norm("ä","ä",norm_nfd); // ä to a and accent
  74. test_norm("ä","ä",norm_nfc);
  75. }
  76. boost::locale::generator gen;
  77. bool eight_bit=true;
  78. #define TEST_V(how,source_s,dest_s) \
  79. do { \
  80. TEST_A(char,how,source_s,dest_s); \
  81. if(eight_bit) { \
  82. std::locale tmp=std::locale(); \
  83. std::locale::global(gen("en_US.ISO8859-1")); \
  84. TEST_A(char,how,to<char>(source_s),to<char>(dest_s)); \
  85. std::locale::global(tmp); \
  86. } \
  87. }while(0)
  88. TEST_ALL_CASES;
  89. #undef TEST_V
  90. #define TEST_V(how,source_s,dest_s) TEST_A(wchar_t,how,to<wchar_t>(source_s),to<wchar_t>(dest_s))
  91. TEST_ALL_CASES;
  92. #undef TEST_V
  93. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  94. #define TEST_V(how,source_s,dest_s) TEST_A(char16_t,how,to<char16_t>(source_s),to<char16_t>(dest_s))
  95. TEST_ALL_CASES;
  96. #undef TEST_V
  97. #endif
  98. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  99. #define TEST_V(how,source_s,dest_s) TEST_A(char32_t,how,to<char32_t>(source_s),to<char32_t>(dest_s))
  100. TEST_ALL_CASES;
  101. #undef TEST_V
  102. #endif
  103. }
  104. catch(std::exception const &e) {
  105. std::cerr << "Failed " << e.what() << std::endl;
  106. return EXIT_FAILURE;
  107. }
  108. FINALIZE();
  109. }
  110. #endif // NO ICU
  111. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
  112. // boostinspect:noascii