summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2024-01-12 16:15:10 +0100
committerMitya Selivanov <automainint@guattari.tech>2024-01-12 16:15:10 +0100
commitfdf921c0adff5317e426e798f8a716ca97046383 (patch)
treed0e27a2e798fb078ff5f9cbb5af409b9c5a21061 /source
parentd36df5359dcb371d775a9ef10626cd0f7f996002 (diff)
downloadkit-fdf921c0adff5317e426e798f8a716ca97046383.zip
Refactor; defer with fblocks
Diffstat (limited to 'source')
-rw-r--r--source/kit/defer.h39
-rw-r--r--source/kit/test.h (renamed from source/kit/kit_test.h)14
-rw-r--r--source/tests/_exe.c1
-rw-r--r--source/tests/array_ref.test.c2
-rw-r--r--source/tests/async_function.test.c2
-rw-r--r--source/tests/atomic.test.c2
-rw-r--r--source/tests/bench.test.c2
-rw-r--r--source/tests/bigint.test.c2
-rw-r--r--source/tests/condition_variable.test.c2
-rw-r--r--source/tests/defer.test.c37
-rw-r--r--source/tests/duration.test.c2
-rw-r--r--source/tests/dynamic_array.test.c2
-rw-r--r--source/tests/file.test.c2
-rw-r--r--source/tests/http1.test.c2
-rw-r--r--source/tests/input_buffer.test.c2
-rw-r--r--source/tests/input_stream.test.c2
-rw-r--r--source/tests/lower_bound.test.c2
-rw-r--r--source/tests/main.test.c2
-rw-r--r--source/tests/mersenne_twister_64.test.c2
-rw-r--r--source/tests/move_back.test.c2
-rw-r--r--source/tests/mutex.test.c2
-rw-r--r--source/tests/secure_random.test.c2
-rw-r--r--source/tests/sha256.test.c2
-rw-r--r--source/tests/string_ref.test.c2
-rw-r--r--source/tests/test_cpp.cpp2
-rw-r--r--source/tests/test_signals.cpp2
-rw-r--r--source/tests/test_too_many_assertions.c2
-rw-r--r--source/tests/test_too_many_tests.c2
-rw-r--r--source/tests/thread.test.c2
-rw-r--r--source/tests/xml.test.c2
30 files changed, 110 insertions, 33 deletions
diff --git a/source/kit/defer.h b/source/kit/defer.h
new file mode 100644
index 0000000..bb78b69
--- /dev/null
+++ b/source/kit/defer.h
@@ -0,0 +1,39 @@
+// Requres GCC or Clang with `-fblocks` option available.
+//
+
+#ifndef KIT_DEFER_H
+#define KIT_DEFER_H
+
+#if !defined(__clang__) && !defined(__APPLE__)
+# error C blocks support required
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if defined(__GNUC__) || defined(__clang__)
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wunused-function"
+# pragma GCC diagnostic ignored "-Wunknown-pragmas"
+#endif
+
+static void kit_defer_cleanup_(void (^*b)(void)) { (*b)(); }
+
+#define kit_defer_merge_(a,b) a##b
+#define kit_defer_varname_(a) \
+ kit_defer_merge_(kit_defer_scopevar_,a)
+#define defer \
+ __attribute__((unused, cleanup(kit_defer_cleanup_))) \
+ void (^kit_defer_varname_(__COUNTER__))(void) = ^
+#define defer_ref __block
+
+#if defined(__GNUC__) || defined(__clang__)
+# pragma GCC diagnostic pop
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/source/kit/kit_test.h b/source/kit/test.h
index 176d40c..b6be132 100644
--- a/source/kit/kit_test.h
+++ b/source/kit/test.h
@@ -1,6 +1,6 @@
// ================================================================
//
-// kit_test.h
+// test.h
// https://guattari.tech/git/kit
//
// Header-only unit-testing and microbenchmarks framework for C.
@@ -26,16 +26,16 @@
// Usage example
//
// // foo.test.c
-// #define KIT_TEST_FILE foo // This is required if you want to use
-// // multiple test files.
-// #include "kit_test.h"
+// #define KIT_TEST_FILE foo // This is required if you want to
+// // use multiple test files.
+// #include "test.h"
// TEST("foo") {
// REQUIRE(1);
// REQUIRE_EQ(2 + 2, 4);
// }
//
// // main.c
-// #include "kit_test.h"
+// #include "test.h"
// int main(int argc, char **argv) {
// return run_tests(argc, argv);
// }
@@ -263,8 +263,8 @@ void kit_bench_register(char const *name, char const *file,
kit_bench_end_(kit_bench_index_); \
}
-/* FIXME
- */
+// FIXME
+//
#define KIT_DO_NOT_OPTIMIZE(x) \
do { \
volatile void *bench_ptr_ = &(x); \
diff --git a/source/tests/_exe.c b/source/tests/_exe.c
index f14e620..99a34b8 100644
--- a/source/tests/_exe.c
+++ b/source/tests/_exe.c
@@ -18,6 +18,7 @@
#include "string_ref.test.c"
#include "duration.test.c"
#include "thread.test.c"
+#include "defer.test.c"
#include "xml.test.c"
#include "http1.test.c"
#include "bench.test.c"
diff --git a/source/tests/array_ref.test.c b/source/tests/array_ref.test.c
index 2037bed..921e9fd 100644
--- a/source/tests/array_ref.test.c
+++ b/source/tests/array_ref.test.c
@@ -1,7 +1,7 @@
#include "../kit/array_ref.h"
#define KIT_TEST_FILE array_ref
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
TEST("array ref wrap") {
int foo[] = { 1, 2, 3 };
diff --git a/source/tests/async_function.test.c b/source/tests/async_function.test.c
index 9f90474..34cc7d1 100644
--- a/source/tests/async_function.test.c
+++ b/source/tests/async_function.test.c
@@ -1,7 +1,7 @@
#include "../kit/async_function.h"
#define KIT_TEST_FILE async_function
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
AF_STATE(int, test_foo, );
static AF_DECL(test_foo);
diff --git a/source/tests/atomic.test.c b/source/tests/atomic.test.c
index 061f5d6..d5c172b 100644
--- a/source/tests/atomic.test.c
+++ b/source/tests/atomic.test.c
@@ -2,7 +2,7 @@
#include "../kit/threads.h"
#define KIT_TEST_FILE atomic
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
TEST("atomic store and load") {
ATOMIC(int) value;
diff --git a/source/tests/bench.test.c b/source/tests/bench.test.c
index f5d28aa..011817d 100644
--- a/source/tests/bench.test.c
+++ b/source/tests/bench.test.c
@@ -1,5 +1,5 @@
#define KIT_TEST_FILE bench
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
struct test_foo_ {
double f;
diff --git a/source/tests/bigint.test.c b/source/tests/bigint.test.c
index 3e55c39..d7375f5 100644
--- a/source/tests/bigint.test.c
+++ b/source/tests/bigint.test.c
@@ -2,7 +2,7 @@
#include "../kit/bigint.h"
#define KIT_TEST_FILE bigint
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
TEST("bigint size check") {
REQUIRE_EQ(sizeof(u8), 1);
diff --git a/source/tests/condition_variable.test.c b/source/tests/condition_variable.test.c
index 9deb9ae..83bf2b3 100644
--- a/source/tests/condition_variable.test.c
+++ b/source/tests/condition_variable.test.c
@@ -1,7 +1,7 @@
#include "../kit/threads.h"
#define KIT_TEST_FILE condition_variable
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
typedef struct {
mtx_t m;
diff --git a/source/tests/defer.test.c b/source/tests/defer.test.c
new file mode 100644
index 0000000..0eb8557
--- /dev/null
+++ b/source/tests/defer.test.c
@@ -0,0 +1,37 @@
+#if defined(__clang__) || defined(__APPLE__)
+# include "../kit/defer.h"
+
+# define KIT_TEST_FILE defer_block
+# include "../kit/test.h"
+
+TEST("defer") {
+ int defer_ref x = 1;
+
+ {
+ defer {
+ x = 2;
+ };
+
+ x = 3;
+ }
+
+ REQUIRE_EQ(x, 2);
+}
+
+TEST("defer capture") {
+ int defer_ref x = 1;
+
+ {
+ defer {
+ if (x == 3)
+ x = 2;
+ };
+
+ x = 3;
+ }
+
+ REQUIRE_EQ(x, 2);
+}
+
+# undef KIT_TEST_FILE
+#endif
diff --git a/source/tests/duration.test.c b/source/tests/duration.test.c
index 1bdb68b..32865c7 100644
--- a/source/tests/duration.test.c
+++ b/source/tests/duration.test.c
@@ -1,5 +1,5 @@
#define KIT_TEST_FILE duration
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
#if defined(_WIN32) && !defined(__CYGWIN__)
__declspec(dllimport) void __stdcall Sleep(unsigned long timeout);
diff --git a/source/tests/dynamic_array.test.c b/source/tests/dynamic_array.test.c
index a1c0ebc..0d07233 100644
--- a/source/tests/dynamic_array.test.c
+++ b/source/tests/dynamic_array.test.c
@@ -1,7 +1,7 @@
#include "../kit/dynamic_array.h"
#define KIT_TEST_FILE dynamic_array
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
TEST("dynamic array empty") {
DA_CREATE(v, char, 0);
diff --git a/source/tests/file.test.c b/source/tests/file.test.c
index 646d0ef..8edfcca 100644
--- a/source/tests/file.test.c
+++ b/source/tests/file.test.c
@@ -2,7 +2,7 @@
#include "../kit/string_ref.h"
#define KIT_TEST_FILE file
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
TEST("path cache") {
str_builder_t user = path_user(NULL);
diff --git a/source/tests/http1.test.c b/source/tests/http1.test.c
index 09da5f4..20c1ea7 100644
--- a/source/tests/http1.test.c
+++ b/source/tests/http1.test.c
@@ -1,7 +1,7 @@
#include "../kit/http1.h"
#define KIT_TEST_FILE http1
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
TEST("http1") {
// TODO
diff --git a/source/tests/input_buffer.test.c b/source/tests/input_buffer.test.c
index 89717d1..b8dc19b 100644
--- a/source/tests/input_buffer.test.c
+++ b/source/tests/input_buffer.test.c
@@ -1,7 +1,7 @@
#include "../kit/input_buffer.h"
#define KIT_TEST_FILE input_buffer
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
TEST("input buffer read once") {
str_t text = { .size = 3, .values = "foo" };
diff --git a/source/tests/input_stream.test.c b/source/tests/input_stream.test.c
index 72e43d5..25ef721 100644
--- a/source/tests/input_stream.test.c
+++ b/source/tests/input_stream.test.c
@@ -2,7 +2,7 @@
#include "../kit/file.h"
#define KIT_TEST_FILE input_stream
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
TEST("input stream wrap string") {
char foo[] = "test";
diff --git a/source/tests/lower_bound.test.c b/source/tests/lower_bound.test.c
index d709446..ae7ed72 100644
--- a/source/tests/lower_bound.test.c
+++ b/source/tests/lower_bound.test.c
@@ -2,7 +2,7 @@
#include "../kit/array_ref.h"
#define KIT_TEST_FILE lower_bound
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
static int kit_less_int(int left, int right) {
return left < right;
diff --git a/source/tests/main.test.c b/source/tests/main.test.c
index 6d439b6..d6fabc3 100644
--- a/source/tests/main.test.c
+++ b/source/tests/main.test.c
@@ -1,5 +1,5 @@
#define KIT_TEST_IMPLEMENTATION
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
int main(int argc, char **argv) {
int status = run_tests(argc, argv);
diff --git a/source/tests/mersenne_twister_64.test.c b/source/tests/mersenne_twister_64.test.c
index 4d0d97d..df9e508 100644
--- a/source/tests/mersenne_twister_64.test.c
+++ b/source/tests/mersenne_twister_64.test.c
@@ -2,7 +2,7 @@
#include "../kit/secure_random.h"
#define KIT_TEST_FILE mersenne_twister_64
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
enum { MT64_TEST_SIZE = 1000 };
diff --git a/source/tests/move_back.test.c b/source/tests/move_back.test.c
index 626850a..a97a8b9 100644
--- a/source/tests/move_back.test.c
+++ b/source/tests/move_back.test.c
@@ -1,7 +1,7 @@
#include "../kit/move_back.h"
#define KIT_TEST_FILE move_back
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
static int is_equal(int x, int y) {
return x == y;
diff --git a/source/tests/mutex.test.c b/source/tests/mutex.test.c
index 578e48b..61c1caa 100644
--- a/source/tests/mutex.test.c
+++ b/source/tests/mutex.test.c
@@ -1,7 +1,7 @@
#include "../kit/threads.h"
#define KIT_TEST_FILE mutex
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
enum { SLEEP = 400000000, TICK_COUNT = 200, THREAD_COUNT = 100 };
diff --git a/source/tests/secure_random.test.c b/source/tests/secure_random.test.c
index 62021b1..3452521 100644
--- a/source/tests/secure_random.test.c
+++ b/source/tests/secure_random.test.c
@@ -2,7 +2,7 @@
#include <string.h>
#define KIT_TEST_FILE secure_random
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
TEST("secure random") {
int v[20];
diff --git a/source/tests/sha256.test.c b/source/tests/sha256.test.c
index cf67c03..dff3ca6 100644
--- a/source/tests/sha256.test.c
+++ b/source/tests/sha256.test.c
@@ -2,7 +2,7 @@
#include "../kit/array_ref.h"
#define KIT_TEST_FILE sha256_64
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
#include <string.h>
diff --git a/source/tests/string_ref.test.c b/source/tests/string_ref.test.c
index a5863df..8ce9368 100644
--- a/source/tests/string_ref.test.c
+++ b/source/tests/string_ref.test.c
@@ -1,7 +1,7 @@
#include "../kit/string_ref.h"
#define KIT_TEST_FILE string_ref
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
TEST("static string wrap") {
str_t ref = SZ("foo bar");
diff --git a/source/tests/test_cpp.cpp b/source/tests/test_cpp.cpp
index 3e88ede..d1eab09 100644
--- a/source/tests/test_cpp.cpp
+++ b/source/tests/test_cpp.cpp
@@ -1,5 +1,5 @@
#define KIT_TEST_IMPLEMENTATION
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
TEST("foo") {
REQUIRE(20 + 22 == 42);
diff --git a/source/tests/test_signals.cpp b/source/tests/test_signals.cpp
index 2a9c009..e0298c4 100644
--- a/source/tests/test_signals.cpp
+++ b/source/tests/test_signals.cpp
@@ -1,5 +1,5 @@
#define KIT_TEST_IMPLEMENTATION
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
#include <cstdlib>
#include <stdexcept>
diff --git a/source/tests/test_too_many_assertions.c b/source/tests/test_too_many_assertions.c
index ffff093..f54bc36 100644
--- a/source/tests/test_too_many_assertions.c
+++ b/source/tests/test_too_many_assertions.c
@@ -1,6 +1,6 @@
#define KIT_TEST_IMPLEMENTATION
#define KIT_TEST_ASSERTIONS_LIMIT 10
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
TEST("foo") {
int i;
diff --git a/source/tests/test_too_many_tests.c b/source/tests/test_too_many_tests.c
index b03a3b2..8d1aec0 100644
--- a/source/tests/test_too_many_tests.c
+++ b/source/tests/test_too_many_tests.c
@@ -1,6 +1,6 @@
#define KIT_TEST_IMPLEMENTATION
#define KIT_TESTS_SIZE_LIMIT 40
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
void bar(int index, kit_test_report_fn report) { }
diff --git a/source/tests/thread.test.c b/source/tests/thread.test.c
index 8f849cd..dca2fe0 100644
--- a/source/tests/thread.test.c
+++ b/source/tests/thread.test.c
@@ -1,7 +1,7 @@
#include "../kit/threads.h"
#define KIT_TEST_FILE thread
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
static int test_nothing(void *_) {
return 0;
diff --git a/source/tests/xml.test.c b/source/tests/xml.test.c
index 2a0b114..a3e931d 100644
--- a/source/tests/xml.test.c
+++ b/source/tests/xml.test.c
@@ -1,7 +1,7 @@
#include "../kit/xml.h"
#define KIT_TEST_FILE xml
-#include "../kit/kit_test.h"
+#include "../kit/test.h"
TEST("xml parse tag") {
is_handle_t is = IS_WRAP_STRING(SZ("<foo> </foo>"));