Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: builder children interface #102

Merged
merged 14 commits into from
Nov 5, 2023
Prev Previous commit
Next Next commit
Fixed #101. Added children operator. Added unit tests for node payloads.
  • Loading branch information
Mykola Morozov committed Nov 5, 2023
commit e9b86ce5f7a737e5a0187bdedda7509f9b91d18f
1 change: 1 addition & 0 deletions libs/cpp/lang/include/lang/builders/allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class allocator {
AcquireT acquire;
ReleaseT release;

[[nodiscard]] builder none() const;
builder source_root(const char* filename) const;
builder scope(const char* identifier) const;
builder heap_type(const char* identifier) const;
Expand Down
8 changes: 8 additions & 0 deletions libs/cpp/lang/src/builders/allocator.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#include "lang/builders/allocator.hpp"
#include "lang/builders/builder.hpp"
#include "core/nodes/node_type.h"

using namespace cl::lang::builders;

bool
allocator::equal(const allocator &left, const allocator &right) {
return left.acquire == right.acquire && left.release == right.release;
}

builder
allocator::none() const {

return builder::from_payload(*this, nullptr, ::none);
}
2 changes: 0 additions & 2 deletions libs/cpp/lang/src/builders/builder.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "lang/builders/builder.hpp"
#include "lang/builders/builders.hpp"
#include "lang/builders/allocator.hpp"
#include "lang/language.hpp"
#include "core/node.h"

namespace b = cl::lang::builders;
Expand Down
4 changes: 0 additions & 4 deletions libs/cpp/lang/src/builders/builders.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
#include "lang/builders/builders.hpp"
#include "lang/builders/builder.hpp"

namespace b = cl::lang::builders;
1 change: 0 additions & 1 deletion libs/cpp/lang/src/builders/heap_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "lang/builders/allocator.hpp"
#include "lang/builders/builder.hpp"
#include "lang/utils.hpp"
#include "core/node.h"
#include "core/nodes/node_type.h"

namespace b = cl::lang::builders;
Expand Down
1 change: 0 additions & 1 deletion libs/cpp/lang/src/builders/scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "lang/builders/allocator.hpp"
#include "lang/builders/builder.hpp"
#include "lang/utils.hpp"
#include "core/node.h"
#include "core/nodes/node_type.h"

namespace b = cl::lang::builders;
Expand Down
1 change: 0 additions & 1 deletion libs/cpp/lang/src/builders/source_root.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "lang/builders/allocator.hpp"
#include "lang/builders/builder.hpp"
#include "lang/utils.hpp"
#include "core/node.h"
#include "core/nodes/node_type.h"

namespace b = cl::lang::builders;
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ set(PUBLIC_SOURCES_FILES
"${LIBRARY_BASE_PATH}/src/core/config.cpp"
"${LIBRARY_BASE_PATH}/src/lang/builders/builder_child.cpp"
"${LIBRARY_BASE_PATH}/src/lang/builders/builder_children.cpp"
"${LIBRARY_BASE_PATH}/src/lang/builders/source_root.cpp"
"${LIBRARY_BASE_PATH}/src/lang/builders/scope.cpp"
"${LIBRARY_BASE_PATH}/src/lang/builders/heap_type.cpp"
)

set(DOCTEST_NO_INSTALL ON)
Expand Down
4 changes: 2 additions & 2 deletions tests/src/core/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

TEST_SUITE_BEGIN("core/config");

TEST_CASE("Lifetime") {
TEST_CASE("config lifetime") {
struct cl_config* config = cl_config_init();
REQUIRE_NE((struct cl_config*)nullptr, config);

Expand All @@ -14,7 +14,7 @@ TEST_CASE("Lifetime") {
REQUIRE_EQ(1, term);
}

TEST_CASE("String") {
TEST_CASE("config string") {
struct cl_config* config = cl_config_init();
REQUIRE_NE((struct cl_config*)nullptr, config);

Expand Down
18 changes: 9 additions & 9 deletions tests/src/lang/builders/builder_child.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace b = cl::lang::builders;

TEST_SUITE_BEGIN("lang/builders/builder_child");

TEST_CASE("Child single") {
TEST_CASE("builder_child single") {
auto m = b::allocator{ malloc, free };
auto factory = m.source_root("hello.ts");

Expand All @@ -25,7 +25,7 @@ TEST_CASE("Child single") {
REQUIRE_UNARY_FALSE(strcmp("hello.ts", ((const cl_node_source_root*)root->payload)->file_name));
}

TEST_CASE("Child hierarchy temporary") {
TEST_CASE("builder_child hierarchy temporary") {
auto m = b::allocator{ malloc, free };
auto factory = m.source_root("hello.ts") << m.source_root("world.ts");

Expand All @@ -49,7 +49,7 @@ TEST_CASE("Child hierarchy temporary") {
REQUIRE_UNARY_FALSE(strcmp("world.ts", ((const cl_node_source_root*)child->payload)->file_name));
}

TEST_CASE("Child hierarchy variable") {
TEST_CASE("builder_child hierarchy variable") {
auto m = b::allocator{ malloc, free };
auto a = m.source_root("hello.ts");
auto factory = a << m.source_root("world.ts");
Expand All @@ -74,7 +74,7 @@ TEST_CASE("Child hierarchy variable") {
REQUIRE_UNARY_FALSE(strcmp("world.ts", ((const cl_node_source_root*)child->payload)->file_name));
}

TEST_CASE("Child hierarchy variable rewrite") {
TEST_CASE("builder_child hierarchy variable rewrite") {
auto m = b::allocator{ malloc, free };
auto factory = m.source_root("hello.ts");
factory = factory << m.source_root("world.ts");
Expand All @@ -99,7 +99,7 @@ TEST_CASE("Child hierarchy variable rewrite") {
REQUIRE_UNARY_FALSE(strcmp("world.ts", ((const cl_node_source_root*)child->payload)->file_name));
}

TEST_CASE("Child hierarchy variable rewrite reverse") {
TEST_CASE("builder_child hierarchy variable rewrite reverse") {
auto m = b::allocator{ malloc, free };
auto factory = m.source_root("world.ts");
factory = m.source_root("hello.ts") << factory;
Expand All @@ -124,7 +124,7 @@ TEST_CASE("Child hierarchy variable rewrite reverse") {
REQUIRE_UNARY_FALSE(strcmp("world.ts", ((const cl_node_source_root*)child->payload)->file_name));
}

TEST_CASE("Child hierarchy both variables rewrite") {
TEST_CASE("builder_child hierarchy both variables rewrite") {
auto m = b::allocator{ malloc, free };
auto factory = m.source_root("world.ts");
auto a = m.source_root("hello.ts");
Expand All @@ -150,7 +150,7 @@ TEST_CASE("Child hierarchy both variables rewrite") {
REQUIRE_UNARY_FALSE(strcmp("world.ts", ((const cl_node_source_root*)child->payload)->file_name));
}

TEST_CASE("Child hierarchy temporary nested temporary") {
TEST_CASE("builder_child hierarchy temporary nested temporary") {
auto m = b::allocator{ malloc, free };
auto factory = m.source_root("hello.ts") << m.source_root("world.ts") << m.source_root("!.ts");

Expand Down Expand Up @@ -183,7 +183,7 @@ TEST_CASE("Child hierarchy temporary nested temporary") {
REQUIRE_UNARY_FALSE(strcmp("!.ts", ((const cl_node_source_root*)child->payload)->file_name));
}

TEST_CASE("Child hierarchy variable nested temporary") {
TEST_CASE("builder_child hierarchy variable nested temporary") {
auto m = b::allocator{ malloc, free };
auto factory = m.source_root("hello.ts");
factory = factory << m.source_root("world.ts") << m.source_root("!.ts");
Expand Down Expand Up @@ -217,7 +217,7 @@ TEST_CASE("Child hierarchy variable nested temporary") {
REQUIRE_UNARY_FALSE(strcmp("!.ts", ((const cl_node_source_root*)child->payload)->file_name));
}

TEST_CASE("Child hierarchy variable nested variable") {
TEST_CASE("builder_child hierarchy variable nested variable") {
auto m = b::allocator{ malloc, free };
auto factory = m.source_root("hello.ts");
factory = factory << m.source_root("world.ts");
Expand Down
47 changes: 42 additions & 5 deletions tests/src/lang/builders/builder_children.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace b = cl::lang::builders;

TEST_SUITE_BEGIN("lang/builders/builder_children");

TEST_CASE("Children hierarchy temporary") {
TEST_CASE("builder_children hierarchy temporary") {
auto m = b::allocator{ malloc, free };
auto factory = m.source_root("hello.ts")
<< b::children(m.source_root("world.ts"), m.source_root("!.ts"));
Expand Down Expand Up @@ -46,7 +46,7 @@ TEST_CASE("Children hierarchy temporary") {
REQUIRE_UNARY_FALSE(strcmp("!.ts", ((const cl_node_source_root*)child->payload)->file_name));
}

TEST_CASE("Children hierarchy variable") {
TEST_CASE("builder_children hierarchy variable") {
auto m = b::allocator{ malloc, free };
auto a = m.source_root("hello.ts");
auto factory = a << b::children(m.source_root("world.ts"), m.source_root("!.ts"));
Expand Down Expand Up @@ -81,7 +81,7 @@ TEST_CASE("Children hierarchy variable") {
REQUIRE_UNARY_FALSE(strcmp("!.ts", ((const cl_node_source_root*)child->payload)->file_name));
}

TEST_CASE("Children hierarchy variable rewrite") {
TEST_CASE("builder_children hierarchy variable rewrite") {
auto m = b::allocator{ malloc, free };
auto factory = m.source_root("hello.ts");
factory = factory << b::children(m.source_root("world.ts"), m.source_root("!.ts"));
Expand Down Expand Up @@ -116,7 +116,7 @@ TEST_CASE("Children hierarchy variable rewrite") {
REQUIRE_UNARY_FALSE(strcmp("!.ts", ((const cl_node_source_root*)child->payload)->file_name));
}

TEST_CASE("Children hierarchy variable rewrite reverse") {
TEST_CASE("builder_children hierarchy variable rewrite reverse") {
auto m = b::allocator{ malloc, free };
auto a = b::children(m.source_root("world.ts"), m.source_root("!.ts"));
auto factory = m.source_root("hello.ts") << a;
Expand Down Expand Up @@ -151,7 +151,7 @@ TEST_CASE("Children hierarchy variable rewrite reverse") {
REQUIRE_UNARY_FALSE(strcmp("!.ts", ((const cl_node_source_root*)child->payload)->file_name));
}

TEST_CASE("Children hierarchy mixed children temporary") {
TEST_CASE("builder_children hierarchy mixed children temporary") {
auto m = b::allocator{ malloc, free };
auto a = m.source_root("world.ts");
auto factory = m.source_root("hello.ts") << b::children(a, m.source_root("!.ts"));
Expand Down Expand Up @@ -186,4 +186,41 @@ TEST_CASE("Children hierarchy mixed children temporary") {
REQUIRE_UNARY_FALSE(strcmp("!.ts", ((const cl_node_source_root*)child->payload)->file_name));
}

TEST_CASE("builder_children hierarchy mixed children variable") {
auto m = b::allocator{ malloc, free };
auto a = m.source_root("world.ts");
auto b = b::children(a, m.source_root("!.ts"));
auto c = m.source_root("hello.ts");
auto factory = c << b;

auto root = factory.root_get();
REQUIRE_UNARY(root);
REQUIRE_NE(root, factory.parent_get());
REQUIRE_EQ(2, root->child_count);
REQUIRE_UNARY(root->payload);
REQUIRE_EQ(::source_root, root->payload_type);
REQUIRE_UNARY_FALSE(root->parent);
REQUIRE_UNARY_FALSE(strcmp("hello.ts", ((const cl_node_source_root*)root->payload)->file_name));

auto child = root->children;
REQUIRE_UNARY(child);
REQUIRE_EQ((child + 1), factory.parent_get());
REQUIRE_EQ(0, child->child_count);
REQUIRE_UNARY_FALSE(child->children);
REQUIRE_UNARY(child->payload);
REQUIRE_EQ(::source_root, child->payload_type);
REQUIRE_EQ(root, child->parent);
REQUIRE_UNARY_FALSE(strcmp("world.ts", ((const cl_node_source_root*)child->payload)->file_name));

++child;
REQUIRE_UNARY(child);
REQUIRE_EQ(child, factory.parent_get());
REQUIRE_EQ(0, child->child_count);
REQUIRE_UNARY_FALSE(child->children);
REQUIRE_UNARY(child->payload);
REQUIRE_EQ(::source_root, child->payload_type);
REQUIRE_EQ(root, child->parent);
REQUIRE_UNARY_FALSE(strcmp("!.ts", ((const cl_node_source_root*)child->payload)->file_name));
}

TEST_SUITE_END();
74 changes: 74 additions & 0 deletions tests/src/lang/builders/heap_type.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <cstring>
#include "lang/builders/allocator.hpp"
#include "lang/builders/builder.hpp"
#include "core/node.h"
#include "core/nodes/node_type.h"
#include "core/nodes/heap_type.h"
#include "doctest/doctest.h"

namespace b = cl::lang::builders;

TEST_SUITE_BEGIN("lang/builders/heap_type");

TEST_CASE("heap_type lifetime") {
auto m = b::allocator{ malloc, free };
auto factory = m.heap_type("hello");

auto root = factory.root_get();
REQUIRE_UNARY(root);
REQUIRE_EQ(root, factory.parent_get());
REQUIRE_EQ(0, root->child_count);
REQUIRE_EQ(nullptr, root->children);
REQUIRE_UNARY(root->payload);
REQUIRE_EQ(::heap_type, root->payload_type);
REQUIRE_UNARY_FALSE(root->parent);
REQUIRE_UNARY_FALSE(strcmp("hello", ((const cl_node_heap_type*)root->payload)->identifier));
}

TEST_CASE("heap_type value") {
auto m = b::allocator{ malloc, free };

SUBCASE("empty") {
auto factory = m.heap_type("");

auto root = factory.root_get();
REQUIRE_UNARY(root);
REQUIRE_EQ(root, factory.parent_get());
REQUIRE_EQ(0, root->child_count);
REQUIRE_EQ(nullptr, root->children);
REQUIRE_UNARY(root->payload);
REQUIRE_EQ(::heap_type, root->payload_type);
REQUIRE_UNARY_FALSE(root->parent);
REQUIRE_UNARY_FALSE(strcmp("", ((const cl_node_heap_type*)root->payload)->identifier));
}

SUBCASE("null") {
auto factory = m.heap_type(nullptr);

auto root = factory.root_get();
REQUIRE_UNARY(root);
REQUIRE_EQ(root, factory.parent_get());
REQUIRE_EQ(0, root->child_count);
REQUIRE_EQ(nullptr, root->children);
REQUIRE_UNARY(root->payload);
REQUIRE_EQ(::heap_type, root->payload_type);
REQUIRE_UNARY_FALSE(root->parent);
REQUIRE_EQ(nullptr, ((const cl_node_heap_type*)root->payload)->identifier);
}

SUBCASE("filled") {
auto factory = m.heap_type("hello");

auto root = factory.root_get();
REQUIRE_UNARY(root);
REQUIRE_EQ(root, factory.parent_get());
REQUIRE_EQ(0, root->child_count);
REQUIRE_EQ(nullptr, root->children);
REQUIRE_UNARY(root->payload);
REQUIRE_EQ(::heap_type, root->payload_type);
REQUIRE_UNARY_FALSE(root->parent);
REQUIRE_UNARY_FALSE(strcmp("hello", ((const cl_node_heap_type*)root->payload)->identifier));
}
}

TEST_SUITE_END();
Loading