2023-05-26 18:26:32 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2023-06-20 17:57:43 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
2023-05-26 18:26:32 +01:00
|
|
|
|
2023-07-25 13:45:22 +01:00
|
|
|
#include "TypeGraphParser.h"
|
2023-06-20 17:57:43 +01:00
|
|
|
#include "mocks.h"
|
|
|
|
#include "oi/CodeGen.h"
|
|
|
|
#include "oi/type_graph/TypeGraph.h"
|
|
|
|
#include "oi/type_graph/Types.h"
|
|
|
|
#include "type_graph_utils.h"
|
|
|
|
|
|
|
|
using namespace type_graph;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using ref = std::reference_wrapper<T>;
|
|
|
|
|
|
|
|
namespace {
|
2023-07-25 13:45:22 +01:00
|
|
|
void testTransform(OICodeGen::Config& config,
|
|
|
|
std::string_view input,
|
|
|
|
std::string_view expectedAfter) {
|
|
|
|
input.remove_prefix(1); // Remove initial '\n'
|
|
|
|
TypeGraph typeGraph;
|
|
|
|
TypeGraphParser parser{typeGraph};
|
|
|
|
try {
|
|
|
|
parser.parse(input);
|
|
|
|
} catch (const TypeGraphParserError& err) {
|
|
|
|
FAIL() << "Error parsing input graph: " << err.what();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate input formatting
|
2023-08-02 11:11:52 +01:00
|
|
|
check(typeGraph, input, "parsing input graph");
|
2023-07-25 13:45:22 +01:00
|
|
|
|
|
|
|
MockSymbolService symbols;
|
|
|
|
CodeGen codegen{config, symbols};
|
2023-11-02 13:18:32 +00:00
|
|
|
for (auto& info : getContainerInfos()) {
|
|
|
|
codegen.registerContainer(std::move(info));
|
|
|
|
}
|
2023-07-25 13:45:22 +01:00
|
|
|
codegen.transform(typeGraph);
|
|
|
|
|
2023-08-02 11:11:52 +01:00
|
|
|
check(typeGraph, expectedAfter, "after transform");
|
2023-07-25 13:45:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void testTransform(std::string_view input, std::string_view expectedAfter) {
|
|
|
|
OICodeGen::Config config;
|
|
|
|
testTransform(config, input, expectedAfter);
|
|
|
|
}
|
2023-06-20 17:57:43 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(CodeGenTest, TransformContainerAllocator) {
|
2023-08-01 19:06:02 +01:00
|
|
|
testTransform(R"(
|
2023-11-02 13:18:32 +00:00
|
|
|
[0] Class: std::vector (size: 24)
|
2023-06-20 17:57:43 +01:00
|
|
|
Param
|
|
|
|
Primitive: int32_t
|
|
|
|
Param
|
|
|
|
[1] Struct: MyAlloc (size: 8)
|
2023-06-26 16:07:12 +01:00
|
|
|
Param
|
|
|
|
Primitive: int32_t
|
2023-06-20 17:57:43 +01:00
|
|
|
Function: allocate
|
|
|
|
Function: deallocate
|
|
|
|
)",
|
|
|
|
R"(
|
2023-11-02 13:22:57 +00:00
|
|
|
[2] Container: std::vector<int32_t, DummyAllocator<int32_t, 8, 1, 3>> (size: 24)
|
2023-06-20 17:57:43 +01:00
|
|
|
Param
|
|
|
|
Primitive: int32_t
|
|
|
|
Param
|
2023-11-02 13:22:57 +00:00
|
|
|
[3] DummyAllocator [MyAlloc] (size: 8, align: 1)
|
2023-06-20 17:57:43 +01:00
|
|
|
Primitive: int32_t
|
|
|
|
)");
|
2023-05-26 18:26:32 +01:00
|
|
|
}
|
2023-06-26 16:07:12 +01:00
|
|
|
|
|
|
|
TEST(CodeGenTest, TransformContainerAllocatorParamInParent) {
|
2023-08-01 19:06:02 +01:00
|
|
|
testTransform(R"(
|
2023-11-02 13:18:32 +00:00
|
|
|
[0] Class: std::map (size: 24)
|
2023-06-26 16:07:12 +01:00
|
|
|
Param
|
|
|
|
Primitive: int32_t
|
|
|
|
Param
|
|
|
|
Primitive: int32_t
|
|
|
|
Param
|
|
|
|
[1] Struct: MyAlloc<std::pair<const int, int>> (size: 1)
|
|
|
|
Parent (offset: 0)
|
|
|
|
[2] Struct: MyAllocBase<std::pair<const int, int>> (size: 1)
|
|
|
|
Param
|
2023-11-02 13:18:32 +00:00
|
|
|
[3] Class: std::pair (size: 8)
|
2023-06-26 16:07:12 +01:00
|
|
|
Param
|
|
|
|
Primitive: int32_t
|
|
|
|
Qualifiers: const
|
|
|
|
Param
|
|
|
|
Primitive: int32_t
|
|
|
|
Function: allocate
|
|
|
|
Function: deallocate
|
|
|
|
Function: allocate
|
|
|
|
Function: deallocate
|
|
|
|
)",
|
|
|
|
R"(
|
2023-11-02 13:22:57 +00:00
|
|
|
[4] Container: std::map<int32_t, int32_t, DummyAllocator<std::pair<int32_t const, int32_t>, 0, 1, 6>> (size: 24)
|
2023-06-26 16:07:12 +01:00
|
|
|
Param
|
|
|
|
Primitive: int32_t
|
|
|
|
Param
|
|
|
|
Primitive: int32_t
|
|
|
|
Param
|
2023-11-02 13:22:57 +00:00
|
|
|
[6] DummyAllocator [MyAlloc<std::pair<const int, int>>] (size: 0, align: 1)
|
2023-11-02 13:18:32 +00:00
|
|
|
[5] Container: std::pair<int32_t const, int32_t> (size: 8)
|
2023-06-26 16:07:12 +01:00
|
|
|
Param
|
|
|
|
Primitive: int32_t
|
|
|
|
Qualifiers: const
|
|
|
|
Param
|
|
|
|
Primitive: int32_t
|
|
|
|
)");
|
|
|
|
}
|
2023-07-25 13:45:22 +01:00
|
|
|
|
|
|
|
TEST(CodeGenTest, RemovedMemberAlignment) {
|
|
|
|
OICodeGen::Config config;
|
|
|
|
config.membersToStub = {{"MyClass", "b"}};
|
2023-11-13 16:50:09 +00:00
|
|
|
testTransform(config,
|
|
|
|
R"(
|
2023-07-25 13:45:22 +01:00
|
|
|
[0] Class: MyClass (size: 24)
|
|
|
|
Member: a (offset: 0)
|
|
|
|
Primitive: int8_t
|
|
|
|
Member: b (offset: 8)
|
|
|
|
Primitive: int64_t
|
|
|
|
Member: c (offset: 16)
|
|
|
|
Primitive: int8_t
|
|
|
|
)",
|
|
|
|
R"(
|
2023-08-11 15:09:23 +01:00
|
|
|
[0] Class: MyClass_0 [MyClass] (size: 24, align: 8)
|
|
|
|
Member: a_0 [a] (offset: 0, align: 1)
|
2023-07-25 13:45:22 +01:00
|
|
|
Primitive: int8_t
|
|
|
|
Member: __oi_padding_1 (offset: 1)
|
2023-08-11 15:09:23 +01:00
|
|
|
[1] Array: [int8_t[15]] (length: 15)
|
2023-07-25 13:45:22 +01:00
|
|
|
Primitive: int8_t
|
2023-08-11 15:09:23 +01:00
|
|
|
Member: c_2 [c] (offset: 16, align: 1)
|
2023-07-25 13:45:22 +01:00
|
|
|
Primitive: int8_t
|
|
|
|
Member: __oi_padding_3 (offset: 17)
|
2023-08-11 15:09:23 +01:00
|
|
|
[2] Array: [int8_t[7]] (length: 7)
|
2023-07-25 13:45:22 +01:00
|
|
|
Primitive: int8_t
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CodeGenTest, UnionMembersAlignment) {
|
|
|
|
testTransform(R"(
|
|
|
|
[0] Union: MyUnion (size: 8)
|
|
|
|
Member: a (offset: 0)
|
|
|
|
Primitive: int8_t
|
|
|
|
Member: b (offset: 8)
|
|
|
|
Primitive: int64_t
|
|
|
|
)",
|
|
|
|
R"(
|
2023-08-11 15:09:23 +01:00
|
|
|
[0] Union: MyUnion_0 [MyUnion] (size: 8, align: 8)
|
2023-07-19 14:45:28 +01:00
|
|
|
Member: __oi_padding_0 (offset: 0)
|
2023-08-11 15:09:23 +01:00
|
|
|
[1] Array: [int8_t[8]] (length: 8)
|
2023-07-19 14:45:28 +01:00
|
|
|
Primitive: int8_t
|
2023-07-25 13:45:22 +01:00
|
|
|
)");
|
|
|
|
}
|
2023-11-02 13:18:32 +00:00
|
|
|
|
|
|
|
TEST(CodeGenTest, ReplaceContainersAndDummies) {
|
|
|
|
testTransform(R"(
|
|
|
|
[0] Class: std::vector (size: 24)
|
|
|
|
Param
|
|
|
|
Primitive: uint32_t
|
|
|
|
Param
|
|
|
|
[1] Class: allocator<int> (size: 1)
|
|
|
|
Param
|
|
|
|
Primitive: uint32_t
|
|
|
|
Function: allocate
|
|
|
|
Function: deallocate
|
|
|
|
)",
|
|
|
|
R"(
|
2023-11-02 13:22:57 +00:00
|
|
|
[2] Container: std::vector<uint32_t, DummyAllocator<uint32_t, 0, 1, 3>> (size: 24)
|
2023-11-02 13:18:32 +00:00
|
|
|
Param
|
|
|
|
Primitive: uint32_t
|
|
|
|
Param
|
2023-11-02 13:22:57 +00:00
|
|
|
[3] DummyAllocator [allocator<int>] (size: 0, align: 1)
|
2023-11-02 13:18:32 +00:00
|
|
|
Primitive: uint32_t
|
|
|
|
)");
|
|
|
|
}
|