blob: 3a1224ca301d0a351ce99d91bb9d05165a146ae8 [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// const_reference operator[](size_type pos) const;
12// reference operator[](size_type pos);
13
Howard Hinnant145afa12013-08-23 20:10:18 +000014#ifdef _LIBCPP_DEBUG
Howard Hinnantfc88dbd2013-08-23 17:37:05 +000015#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
16#endif
17
Howard Hinnant3e519522010-05-11 19:42:16 +000018#include <string>
19#include <cassert>
20
Marshall Clowe34f6f62013-11-26 20:58:02 +000021#include "min_allocator.h"
Howard Hinnanteec72182013-06-28 16:59:19 +000022
JF Bastien2df59c52019-02-04 20:31:13 +000023int main(int, char**)
Howard Hinnant3e519522010-05-11 19:42:16 +000024{
Howard Hinnanteec72182013-06-28 16:59:19 +000025 {
Howard Hinnant3e519522010-05-11 19:42:16 +000026 typedef std::string S;
27 S s("0123456789");
28 const S& cs = s;
29 for (S::size_type i = 0; i < cs.size(); ++i)
30 {
Stephan T. Lavaveje9c72882016-12-06 01:13:51 +000031 assert(s[i] == static_cast<char>('0' + i));
Howard Hinnant3e519522010-05-11 19:42:16 +000032 assert(cs[i] == s[i]);
33 }
34 assert(cs[cs.size()] == '\0');
35 const S s2 = S();
36 assert(s2[0] == '\0');
Howard Hinnanteec72182013-06-28 16:59:19 +000037 }
Eric Fiselierf2f2a632016-06-14 21:31:42 +000038#if TEST_STD_VER >= 11
Howard Hinnanteec72182013-06-28 16:59:19 +000039 {
40 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
41 S s("0123456789");
42 const S& cs = s;
43 for (S::size_type i = 0; i < cs.size(); ++i)
44 {
Stephan T. Lavaveje9c72882016-12-06 01:13:51 +000045 assert(s[i] == static_cast<char>('0' + i));
Howard Hinnanteec72182013-06-28 16:59:19 +000046 assert(cs[i] == s[i]);
47 }
48 assert(cs[cs.size()] == '\0');
49 const S s2 = S();
50 assert(s2[0] == '\0');
51 }
52#endif
Howard Hinnant145afa12013-08-23 20:10:18 +000053#ifdef _LIBCPP_DEBUG
Howard Hinnantfc88dbd2013-08-23 17:37:05 +000054 {
55 std::string s;
56 char c = s[0];
57 assert(c == '\0');
58 c = s[1];
59 assert(false);
60 }
61#endif
JF Bastien2df59c52019-02-04 20:31:13 +000062
63 return 0;
Howard Hinnant3e519522010-05-11 19:42:16 +000064}