xref: /llvm-project/libcxx/test/std/strings/string.view/string.view.comparison/less.pass.cpp (revision 164c204a19f7859d570003d4c5e82faf48cb65a9)
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 
94fc50236SJoe Loser // UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14)
104fc50236SJoe 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 TConvertibleTo29a40bada9SBrendan Emery   TEST_CONSTEXPR operator T() const { return t_; }
300efd9a03SArthur O'Dwyer };
310efd9a03SArthur O'Dwyer 
320efd9a03SArthur O'Dwyer template <class SV>
test()33a40bada9SBrendan Emery TEST_CONSTEXPR_CXX14 bool test() {
340efd9a03SArthur O'Dwyer   typedef typename SV::value_type CharT;
350efd9a03SArthur O'Dwyer   typedef typename SV::traits_type Traits;
360efd9a03SArthur O'Dwyer 
370efd9a03SArthur O'Dwyer   // Test the behavior of the operator, both with and without implicit conversions.
380efd9a03SArthur O'Dwyer   SV v[] = {
390efd9a03SArthur O'Dwyer       SV(MAKE_CSTRING(CharT, "")),
400efd9a03SArthur O'Dwyer       SV(MAKE_CSTRING(CharT, "abc")),
410efd9a03SArthur O'Dwyer       SV(MAKE_CSTRING(CharT, "abcdef")),
420efd9a03SArthur O'Dwyer       SV(MAKE_CSTRING(CharT, "acb")),
430efd9a03SArthur O'Dwyer   };
440efd9a03SArthur O'Dwyer   for (int i = 0; i < 4; ++i) {
450efd9a03SArthur O'Dwyer     for (int j = 0; j < 4; ++j) {
460efd9a03SArthur O'Dwyer       // See http://eel.is/c++draft/string.view#tab:string.view.comparison.overloads
470efd9a03SArthur O'Dwyer       bool expected = (i < j);
480efd9a03SArthur O'Dwyer       assert((v[i] < v[j]) == expected);
490efd9a03SArthur O'Dwyer       assert((v[i].data() < v[j]) == expected);
500efd9a03SArthur O'Dwyer       assert((v[i] < v[j].data()) == expected);
510efd9a03SArthur O'Dwyer       assert((ConvertibleTo<SV>(v[i]) < v[j]) == expected);
520efd9a03SArthur O'Dwyer       assert((v[i] < ConvertibleTo<SV>(v[j])) == expected);
530efd9a03SArthur O'Dwyer 
54*164c204aSStephan T. Lavavej       if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
550efd9a03SArthur O'Dwyer         assert((std::basic_string<CharT, Traits>(v[i]) < v[j]) == expected);
560efd9a03SArthur O'Dwyer         assert((v[i] < std::basic_string<CharT, Traits>(v[j])) == expected);
570efd9a03SArthur O'Dwyer       }
580efd9a03SArthur O'Dwyer     }
590efd9a03SArthur O'Dwyer   }
600efd9a03SArthur O'Dwyer 
610efd9a03SArthur O'Dwyer   // Test its behavior with embedded null bytes.
620efd9a03SArthur O'Dwyer   SV abc     = SV(MAKE_CSTRING(CharT, "abc"));
630efd9a03SArthur O'Dwyer   SV abc0def = SV(MAKE_CSTRING(CharT, "abc\0def"), 7);
640efd9a03SArthur O'Dwyer   SV abcdef  = SV(MAKE_CSTRING(CharT, "abcdef"));
650efd9a03SArthur O'Dwyer   assert((abc < abc0def) == true);
660efd9a03SArthur O'Dwyer   assert((abc < abcdef) == true);
670efd9a03SArthur O'Dwyer   assert((abc0def < abc) == false);
680efd9a03SArthur O'Dwyer   assert((abc0def < abcdef) == true);
690efd9a03SArthur O'Dwyer   assert((abcdef < abc) == false);
700efd9a03SArthur O'Dwyer   assert((abcdef < abc0def) == false);
710efd9a03SArthur O'Dwyer 
720efd9a03SArthur O'Dwyer   assert((abc.data() < abc0def) == true);
730efd9a03SArthur O'Dwyer   assert((abc0def < abc.data()) == false);
740efd9a03SArthur O'Dwyer 
75*164c204aSStephan T. Lavavej   if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
760efd9a03SArthur O'Dwyer     assert((std::basic_string<CharT, Traits>(abc) < abc0def) == true);
770efd9a03SArthur O'Dwyer     assert((abc0def < std::basic_string<CharT, Traits>(abc)) == false);
780efd9a03SArthur O'Dwyer   }
790efd9a03SArthur O'Dwyer 
800efd9a03SArthur O'Dwyer   return true;
810efd9a03SArthur O'Dwyer }
820efd9a03SArthur O'Dwyer 
main(int,char **)83a40bada9SBrendan Emery int main(int, char**) {
840efd9a03SArthur O'Dwyer   test<std::string_view>();
850efd9a03SArthur O'Dwyer #ifndef TEST_HAS_NO_WIDE_CHARACTERS
860efd9a03SArthur O'Dwyer   test<std::wstring_view>();
870efd9a03SArthur O'Dwyer #endif
880efd9a03SArthur O'Dwyer #if TEST_STD_VER >= 11
890efd9a03SArthur O'Dwyer   test<std::u16string_view>();
900efd9a03SArthur O'Dwyer   test<std::u32string_view>();
910efd9a03SArthur O'Dwyer #endif
920efd9a03SArthur O'Dwyer #if TEST_STD_VER > 14
930efd9a03SArthur O'Dwyer   static_assert(test<std::string_view>(), "");
940efd9a03SArthur O'Dwyer #  ifndef TEST_HAS_NO_WIDE_CHARACTERS
950efd9a03SArthur O'Dwyer   static_assert(test<std::wstring_view>(), "");
960efd9a03SArthur O'Dwyer #  endif
970efd9a03SArthur O'Dwyer   static_assert(test<std::u16string_view>(), "");
980efd9a03SArthur O'Dwyer   static_assert(test<std::u32string_view>(), "");
990efd9a03SArthur O'Dwyer #endif
1000efd9a03SArthur O'Dwyer 
1010efd9a03SArthur O'Dwyer #if TEST_STD_VER > 11
1020efd9a03SArthur O'Dwyer   test<std::basic_string_view<char, constexpr_char_traits<char>>>();
1030efd9a03SArthur O'Dwyer   static_assert(test<std::basic_string_view<char, constexpr_char_traits<char>>>(), "");
1040efd9a03SArthur O'Dwyer #endif
1050efd9a03SArthur O'Dwyer 
1060efd9a03SArthur O'Dwyer #if TEST_STD_VER > 17
1070efd9a03SArthur O'Dwyer   test<std::u8string_view>();
1080efd9a03SArthur O'Dwyer   static_assert(test<std::u8string_view>());
1090efd9a03SArthur O'Dwyer #endif
1100efd9a03SArthur O'Dwyer 
1110efd9a03SArthur O'Dwyer   return 0;
1120efd9a03SArthur O'Dwyer }
113