summaryrefslogtreecommitdiff
path: root/source/test/unittests
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2023-03-14 17:28:39 +0100
committerMitya Selivanov <automainint@guattari.tech>2023-03-14 17:28:39 +0100
commit4001560d9bc3d665fe767d06b3a7fb1727afe31c (patch)
tree79203645ab4db24cd9669f0df8a0607ba6b1783d /source/test/unittests
parent9255ee21ec2ea75da86bf892468e1a5520bff669 (diff)
downloadkit-4001560d9bc3d665fe767d06b3a7fb1727afe31c.zip
[input_buffer] Read while
Diffstat (limited to 'source/test/unittests')
-rw-r--r--source/test/unittests/input_buffer.test.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/source/test/unittests/input_buffer.test.c b/source/test/unittests/input_buffer.test.c
index f1b3738..c062052 100644
--- a/source/test/unittests/input_buffer.test.c
+++ b/source/test/unittests/input_buffer.test.c
@@ -55,3 +55,51 @@ TEST("input buffer read twice") {
ib_destroy(first);
is_destroy(in);
}
+
+static int is_integer_(str_t const data) {
+ for (ptrdiff_t i = 0; i < data.size; i++)
+ if (data.values[i] < '0' || data.values[i] > '9')
+ return 0;
+ return 1;
+}
+
+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_handle_t first = IB_WRAP(in);
+
+ ib_handle_t second = ib_read_while(first, is_integer_);
+
+ REQUIRE(second.status == KIT_OK);
+ REQUIRE(second.data.size == 5);
+ REQUIRE(AR_EQUAL(num, second.data));
+
+ ib_destroy(second);
+ ib_destroy(first);
+ 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_handle_t first = IB_WRAP(in);
+
+ ib_handle_t second = ib_read_while(first, is_integer_);
+ ib_handle_t third = ib_read(second, 1);
+ ib_handle_t fourth = ib_read_while(third, is_integer_);
+
+ 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);
+ is_destroy(in);
+}