nref: add tests
This commit is contained in:
parent
30954cd078
commit
030ca48d2f
2 changed files with 84 additions and 0 deletions
|
@ -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)
|
||||
|
|
80
test/nref.cpp
Normal file
80
test/nref.cpp
Normal file
|
@ -0,0 +1,80 @@
|
|||
/** See the end of this file for copyright and license terms. */
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
#include <errno.h>
|
||||
|
||||
#include <neo.h>
|
||||
|
||||
struct nref_test {
|
||||
NREF_FIELD;
|
||||
std::function<void(void)> cb;
|
||||
nref_test(std::function<void(void)> 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 <owo@fef.moe>.
|
||||
*
|
||||
* 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
|
||||
* <https://git.pixie.town/thufie/CNPL>.
|
||||
*
|
||||
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPLv6+ for details.
|
||||
*/
|
Loading…
Reference in a new issue