object-introspection/test/integration/unions.toml
Jake Hillion 5071519e45 oil v2
2023-08-23 15:59:53 +01:00

114 lines
4.9 KiB
TOML

# TODO: The tagged union tests should be updated when tagged union support is in:
# https://github.com/facebookexperimental/object-introspection/issues/234
includes = ["string", "unordered_map", "vector"]
definitions = '''
union MyUnion {
int n;
std::vector<int> vec;
std::unordered_map<std::string, std::string> map;
MyUnion(int n_) : n(n_) {}
MyUnion(const std::vector<int>& vec_) : vec(vec_) {}
MyUnion(const std::unordered_map<std::string, std::string>& map_) : map(map_) {}
MyUnion(const MyUnion& /*other*/) { /* we don't know which field to copy */ }
~MyUnion() {}
};
struct TaggedUnion {
MyUnion storage;
uint8_t tag;
TaggedUnion(const MyUnion& storage_, uint8_t tag_) : storage(0), tag(tag_) {
copyStorage(storage_);
}
TaggedUnion(const TaggedUnion& other) : storage(0), tag(other.tag) {
copyStorage(other.storage);
}
void copyStorage(const MyUnion& other) {
if (tag == 0) {
storage.n = other.n;
} else if (tag == 1) {
new (&storage.vec) decltype(MyUnion::vec){other.vec};
} else if (tag == 2) {
new (&storage.map) decltype(MyUnion::map){other.map};
} else {
assert(false);
}
}
};
'''
[cases]
[cases.int]
param_types = ["const MyUnion&"]
setup = "return 123;"
expect_json = '[{"staticSize":56, "dynamicSize":0, "exclusiveSize":56, "NOT":"members"}]'
expect_json_v2 = '[{"staticSize":56, "dynamicSize":0, "exclusiveSize":56, "members":[]}]'
[cases.vector]
param_types = ["const MyUnion&"]
setup = "return std::vector{1,2,3};"
expect_json = '[{"staticSize":56, "dynamicSize":0, "exclusiveSize":56, "NOT":"members"}]'
expect_json_v2 = '[{"staticSize":56, "dynamicSize":0, "exclusiveSize":56, "members":[]}]'
[cases.unordered_map]
param_types = ["const MyUnion&"]
setup = 'return std::unordered_map<std::string, std::string>{{"a", "b"}, {"c","d"}};'
expect_json = '[{"staticSize":56, "dynamicSize":0, "exclusiveSize":56, "NOT":"members"}]'
expect_json_v2 = '[{"staticSize":56, "dynamicSize":0, "exclusiveSize":56, "members":[]}]'
[cases.alignment]
# Wrap the union in a pair as a way of inferring its alignment
param_types = ["const std::pair<char, MyUnion>&"]
setup = 'return {{0, MyUnion{123}}};'
expect_json = '''[
{"staticSize":64, "dynamicSize":0, "exclusiveSize":7, "members":[
{"staticSize":1, "dynamicSize":0, "exclusiveSize":1},
{"staticSize":56, "dynamicSize":0, "exclusiveSize":56, "NOT":"members"}
]}]'''
expect_json_v2 = '''[
{"staticSize":64, "dynamicSize":0, "exclusiveSize":7, "members":[
{"staticSize":1, "dynamicSize":0, "exclusiveSize":1},
{"staticSize":56, "dynamicSize":0, "exclusiveSize":56, "members":[]}
]}]'''
[cases.tagged_int]
param_types = ["const TaggedUnion&"]
setup = "return TaggedUnion{MyUnion{123}, 0};"
expect_json = '''[
{"staticSize":64, "dynamicSize":0, "exclusiveSize":7, "members":[
{"name":"storage", "staticSize":56, "dynamicSize":0, "exclusiveSize":56, "NOT":"members"},
{"name":"tag", "staticSize":1, "dynamicSize":0, "exclusiveSize":1, "NOT":"members"}
]}]'''
expect_json_v2 = '''[
{"staticSize":64, "dynamicSize":0, "exclusiveSize":7, "members":[
{"name":"storage", "staticSize":56, "dynamicSize":0, "exclusiveSize":56, "members":[]},
{"name":"tag", "staticSize":1, "dynamicSize":0, "exclusiveSize":1, "members":[]}
]}]'''
[cases.tagged_vector]
param_types = ["const TaggedUnion&"]
setup = "return TaggedUnion{MyUnion{std::vector{1,2,3}}, 1};"
expect_json = '''[
{"staticSize":64, "dynamicSize":0, "exclusiveSize":7, "members":[
{"name":"storage", "staticSize":56, "dynamicSize":0, "exclusiveSize":56, "NOT":"members"},
{"name":"tag", "staticSize":1, "dynamicSize":0, "exclusiveSize":1, "NOT":"members"}
]}]'''
expect_json_v2 = '''[
{"staticSize":64, "dynamicSize":0, "exclusiveSize":7, "members":[
{"name":"storage", "staticSize":56, "dynamicSize":0, "exclusiveSize":56, "members":[]},
{"name":"tag", "staticSize":1, "dynamicSize":0, "exclusiveSize":1, "members":[]}
]}]'''
[cases.tagged_unordered_map]
param_types = ["const TaggedUnion&"]
setup = 'return TaggedUnion{MyUnion{std::unordered_map<std::string, std::string>{{"a", "b"}, {"c","d"}}}, 2};'
expect_json = '''[
{"staticSize":64, "dynamicSize":0, "exclusiveSize":7, "members":[
{"name":"storage", "staticSize":56, "dynamicSize":0, "exclusiveSize":56, "NOT":"members"},
{"name":"tag", "staticSize":1, "dynamicSize":0, "exclusiveSize":1, "NOT":"members"}
]}]'''
expect_json_v2 = '''[
{"staticSize":64, "dynamicSize":0, "exclusiveSize":7, "members":[
{"name":"storage", "staticSize":56, "dynamicSize":0, "exclusiveSize":56, "members":[]},
{"name":"tag", "staticSize":1, "dynamicSize":0, "exclusiveSize":1, "members":[]}
]}]'''