blob: eaaa678126ae4a11925f019ae819bda0b5c4d1df [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 charT& front() const;
12// charT& front();
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 Clow7fc6a552019-05-31 18:35:30 +000021#include "test_macros.h"
Marshall Clowe34f6f62013-11-26 20:58:02 +000022#include "min_allocator.h"
Howard Hinnanteec72182013-06-28 16:59:19 +000023
Howard Hinnant3e519522010-05-11 19:42:16 +000024template <class S>
25void
26test(S s)
27{
28 const S& cs = s;
Marshall Clow9ea0e472019-03-19 03:30:07 +000029 ASSERT_SAME_TYPE(decltype( s.front()), typename S::reference);
30 ASSERT_SAME_TYPE(decltype(cs.front()), typename S::const_reference);
31 LIBCPP_ASSERT_NOEXCEPT( s.front());
32 LIBCPP_ASSERT_NOEXCEPT( cs.front());
Howard Hinnant3e519522010-05-11 19:42:16 +000033 assert(&cs.front() == &cs[0]);
34 assert(&s.front() == &s[0]);
35 s.front() = typename S::value_type('z');
36 assert(s.front() == typename S::value_type('z'));
37}
38
JF Bastien2df59c52019-02-04 20:31:13 +000039int main(int, char**)
Howard Hinnant3e519522010-05-11 19:42:16 +000040{
Howard Hinnanteec72182013-06-28 16:59:19 +000041 {
Howard Hinnant3e519522010-05-11 19:42:16 +000042 typedef std::string S;
43 test(S("1"));
44 test(S("1234567890123456789012345678901234567890"));
Howard Hinnanteec72182013-06-28 16:59:19 +000045 }
Eric Fiselierf2f2a632016-06-14 21:31:42 +000046#if TEST_STD_VER >= 11
Howard Hinnanteec72182013-06-28 16:59:19 +000047 {
48 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
49 test(S("1"));
50 test(S("1234567890123456789012345678901234567890"));
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;
Marshall Clowccbe5672019-05-01 11:25:58 +000056 (void) s.front();
Howard Hinnantfc88dbd2013-08-23 17:37:05 +000057 assert(false);
58 }
59#endif
JF Bastien2df59c52019-02-04 20:31:13 +000060
61 return 0;
Howard Hinnant3e519522010-05-11 19:42:16 +000062}