Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / string.view / string.view.comparison / less.pass.cpp
blob7461b05ce30853976e4bc4c0cb446c9fc6570394
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14)
11 // <string_view>
13 // template<class charT, class traits>
14 // constexpr bool operator<(basic_string_view<charT, traits> lhs, basic_string_view<charT, traits> rhs);
15 // (plus "sufficient additional overloads" to make implicit conversions work as intended)
17 #include <string_view>
18 #include <cassert>
19 #include <string>
21 #include "test_macros.h"
22 #include "constexpr_char_traits.h"
23 #include "make_string.h"
25 template <class T>
26 struct ConvertibleTo {
27 T t_;
28 TEST_CONSTEXPR explicit ConvertibleTo(T t) : t_(t) {}
29 TEST_CONSTEXPR operator T() const { return t_; }
32 template <class SV>
33 TEST_CONSTEXPR_CXX14 bool test() {
34 typedef typename SV::value_type CharT;
35 typedef typename SV::traits_type Traits;
37 // Test the behavior of the operator, both with and without implicit conversions.
38 SV v[] = {
39 SV(MAKE_CSTRING(CharT, "")),
40 SV(MAKE_CSTRING(CharT, "abc")),
41 SV(MAKE_CSTRING(CharT, "abcdef")),
42 SV(MAKE_CSTRING(CharT, "acb")),
44 for (int i = 0; i < 4; ++i) {
45 for (int j = 0; j < 4; ++j) {
46 // See http://eel.is/c++draft/string.view#tab:string.view.comparison.overloads
47 bool expected = (i < j);
48 assert((v[i] < v[j]) == expected);
49 assert((v[i].data() < v[j]) == expected);
50 assert((v[i] < v[j].data()) == expected);
51 assert((ConvertibleTo<SV>(v[i]) < v[j]) == expected);
52 assert((v[i] < ConvertibleTo<SV>(v[j])) == expected);
54 if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
55 assert((std::basic_string<CharT, Traits>(v[i]) < v[j]) == expected);
56 assert((v[i] < std::basic_string<CharT, Traits>(v[j])) == expected);
61 // Test its behavior with embedded null bytes.
62 SV abc = SV(MAKE_CSTRING(CharT, "abc"));
63 SV abc0def = SV(MAKE_CSTRING(CharT, "abc\0def"), 7);
64 SV abcdef = SV(MAKE_CSTRING(CharT, "abcdef"));
65 assert((abc < abc0def) == true);
66 assert((abc < abcdef) == true);
67 assert((abc0def < abc) == false);
68 assert((abc0def < abcdef) == true);
69 assert((abcdef < abc) == false);
70 assert((abcdef < abc0def) == false);
72 assert((abc.data() < abc0def) == true);
73 assert((abc0def < abc.data()) == false);
75 if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
76 assert((std::basic_string<CharT, Traits>(abc) < abc0def) == true);
77 assert((abc0def < std::basic_string<CharT, Traits>(abc)) == false);
80 return true;
83 int main(int, char**) {
84 test<std::string_view>();
85 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
86 test<std::wstring_view>();
87 #endif
88 #if TEST_STD_VER >= 11
89 test<std::u16string_view>();
90 test<std::u32string_view>();
91 #endif
92 #if TEST_STD_VER > 14
93 static_assert(test<std::string_view>(), "");
94 # ifndef TEST_HAS_NO_WIDE_CHARACTERS
95 static_assert(test<std::wstring_view>(), "");
96 # endif
97 static_assert(test<std::u16string_view>(), "");
98 static_assert(test<std::u32string_view>(), "");
99 #endif
101 #if TEST_STD_VER > 11
102 test<std::basic_string_view<char, constexpr_char_traits<char>>>();
103 static_assert(test<std::basic_string_view<char, constexpr_char_traits<char>>>(), "");
104 #endif
106 #if TEST_STD_VER > 17
107 test<std::u8string_view>();
108 static_assert(test<std::u8string_view>());
109 #endif
111 return 0;