Compare commits

...

2 Commits

Author SHA1 Message Date
falsycat 520e745a97 add refcnt util 2023-11-29 08:36:05 +09:00
falsycat cb9e6439c5 fix an issue that multiple tests are not run 2023-11-29 08:35:59 +09:00
4 changed files with 55 additions and 1 deletions

View File

@ -1,6 +1,6 @@
#!/bin/bash
tests=$(cat $@ | sed -e '/^NF7_TEST(/!d; s|^NF7_TEST(\([^)]*\)).*$|\1|' | xargs echo)
tests=$(cat ${@//;/ } | sed -e '/^NF7_TEST(/!d; s|^NF7_TEST(\([^)]*\)).*$|\1|' | xargs echo)
echo "#include \"test/common.h\""
echo "#include \"test/run.h\""

View File

@ -6,10 +6,12 @@ target_sources(nf7util
array.h
log.h
malloc.h
refcnt.h
signal.h
)
target_tests(nf7util
array.test.c
refcnt.test.c
)
target_link_libraries(nf7util
PRIVATE

27
util/refcnt.h Normal file
View File

@ -0,0 +1,27 @@
// No copyright
#pragma once
#include <assert.h>
#include "util/malloc.h"
#define NF7_REFCNT_DECL(ATTR, T) \
ATTR void T##_ref(struct T*); \
ATTR bool T##_unref(struct T*); \
static_assert(true)
#define NF7_REFCNT_IMPL(ATTR, T, DELETER) \
ATTR void T##_ref(struct T* this) { \
++this->refcnt; \
} \
ATTR bool T##_unref(struct T* this) { \
assert(0 < this->refcnt); \
if (0 == --this->refcnt) { \
{DELETER}; \
return true; \
} \
return false; \
} \
static_assert(true)

25
util/refcnt.test.c Normal file
View File

@ -0,0 +1,25 @@
// No copyright
#include "util/refcnt.h"
#include <stdint.h>
#include "test/common.h"
struct mystruct {
bool deleted;
uint64_t refcnt;
};
NF7_REFCNT_IMPL(static inline, mystruct, {this->deleted = true;});
NF7_TEST(nf7_util_refcnt_test_delete) {
struct mystruct sut = {0};
mystruct_ref(&sut);
mystruct_ref(&sut);
return
nf7_test_expect(!mystruct_unref(&sut)) &&
nf7_test_expect(!sut.deleted) &&
nf7_test_expect(mystruct_unref(&sut)) &&
nf7_test_expect(sut.deleted);
}