From f2fb8311d373cfd498dfbebc539982b8ad174615 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov <0x7fffff@guattari.ru> Date: Sat, 6 Aug 2022 16:32:59 +0400 Subject: input buffer, dynamic array --- source/test/unittests/CMakeLists.txt | 3 +- source/test/unittests/array_ref.test.c | 42 +++++++ source/test/unittests/async_function.test.c | 2 +- source/test/unittests/dynamic_array.test.c | 174 ++++++++++++++++++++++++++++ source/test/unittests/input_buffer.test.c | 20 ++++ source/test/unittests/input_stream.test.c | 23 ++++ 6 files changed, 262 insertions(+), 2 deletions(-) create mode 100644 source/test/unittests/array_ref.test.c create mode 100644 source/test/unittests/dynamic_array.test.c create mode 100644 source/test/unittests/input_buffer.test.c create mode 100644 source/test/unittests/input_stream.test.c (limited to 'source/test') diff --git a/source/test/unittests/CMakeLists.txt b/source/test/unittests/CMakeLists.txt index d5cc573..ff99c9c 100644 --- a/source/test/unittests/CMakeLists.txt +++ b/source/test/unittests/CMakeLists.txt @@ -1,4 +1,5 @@ target_sources( ${KIT_TEST_SUITE} PRIVATE - async_function.test.c main.test.c) + async_function.test.c main.test.c array_ref.test.c + input_stream.test.c input_buffer.test.c dynamic_array.test.c) diff --git a/source/test/unittests/array_ref.test.c b/source/test/unittests/array_ref.test.c new file mode 100644 index 0000000..d8cbcec --- /dev/null +++ b/source/test/unittests/array_ref.test.c @@ -0,0 +1,42 @@ +#include "../../kit/array_ref.h" + +#define KIT_TEST_FILE array_ref +#include "../../kit_test/test.h" + +TEST("array ref equal") { + int foo[] = { 1, 2, 3, 4, 5, 6, 7 }; + int bar[] = { 3, 4, 5 }; + + AR(foo_ref, int) = { .size = 3, .values = foo + 2 }; + AR(bar_ref, int) = { .size = 3, .values = bar }; + + REQUIRE(AR_EQUAL(foo_ref, bar_ref)); +} + +static int compare(int const *left, int const *right) { + return *left - *right; +} + +TEST("array ref compare") { + int foo[] = { 1, 2, 3, 5 }; + int bar[] = { 1, 2, 4, 5 }; + + AR(foo_ref, int) = { .size = 3, .values = foo }; + AR(bar_ref, int) = { .size = 3, .values = bar }; + + REQUIRE(AR_COMPARE(foo_ref, bar_ref, compare) < 0); + REQUIRE(AR_COMPARE(bar_ref, foo_ref, compare) > 0); + REQUIRE(AR_COMPARE(foo_ref, foo_ref, compare) == 0); +} + +TEST("array ref different element sizes") { + int foo[] = { 1, 2, 3 }; + char bar[] = { 1, 2, 3 }; + + AR(foo_ref, int) = { .size = 3, .values = foo }; + AR(bar_ref, char) = { .size = 3, .values = bar }; + + REQUIRE(!AR_EQUAL(foo_ref, bar_ref)); + REQUIRE(AR_COMPARE(foo_ref, bar_ref, compare) > 0); + REQUIRE(AR_COMPARE(bar_ref, foo_ref, compare) < 0); +} diff --git a/source/test/unittests/async_function.test.c b/source/test/unittests/async_function.test.c index 1f50445..4fe2c60 100644 --- a/source/test/unittests/async_function.test.c +++ b/source/test/unittests/async_function.test.c @@ -1,6 +1,6 @@ #include "../../kit/async_function.h" -#define KIT_TEST_FILE async_function_test +#define KIT_TEST_FILE async_function #include "../../kit_test/test.h" CORO(int, test_foo) { diff --git a/source/test/unittests/dynamic_array.test.c b/source/test/unittests/dynamic_array.test.c new file mode 100644 index 0000000..fad8d6d --- /dev/null +++ b/source/test/unittests/dynamic_array.test.c @@ -0,0 +1,174 @@ +#include "../../kit/dynamic_array.h" + +#define KIT_TEST_FILE dynamic_array +#include "../../kit_test/test.h" + +TEST("dynamic array empty") { + DA_CREATE(v, char, 0); + + REQUIRE(v.size == 0); + + DA_DESTROY(v); +} + +TEST("dynamic array resize") { + DA_CREATE(v, char, 0); + DA_RESIZE(v, 10); + + REQUIRE(v.size == 10); + + DA_DESTROY(v); +} + +TEST("dynamic array grow") { + DA_CREATE(v, char, 2); + DA_RESIZE(v, 10); + + REQUIRE(v.size == 10); + + DA_DESTROY(v); +} + +TEST("dynamic array diminish") { + DA_CREATE(v, char, 10); + DA_RESIZE(v, 9); + + REQUIRE(v.size == 9); + + DA_DESTROY(v); +} + +TEST("dynamic array of chars") { + DA_CREATE(v, char, 100); + + REQUIRE(v.size == 100); + REQUIRE(v.capacity >= 100); + REQUIRE(v.values != NULL); + + DA_DESTROY(v); +} + +TEST("dynamic array push") { + DA_CREATE(v, char, 0); + DA_APPEND(v, 'x'); + + REQUIRE(v.size == 1); + REQUIRE(v.values[0] == 'x'); + + DA_DESTROY(v); +} + +TEST("dynamic array insert front") { + DA_CREATE(v, char, 3); + + v.values[0] = 'a'; + v.values[1] = 'b'; + v.values[2] = 'c'; + + DA_INSERT(v, 0, 'x'); + + REQUIRE(v.size == 4); + REQUIRE(v.values[0] == 'x'); + REQUIRE(v.values[1] == 'a'); + REQUIRE(v.values[2] == 'b'); + REQUIRE(v.values[3] == 'c'); + + DA_DESTROY(v); +} + +TEST("dynamic array insert back") { + DA_CREATE(v, char, 3); + + v.values[0] = 'a'; + v.values[1] = 'b'; + v.values[2] = 'c'; + + DA_INSERT(v, 3, 'x'); + + REQUIRE(v.size == 4); + REQUIRE(v.values[0] == 'a'); + REQUIRE(v.values[1] == 'b'); + REQUIRE(v.values[2] == 'c'); + REQUIRE(v.values[3] == 'x'); + + DA_DESTROY(v); +} + +TEST("dynamic array insert middle") { + DA_CREATE(v, char, 3); + + v.values[0] = 'a'; + v.values[1] = 'b'; + v.values[2] = 'c'; + + DA_INSERT(v, 2, 'x'); + + REQUIRE(v.size == 4); + REQUIRE(v.values[0] == 'a'); + REQUIRE(v.values[1] == 'b'); + REQUIRE(v.values[2] == 'x'); + REQUIRE(v.values[3] == 'c'); + + DA_DESTROY(v); +} + +TEST("dynamic array erase front") { + DA_CREATE(v, char, 3); + + v.values[0] = 'a'; + v.values[1] = 'b'; + v.values[2] = 'c'; + + DA_ERASE(v, 0); + + REQUIRE(v.size == 2); + REQUIRE(v.values[0] == 'b'); + REQUIRE(v.values[1] == 'c'); + + DA_DESTROY(v); +} + +TEST("dynamic array erase back") { + DA_CREATE(v, char, 3); + + v.values[0] = 'a'; + v.values[1] = 'b'; + v.values[2] = 'c'; + + DA_ERASE(v, 2); + + REQUIRE(v.size == 2); + REQUIRE(v.values[0] == 'a'); + REQUIRE(v.values[1] == 'b'); + + DA_DESTROY(v); +} + +TEST("dynamic array erase middle") { + DA_CREATE(v, char, 3); + + v.values[0] = 'a'; + v.values[1] = 'b'; + v.values[2] = 'c'; + + DA_ERASE(v, 1); + + REQUIRE(v.size == 2); + REQUIRE(v.values[0] == 'a'); + REQUIRE(v.values[1] == 'c'); + + DA_DESTROY(v); +} + +TEST("dynamic array of ints") { + DA_CREATE(v, int, 10); + DA_RESIZE(v, 5); + v.values[4] = 42; + DA_APPEND(v, 43); + + REQUIRE(v.size == 6); + REQUIRE(v.values[4] == 42); + REQUIRE(v.values[5] == 43); + + DA_DESTROY(v); +} diff --git a/source/test/unittests/input_buffer.test.c b/source/test/unittests/input_buffer.test.c new file mode 100644 index 0000000..c7edb8f --- /dev/null +++ b/source/test/unittests/input_buffer.test.c @@ -0,0 +1,20 @@ +#include "../../kit/input_buffer.h" + +#define KIT_TEST_FILE input_buffer +#include "../../kit_test/test.h" + +TEST("input buffer") { + cstr text = { .size = 3, .values = "foo" }; + struct is_handle in = IS_WRAP_STRING(text); + struct ib_handle first = IB_WRAP(in); + + struct ib_handle second = ib_read(first, 3); + + REQUIRE(!second.error); + REQUIRE(second.data.size == 3); + REQUIRE(AR_EQUAL(text, second.data)); + + ib_destroy(second); + ib_destroy(first); + is_destroy(in); +} diff --git a/source/test/unittests/input_stream.test.c b/source/test/unittests/input_stream.test.c new file mode 100644 index 0000000..9fef47a --- /dev/null +++ b/source/test/unittests/input_stream.test.c @@ -0,0 +1,23 @@ +#include "../../kit/input_stream.h" + +#define KIT_TEST_FILE input_stream +#include "../../kit_test/test.h" + +TEST("input stream wrap string") { + char foo[] = "test"; + char bar[] = "test"; + + cstr foo_ref = { .size = sizeof(foo) - 1, .values = foo }; + cstr bar_ref = { .size = sizeof(bar) - 1, .values = bar }; + + struct is_handle in = IS_WRAP_STRING(foo_ref); + + char buf[4]; + out_str buf_ref = { .size = sizeof(buf), .values = buf }; + + REQUIRE(IS_READ(in, buf_ref) == buf_ref.size); + REQUIRE(AR_EQUAL(foo_ref, bar_ref)); + REQUIRE(AR_EQUAL(buf_ref, bar_ref)); + + is_destroy(in); +} -- cgit v1.2.3