compare.sv.pass.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <string_view>
  9. // constexpr int compare(basic_string_view str) const noexcept;
  10. #include <string_view>
  11. #include <cassert>
  12. #include "test_macros.h"
  13. #include "constexpr_char_traits.h"
  14. int sign ( int x ) { return x > 0 ? 1 : ( x < 0 ? -1 : 0 ); }
  15. template<typename CharT>
  16. void test1 ( std::basic_string_view<CharT> sv1,
  17. std::basic_string_view<CharT> sv2, int expected ) {
  18. assert ( sign( sv1.compare(sv2)) == sign(expected));
  19. }
  20. template<typename CharT>
  21. void test ( const CharT *s1, const CharT *s2, int expected ) {
  22. typedef std::basic_string_view<CharT> string_view_t;
  23. string_view_t sv1 ( s1 );
  24. string_view_t sv2 ( s2 );
  25. test1(sv1, sv2, expected);
  26. }
  27. int main(int, char**) {
  28. test("", "", 0);
  29. test("", "abcde", -5);
  30. test("", "abcdefghij", -10);
  31. test("", "abcdefghijklmnopqrst", -20);
  32. test("abcde", "", 5);
  33. test("abcde", "abcde", 0);
  34. test("abcde", "abcdefghij", -5);
  35. test("abcde", "abcdefghijklmnopqrst", -15);
  36. test("abcdefghij", "", 10);
  37. test("abcdefghij", "abcde", 5);
  38. test("abcdefghij", "abcdefghij", 0);
  39. test("abcdefghij", "abcdefghijklmnopqrst", -10);
  40. test("abcdefghijklmnopqrst", "", 20);
  41. test("abcdefghijklmnopqrst", "abcde", 15);
  42. test("abcdefghijklmnopqrst", "abcdefghij", 10);
  43. test("abcdefghijklmnopqrst", "abcdefghijklmnopqrst", 0);
  44. test(L"", L"", 0);
  45. test(L"", L"abcde", -5);
  46. test(L"", L"abcdefghij", -10);
  47. test(L"", L"abcdefghijklmnopqrst", -20);
  48. test(L"abcde", L"", 5);
  49. test(L"abcde", L"abcde", 0);
  50. test(L"abcde", L"abcdefghij", -5);
  51. test(L"abcde", L"abcdefghijklmnopqrst", -15);
  52. test(L"abcdefghij", L"", 10);
  53. test(L"abcdefghij", L"abcde", 5);
  54. test(L"abcdefghij", L"abcdefghij", 0);
  55. test(L"abcdefghij", L"abcdefghijklmnopqrst", -10);
  56. test(L"abcdefghijklmnopqrst", L"", 20);
  57. test(L"abcdefghijklmnopqrst", L"abcde", 15);
  58. test(L"abcdefghijklmnopqrst", L"abcdefghij", 10);
  59. test(L"abcdefghijklmnopqrst", L"abcdefghijklmnopqrst", 0);
  60. #if TEST_STD_VER >= 11
  61. test(u"", u"", 0);
  62. test(u"", u"abcde", -5);
  63. test(u"", u"abcdefghij", -10);
  64. test(u"", u"abcdefghijklmnopqrst", -20);
  65. test(u"abcde", u"", 5);
  66. test(u"abcde", u"abcde", 0);
  67. test(u"abcde", u"abcdefghij", -5);
  68. test(u"abcde", u"abcdefghijklmnopqrst", -15);
  69. test(u"abcdefghij", u"", 10);
  70. test(u"abcdefghij", u"abcde", 5);
  71. test(u"abcdefghij", u"abcdefghij", 0);
  72. test(u"abcdefghij", u"abcdefghijklmnopqrst", -10);
  73. test(u"abcdefghijklmnopqrst", u"", 20);
  74. test(u"abcdefghijklmnopqrst", u"abcde", 15);
  75. test(u"abcdefghijklmnopqrst", u"abcdefghij", 10);
  76. test(u"abcdefghijklmnopqrst", u"abcdefghijklmnopqrst", 0);
  77. test(U"", U"", 0);
  78. test(U"", U"abcde", -5);
  79. test(U"", U"abcdefghij", -10);
  80. test(U"", U"abcdefghijklmnopqrst", -20);
  81. test(U"abcde", U"", 5);
  82. test(U"abcde", U"abcde", 0);
  83. test(U"abcde", U"abcdefghij", -5);
  84. test(U"abcde", U"abcdefghijklmnopqrst", -15);
  85. test(U"abcdefghij", U"", 10);
  86. test(U"abcdefghij", U"abcde", 5);
  87. test(U"abcdefghij", U"abcdefghij", 0);
  88. test(U"abcdefghij", U"abcdefghijklmnopqrst", -10);
  89. test(U"abcdefghijklmnopqrst", U"", 20);
  90. test(U"abcdefghijklmnopqrst", U"abcde", 15);
  91. test(U"abcdefghijklmnopqrst", U"abcdefghij", 10);
  92. test(U"abcdefghijklmnopqrst", U"abcdefghijklmnopqrst", 0);
  93. #endif
  94. #if TEST_STD_VER > 11
  95. {
  96. typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
  97. constexpr SV sv1 { "abcde", 5 };
  98. constexpr SV sv2 { "abcde", 5 };
  99. constexpr SV sv3 { "edcba0", 6 };
  100. static_assert ( sv1.compare(sv2) == 0, "" );
  101. static_assert ( sv2.compare(sv1) == 0, "" );
  102. static_assert ( sv3.compare(sv2) > 0, "" );
  103. static_assert ( sv2.compare(sv3) < 0, "" );
  104. }
  105. #endif
  106. return 0;
  107. }