85 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <string>10 11// basic_string<charT,traits,Allocator>&12// assign(basic_string_view<charT,traits> sv); // constexpr since C++2013 14#include <string>15#include <string_view>16#include <cassert>17 18#include "test_macros.h"19#include "min_allocator.h"20#include "test_allocator.h"21 22template <class S, class SV>23TEST_CONSTEXPR_CXX20 void test(S s, SV sv, S expected) {24 s.assign(sv);25 LIBCPP_ASSERT(s.__invariants());26 assert(s == expected);27}28 29template <class S, class SV>30TEST_CONSTEXPR_CXX20 void testAlloc(S s, SV sv, const typename S::allocator_type& a) {31 s.assign(sv);32 LIBCPP_ASSERT(s.__invariants());33 assert(s == sv);34 assert(s.get_allocator() == a);35}36 37template <class S>38TEST_CONSTEXPR_CXX20 void test_string() {39 typedef std::string_view SV;40 test(S(), SV(), S());41 test(S(), SV("12345"), S("12345"));42 test(S(), SV("1234567890"), S("1234567890"));43 test(S(), SV("12345678901234567890"), S("12345678901234567890"));44 45 test(S("12345"), SV(), S());46 test(S("12345"), SV("12345"), S("12345"));47 test(S("12345"), SV("1234567890"), S("1234567890"));48 test(S("12345"), SV("12345678901234567890"), S("12345678901234567890"));49 50 test(S("1234567890"), SV(), S());51 test(S("1234567890"), SV("12345"), S("12345"));52 test(S("1234567890"), SV("1234567890"), S("1234567890"));53 test(S("1234567890"), SV("12345678901234567890"), S("12345678901234567890"));54 55 test(S("12345678901234567890"), SV(), S());56 test(S("12345678901234567890"), SV("12345"), S("12345"));57 test(S("12345678901234567890"), SV("1234567890"), S("1234567890"));58 test(S("12345678901234567890"), SV("12345678901234567890"), S("12345678901234567890"));59 60 using A = typename S::allocator_type;61 62 testAlloc(S(), SV(), A());63 testAlloc(S(), SV("12345"), A());64 testAlloc(S(), SV("1234567890"), A());65 testAlloc(S(), SV("12345678901234567890"), A());66}67 68TEST_CONSTEXPR_CXX20 bool test() {69 test_string<std::string>();70#if TEST_STD_VER >= 1171 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();72#endif73 74 return true;75}76 77int main(int, char**) {78 test();79#if TEST_STD_VER > 1780 static_assert(test());81#endif82 83 return 0;84}85