90 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// iterator insert(const_iterator p, charT c); // constexpr since C++2012 13#include <string>14#include <stdexcept>15#include <cassert>16 17#include "test_macros.h"18#include "min_allocator.h"19#include "asan_testing.h"20 21template <class S>22TEST_CONSTEXPR_CXX20 void test(S& s, typename S::const_iterator p, typename S::value_type c, S expected) {23 LIBCPP_ASSERT(is_string_asan_correct(s));24 bool sufficient_cap = s.size() < s.capacity();25 typename S::difference_type pos = p - s.begin();26 typename S::iterator i = s.insert(p, c);27 LIBCPP_ASSERT(s.__invariants());28 assert(s == expected);29 assert(i - s.begin() == pos);30 assert(*i == c);31 if (sufficient_cap)32 assert(i == p);33 LIBCPP_ASSERT(is_string_asan_correct(s));34}35 36template <class S>37TEST_CONSTEXPR_CXX20 void test_string() {38 S s;39 test(s, s.begin(), '1', S("1"));40 test(s, s.begin(), 'a', S("a1"));41 test(s, s.end(), 'b', S("a1b"));42 test(s, s.end() - 1, 'c', S("a1cb"));43 test(s, s.end() - 2, 'd', S("a1dcb"));44 test(s, s.end() - 3, '2', S("a12dcb"));45 test(s, s.end() - 4, '3', S("a132dcb"));46 test(s, s.end() - 5, '4', S("a1432dcb"));47 test(s, s.begin() + 1, '5', S("a51432dcb"));48 test(s, s.begin() + 2, '6', S("a561432dcb"));49 test(s, s.begin() + 3, '7', S("a5671432dcb"));50 test(s, s.begin() + 4, 'A', S("a567A1432dcb"));51 test(s, s.begin() + 5, 'B', S("a567AB1432dcb"));52 test(s, s.begin() + 6, 'C', S("a567ABC1432dcb"));53 test(s, s.begin(), 'x', S("xa567ABC1432dcb"));54 test(s, s.begin(), 'x', S("xxa567ABC1432dcb"));55 test(s, s.begin(), 'x', S("xxxa567ABC1432dcb"));56 test(s, s.begin(), 'x', S("xxxxa567ABC1432dcb"));57 test(s, s.begin(), 'x', S("xxxxxa567ABC1432dcb"));58 test(s, s.begin(), 'x', S("xxxxxxa567ABC1432dcb"));59 test(s, s.begin(), 'x', S("xxxxxxxa567ABC1432dcb"));60 test(s, s.begin(), 'x', S("xxxxxxxxa567ABC1432dcb"));61 test(s, s.begin(), 'x', S("xxxxxxxxxa567ABC1432dcb"));62 test(s, s.begin(), 'x', S("xxxxxxxxxxa567ABC1432dcb"));63 test(s, s.begin(), 'x', S("xxxxxxxxxxxa567ABC1432dcb"));64 test(s, s.begin(), 'x', S("xxxxxxxxxxxxa567ABC1432dcb"));65 test(s, s.begin(), 'x', S("xxxxxxxxxxxxxa567ABC1432dcb"));66 test(s, s.begin(), 'x', S("xxxxxxxxxxxxxxa567ABC1432dcb"));67 test(s, s.begin(), 'x', S("xxxxxxxxxxxxxxxa567ABC1432dcb"));68 test(s, s.begin(), 'x', S("xxxxxxxxxxxxxxxxa567ABC1432dcb"));69 test(s, s.begin() + 1, 'x', S("xxxxxxxxxxxxxxxxxa567ABC1432dcb"));70}71 72TEST_CONSTEXPR_CXX20 bool test() {73 test_string<std::string>();74#if TEST_STD_VER >= 1175 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();76 test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>();77#endif78 79 return true;80}81 82int main(int, char**) {83 test();84#if TEST_STD_VER > 1785 static_assert(test());86#endif87 88 return 0;89}90