diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0a0805d..9e5f874 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -18,6 +18,10 @@ include(Catch) add_executable(neo_test neo_test.cpp) include(string/string.cmake) +target_sources(neo_test PRIVATE + nref.cpp +) + target_link_libraries(neo_test PRIVATE neo Catch2::Catch2) catch_discover_tests(neo_test) diff --git a/test/nref.cpp b/test/nref.cpp new file mode 100644 index 0000000..4a9a468 --- /dev/null +++ b/test/nref.cpp @@ -0,0 +1,80 @@ +/** See the end of this file for copyright and license terms. */ + +#include +#include + +#include + +struct nref_test { + NREF_FIELD; + std::function cb; + nref_test(std::function cb) + { + this->cb = cb; + } +}; + +void test_destroy(struct nref_test *ptr) +{ + ptr->cb(); + delete ptr; +} + +SCENARIO( "nref: call destroy callback when count reaches 0", "[src/nref.c]" ) +{ + GIVEN( "structure is initialized" ) + { + bool called = false; + struct nref_test *instance = new nref_test([&called]() { + called = true; + }); + nref_init(instance, test_destroy); + + REQUIRE( !called ); + + WHEN( "nget is called" ) + { + nget(instance); + + THEN( "destroy is not called" ) + { + REQUIRE( !called ); + } + } + + WHEN( "nput is called" ) + { + nput(instance); + + THEN( "destroy is called" ) + { + REQUIRE( instance == nil ); + } + } + + WHEN( "nget is called, then nput is called twice" ) + { + nget(instance); + nput(instance); + nput(instance); + + THEN( "destroy is called" ) + { + REQUIRE( called ); + } + } + } +} + +/* + * This file is part of libneo. + * Copyright (c) 2021 Fefie . + * + * libneo is non-violent software: you may only use, redistribute, + * and/or modify it under the terms of the CNPLv6+ as found in + * the LICENSE file in the source code root directory or at + * . + * + * libneo comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */