aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2025-07-12 12:45:43 -0400
committerEugene Triguba <eugenetriguba@gmail.com>2025-07-12 12:45:43 -0400
commite9fd3702154962dcfceec73be88035fe75de7325 (patch)
tree3304885a6886d4ca2ca7c628fe91ed469927e9b8 /tests
parentRemove unnecessary memset for word buffer (diff)
downloadcli-e9fd3702154962dcfceec73be88035fe75de7325.zip
Add test for clear to linebuffer tests
Diffstat (limited to 'tests')
-rw-r--r--tests/test_linebuffer.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/test_linebuffer.c b/tests/test_linebuffer.c
index e039443..2c097f6 100644
--- a/tests/test_linebuffer.c
+++ b/tests/test_linebuffer.c
@@ -9,6 +9,12 @@
#include <stdlib.h>
#include <string.h>
+/**
+ * GIVEN a specific buffer capacity
+ * WHEN we attempt to create a LineBuffer
+ * THEN we expect it to be created successfully
+ * with the expected internal state
+ */
void test_linebuffer_create(void **state) {
const int buffer_capacity = 50;
LineBuffer *lb = linebuffer_create(buffer_capacity);
@@ -19,6 +25,12 @@ void test_linebuffer_create(void **state) {
linebuffer_destroy(lb);
}
+/**
+ * GIVEN a specific buffer capacity
+ * WHEN we to append words into the line buffer
+ * THEN we expect it to keep track of the buffer,
+ * buffer length, and number of words correctly.
+ */
void test_linebuffer_append_word(void **state) {
LineBuffer *lb = linebuffer_create(50);
assert_non_null(lb);
@@ -36,6 +48,35 @@ void test_linebuffer_append_word(void **state) {
linebuffer_destroy(lb);
}
+/**
+ * GIVEN a line buffer with some words in it
+ * WHEN we clear the buffer
+ * THEN we expect the buffer state variables
+ * to be reset
+ */
+void test_linebuffer_clear(void **state) {
+ LineBuffer *lb = linebuffer_create(50);
+ assert_non_null(lb);
+
+ linebuffer_append_word(lb, "hello");
+ assert_string_equal(lb->_buffer, "hello");
+ assert_int_equal(lb->_buffer_length, 5);
+ assert_int_equal(lb->_num_words, 1);
+
+ linebuffer_clear(lb);
+ assert_string_equal(lb->_buffer, "");
+ assert_int_equal(lb->_buffer_length, 0);
+ assert_int_equal(lb->_num_words, 0);
+
+ linebuffer_destroy(lb);
+}
+
+/**
+ * GIVEN a line buffer
+ * WHEN we append words to it and check the space remaining
+ * THEN it should correctly report the space left based on
+ * the number of characters we had inserted.
+ */
void test_linebuffer_space_remaining(void **state) {
LineBuffer *lb = linebuffer_create(50);
assert_non_null(lb);
@@ -53,6 +94,12 @@ void test_linebuffer_space_remaining(void **state) {
linebuffer_destroy(lb);
}
+/**
+ * GIVEN a line buffer with a few words in it
+ * WHEN we write out the line buffer to a file
+ * THEN it should correctly have written the buffer
+ * content out (not justified).
+ */
void test_linebuffer_write(void **state) {
LineBuffer *lb = linebuffer_create(50);
assert_non_null(lb);
@@ -73,6 +120,11 @@ void test_linebuffer_write(void **state) {
linebuffer_destroy(lb);
}
+/**
+ * GIVEN a line buffer with a few words in it
+ * WHEN we write out the line buffer to a file as justified
+ * THEN it should written out the justified content
+ */
void test_linebuffer_write_justified(void **state) {
LineBuffer *lb = linebuffer_create(20);
assert_non_null(lb);
@@ -97,6 +149,7 @@ int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_linebuffer_create),
cmocka_unit_test(test_linebuffer_append_word),
+ cmocka_unit_test(test_linebuffer_clear),
cmocka_unit_test(test_linebuffer_space_remaining),
cmocka_unit_test(test_linebuffer_write),
cmocka_unit_test(test_linebuffer_write_justified),