From e9fd3702154962dcfceec73be88035fe75de7325 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 12 Jul 2025 12:45:43 -0400 Subject: Add test for clear to linebuffer tests --- tests/test_linebuffer.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'tests') 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 #include +/** + * 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), -- cgit v1.2.3