Debian Codegraph @

libstdc++-v3/testsuite/21_strings/basic_string/cons/char/103919.cc

raw
1 // { dg-do run { target c++17 } }
2
3 #include <string>
4 #include <new>
5 #include <cstdlib>
6 #include <cstring>
7 #include <testsuite_hooks.h>
8
9 std::size_t counter = 0;
10
11 void* operator new(std::size_t n)
12 {
13 counter += n;
14 return std::malloc(n);
15 }
16
17 void operator delete(void* p) noexcept
18 {
19 std::free(p);
20 }
21
22 void operator delete(void* p, std::size_t) noexcept
23 {
24 std::free(p);
25 }
26
27 int main()
28 {
29 const char* str = "A string that is considerably longer than the SSO buffer";
30
31 // PR libstdc++/103919
32 // basic_string(const T&, size_t, size_t) constructor is overconstrained
33 counter = 0;
34 std::string s(str, 2, 6);
35 VERIFY( s == "string" );
36 #if _GLIBCXX_USE_CXX11_ABI
37 // The string fits in the SSO buffer, so nothing is allocated.
38 VERIFY( counter == 0 );
39 #else
40 // The COW string allocates a string rep and 7 chars.
41 VERIFY( counter < std::strlen(str) );
42 #endif
43 }