1
1
# This file contains tests for core key-capture features at the top, followed
2
2
# by basic tests for each container type which supports key capture.
3
3
4
- includes = [" map" ]
4
+ includes = [" map" , " sys/mman.h " ]
5
5
definitions = '''
6
6
struct MapHolder {
7
7
std::map<int, int> captureMyKeys;
@@ -13,6 +13,33 @@ enum class MyEnum {
13
13
Two = 2,
14
14
Three = 3,
15
15
};
16
+
17
+ struct KeyObj {
18
+ int n;
19
+ auto operator<=>(const KeyObj &) const = default;
20
+ };
21
+ template <typename T>
22
+ class FixedAllocator {
23
+ public:
24
+ using value_type = T;
25
+ T* allocate(std::size_t n) {
26
+ uintptr_t loc = base_ + num_*0x1000;
27
+ num_++;
28
+ return (T*)mmap((void*)loc,
29
+ n,
30
+ PROT_READ|PROT_WRITE,
31
+ MAP_PRIVATE|MAP_ANONYMOUS,
32
+ -1,
33
+ 0);
34
+ }
35
+ void deallocate(T*, std::size_t) noexcept {
36
+ // cba
37
+ }
38
+
39
+ private:
40
+ uintptr_t base_ = 0x300000;
41
+ size_t num_ = 0;
42
+ };
16
43
'''
17
44
[cases ]
18
45
[cases .int ]
@@ -118,6 +145,46 @@ enum class MyEnum {
118
145
]
119
146
}]'''
120
147
148
+ [cases .object ]
149
+ oid_skip = " Requires TreeBuilderV2"
150
+ param_types = [" const std::map<KeyObj, int, std::less<KeyObj>, FixedAllocator<std::pair<const KeyObj, int>>>&" ]
151
+ # We're going to record the addresses of these objects, so we use a custom
152
+ # allocator based on mmap to get them at a known location in memory
153
+ setup = '''
154
+ std::map<KeyObj, int, std::less<KeyObj>, FixedAllocator<std::pair<const KeyObj, int>>> m;
155
+ m.insert({KeyObj{1}, 1});
156
+ m.insert({KeyObj{2}, 2});
157
+ return m;
158
+ '''
159
+ config_suffix = '''
160
+ [[codegen.capture_keys]]
161
+ top_level = true
162
+ '''
163
+ expect_json_v2 = ''' [{
164
+ "name": "a0",
165
+ "typePath": ["a0"],
166
+ "members": [
167
+ {
168
+ "name": "[]",
169
+ "typePath": ["a0","[]"],
170
+ "data": "0x300020",
171
+ "members": [
172
+ {"name": "key", "typePath": ["a0","[]","key"]},
173
+ {"name": "value", "typePath": ["a0","[]","value"]}
174
+ ]
175
+ },
176
+ {
177
+ "name": "[]",
178
+ "typePath": ["a0","[]"],
179
+ "data": "0x301020",
180
+ "members": [
181
+ {"name": "key", "typePath": ["a0","[]","key"]},
182
+ {"name": "value", "typePath": ["a0","[]","value"]}
183
+ ]
184
+ }
185
+ ]
186
+ }]'''
187
+
121
188
[cases .multi_level ]
122
189
oid_skip = " Requires TreeBuilderV2"
123
190
param_types = [" const std::map<int, std::map<std::string, std::vector<int>>>&" ]
0 commit comments