test_collate.cpp 4.1 KB

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