File: visualization_tests.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (199 lines) | stat: -rw-r--r-- 7,539 bytes parent folder | download | duplicates (5)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2024 Braden Ganetsky
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

// This is a file for testing of visualizations,
// such as Visual Studio Natvis or GDB pretty printers.
// Run this test and break at the label called `break_here`.
// Inspect the variables to test correctness.

#if 0 // Change to `#if 1` to test turning off SIMD optimizations
#define BOOST_UNORDERED_DISABLE_SSE2
#define BOOST_UNORDERED_DISABLE_NEON
#endif

#define BOOST_UNORDERED_ENABLE_STATS

#include <boost/core/lightweight_test.hpp>

#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/unordered/concurrent_flat_map.hpp>
#include <boost/unordered/concurrent_flat_set.hpp>
#include <boost/unordered/concurrent_node_map.hpp>
#include <boost/unordered/concurrent_node_set.hpp>
#include <boost/unordered/unordered_flat_map.hpp>
#include <boost/unordered/unordered_flat_set.hpp>
#include <boost/unordered/unordered_map.hpp>
#include <boost/unordered/unordered_node_map.hpp>
#include <boost/unordered/unordered_node_set.hpp>
#include <boost/unordered/unordered_set.hpp>
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <memory>
#include <string>
#include <typeinfo>

// Prevent any "unused" errors
template <class... Args> void use(Args&&...) {}

using map_value_type = std::pair<const std::string, int>;
using set_value_type = std::string;

template <class Tester> void visualization_test(Tester& tester)
{
  // clang-format off
    auto fca_map_ptr      = tester.template construct_map<boost::unordered_map>();
    auto fca_multimap_ptr = tester.template construct_map<boost::unordered_multimap>();
    auto fca_set_ptr      = tester.template construct_set<boost::unordered_set>();
    auto fca_multiset_ptr = tester.template construct_set<boost::unordered_multiset>();
    auto& fca_map      = *fca_map_ptr;
    auto& fca_multimap = *fca_multimap_ptr;
    auto& fca_set      = *fca_set_ptr;
    auto& fca_multiset = *fca_multiset_ptr;

    auto foa_flat_map_ptr = tester.template construct_map<boost::unordered_flat_map>();
    auto foa_flat_set_ptr = tester.template construct_set<boost::unordered_flat_set>();
    auto foa_node_map_ptr = tester.template construct_map<boost::unordered_node_map>();
    auto foa_node_set_ptr = tester.template construct_set<boost::unordered_node_set>();
    auto& foa_flat_map = *foa_flat_map_ptr;
    auto& foa_flat_set = *foa_flat_set_ptr;
    auto& foa_node_map = *foa_node_map_ptr;
    auto& foa_node_set = *foa_node_set_ptr;

    auto cfoa_flat_map_ptr = tester.template construct_map<boost::concurrent_flat_map>();
    auto cfoa_flat_set_ptr = tester.template construct_set<boost::concurrent_flat_set>();
    auto cfoa_node_map_ptr = tester.template construct_map<boost::concurrent_node_map>();
    auto cfoa_node_set_ptr = tester.template construct_set<boost::concurrent_node_set>();
    auto& cfoa_flat_map = *cfoa_flat_map_ptr;
    auto& cfoa_flat_set = *cfoa_flat_set_ptr;
    auto& cfoa_node_map = *cfoa_node_map_ptr;
    auto& cfoa_node_set = *cfoa_node_set_ptr;
  // clang-format on

  for (int i = 0; i < 5; ++i) {
    const auto str = std::to_string(i * 2);
    const auto num = i * 11;

    fca_map.emplace(str, num);
    fca_multimap.emplace(str, num);
    fca_multimap.emplace(str, num + 1);
    foa_flat_map.emplace(str, num);
    foa_node_map.emplace(str, num);
    cfoa_flat_map.emplace(str, num);
    cfoa_node_map.emplace(str, num);

    fca_set.emplace(str);
    fca_multiset.emplace(str);
    fca_multiset.emplace(str);
    foa_flat_set.emplace(str);
    foa_node_set.emplace(str);
    cfoa_flat_set.emplace(str);
    cfoa_node_set.emplace(str);
  }

  auto fca_map_begin = fca_map.begin();
  auto fca_map_end = fca_map.end();
  auto fca_multimap_begin = fca_multimap.begin();
  auto fca_multimap_end = fca_multimap.end();
  auto fca_set_begin = fca_set.begin();
  auto fca_set_end = fca_set.end();
  auto fca_multiset_begin = fca_multiset.begin();
  auto fca_multiset_end = fca_multiset.end();

  auto foa_flat_map_begin = foa_flat_map.begin();
  auto foa_flat_map_end = foa_flat_map.end();
  auto foa_flat_set_begin = foa_flat_set.begin();
  auto foa_flat_set_end = foa_flat_set.end();
  auto foa_node_map_begin = foa_node_map.begin();
  auto foa_node_map_end = foa_node_map.end();
  auto foa_node_set_begin = foa_node_set.begin();
  auto foa_node_set_end = foa_node_set.end();

  use(cfoa_flat_map, cfoa_flat_set, cfoa_node_map, cfoa_node_set);
  use(fca_map_begin, fca_map_end, fca_multimap_begin, fca_multimap_end,
    fca_set_begin, fca_set_end, fca_multiset_begin, fca_multiset_end);
  use(foa_flat_map_begin, foa_flat_map_end, foa_flat_set_begin,
    foa_flat_set_end, foa_node_map_begin, foa_node_map_end, foa_node_set_begin,
    foa_node_set_end);

  goto break_here;
break_here:;
}

class offset_ptr_tester_
{
  static constexpr std::size_t SEGMENT_SIZE = 64 * 1024;
  std::string segment_name = to_string(boost::uuids::random_generator()());
  boost::interprocess::managed_shared_memory segment{
    boost::interprocess::create_only, segment_name.c_str(), SEGMENT_SIZE};

  using map_allocator = boost::interprocess::allocator<map_value_type,
    boost::interprocess::managed_shared_memory::segment_manager>;
  using set_allocator = boost::interprocess::allocator<set_value_type,
    boost::interprocess::managed_shared_memory::segment_manager>;

  template <template <class...> class MapTemplate>
  using map_type = MapTemplate<std::string, int, boost::hash<std::string>,
    std::equal_to<std::string>, map_allocator>;
  template <template <class...> class SetTemplate>
  using set_type = SetTemplate<std::string, boost::hash<std::string>,
    std::equal_to<std::string>, set_allocator>;

public:
  offset_ptr_tester_()
  {
    boost::interprocess::shared_memory_object::remove(segment_name.c_str());
  }
  ~offset_ptr_tester_()
  {
    boost::interprocess::shared_memory_object::remove(segment_name.c_str());
  }

  template <template <class...> class MapTemplate>
  map_type<MapTemplate>* construct_map()
  {
    return &*segment.construct<map_type<MapTemplate> >(
      typeid(map_type<MapTemplate>).name())(
      map_allocator(segment.get_segment_manager()));
  }
  template <template <class...> class SetTemplate>
  set_type<SetTemplate>* construct_set()
  {
    return &*segment.construct<set_type<SetTemplate> >(
      typeid(set_type<SetTemplate>).name())(
      set_allocator(segment.get_segment_manager()));
  }
} offset_ptr_tester;

class default_tester_
{
  template <template <class...> class MapTemplate>
  using map_type = MapTemplate<std::string, int, boost::hash<std::string>,
    std::equal_to<std::string>, std::allocator<map_value_type> >;
  template <template <class...> class SetTemplate>
  using set_type = SetTemplate<std::string, boost::hash<std::string>,
    std::equal_to<std::string>, std::allocator<set_value_type> >;

public:
  template <template <class...> class MapTemplate>
  std::unique_ptr<map_type<MapTemplate> > construct_map()
  {
    using type = map_type<MapTemplate>;
    return std::unique_ptr<type>(new type());
  }
  template <template <class...> class SetTemplate>
  std::unique_ptr<set_type<SetTemplate> > construct_set()
  {
    using type = set_type<SetTemplate>;
    return std::unique_ptr<type>(new type());
  }
} default_tester;

int main()
{
  visualization_test(default_tester);
  visualization_test(offset_ptr_tester);

  return boost::report_errors();
}