10efd9a03SArthur O'Dwyer //===----------------------------------------------------------------------===//
20efd9a03SArthur O'Dwyer //
30efd9a03SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40efd9a03SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
50efd9a03SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60efd9a03SArthur O'Dwyer //
70efd9a03SArthur O'Dwyer //===----------------------------------------------------------------------===//
80efd9a03SArthur O'Dwyer 
9*4fc50236SJoe Loser // UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14)
10*4fc50236SJoe Loser 
110efd9a03SArthur O'Dwyer // <string_view>
120efd9a03SArthur O'Dwyer 
130efd9a03SArthur O'Dwyer // template<class charT, class traits>
140efd9a03SArthur O'Dwyer //   constexpr bool operator<=(basic_string_view<charT, traits> lhs, basic_string_view<charT, traits> rhs);
150efd9a03SArthur O'Dwyer // (plus "sufficient additional overloads" to make implicit conversions work as intended)
160efd9a03SArthur O'Dwyer 
170efd9a03SArthur O'Dwyer #include <string_view>
180efd9a03SArthur O'Dwyer #include <cassert>
190efd9a03SArthur O'Dwyer #include <string>
200efd9a03SArthur O'Dwyer 
210efd9a03SArthur O'Dwyer #include "test_macros.h"
220efd9a03SArthur O'Dwyer #include "constexpr_char_traits.h"
230efd9a03SArthur O'Dwyer #include "make_string.h"
240efd9a03SArthur O'Dwyer 
250efd9a03SArthur O'Dwyer template<class T>
260efd9a03SArthur O'Dwyer struct ConvertibleTo {
270efd9a03SArthur O'Dwyer     T t_;
ConvertibleToConvertibleTo280efd9a03SArthur O'Dwyer     TEST_CONSTEXPR explicit ConvertibleTo(T t) : t_(t) {}
operator TConvertibleTo290efd9a03SArthur O'Dwyer     TEST_CONSTEXPR operator T() const {
300efd9a03SArthur O'Dwyer         return t_;
310efd9a03SArthur O'Dwyer     }
320efd9a03SArthur O'Dwyer };
330efd9a03SArthur O'Dwyer 
340efd9a03SArthur O'Dwyer template<class SV>
test()350efd9a03SArthur O'Dwyer TEST_CONSTEXPR_CXX14 bool test()
360efd9a03SArthur O'Dwyer {
370efd9a03SArthur O'Dwyer     typedef typename SV::value_type CharT;
380efd9a03SArthur O'Dwyer     typedef typename SV::traits_type Traits;
390efd9a03SArthur O'Dwyer 
400efd9a03SArthur O'Dwyer     // Test the behavior of the operator, both with and without implicit conversions.
410efd9a03SArthur O'Dwyer     SV v[] = {
420efd9a03SArthur O'Dwyer         SV(MAKE_CSTRING(CharT, "")),
430efd9a03SArthur O'Dwyer         SV(MAKE_CSTRING(CharT, "abc")),
440efd9a03SArthur O'Dwyer         SV(MAKE_CSTRING(CharT, "abcdef")),
450efd9a03SArthur O'Dwyer         SV(MAKE_CSTRING(CharT, "acb")),
460efd9a03SArthur O'Dwyer     };
470efd9a03SArthur O'Dwyer     for (int i = 0; i < 4; ++i) {
480efd9a03SArthur O'Dwyer         for (int j = 0; j < 4; ++j) {
490efd9a03SArthur O'Dwyer             // See http://eel.is/c++draft/string.view#tab:string.view.comparison.overloads
500efd9a03SArthur O'Dwyer             bool expected = (i <= j);
510efd9a03SArthur O'Dwyer             assert((v[i] <= v[j]) == expected);
520efd9a03SArthur O'Dwyer             assert((v[i].data() <= v[j]) == expected);
530efd9a03SArthur O'Dwyer             assert((v[i] <= v[j].data()) == expected);
540efd9a03SArthur O'Dwyer             assert((ConvertibleTo<SV>(v[i]) <= v[j]) == expected);
550efd9a03SArthur O'Dwyer             assert((v[i] <= ConvertibleTo<SV>(v[j])) == expected);
560efd9a03SArthur O'Dwyer 
5793a375a1SJoe Loser             if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
580efd9a03SArthur O'Dwyer                 assert((std::basic_string<CharT, Traits>(v[i]) <= v[j]) == expected);
590efd9a03SArthur O'Dwyer                 assert((v[i] <= std::basic_string<CharT, Traits>(v[j])) == expected);
600efd9a03SArthur O'Dwyer             }
610efd9a03SArthur O'Dwyer         }
620efd9a03SArthur O'Dwyer     }
630efd9a03SArthur O'Dwyer 
640efd9a03SArthur O'Dwyer     // Test its behavior with embedded null bytes.
650efd9a03SArthur O'Dwyer     SV abc = SV(MAKE_CSTRING(CharT, "abc"));
660efd9a03SArthur O'Dwyer     SV abc0def = SV(MAKE_CSTRING(CharT, "abc\0def"), 7);
670efd9a03SArthur O'Dwyer     SV abcdef = SV(MAKE_CSTRING(CharT, "abcdef"));
680efd9a03SArthur O'Dwyer     assert((abc <= abc0def) == true);
690efd9a03SArthur O'Dwyer     assert((abc <= abcdef) == true);
700efd9a03SArthur O'Dwyer     assert((abc0def <= abc) == false);
710efd9a03SArthur O'Dwyer     assert((abc0def <= abcdef) == true);
720efd9a03SArthur O'Dwyer     assert((abcdef <= abc) == false);
730efd9a03SArthur O'Dwyer     assert((abcdef <= abc0def) == false);
740efd9a03SArthur O'Dwyer 
750efd9a03SArthur O'Dwyer     assert((abc.data() <= abc0def) == true);
760efd9a03SArthur O'Dwyer     assert((abc0def <= abc.data()) == false);
770efd9a03SArthur O'Dwyer 
7893a375a1SJoe Loser     if (!TEST_IS_CONSTANT_EVALUATED || TEST_STD_VER >= 20) {
790efd9a03SArthur O'Dwyer         assert((std::basic_string<CharT, Traits>(abc) <= abc0def) == true);
800efd9a03SArthur O'Dwyer         assert((abc0def <= std::basic_string<CharT, Traits>(abc)) == false);
810efd9a03SArthur O'Dwyer     }
820efd9a03SArthur O'Dwyer 
830efd9a03SArthur O'Dwyer     return true;
840efd9a03SArthur O'Dwyer }
850efd9a03SArthur O'Dwyer 
main(int,char **)860efd9a03SArthur O'Dwyer int main(int, char**)
870efd9a03SArthur O'Dwyer {
880efd9a03SArthur O'Dwyer     test<std::string_view>();
890efd9a03SArthur O'Dwyer #ifndef TEST_HAS_NO_WIDE_CHARACTERS
900efd9a03SArthur O'Dwyer     test<std::wstring_view>();
910efd9a03SArthur O'Dwyer #endif
920efd9a03SArthur O'Dwyer #if TEST_STD_VER >= 11
930efd9a03SArthur O'Dwyer     test<std::u16string_view>();
940efd9a03SArthur O'Dwyer     test<std::u32string_view>();
950efd9a03SArthur O'Dwyer #endif
960efd9a03SArthur O'Dwyer #if TEST_STD_VER > 14
970efd9a03SArthur O'Dwyer     static_assert(test<std::string_view>(), "");
980efd9a03SArthur O'Dwyer #ifndef TEST_HAS_NO_WIDE_CHARACTERS
990efd9a03SArthur O'Dwyer     static_assert(test<std::wstring_view>(), "");
1000efd9a03SArthur O'Dwyer #endif
1010efd9a03SArthur O'Dwyer     static_assert(test<std::u16string_view>(), "");
1020efd9a03SArthur O'Dwyer     static_assert(test<std::u32string_view>(), "");
1030efd9a03SArthur O'Dwyer #endif
1040efd9a03SArthur O'Dwyer 
1050efd9a03SArthur O'Dwyer #if TEST_STD_VER > 11
1060efd9a03SArthur O'Dwyer     test<std::basic_string_view<char, constexpr_char_traits<char>>>();
1070efd9a03SArthur O'Dwyer     static_assert(test<std::basic_string_view<char, constexpr_char_traits<char>>>(), "");
1080efd9a03SArthur O'Dwyer #endif
1090efd9a03SArthur O'Dwyer 
1100efd9a03SArthur O'Dwyer #if TEST_STD_VER > 17
1110efd9a03SArthur O'Dwyer     test<std::u8string_view>();
1120efd9a03SArthur O'Dwyer     static_assert(test<std::u8string_view>());
1130efd9a03SArthur O'Dwyer #endif
1140efd9a03SArthur O'Dwyer 
1150efd9a03SArthur O'Dwyer     return 0;
1160efd9a03SArthur O'Dwyer }
117