summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/constexpr.cc
blob: 7822c89497b1cb6f04dbb845bbe682ae5dffd3fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// { dg-do compile { target c++20 } }
// { dg-require-effective-target cxx11_abi }
// { dg-add-options no_pch }

#include <string>

#ifndef __cpp_lib_constexpr_string
# error "Feature-test macro for constexpr std::string missing in <string>"
#elif __cpp_lib_constexpr_string != 201907L
# error "Feature-test macro for constexpr std::string has wrong value in <string>"
#endif

#include <testsuite_hooks.h>

using C = char;
using T = std::char_traits<C>;

template<typename T>
struct Alloc : std::allocator<T>
{
  using std::allocator<T>::allocator;

  constexpr explicit Alloc(int p) : personality(p) { }

  template<typename U>
    constexpr Alloc(const Alloc<U>& a) : personality(a.personality) { }

  int personality = 0;

  constexpr Alloc select_on_container_copy_construction() const
  { return Alloc(-1); }

  constexpr bool operator==(const Alloc& a) const noexcept
  { return personality == a.personality; }
};

constexpr bool
test_default_ctor()
{
  std::basic_string<C> s0;
  VERIFY( s0.empty() );

  std::basic_string<C> s1(std::allocator<C>{});
  VERIFY( s1.empty() );

  std::basic_string<C, T, Alloc<C>> s2;
  VERIFY( s2.empty() );

  std::basic_string<C, T, Alloc<C>> s3(Alloc<C>(3));
  VERIFY( s3.empty() );
  VERIFY( s3.get_allocator().personality == 3 );

  return true;
}

static_assert( test_default_ctor() );

constexpr bool
test_cstr()
{
  const C cs[] = "This has an embedded \0 null";
  const auto len = (sizeof(cs) - 1)/sizeof(C);

  std::basic_string<C> s1(cs);
  VERIFY( s1.length() == 21 );
  std::basic_string<C> s2(cs, std::allocator<C>{});
  VERIFY( s2 == s1 );

  std::basic_string<C> s3(cs, len);
  VERIFY( s3.length() == len );
  VERIFY( s3[len] == '\0' );
  std::basic_string<C> s4(cs, len, std::allocator<C>{});
  VERIFY( s4 == s3 );

  std::basic_string<C, T, Alloc<C>> s5(cs);
  VERIFY( s5 == std::basic_string_view<C>(s1) );

  std::basic_string<C, T, Alloc<C>> s6(cs, Alloc<C>(6));
  VERIFY( s6 == std::basic_string_view<C>(s1) );
  VERIFY( s6.get_allocator().personality == 6 );

  std::basic_string<C, T, Alloc<C>> s7(cs, len, Alloc<C>(7));
  VERIFY( s7 == std::basic_string_view<C>(s3) );
  VERIFY( s7.get_allocator().personality == 7 );

  return true;
}

static_assert( test_cstr() );

constexpr bool
test_copy()
{
  const std::basic_string<C> short_string = "sh";
  const std::basic_string<C> long_string = "string longer than the SSO buffer";

  std::basic_string<C> s1 = short_string;
  VERIFY( s1 == short_string );
  std::basic_string<C> s2(short_string, s1.get_allocator());
  VERIFY( s2 == short_string );

  std::basic_string<C> s3 = long_string;
  VERIFY( s3 == long_string );
  std::basic_string<C> s4(long_string, s1.get_allocator());
  VERIFY( s4 == long_string );

  std::basic_string<C, T, Alloc<C>> a_short_string = short_string.c_str();
  std::basic_string<C, T, Alloc<C>> a_long_string = long_string.c_str();

  std::basic_string<C, T, Alloc<C>> s5(a_short_string);
  VERIFY( s5 == a_short_string );
  std::basic_string<C, T, Alloc<C>> s6(a_short_string, s5.get_allocator());
  VERIFY( s6 == a_short_string );
  std::basic_string<C, T, Alloc<C>> s7(a_short_string, Alloc<C>(7));
  VERIFY( s7 == a_short_string );
  VERIFY( s7.get_allocator().personality == 7 );

  std::basic_string<C, T, Alloc<C>> s8 = a_long_string;
  VERIFY( s8 == a_long_string );
  std::basic_string<C, T, Alloc<C>> s9(a_long_string, s5.get_allocator());
  VERIFY( s9 == a_long_string );
  std::basic_string<C, T, Alloc<C>> s10(a_long_string, Alloc<C>(10));
  VERIFY( s10 == a_long_string );
  VERIFY( s10.get_allocator().personality == 10 );

  return true;
}

static_assert( test_copy() );

constexpr bool
test_move()
{
  const std::basic_string<C> short_string = "sh";
  const std::basic_string<C> long_string = "string longer than the SSO buffer";

  std::basic_string<C> s0 = short_string;

  std::basic_string<C> s1 = std::move(s0);
  VERIFY( s1 == short_string );
  std::basic_string<C> s2(std::move(s1), std::allocator<C>());
  VERIFY( s2 == short_string );

  s0 = long_string;
  std::basic_string<C> s3 = std::move(s0);
  VERIFY( s3 == long_string );
  std::basic_string<C> s4(std::move(s3), s1.get_allocator());
  VERIFY( s4 == long_string );

  std::basic_string<C, T, Alloc<C>> a_short_string = short_string.c_str();
  std::basic_string<C, T, Alloc<C>> a_long_string = long_string.c_str();

  auto sa0 = a_short_string;
  std::basic_string<C, T, Alloc<C>> s5 = std::move(sa0);
  VERIFY( s5 == a_short_string );
  std::basic_string<C, T, Alloc<C>> s6(std::move(s5), sa0.get_allocator());
  VERIFY( s6 == a_short_string );
  std::basic_string<C, T, Alloc<C>> s7(std::move(s6), Alloc<C>(7));
  VERIFY( s7 == a_short_string );
  VERIFY( s7.get_allocator().personality == 7 );

  sa0 = a_long_string;
  std::basic_string<C, T, Alloc<C>> s8 = std::move(sa0);
  VERIFY( s8 == a_long_string );
  std::basic_string<C, T, Alloc<C>> s9(std::move(s8), s5.get_allocator());
  VERIFY( s9 == a_long_string );
  std::basic_string<C, T, Alloc<C>> s10(std::move(s9), Alloc<C>(10));
  VERIFY( s10 == a_long_string );
  VERIFY( s10.get_allocator().personality == 10 );

  return true;
}

static_assert( test_move() );