112 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// explicit basic_string(const Allocator& a = Allocator()); // constexpr since C++2012 13#include <string>14#include <cassert>15 16#include "test_macros.h"17#include "test_allocator.h"18#include "min_allocator.h"19#include "asan_testing.h"20 21template <class S>22TEST_CONSTEXPR_CXX20 void test() {23 {24#if TEST_STD_VER > 1425 static_assert((noexcept(S{})), "");26#elif TEST_STD_VER >= 1127 static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "");28#endif29 S s;30 LIBCPP_ASSERT(s.__invariants());31 assert(s.data());32 assert(s.size() == 0);33 assert(s.capacity() >= s.size());34 assert(s.get_allocator() == typename S::allocator_type());35 LIBCPP_ASSERT(is_string_asan_correct(s));36 }37 {38#if TEST_STD_VER > 1439 static_assert((noexcept(S{typename S::allocator_type{}})), "");40#elif TEST_STD_VER >= 1141 static_assert((noexcept(S(typename S::allocator_type())) ==42 std::is_nothrow_copy_constructible<typename S::allocator_type>::value),43 "");44#endif45 S s(typename S::allocator_type(5));46 LIBCPP_ASSERT(s.__invariants());47 assert(s.data());48 assert(s.size() == 0);49 assert(s.capacity() >= s.size());50 assert(s.get_allocator() == typename S::allocator_type(5));51 LIBCPP_ASSERT(is_string_asan_correct(s));52 }53}54 55#if TEST_STD_VER >= 1156 57template <class S>58TEST_CONSTEXPR_CXX20 void test2() {59 {60# if TEST_STD_VER > 1461 static_assert((noexcept(S{})), "");62# elif TEST_STD_VER >= 1163 static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "");64# endif65 S s;66 LIBCPP_ASSERT(s.__invariants());67 assert(s.data());68 assert(s.size() == 0);69 assert(s.capacity() >= s.size());70 assert(s.get_allocator() == typename S::allocator_type());71 LIBCPP_ASSERT(is_string_asan_correct(s));72 }73 {74# if TEST_STD_VER > 1475 static_assert((noexcept(S{typename S::allocator_type{}})), "");76# elif TEST_STD_VER >= 1177 static_assert((noexcept(S(typename S::allocator_type())) ==78 std::is_nothrow_copy_constructible<typename S::allocator_type>::value),79 "");80# endif81 S s(typename S::allocator_type{});82 LIBCPP_ASSERT(s.__invariants());83 assert(s.data());84 assert(s.size() == 0);85 assert(s.capacity() >= s.size());86 assert(s.get_allocator() == typename S::allocator_type());87 LIBCPP_ASSERT(is_string_asan_correct(s));88 }89}90 91#endif92 93TEST_CONSTEXPR_CXX20 bool test() {94 test<std::basic_string<char, std::char_traits<char>, test_allocator<char> > >();95#if TEST_STD_VER >= 1196 test2<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();97 test2<std::basic_string<char, std::char_traits<char>, safe_allocator<char> > >();98 test2<std::basic_string<char, std::char_traits<char>, explicit_allocator<char> > >();99#endif100 101 return true;102}103 104int main(int, char**) {105 test();106#if TEST_STD_VER > 17107 static_assert(test());108#endif109 110 return 0;111}112