File: str.allocator_propagation.pass.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (115 lines) | stat: -rw-r--r-- 4,312 bytes parent folder | download | duplicates (9)
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
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17
// TODO: Change to XFAIL once https://github.com/llvm/llvm-project/issues/40340 is fixed
// UNSUPPORTED: availability-pmr-missing

// This test ensures that we properly propagate allocators from ostringstream's
// inner string object to the new string returned from .str().
// `str() const&` is specified to preserve the allocator (not copy the string).
// `str() &&` isn't specified, but should preserve the allocator (move the string).

#include <cassert>
#include <memory>
#include <memory_resource>
#include <sstream>
#include <string>
#include <type_traits>

#include "make_string.h"
#include "test_allocator.h"
#include "test_macros.h"

template <class CharT>
void test_soccc_behavior() {
  using Alloc = SocccAllocator<CharT>;
  using SS    = std::basic_ostringstream<CharT, std::char_traits<CharT>, Alloc>;
  using S     = std::basic_string<CharT, std::char_traits<CharT>, Alloc>;
  {
    SS ss = SS(std::ios_base::out, Alloc(10));

    // [stringbuf.members]/6 specifies that the allocator is copied,
    // not select_on_container_copy_construction'ed.
    //
    S copied = ss.str();
    assert(copied.get_allocator().count_ == 10);
    assert(ss.rdbuf()->get_allocator().count_ == 10);
    assert(copied.empty());

    // sanity-check that SOCCC does in fact work
    assert(S(copied).get_allocator().count_ == 11);

    // [stringbuf.members]/10 doesn't specify the allocator to use,
    // but copying the allocator as-if-by moving the string makes sense.
    //
    S moved = std::move(ss).str();
    assert(moved.get_allocator().count_ == 10);
    assert(ss.rdbuf()->get_allocator().count_ == 10);
    assert(moved.empty());
  }
}

template <class CharT>
void test_allocation_is_pilfered() {
  using SS = std::basic_ostringstream<CharT, std::char_traits<CharT>, std::pmr::polymorphic_allocator<CharT>>;
  using S  = std::pmr::basic_string<CharT>;
  alignas(void*) char buf[80 * sizeof(CharT)];
  const CharT* initial =
      MAKE_CSTRING(CharT, "a very long string that exceeds the small string optimization buffer length");
  {
    std::pmr::set_default_resource(std::pmr::null_memory_resource());
    auto mr1 = std::pmr::monotonic_buffer_resource(buf, sizeof(buf), std::pmr::null_memory_resource());
    SS ss    = SS(S(initial, &mr1));
    S s      = std::move(ss).str();
    assert(s == initial);
  }
}

template <class CharT>
void test_no_foreign_allocations() {
  using SS = std::basic_ostringstream<CharT, std::char_traits<CharT>, std::pmr::polymorphic_allocator<CharT>>;
  using S  = std::pmr::basic_string<CharT>;
  const CharT* initial =
      MAKE_CSTRING(CharT, "a very long string that exceeds the small string optimization buffer length");
  {
    std::pmr::set_default_resource(std::pmr::null_memory_resource());
    auto mr1 = std::pmr::monotonic_buffer_resource(std::pmr::new_delete_resource());
    auto ss  = SS(S(initial, &mr1));
    assert(ss.rdbuf()->get_allocator().resource() == &mr1);

    // [stringbuf.members]/6 specifies that the result of `str() const &`
    // does NOT use the default allocator; it uses the original allocator.
    //
    S copied = ss.str();
    assert(copied.get_allocator().resource() == &mr1);
    assert(ss.rdbuf()->get_allocator().resource() == &mr1);
    assert(copied == initial);

    // [stringbuf.members]/10 doesn't specify the allocator to use,
    // but copying the allocator as-if-by moving the string makes sense.
    //
    S moved = std::move(ss).str();
    assert(moved.get_allocator().resource() == &mr1);
    assert(ss.rdbuf()->get_allocator().resource() == &mr1);
    assert(moved == initial);
  }
}

int main(int, char**) {
  test_soccc_behavior<char>();
  test_allocation_is_pilfered<char>();
  test_no_foreign_allocations<char>();
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
  test_soccc_behavior<wchar_t>();
  test_allocation_is_pilfered<wchar_t>();
  test_no_foreign_allocations<wchar_t>();
#endif

  return 0;
}