blob: 02187c5193af9aa2204e7a460d8892a7c2461ee1 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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-exception
Howard Hinnant3e519522010-05-11 19:42:16 +00006//
7//===----------------------------------------------------------------------===//
8
9// <string>
10
11// size_type capacity() const;
12
13#include <string>
14#include <cassert>
15
Marshall Clowc3deeb52013-12-03 00:18:10 +000016#include "test_allocator.h"
Marshall Clowe34f6f62013-11-26 20:58:02 +000017#include "min_allocator.h"
Howard Hinnant3e519522010-05-11 19:42:16 +000018
Roger Ferrer Ibanez9d03c032016-11-29 16:40:19 +000019#include "test_macros.h"
20
Howard Hinnant3e519522010-05-11 19:42:16 +000021template <class S>
22void
23test(S s)
24{
25 S::allocator_type::throw_after = 0;
Roger Ferrer Ibanez9d03c032016-11-29 16:40:19 +000026#ifndef TEST_HAS_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +000027 try
Roger Ferrer Ibanez9d03c032016-11-29 16:40:19 +000028#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000029 {
30 while (s.size() < s.capacity())
31 s.push_back(typename S::value_type());
32 assert(s.size() == s.capacity());
33 }
Roger Ferrer Ibanez9d03c032016-11-29 16:40:19 +000034#ifndef TEST_HAS_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +000035 catch (...)
36 {
37 assert(false);
38 }
Roger Ferrer Ibanez9d03c032016-11-29 16:40:19 +000039#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000040 S::allocator_type::throw_after = INT_MAX;
41}
42
JF Bastien2df59c52019-02-04 20:31:13 +000043int main(int, char**)
Howard Hinnant3e519522010-05-11 19:42:16 +000044{
Howard Hinnanteec72182013-06-28 16:59:19 +000045 {
Howard Hinnant3e519522010-05-11 19:42:16 +000046 typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > S;
47 S s;
48 test(s);
49 s.assign(10, 'a');
50 s.erase(5);
51 test(s);
52 s.assign(100, 'a');
53 s.erase(50);
54 test(s);
Howard Hinnanteec72182013-06-28 16:59:19 +000055 }
Eric Fiselierf2f2a632016-06-14 21:31:42 +000056#if TEST_STD_VER >= 11
Howard Hinnanteec72182013-06-28 16:59:19 +000057 {
58 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
59 S s;
60 assert(s.capacity() > 0);
61 }
62#endif
JF Bastien2df59c52019-02-04 20:31:13 +000063
64 return 0;
Howard Hinnant3e519522010-05-11 19:42:16 +000065}