Commits (5)
......@@ -265,12 +265,14 @@ public:
}
Node(Node&& other)
: c_node(other.c_node)
, own_c_node(other.own_c_node)
{
other.c_node = nullptr;
other.own_c_node = false;
}
~Node()
{
if (this->c_node)
if (this->c_node && this->own_c_node)
{
conduit_node_destroy(this->c_node);
}
......@@ -984,7 +986,9 @@ public:
Node& operator=(Node&& other)
{
this->c_node = other.c_node;
this->own_c_node = other.own_c_node;
other.c_node = nullptr;
other.own_c_node = false;
return *this;
}
//-----------------------------------------------------------------------------
......@@ -1566,9 +1570,11 @@ public:
private:
conduit_node* c_node;
bool own_c_node = true;
explicit Node(conduit_node* other)
: c_node(other)
, own_c_node(false)
{
}
......@@ -1638,11 +1644,7 @@ inline conduit_node* c_node(const Node* n)
inline Node cpp_node(conduit_node* n)
{
conduit_node* n2 = conduit_node_create();
// Setting it as external tells the underlying conduit_node
// that we do not own this data. So, we won't ever free it.
conduit_node_set_external_node(n2, n);
return Node(n2);
return Node(n);
}
}
......
......@@ -7,3 +7,7 @@ add_test(NAME catalyst-impl-double COMMAND test_double_impl)
add_executable(test_catalyst_results catalyst_results/test_catalyst_results.c)
target_link_libraries(test_catalyst_results catalyst::catalyst)
add_test(NAME catalyst-results COMMAND test_catalyst_results)
add_executable(test_catalyst_conduit catalyst_conduit/test_catalyst_conduit.cpp)
target_link_libraries(test_catalyst_conduit catalyst::catalyst)
add_test(NAME catalyst-conduit COMMAND test_catalyst_conduit)
/*
* Distributed under OSI-approved BSD 3-Clause License. See
* accompanying License.txt
*/
#include <catalyst_conduit.hpp>
#include <stdio.h>
#include <stdlib.h>
int test_cpp_node_wrapping_data()
{
int ret = EXIT_SUCCESS;
conduit_cpp::Node first_node;
conduit_node* first_node_c_node = conduit_cpp::c_node(&first_node);
conduit_cpp::Node second_node = conduit_cpp::cpp_node(first_node_c_node);
second_node["test"] = 2;
if (!first_node.has_child("test"))
{
ret = EXIT_FAILURE;
}
else
{
auto child_node = first_node["test"];
if (!child_node.dtype().is_int())
{
ret = EXIT_FAILURE;
}
else
{
int value = child_node.as_int();
if (value != 2)
{
ret = EXIT_FAILURE;
}
}
}
return ret;
}
int test_cpp_node_wrapping_on_child_node()
{
int ret = EXIT_SUCCESS;
conduit_cpp::Node first_node;
auto child_node = first_node["test"];
conduit_node* child_node_c_node = conduit_cpp::c_node(&child_node);
conduit_cpp::Node child_node_wrapping = conduit_cpp::cpp_node(child_node_c_node);
child_node_wrapping["sub_key"] = 5;
if (!first_node.has_path("test/sub_key"))
{
ret = EXIT_FAILURE;
}
else
{
auto sub_node = first_node["test/sub_key"];
if (!sub_node.dtype().is_int())
{
ret = EXIT_FAILURE;
}
else
{
int value = sub_node.as_int();
if (value != 5)
{
ret = EXIT_FAILURE;
}
}
}
return ret;
}
int test_cpp_node_wrapping_destructor()
{
int ret = EXIT_SUCCESS;
conduit_cpp::Node* first_node = new conduit_cpp::Node();
conduit_node* first_node_c_node = conduit_cpp::c_node(first_node);
conduit_cpp::Node second_node = conduit_cpp::cpp_node(first_node_c_node);
second_node["test"] = 2;
delete first_node;
// // second_node has invalid data, the following lines crash.
// auto value = second_node["test"].as_int();
// if (value != 2)
// {
// ret = EXIT_FAILURE;
// }
return ret;
}
int test_cpp_node_copy()
{
int ret = EXIT_SUCCESS;
conduit_cpp::Node n;
conduit_cpp::Node clone = n;
return ret;
}
int main(int argc, char* argv[])
{
int ret = EXIT_SUCCESS;
int test_ret = test_cpp_node_wrapping_data();
if (test_ret != EXIT_SUCCESS)
{
ret = test_ret;
}
if (ret == EXIT_SUCCESS)
{
test_ret = test_cpp_node_wrapping_on_child_node();
if (test_ret != EXIT_SUCCESS)
{
ret = test_ret;
}
}
if (ret == EXIT_SUCCESS)
{
test_ret = test_cpp_node_wrapping_destructor();
if (test_ret != EXIT_SUCCESS)
{
ret = test_ret;
}
}
if (ret == EXIT_SUCCESS)
{
test_ret = test_cpp_node_copy();
if (test_ret != EXIT_SUCCESS)
{
ret = test_ret;
}
}
return ret;
}