Skip to content

Commit 6bab044

Browse files
committed
[libc++] Add ABI tests for unordered_{map,set}
1 parent 771b7af commit 6bab044

File tree

2 files changed

+262
-0
lines changed

2 files changed

+262
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// 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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <cstdint>
10+
#include <unordered_map>
11+
12+
#include "min_allocator.h"
13+
#include "test_allocator.h"
14+
#include "test_macros.h"
15+
16+
template <class T>
17+
class small_pointer {
18+
std::uint16_t offset;
19+
};
20+
21+
template <class T>
22+
class small_iter_allocator {
23+
public:
24+
using value_type = T;
25+
using pointer = small_pointer<T>;
26+
using size_type = std::uint16_t;
27+
using difference_type = std::int16_t;
28+
29+
small_iter_allocator() TEST_NOEXCEPT {}
30+
31+
template <class U>
32+
small_iter_allocator(small_iter_allocator<U>) TEST_NOEXCEPT {}
33+
34+
T* allocate(std::size_t n);
35+
void deallocate(T* p, std::size_t);
36+
37+
friend bool operator==(small_iter_allocator, small_iter_allocator) { return true; }
38+
friend bool operator!=(small_iter_allocator, small_iter_allocator) { return false; }
39+
};
40+
41+
template <class T>
42+
class final_small_iter_allocator final {
43+
public:
44+
using value_type = T;
45+
using pointer = small_pointer<T>;
46+
using size_type = std::uint16_t;
47+
using difference_type = std::int16_t;
48+
49+
final_small_iter_allocator() TEST_NOEXCEPT {}
50+
51+
template <class U>
52+
final_small_iter_allocator(final_small_iter_allocator<U>) TEST_NOEXCEPT {}
53+
54+
T* allocate(std::size_t n);
55+
void deallocate(T* p, std::size_t);
56+
57+
friend bool operator==(final_small_iter_allocator, final_small_iter_allocator) { return true; }
58+
friend bool operator!=(final_small_iter_allocator, final_small_iter_allocator) { return false; }
59+
};
60+
61+
template <class T, class Alloc>
62+
using unordered_map_alloc = std::unordered_map<T, T, std::hash<T>, std::equal_to<T>, Alloc>;
63+
64+
#if __SIZE_WIDTH__ == 64
65+
66+
static_assert(sizeof(unordered_map_alloc<int, std::allocator<std::pair<const int, int> > >) == 40, "");
67+
static_assert(sizeof(unordered_map_alloc<int, min_allocator<std::pair<const int, int> > >) == 40, "");
68+
static_assert(sizeof(unordered_map_alloc<int, test_allocator<std::pair<const int, int> > >) == 64, "");
69+
static_assert(sizeof(unordered_map_alloc<int, small_iter_allocator<std::pair<const int, int> > >) == 12, "");
70+
static_assert(sizeof(unordered_map_alloc<int, final_small_iter_allocator<std::pair<const int, int> > >) == 16, "");
71+
72+
static_assert(sizeof(unordered_map_alloc<char, std::allocator<std::pair<const char, char> > >) == 40, "");
73+
static_assert(sizeof(unordered_map_alloc<char, min_allocator<std::pair<const char, char> > >) == 40, "");
74+
static_assert(sizeof(unordered_map_alloc<char, test_allocator<std::pair<const char, char> > >) == 64, "");
75+
static_assert(sizeof(unordered_map_alloc<char, small_iter_allocator<std::pair<const char, char> > >) == 12, "");
76+
static_assert(sizeof(unordered_map_alloc<char, final_small_iter_allocator<std::pair<const char, char> > >) == 16, "");
77+
78+
static_assert(TEST_ALIGNOF(unordered_map_alloc<int, std::allocator<std::pair<const int, int> > >) == 8, "");
79+
static_assert(TEST_ALIGNOF(unordered_map_alloc<int, min_allocator<std::pair<const int, int> > >) == 8, "");
80+
static_assert(TEST_ALIGNOF(unordered_map_alloc<int, test_allocator<std::pair<const int, int> > >) == 8, "");
81+
static_assert(TEST_ALIGNOF(unordered_map_alloc<int, small_iter_allocator<std::pair<const int, int> > >) == 4, "");
82+
static_assert(TEST_ALIGNOF(unordered_map_alloc<int, final_small_iter_allocator<std::pair<const int, int> > >) == 4, "");
83+
84+
static_assert(TEST_ALIGNOF(unordered_map_alloc<char, std::allocator<std::pair<const char, char> > >) == 8, "");
85+
static_assert(TEST_ALIGNOF(unordered_map_alloc<char, min_allocator<std::pair<const char, char> > >) == 8, "");
86+
static_assert(TEST_ALIGNOF(unordered_map_alloc<char, test_allocator<std::pair<const char, char> > >) == 8, "");
87+
static_assert(TEST_ALIGNOF(unordered_map_alloc<char, small_iter_allocator<std::pair<const char, char> > >) == 4, "");
88+
static_assert(TEST_ALIGNOF(unordered_map_alloc<char, final_small_iter_allocator<std::pair<const char, char> > >) == 4,
89+
"");
90+
91+
struct TEST_ALIGNAS(32) AlignedHash {};
92+
struct UnalignedEqualTo {};
93+
94+
static_assert(sizeof(std::unordered_map<int, int, AlignedHash, UnalignedEqualTo>) == 96, "");
95+
static_assert(TEST_ALIGNOF(std::unordered_map<int, int, AlignedHash, UnalignedEqualTo>) == 32, "");
96+
97+
#elif __SIZE_WIDTH__ == 32
98+
99+
static_assert(sizeof(unordered_map_alloc<int, std::allocator<std::pair<const int, int> > >) == 20, "");
100+
static_assert(sizeof(unordered_map_alloc<int, min_allocator<std::pair<const int, int> > >) == 20, "");
101+
static_assert(sizeof(unordered_map_alloc<int, test_allocator<std::pair<const int, int> > >) == 44, "");
102+
static_assert(sizeof(unordered_map_alloc<int, small_iter_allocator<std::pair<const int, int> > >) == 12, "");
103+
static_assert(sizeof(unordered_map_alloc<int, final_small_iter_allocator<std::pair<const int, int> > >) == 16, "");
104+
105+
static_assert(sizeof(unordered_map_alloc<char, std::allocator<std::pair<const char, char> > >) == 20, "");
106+
static_assert(sizeof(unordered_map_alloc<char, min_allocator<std::pair<const char, char> > >) == 20, "");
107+
static_assert(sizeof(unordered_map_alloc<char, test_allocator<std::pair<const char, char> > >) == 44, "");
108+
static_assert(sizeof(unordered_map_alloc<char, small_iter_allocator<std::pair<const char, char> > >) == 12, "");
109+
static_assert(sizeof(unordered_map_alloc<char, final_small_iter_allocator<std::pair<const char, char> > >) == 16, "");
110+
111+
static_assert(TEST_ALIGNOF(unordered_map_alloc<int, std::allocator<std::pair<const int, int> > >) == 4, "");
112+
static_assert(TEST_ALIGNOF(unordered_map_alloc<int, min_allocator<std::pair<const int, int> > >) == 4, "");
113+
static_assert(TEST_ALIGNOF(unordered_map_alloc<int, test_allocator<std::pair<const int, int> > >) == 4, "");
114+
static_assert(TEST_ALIGNOF(unordered_map_alloc<int, small_iter_allocator<std::pair<const int, int> > >) == 4, "");
115+
static_assert(TEST_ALIGNOF(unordered_map_alloc<int, final_small_iter_allocator<std::pair<const int, int> > >) == 4, "");
116+
117+
static_assert(TEST_ALIGNOF(unordered_map_alloc<char, std::allocator<std::pair<const char, char> > >) == 4, "");
118+
static_assert(TEST_ALIGNOF(unordered_map_alloc<char, min_allocator<std::pair<const char, char> > >) == 4, "");
119+
static_assert(TEST_ALIGNOF(unordered_map_alloc<char, test_allocator<std::pair<const char, char> > >) == 4, "");
120+
static_assert(TEST_ALIGNOF(unordered_map_alloc<char, small_iter_allocator<std::pair<const char, char> > >) == 4, "");
121+
static_assert(TEST_ALIGNOF(unordered_map_alloc<char, final_small_iter_allocator<std::pair<const char, char> > >) == 4,
122+
"");
123+
124+
struct TEST_ALIGNAS(32) AlignedHash {};
125+
struct UnalignedEqualTo {};
126+
127+
static_assert(sizeof(std::unordered_map<int, int, AlignedHash, UnalignedEqualTo>) == 96);
128+
static_assert(TEST_ALIGNOF(std::unordered_map<int, int, AlignedHash, UnalignedEqualTo>) == 32);
129+
130+
#else
131+
# error std::size_t has an unexpected size
132+
#endif
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// 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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <cstdint>
10+
#include <unordered_set>
11+
12+
#include "min_allocator.h"
13+
#include "test_allocator.h"
14+
#include "test_macros.h"
15+
16+
template <class T>
17+
class small_pointer {
18+
std::uint16_t offset;
19+
};
20+
21+
template <class T>
22+
class small_iter_allocator {
23+
public:
24+
using value_type = T;
25+
using pointer = small_pointer<T>;
26+
using size_type = std::uint16_t;
27+
using difference_type = std::int16_t;
28+
29+
small_iter_allocator() TEST_NOEXCEPT {}
30+
31+
template <class U>
32+
small_iter_allocator(small_iter_allocator<U>) TEST_NOEXCEPT {}
33+
34+
T* allocate(std::size_t n);
35+
void deallocate(T* p, std::size_t);
36+
37+
friend bool operator==(small_iter_allocator, small_iter_allocator) { return true; }
38+
friend bool operator!=(small_iter_allocator, small_iter_allocator) { return false; }
39+
};
40+
41+
template <class T>
42+
class final_small_iter_allocator final {
43+
public:
44+
using value_type = T;
45+
using pointer = small_pointer<T>;
46+
using size_type = std::uint16_t;
47+
using difference_type = std::int16_t;
48+
49+
final_small_iter_allocator() TEST_NOEXCEPT {}
50+
51+
template <class U>
52+
final_small_iter_allocator(final_small_iter_allocator<U>) TEST_NOEXCEPT {}
53+
54+
T* allocate(std::size_t n);
55+
void deallocate(T* p, std::size_t);
56+
57+
friend bool operator==(final_small_iter_allocator, final_small_iter_allocator) { return true; }
58+
friend bool operator!=(final_small_iter_allocator, final_small_iter_allocator) { return false; }
59+
};
60+
61+
template <class T, class Alloc>
62+
using unordered_set_alloc = std::unordered_set<T, std::hash<T>, std::equal_to<T>, Alloc>;
63+
64+
#if __SIZE_WIDTH__ == 64
65+
66+
static_assert(sizeof(unordered_set_alloc<int, std::allocator<int> >) == 40, "");
67+
static_assert(sizeof(unordered_set_alloc<int, min_allocator<int> >) == 40, "");
68+
static_assert(sizeof(unordered_set_alloc<int, test_allocator<int> >) == 64, "");
69+
static_assert(sizeof(unordered_set_alloc<int, small_iter_allocator<int> >) == 12, "");
70+
static_assert(sizeof(unordered_set_alloc<int, final_small_iter_allocator<int> >) == 16, "");
71+
72+
static_assert(sizeof(unordered_set_alloc<char, std::allocator<char> >) == 40, "");
73+
static_assert(sizeof(unordered_set_alloc<char, min_allocator<char> >) == 40, "");
74+
static_assert(sizeof(unordered_set_alloc<char, test_allocator<char> >) == 64, "");
75+
static_assert(sizeof(unordered_set_alloc<char, small_iter_allocator<char> >) == 12, "");
76+
static_assert(sizeof(unordered_set_alloc<char, final_small_iter_allocator<char> >) == 16, "");
77+
78+
static_assert(TEST_ALIGNOF(unordered_set_alloc<int, std::allocator<int> >) == 8, "");
79+
static_assert(TEST_ALIGNOF(unordered_set_alloc<int, min_allocator<int> >) == 8, "");
80+
static_assert(TEST_ALIGNOF(unordered_set_alloc<int, test_allocator<int> >) == 8, "");
81+
static_assert(TEST_ALIGNOF(unordered_set_alloc<int, small_iter_allocator<int> >) == 4, "");
82+
static_assert(TEST_ALIGNOF(unordered_set_alloc<int, final_small_iter_allocator<int> >) == 4, "");
83+
84+
static_assert(TEST_ALIGNOF(unordered_set_alloc<char, std::allocator<char> >) == 8, "");
85+
static_assert(TEST_ALIGNOF(unordered_set_alloc<char, min_allocator<char> >) == 8, "");
86+
static_assert(TEST_ALIGNOF(unordered_set_alloc<char, test_allocator<char> >) == 8, "");
87+
static_assert(TEST_ALIGNOF(unordered_set_alloc<char, small_iter_allocator<char> >) == 4, "");
88+
static_assert(TEST_ALIGNOF(unordered_set_alloc<char, final_small_iter_allocator<char> >) == 4, "");
89+
90+
struct TEST_ALIGNAS(32) AlignedHash {};
91+
struct UnalignedEqualTo {};
92+
93+
static_assert(sizeof(std::unordered_set<int, AlignedHash, UnalignedEqualTo>) == 96, "");
94+
static_assert(TEST_ALIGNOF(std::unordered_set<int, AlignedHash, UnalignedEqualTo>) == 32, "");
95+
96+
#elif __SIZE_WIDTH__ == 32
97+
98+
static_assert(sizeof(unordered_set_alloc<int, std::allocator<int> >) == 40, "");
99+
static_assert(sizeof(unordered_set_alloc<int, min_allocator<int> >) == 40, "");
100+
static_assert(sizeof(unordered_set_alloc<int, test_allocator<int> >) == 64, "");
101+
static_assert(sizeof(unordered_set_alloc<int, small_iter_allocator<int> >) == 12, "");
102+
static_assert(sizeof(unordered_set_alloc<int, final_small_iter_allocator<int> >) == 16, "");
103+
104+
static_assert(sizeof(unordered_set_alloc<char, std::allocator<char> >) == 40, "");
105+
static_assert(sizeof(unordered_set_alloc<char, min_allocator<char> >) == 40, "");
106+
static_assert(sizeof(unordered_set_alloc<char, test_allocator<char> >) == 64, "");
107+
static_assert(sizeof(unordered_set_alloc<char, small_iter_allocator<char> >) == 12, "");
108+
static_assert(sizeof(unordered_set_alloc<char, final_small_iter_allocator<char> >) == 16, "");
109+
110+
static_assert(TEST_ALIGNOF(unordered_set_alloc<int, std::allocator<int> >) == 8, "");
111+
static_assert(TEST_ALIGNOF(unordered_set_alloc<int, min_allocator<int> >) == 8, "");
112+
static_assert(TEST_ALIGNOF(unordered_set_alloc<int, test_allocator<int> >) == 8, "");
113+
static_assert(TEST_ALIGNOF(unordered_set_alloc<int, small_iter_allocator<int> >) == 4, "");
114+
static_assert(TEST_ALIGNOF(unordered_set_alloc<int, final_small_iter_allocator<int> >) == 4, "");
115+
116+
static_assert(TEST_ALIGNOF(unordered_set_alloc<char, std::allocator<char> >) == 8, "");
117+
static_assert(TEST_ALIGNOF(unordered_set_alloc<char, min_allocator<char> >) == 8, "");
118+
static_assert(TEST_ALIGNOF(unordered_set_alloc<char, test_allocator<char> >) == 8, "");
119+
static_assert(TEST_ALIGNOF(unordered_set_alloc<char, small_iter_allocator<char> >) == 4, "");
120+
static_assert(TEST_ALIGNOF(unordered_set_alloc<char, final_small_iter_allocator<char> >) == 4, "");
121+
122+
struct TEST_ALIGNAS(32) AlignedHash {};
123+
struct UnalignedEqualTo {};
124+
125+
static_assert(sizeof(std::unordered_set<int, AlignedHash, UnalignedEqualTo>) == 96);
126+
static_assert(TEST_ALIGNOF(std::unordered_set<int, AlignedHash, UnalignedEqualTo>) == 32);
127+
128+
#else
129+
# error std::size_t has an unexpected size
130+
#endif

0 commit comments

Comments
 (0)