113 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// template<class InputIterator>12// basic_string(InputIterator begin, InputIterator end,13// const Allocator& a = Allocator()); // constexpr since C++2014 15#include <string>16#include <iterator>17#include <cassert>18#include <cstddef>19 20#include "test_macros.h"21#include "test_allocator.h"22#include "test_iterators.h"23#include "min_allocator.h"24#include "asan_testing.h"25 26template <class Alloc, class It>27TEST_CONSTEXPR_CXX20 void test(It first, It last) {28 typedef typename std::iterator_traits<It>::value_type charT;29 typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;30 S s2(first, last);31 LIBCPP_ASSERT(s2.__invariants());32 assert(s2.size() == static_cast<std::size_t>(std::distance(first, last)));33 unsigned i = 0;34 for (It it = first; it != last;) {35 assert(s2[i] == *it);36 ++it;37 ++i;38 }39 assert(s2.get_allocator() == Alloc());40 assert(s2.capacity() >= s2.size());41 LIBCPP_ASSERT(is_string_asan_correct(s2));42}43 44template <class Alloc, class It>45TEST_CONSTEXPR_CXX20 void test(It first, It last, const Alloc& a) {46 typedef typename std::iterator_traits<It>::value_type charT;47 typedef std::basic_string<charT, std::char_traits<charT>, Alloc> S;48 S s2(first, last, a);49 LIBCPP_ASSERT(s2.__invariants());50 assert(s2.size() == static_cast<std::size_t>(std::distance(first, last)));51 unsigned i = 0;52 for (It it = first; it != last;) {53 assert(s2[i] == *it);54 ++it;55 ++i;56 }57 assert(s2.get_allocator() == a);58 assert(s2.capacity() >= s2.size());59 LIBCPP_ASSERT(is_string_asan_correct(s2));60}61 62template <class Alloc>63TEST_CONSTEXPR_CXX20 void test_string(const Alloc& a) {64 const char* s = "12345678901234567890123456789012345678901234567890";65 66 test<Alloc>(s, s);67 test<Alloc>(s, s, Alloc(a));68 69 test<Alloc>(s, s + 1);70 test<Alloc>(s, s + 1, Alloc(a));71 72 test<Alloc>(s, s + 10);73 test<Alloc>(s, s + 10, Alloc(a));74 75 test<Alloc>(s, s + 50);76 test<Alloc>(s, s + 50, Alloc(a));77 78 test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s));79 test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), Alloc(a));80 81 test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 1));82 test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 1), Alloc(a));83 84 test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 10));85 test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 10), Alloc(a));86 87 test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 50));88 test<Alloc>(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 50), Alloc(a));89}90 91TEST_CONSTEXPR_CXX20 bool test() {92 test_string(test_allocator<char>());93 test_string(test_allocator<char>(2));94#if TEST_STD_VER >= 1195 test_string(min_allocator<char>());96#endif97 {98 static_assert((!std::is_constructible<std::string, std::string, std::string>::value), "");99 static_assert((!std::is_constructible<std::string, std::string, std::string, std::allocator<char> >::value), "");100 }101 102 return true;103}104 105int main(int, char**) {106 test();107#if TEST_STD_VER > 17108 static_assert(test());109#endif110 111 return 0;112}113