test_posix_convert.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/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 "test_posix_tools.hpp"
  23. #include <iostream>
  24. #include <wctype.h>
  25. template<typename CharType>
  26. void test_one(std::locale const &l,std::string src,std::string tgtl,std::string tgtu)
  27. {
  28. TEST(boost::locale::to_upper(to_correct_string<CharType>(src,l),l) == to_correct_string<CharType>(tgtu,l));
  29. TEST(boost::locale::to_lower(to_correct_string<CharType>(src,l),l) == to_correct_string<CharType>(tgtl,l));
  30. TEST(boost::locale::fold_case(to_correct_string<CharType>(src,l),l) == to_correct_string<CharType>(tgtl,l));
  31. }
  32. template<typename CharType>
  33. void test_char()
  34. {
  35. boost::locale::generator gen;
  36. std::cout << "- Testing at least C" << std::endl;
  37. std::locale l = gen("en_US.UTF-8");
  38. test_one<CharType>(l,"Hello World i","hello world i","HELLO WORLD I");
  39. std::string name = "en_US.UTF-8";
  40. if(have_locale(name)) {
  41. std::cout << "- Testing " << name << std::endl;
  42. std::locale l=gen(name);
  43. test_one<CharType>(l,"Façade","façade","FAÇADE");
  44. }
  45. else {
  46. std::cout << "- en_US.UTF-8 is not supported, skipping" << std::endl;
  47. }
  48. name = "en_US.ISO8859-1";
  49. if(have_locale(name)) {
  50. std::cout << "Testing " << name << std::endl;
  51. std::locale l=gen(name);
  52. test_one<CharType>(l,"Hello World","hello world","HELLO WORLD");
  53. #if defined(__APPLE__) || defined(__FreeBSD__)
  54. if(sizeof(CharType)!=1)
  55. #endif
  56. test_one<CharType>(l,"Façade","façade","FAÇADE");
  57. }
  58. else {
  59. std::cout << "- en_US.ISO8859-1 is not supported, skipping" << std::endl;
  60. }
  61. name = "tr_TR.UTF-8";
  62. if(have_locale(name)) {
  63. std::cout << "Testing " << name << std::endl;
  64. locale_t cl = newlocale(LC_ALL_MASK,name.c_str(),0);
  65. try {
  66. TEST(cl);
  67. if(towupper_l(L'i',cl) == 0x130) {
  68. test_one<CharType>(gen(name),"i","i","İ");
  69. }
  70. else {
  71. std::cout <<" Turkish locale is not supported well" << std::endl;
  72. }
  73. }
  74. catch(...) {
  75. if(cl) freelocale(cl);
  76. throw;
  77. }
  78. if(cl) freelocale(cl);
  79. }
  80. else
  81. {
  82. std::cout << "- tr_TR.UTF-8 is not supported, skipping" << std::endl;
  83. }
  84. }
  85. int main()
  86. {
  87. try {
  88. boost::locale::localization_backend_manager mgr = boost::locale::localization_backend_manager::global();
  89. mgr.select("posix");
  90. boost::locale::localization_backend_manager::global(mgr);
  91. std::cout << "Testing char" << std::endl;
  92. test_char<char>();
  93. std::cout << "Testing wchar_t" << std::endl;
  94. test_char<wchar_t>();
  95. }
  96. catch(std::exception const &e) {
  97. std::cerr << "Failed " << e.what() << std::endl;
  98. return EXIT_FAILURE;
  99. }
  100. FINALIZE();
  101. }
  102. #endif // POSIX
  103. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
  104. // boostinspect:noascii