From 030ca48d2f7d09d9ed36a15cc1b3145bafaea0d1 Mon Sep 17 00:00:00 2001 From: fef Date: Fri, 16 Jul 2021 22:39:37 +0200 Subject: [PATCH] nref: add tests --- test/CMakeLists.txt | 4 +++ test/nref.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 test/nref.cpp 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. + */