test_winapi_convert.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_WINAPI_BACKEND
  9. #include <iostream>
  10. int main()
  11. {
  12. std::cout << "WinAPI Backend is not build... Skipping" << std::endl;
  13. }
  14. #else
  15. #include <boost/locale/conversion.hpp>
  16. #include <boost/locale/localization_backend.hpp>
  17. #include <boost/locale/generator.hpp>
  18. #include <boost/locale/info.hpp>
  19. #include <iomanip>
  20. #include "test_locale.hpp"
  21. #include "test_locale_tools.hpp"
  22. #include <iostream>
  23. template<typename CharType>
  24. void test_one(std::locale const &l,std::string src,std::string tgtl,std::string tgtu)
  25. {
  26. TEST(boost::locale::to_upper(to_correct_string<CharType>(src,l),l) == to_correct_string<CharType>(tgtu,l));
  27. TEST(boost::locale::to_lower(to_correct_string<CharType>(src,l),l) == to_correct_string<CharType>(tgtl,l));
  28. TEST(boost::locale::fold_case(to_correct_string<CharType>(src,l),l) == to_correct_string<CharType>(tgtl,l));
  29. }
  30. template<typename CharType>
  31. void test_char()
  32. {
  33. boost::locale::generator gen;
  34. std::cout << "- Testing at least C" << std::endl;
  35. std::locale l = gen("en_US.UTF-8");
  36. test_one<CharType>(l,"Hello World i","hello world i","HELLO WORLD I");
  37. std::string name = "en_US.UTF-8";
  38. std::cout << "- Testing " << name << std::endl;
  39. l=gen(name);
  40. test_one<CharType>(l,"Façade","façade","FAÇADE");
  41. name = "tr_TR.UTF-8";
  42. std::cout << "Testing " << name << std::endl;
  43. test_one<CharType>(gen(name),"i","i","İ");
  44. }
  45. template<typename Char>
  46. void test_normc(std::basic_string<Char> orig,std::basic_string<Char> normal,boost::locale::norm_type type)
  47. {
  48. std::locale l = boost::locale::generator().generate("en_US.UTF-8");
  49. TEST(boost::locale::normalize(orig,type,l)==normal);
  50. TEST(boost::locale::normalize(orig.c_str(),type,l)==normal);
  51. TEST(boost::locale::normalize(orig.c_str(),orig.c_str()+orig.size(),type,l)==normal);
  52. }
  53. void test_norm(std::string orig,std::string normal,boost::locale::norm_type type)
  54. {
  55. test_normc<char>(orig,normal,type);
  56. test_normc<wchar_t>(to<wchar_t>(orig),to<wchar_t>(normal),type);
  57. }
  58. int main()
  59. {
  60. try {
  61. boost::locale::localization_backend_manager mgr = boost::locale::localization_backend_manager::global();
  62. mgr.select("winapi");
  63. boost::locale::localization_backend_manager::global(mgr);
  64. std::cout << "Testing char" << std::endl;
  65. test_char<char>();
  66. std::cout << "Testing wchar_t" << std::endl;
  67. test_char<wchar_t>();
  68. std::cout << "Testing Unicode normalization" << std::endl;
  69. test_norm("\xEF\xAC\x81","\xEF\xAC\x81",boost::locale::norm_nfd); /// ligature fi
  70. test_norm("\xEF\xAC\x81","\xEF\xAC\x81",boost::locale::norm_nfc);
  71. #if defined(_WIN32_NT) && _WIN32_NT >= 0x600
  72. test_norm("\xEF\xAC\x81","fi",boost::locale::norm_nfkd);
  73. test_norm("\xEF\xAC\x81","fi",boost::locale::norm_nfkc);
  74. #endif
  75. test_norm("ä","ä",boost::locale::norm_nfd); // ä to a and accent
  76. test_norm("ä","ä",boost::locale::norm_nfc);
  77. }
  78. catch(std::exception const &e) {
  79. std::cerr << "Failed " << e.what() << std::endl;
  80. return EXIT_FAILURE;
  81. }
  82. FINALIZE();
  83. }
  84. #endif // no winapi
  85. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
  86. // boostinspect:noascii