test_posix_collate.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/config.hpp>
  16. #include <boost/locale/conversion.hpp>
  17. #include <boost/locale/localization_backend.hpp>
  18. #include <boost/locale/generator.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. int get_sign(int x)
  26. {
  27. if(x<0)
  28. return -1;
  29. else if(x==0)
  30. return 0;
  31. return 1;
  32. }
  33. template<typename CharType>
  34. void test_one(std::locale const &l,std::string ia,std::string ib,int diff)
  35. {
  36. std::basic_string<CharType> a=to_correct_string<CharType>(ia,l);
  37. std::basic_string<CharType> b=to_correct_string<CharType>(ib,l);
  38. if(diff < 0) {
  39. TEST(l(a,b));
  40. TEST(!l(b,a));
  41. }
  42. else if(diff == 0) {
  43. TEST(!l(a,b));
  44. TEST(!l(b,a));
  45. }
  46. else {
  47. TEST(!l(a,b));
  48. TEST(l(b,a));
  49. }
  50. std::collate<CharType> const &col = std::use_facet<std::collate<CharType> >(l);
  51. TEST(diff == col.compare(a.c_str(),a.c_str()+a.size(),b.c_str(),b.c_str()+b.size()));
  52. TEST(diff == get_sign(col.transform(a.c_str(),a.c_str()+a.size()).compare(col.transform(b.c_str(),b.c_str()+b.size()))));
  53. if(diff == 0) {
  54. TEST(col.hash(a.c_str(),a.c_str()+a.size()) == col.hash(b.c_str(),b.c_str()+b.size()));
  55. }
  56. }
  57. template<typename CharType>
  58. void test_char()
  59. {
  60. boost::locale::generator gen;
  61. std::cout << "- Testing at least C" << std::endl;
  62. std::locale l = gen("en_US.UTF-8");
  63. test_one<CharType>(l,"a","b",-1);
  64. test_one<CharType>(l,"a","a",0);
  65. std::string name;
  66. #if !defined(__APPLE__) && !defined(__FreeBSD__)
  67. std::string names[] = { "en_US.UTF-8", "en_US.ISO8859-1" };
  68. for(unsigned i=0;i<sizeof(names)/sizeof(names[0]);i++) {
  69. if(have_locale(names[i])) {
  70. name = names[i];
  71. std::cout << "- Testing " << name << std::endl;
  72. std::locale l=gen(name);
  73. test_one<CharType>(l,"a","ç",-1);
  74. test_one<CharType>(l,"ç","d",-1);
  75. }
  76. else {
  77. std::cout << "- " << names[i] << " not supported, skipping" << std::endl;
  78. }
  79. }
  80. #else
  81. std::cout << "- Collation is broken on this OS C standard library, skipping" << std::endl;
  82. #endif
  83. }
  84. int main()
  85. {
  86. try {
  87. boost::locale::localization_backend_manager mgr = boost::locale::localization_backend_manager::global();
  88. mgr.select("posix");
  89. boost::locale::localization_backend_manager::global(mgr);
  90. std::cout << "Testing char" << std::endl;
  91. test_char<char>();
  92. std::cout << "Testing wchar_t" << std::endl;
  93. test_char<wchar_t>();
  94. }
  95. catch(std::exception const &e) {
  96. std::cerr << "Failed " << e.what() << std::endl;
  97. return EXIT_FAILURE;
  98. }
  99. FINALIZE();
  100. }
  101. #endif // NO POSIX
  102. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
  103. // boostinspect:noascii