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