From 62c324f89216da1c311c8225f6c9087e441734de Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 12 Jul 2025 12:28:57 -0400 Subject: Simplify justify function and add additional read_word test --- src/main.c | 6 ------ tests/test_word.c | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index 8337dc5..57898f4 100644 --- a/src/main.c +++ b/src/main.c @@ -50,12 +50,6 @@ int justify(size_t linebuffer_capacity, FILE *in, FILE *out) { while ((read_word_result = read_word(in, word, MAX_WORD_LENGTH + 1)) == READ_WORD_SUCCESS) { - word_length = strlen(word); - if (word_length == 0) { - linebuffer_write(lb, out); - linebuffer_destroy(lb); - return EXIT_SUCCESS; - } if (!linebuffer_append_word(lb, word)) { linebuffer_write_justified(lb, out); linebuffer_clear(lb); diff --git a/tests/test_word.c b/tests/test_word.c index aa58a34..54dbf45 100644 --- a/tests/test_word.c +++ b/tests/test_word.c @@ -124,6 +124,24 @@ void test_read_word_empty_input(void **state) { fclose(fp); } +/** + * GIVEN an input string that is only whitespace characters + * WHEN we read a word from the file + * THEN we expect to get EOF on read + */ +void test_read_word_input_with_only_spaces(void **state) { + const char *input = " \t\t\n\n"; + const char *expected = ""; + FILE *fp = create_tmp_file(input); + char word[100]; + + int result = read_word(fp, word, sizeof(word)); + + assert_int_equal(result, READ_WORD_ERR_EOF); + assert_string_equal(word, expected); + fclose(fp); +} + /** * GIVEN a NULL input * WHEN we read a word from the file @@ -198,6 +216,7 @@ int main(void) { cmocka_unit_test(test_read_word_trailing_spaces), cmocka_unit_test(test_read_word_multiple_spaces), cmocka_unit_test(test_read_word_empty_input), + cmocka_unit_test(test_read_word_input_with_only_spaces), cmocka_unit_test(test_read_word_null_fp), cmocka_unit_test(test_read_word_null_word), cmocka_unit_test(test_read_word_zero_length), -- cgit v1.2.3