test_winapi_collate.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/collator.hpp>
  16. #include <boost/locale/generator.hpp>
  17. #include <boost/locale/localization_backend.hpp>
  18. #include <iomanip>
  19. #include "test_locale.hpp"
  20. template<typename Char>
  21. void test_comp(std::locale l,std::basic_string<Char> left,std::basic_string<Char> right,int ilevel,int expected)
  22. {
  23. typedef std::basic_string<Char> string_type;
  24. boost::locale::collator_base::level_type level = static_cast<boost::locale::collator_base::level_type>(ilevel);
  25. TEST(boost::locale::comparator<Char>(l,level)(left,right) == (expected < 0));
  26. if(ilevel==4) {
  27. std::collate<Char> const &coll=std::use_facet<std::collate<Char> >(l);
  28. string_type lt=coll.transform(left.c_str(),left.c_str()+left.size());
  29. string_type rt=coll.transform(right.c_str(),right.c_str()+right.size());
  30. if(expected < 0)
  31. TEST(lt<rt);
  32. else if(expected == 0) {
  33. TEST(lt==rt);
  34. }
  35. else
  36. TEST(lt > rt);
  37. long lh=coll.hash(left.c_str(),left.c_str()+left.size());
  38. long rh=coll.hash(right.c_str(),right.c_str()+right.size());
  39. if(expected == 0)
  40. TEST(lh==rh);
  41. else
  42. TEST(lh!=rh);
  43. }
  44. boost::locale::collator<Char> const &coll=std::use_facet<boost::locale::collator<Char> >(l);
  45. string_type lt=coll.transform(level,left.c_str(),left.c_str()+left.size());
  46. TEST(lt==coll.transform(level,left));
  47. string_type rt=coll.transform(level,right.c_str(),right.c_str()+right.size());
  48. TEST(rt==coll.transform(level,right));
  49. if(expected < 0)
  50. TEST(lt<rt);
  51. else if(expected == 0)
  52. TEST(lt==rt);
  53. else
  54. TEST(lt > rt);
  55. long lh=coll.hash(level,left.c_str(),left.c_str()+left.size());
  56. TEST(lh==coll.hash(level,left));
  57. long rh=coll.hash(level,right.c_str(),right.c_str()+right.size());
  58. TEST(rh==coll.hash(level,right));
  59. if(expected == 0)
  60. TEST(lh==rh);
  61. else
  62. TEST(lh!=rh);
  63. }
  64. #define TEST_COMP(c,_l,_r) test_comp<c>(l,_l,_r,level,expected)
  65. void compare(std::string left,std::string right,int level,int expected)
  66. {
  67. boost::locale::generator gen;
  68. std::locale l=gen("en_US.UTF-8");
  69. if(level == 4)
  70. TEST(l(left,right) == (expected < 0));
  71. TEST_COMP(char,left,right);
  72. TEST_COMP(wchar_t,to<wchar_t>(left),to<wchar_t>(right));
  73. }
  74. void test_collate()
  75. {
  76. int
  77. primary = 0,
  78. secondary = 1,
  79. tertiary = 2,
  80. quaternary = 3,
  81. identical = 4;
  82. int le = -1,gt = 1,eq = 0;
  83. compare("a","A",primary,eq);
  84. compare("a","A",secondary,eq);
  85. compare("A","a",tertiary,gt);
  86. compare("a","A",tertiary,le);
  87. compare("a","A",quaternary,le);
  88. compare("A","a",quaternary,gt);
  89. compare("a","A",identical,le);
  90. compare("A","a",identical,gt);
  91. compare("a","ä",primary,eq); // a , ä
  92. compare("a","ä",secondary,le); // a , ä
  93. compare("ä","a",secondary,gt); // a , ä
  94. compare("a","ä",quaternary,le); // a , ä
  95. compare("ä","a",quaternary,gt); // a , ä
  96. compare("a","ä",identical,le); // a , ä
  97. compare("ä","a",identical,gt); // a , ä
  98. }
  99. int main()
  100. {
  101. try {
  102. boost::locale::localization_backend_manager mgr = boost::locale::localization_backend_manager::global();
  103. mgr.select("winapi");
  104. boost::locale::localization_backend_manager::global(mgr);
  105. test_collate();
  106. }
  107. catch(std::exception const &e) {
  108. std::cerr << "Failed " << e.what() << std::endl;
  109. return EXIT_FAILURE;
  110. }
  111. FINALIZE();
  112. }
  113. #endif // NO WINAPI
  114. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
  115. // boostinspect:noascii