type_index_constexpr_test.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // Copyright 2015-2019 Antony Polukhin.
  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. #include <boost/type_index/ctti_type_index.hpp>
  8. #include <algorithm>
  9. #include <string>
  10. #include <boost/core/lightweight_test.hpp>
  11. const char* hello1 = "Hello word";
  12. const char* hello1_end = hello1 + sizeof("Hello word");
  13. const char* hello2 = "Hello word, pal!";
  14. const char* hello2_end = hello2 + sizeof("Hello word, pal!");
  15. void strcmp_same() {
  16. using boost::typeindex::detail::constexpr_strcmp;
  17. BOOST_TEST(
  18. constexpr_strcmp(hello1, hello1) == 0
  19. );
  20. BOOST_TEST(
  21. constexpr_strcmp(hello2, hello2) == 0
  22. );
  23. BOOST_TEST(
  24. constexpr_strcmp(hello1, hello2) != 0
  25. );
  26. BOOST_TEST(
  27. constexpr_strcmp(hello2, hello1) != 0
  28. );
  29. BOOST_TEST(
  30. (constexpr_strcmp(hello2, hello1) < 0)
  31. ==
  32. (std::strcmp(hello2, hello1) < 0)
  33. );
  34. BOOST_TEST(
  35. (constexpr_strcmp(hello1, hello2) < 0)
  36. ==
  37. (std::strcmp(hello1, hello2) < 0)
  38. );
  39. }
  40. void search_same() {
  41. using boost::typeindex::detail::constexpr_search;
  42. BOOST_TEST(
  43. constexpr_search(hello1, hello1_end, hello2, hello2_end) == std::search(hello1, hello1_end, hello2, hello2_end)
  44. );
  45. BOOST_TEST(
  46. constexpr_search(hello2, hello2_end, hello1, hello1_end) == std::search(hello2, hello2_end, hello1, hello1_end)
  47. );
  48. const char* word = "word";
  49. const char* word_end = word + sizeof("word") - 1;
  50. BOOST_TEST(
  51. constexpr_search(hello1, hello1_end, word, word_end) == std::search(hello1, hello1_end, word, word_end)
  52. );
  53. BOOST_TEST(
  54. constexpr_search(hello2, hello2_end, word, word_end) == std::search(hello2, hello2_end, word, word_end)
  55. );
  56. }
  57. template <class T, std::size_t N>
  58. BOOST_CXX14_CONSTEXPR bool in_namespace(const char (&ns)[N]) BOOST_NOEXCEPT {
  59. BOOST_CXX14_CONSTEXPR const char* name = boost::typeindex::ctti_type_index::type_id<T>().raw_name();
  60. for (std::size_t i = 0; i < N - 1; ++i)
  61. if (name[i] != ns[i])
  62. return false;
  63. return true;
  64. }
  65. template <class T>
  66. BOOST_CXX14_CONSTEXPR bool is_boost_namespace() BOOST_NOEXCEPT {
  67. return in_namespace<T>("boost::") || in_namespace<T>("class boost::") || in_namespace<T>("struct boost::");
  68. }
  69. void constexpr_test() {
  70. using namespace boost::typeindex;
  71. BOOST_CXX14_CONSTEXPR ctti_type_index t_int0 = ctti_type_index::type_id<int>();
  72. (void)t_int0;
  73. BOOST_CXX14_CONSTEXPR ctti_type_index t_short0 = ctti_type_index::type_id<short>();
  74. (void)t_short0;
  75. BOOST_CXX14_CONSTEXPR ctti_type_index t_int1 = ctti_type_index::type_id<int>();
  76. (void)t_int1;
  77. BOOST_CXX14_CONSTEXPR ctti_type_index t_short1 = ctti_type_index::type_id<short>();
  78. (void)t_short1;
  79. // Following tests are known to fail on _MSC_VER == 1916.
  80. #if !defined(_MSC_VER) || _MSC_VER > 1916
  81. BOOST_CXX14_CONSTEXPR bool same0 = (t_int0 == t_int1);
  82. BOOST_TEST(same0);
  83. BOOST_CXX14_CONSTEXPR bool same1 = (t_short1 == t_short0);
  84. BOOST_TEST(same1);
  85. BOOST_CXX14_CONSTEXPR bool same2 = (t_int1 == t_int1);
  86. BOOST_TEST(same2);
  87. BOOST_CXX14_CONSTEXPR bool same3 = (t_short0 == t_short0);
  88. BOOST_TEST(same3);
  89. BOOST_CXX14_CONSTEXPR bool same4 = !(t_short0 < t_short0 || t_short0 > t_short0);
  90. BOOST_TEST(same4);
  91. BOOST_CXX14_CONSTEXPR bool same5 = (t_short0 <= t_short0 && t_short0 >= t_short0);
  92. BOOST_TEST(same5);
  93. BOOST_CXX14_CONSTEXPR bool not_same0 = (t_int0 != t_short1);
  94. BOOST_TEST(not_same0);
  95. BOOST_CXX14_CONSTEXPR bool not_same1 = (t_int1 != t_short0);
  96. BOOST_TEST(not_same1);
  97. BOOST_CXX14_CONSTEXPR bool not_same2 = (t_int1 < t_short0 || t_int1 > t_short0);
  98. BOOST_TEST(not_same2);
  99. BOOST_CXX14_CONSTEXPR const char* int_name = t_int0.name();
  100. BOOST_TEST(*int_name != '\0');
  101. BOOST_CXX14_CONSTEXPR const char* short_name = t_short0.name();
  102. BOOST_TEST(*short_name != '\0');
  103. BOOST_CXX14_CONSTEXPR bool in_namespace = is_boost_namespace<ctti_type_index>();
  104. BOOST_TEST(in_namespace);
  105. BOOST_CXX14_CONSTEXPR bool not_in_namespace = !is_boost_namespace<std::string>();
  106. BOOST_TEST(not_in_namespace);
  107. #endif // #if !defined(_MSC_VER) || _MSC_VER > 1916
  108. }
  109. int main() {
  110. strcmp_same();
  111. search_same();
  112. constexpr_test();
  113. return boost::report_errors();
  114. }