summaryrefslogtreecommitdiff
path: root/source/tests/input_buffer.test.c
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2024-01-14 08:28:13 +0100
committerMitya Selivanov <automainint@guattari.tech>2024-01-14 08:28:13 +0100
commit6337eb74d3d9028640fdffa0a0e39735a4e3ba93 (patch)
tree06a271fe04731d7ec0c546a82d3f4ec765117f3a /source/tests/input_buffer.test.c
parent84908dbff43e195b9f106a98feba162f814af2c5 (diff)
downloadkit-6337eb74d3d9028640fdffa0a0e39735a4e3ba93.zip
Short type names; Rework input buffer
Diffstat (limited to 'source/tests/input_buffer.test.c')
-rw-r--r--source/tests/input_buffer.test.c204
1 files changed, 141 insertions, 63 deletions
diff --git a/source/tests/input_buffer.test.c b/source/tests/input_buffer.test.c
index b8dc19b..1ec1ad4 100644
--- a/source/tests/input_buffer.test.c
+++ b/source/tests/input_buffer.test.c
@@ -4,59 +4,54 @@
#include "../kit/test.h"
TEST("input buffer read once") {
- str_t text = { .size = 3, .values = "foo" };
- is_handle_t in = IS_WRAP_STRING(text);
- ib_t first = IB_WRAP(in);
+ str_t text = { .size = 3, .values = "foo" };
+ is_handle_t in = IS_WRAP_STRING(text);
+ input_buffer_t buf = ib_wrap(in, NULL);
- ib_t second = ib_read(first, 3);
+ ib_token_t tok = ib_read(ib_token(&buf), 3);
- REQUIRE(second.status == KIT_OK);
- REQUIRE(second.data.size == 3);
- REQUIRE(AR_EQUAL(text, second.data));
+ REQUIRE_EQ(tok.status, KIT_OK);
+ REQUIRE_EQ(tok.size, 3);
+ REQUIRE(AR_EQUAL(text, ib_str(tok)));
- ib_destroy(second);
- ib_destroy(first);
+ ib_destroy(&buf);
is_destroy(in);
}
TEST("input buffer read again") {
- str_t text = { .size = 6, .values = "foobar" };
- str_t foo = { .size = 3, .values = "foo" };
- is_handle_t in = IS_WRAP_STRING(text);
- ib_t first = IB_WRAP(in);
+ str_t text = { .size = 6, .values = "foobar" };
+ str_t foo = { .size = 3, .values = "foo" };
+ is_handle_t in = IS_WRAP_STRING(text);
+ input_buffer_t buf = ib_wrap(in, NULL);
- ib_t second = ib_read(first, 3);
- ib_t third = ib_read(first, 3);
+ ib_token_t first = ib_read(ib_token(&buf), 3);
+ ib_token_t second = ib_read(ib_token(&buf), 3);
- REQUIRE(AR_EQUAL(foo, second.data));
- REQUIRE(AR_EQUAL(foo, third.data));
+ REQUIRE(AR_EQUAL(foo, ib_str(first)));
+ REQUIRE(AR_EQUAL(foo, ib_str(second)));
- ib_destroy(third);
- ib_destroy(second);
- ib_destroy(first);
+ ib_destroy(&buf);
is_destroy(in);
}
TEST("input buffer read twice") {
- str_t text = { .size = 6, .values = "foobar" };
- str_t foo = { .size = 3, .values = "foo" };
- str_t bar = { .size = 3, .values = "bar" };
- is_handle_t in = IS_WRAP_STRING(text);
- ib_t first = IB_WRAP(in);
+ str_t text = { .size = 6, .values = "foobar" };
+ str_t foo = { .size = 3, .values = "foo" };
+ str_t bar = { .size = 3, .values = "bar" };
+ is_handle_t in = IS_WRAP_STRING(text);
+ input_buffer_t buf = ib_wrap(in, NULL);
- ib_t second = ib_read(first, 3);
- ib_t third = ib_read(second, 3);
+ ib_token_t first = ib_read(ib_token(&buf), 3);
+ ib_token_t second = ib_read(first, 3);
- REQUIRE(AR_EQUAL(foo, second.data));
- REQUIRE(AR_EQUAL(bar, third.data));
+ REQUIRE(AR_EQUAL(foo, ib_str(first)));
+ REQUIRE(AR_EQUAL(bar, ib_str(second)));
- ib_destroy(third);
- ib_destroy(second);
- ib_destroy(first);
+ ib_destroy(&buf);
is_destroy(in);
}
-static i8 is_integer_(str_t const data, void *_) {
+static b8 is_integer_(str_t const data, void *_) {
for (ptrdiff_t i = 0; i < data.size; i++)
if (data.values[i] < '0' || data.values[i] > '9')
return 0;
@@ -64,43 +59,126 @@ static i8 is_integer_(str_t const data, void *_) {
}
TEST("input buffer read integer once") {
- str_t text = { .size = 9, .values = "31415 foo" };
- str_t num = { .size = 5, .values = "31415" };
- is_handle_t in = IS_WRAP_STRING(text);
- ib_t first = IB_WRAP(in);
+ str_t text = { .size = 9, .values = "31415 foo" };
+ str_t num = { .size = 5, .values = "31415" };
+ is_handle_t in = IS_WRAP_STRING(text);
+ input_buffer_t buf = ib_wrap(in, NULL);
- ib_t second = ib_while(first, is_integer_, NULL);
+ ib_token_t tok = ib_while(ib_token(&buf), is_integer_, NULL);
- REQUIRE(second.status == KIT_OK);
- REQUIRE(second.data.size == 5);
- REQUIRE(AR_EQUAL(num, second.data));
+ REQUIRE_EQ(tok.status, KIT_OK);
+ REQUIRE_EQ(tok.size, 5);
+ REQUIRE(AR_EQUAL(num, ib_str(tok)));
- ib_destroy(second);
- ib_destroy(first);
+ ib_destroy(&buf);
is_destroy(in);
}
TEST("input buffer read integer twice") {
- str_t text = { .size = 6, .values = "314 15" };
- str_t num_0 = { .size = 3, .values = "314" };
- str_t num_1 = { .size = 2, .values = "15" };
- is_handle_t in = IS_WRAP_STRING(text);
- ib_t first = IB_WRAP(in);
-
- ib_t second = ib_while(first, is_integer_, NULL);
- ib_t third = ib_read(second, 1);
- ib_t fourth = ib_while(third, is_integer_, NULL);
-
- REQUIRE(fourth.status == KIT_OK);
- REQUIRE(second.data.size == 3);
- REQUIRE(fourth.data.size == 2);
- REQUIRE(AR_EQUAL(num_0, second.data));
- REQUIRE(AR_EQUAL(num_1, fourth.data));
-
- ib_destroy(first);
- ib_destroy(second);
- ib_destroy(third);
- ib_destroy(fourth);
+ str_t text = { .size = 6, .values = "314 15" };
+ str_t num_0 = { .size = 3, .values = "314" };
+ str_t num_1 = { .size = 2, .values = "15" };
+ is_handle_t in = IS_WRAP_STRING(text);
+ input_buffer_t buf = ib_wrap(in, NULL);
+
+ ib_token_t first = ib_while(ib_token(&buf), is_integer_, NULL);
+ ib_token_t second = ib_read(first, 1);
+ ib_token_t third = ib_while(second, is_integer_, NULL);
+
+ REQUIRE_EQ(third.status, KIT_OK);
+ REQUIRE_EQ(first.size, 3);
+ REQUIRE_EQ(third.size, 2);
+ REQUIRE(AR_EQUAL(num_0, ib_str(first)));
+ REQUIRE(AR_EQUAL(num_1, ib_str(third)));
+
+ ib_destroy(&buf);
+ is_destroy(in);
+}
+
+TEST("input buffer any") {
+ str_t text = SZ("01234bbdac");
+ str_t expect = SZ("01234");
+ is_handle_t in = IS_WRAP_STRING(text);
+ input_buffer_t buf = ib_wrap(in, NULL);
+
+ ib_token_t tok = ib_any(ib_token(&buf), SZ("01234"));
+
+ REQUIRE_EQ(tok.status, KIT_OK);
+ REQUIRE(AR_EQUAL(expect, ib_str(tok)));
+
+ ib_destroy(&buf);
+ is_destroy(in);
+}
+
+TEST("input buffer none") {
+ str_t text = SZ("01234bbdac");
+ str_t expect = SZ("01234");
+ is_handle_t in = IS_WRAP_STRING(text);
+ input_buffer_t buf = ib_wrap(in, NULL);
+
+ ib_token_t tok = ib_none(ib_token(&buf), SZ("abcd"));
+
+ REQUIRE_EQ(tok.status, KIT_OK);
+ REQUIRE(AR_EQUAL(expect, ib_str(tok)));
+
+ ib_destroy(&buf);
+ is_destroy(in);
+}
+
+TEST("input buffer until") {
+ str_t text = SZ("01234bbdac");
+ str_t expect = SZ("01234");
+ is_handle_t in = IS_WRAP_STRING(text);
+ input_buffer_t buf = ib_wrap(in, NULL);
+
+ ib_token_t tok = ib_none(ib_token(&buf), SZ("bbdac"));
+
+ REQUIRE_EQ(tok.status, KIT_OK);
+ REQUIRE(AR_EQUAL(expect, ib_str(tok)));
+
+ ib_destroy(&buf);
+ is_destroy(in);
+}
+
+TEST("input buffer exact success") {
+ str_t text = SZ("01234bbdac");
+ str_t expect = SZ("01234");
+ is_handle_t in = IS_WRAP_STRING(text);
+ input_buffer_t buf = ib_wrap(in, NULL);
+
+ ib_token_t tok = ib_exact(ib_token(&buf), SZ("01234"));
+
+ REQUIRE_EQ(tok.status, KIT_OK);
+ REQUIRE(AR_EQUAL(expect, ib_str(tok)));
+
+ ib_destroy(&buf);
+ is_destroy(in);
+}
+
+TEST("input buffer exact fail") {
+ str_t text = SZ("01234bbdac");
+ is_handle_t in = IS_WRAP_STRING(text);
+ input_buffer_t buf = ib_wrap(in, NULL);
+
+ ib_token_t tok = ib_exact(ib_token(&buf), SZ("bbdac"));
+
+ REQUIRE_EQ(tok.status, KIT_PARSING_FAILED);
+
+ ib_destroy(&buf);
+ is_destroy(in);
+}
+
+TEST("input buffer use after free") {
+ str_t text = SZ("foobarfoobar");
+ is_handle_t in = IS_WRAP_STRING(text);
+ input_buffer_t buf = ib_wrap(in, NULL);
+
+ ib_token_t first = ib_exact(ib_token(&buf), SZ("foobar"));
+ ib_token_t second = ib_exact(first, ib_str(first));
+
+ REQUIRE_EQ(second.status, KIT_OK);
+
+ ib_destroy(&buf);
is_destroy(in);
}