Skip to content

Commit fb66f3e

Browse files
committed
CaptureKeys: Add test for capturing object pointers
1 parent d01c32c commit fb66f3e

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

test/integration/capture_keys.toml

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This file contains tests for core key-capture features at the top, followed
22
# by basic tests for each container type which supports key capture.
33

4-
includes = ["map"]
4+
includes = ["map","sys/mman.h"]
55
definitions = '''
66
struct MapHolder {
77
std::map<int, int> captureMyKeys;
@@ -13,6 +13,33 @@ enum class MyEnum {
1313
Two = 2,
1414
Three = 3,
1515
};
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+
};
1643
'''
1744
[cases]
1845
[cases.int]
@@ -118,6 +145,46 @@ enum class MyEnum {
118145
]
119146
}]'''
120147

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+
121188
[cases.multi_level]
122189
oid_skip = "Requires TreeBuilderV2"
123190
param_types = ["const std::map<int, std::map<std::string, std::vector<int>>>&"]

0 commit comments

Comments
 (0)