aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2025-07-12 12:02:55 -0400
committerEugene Triguba <eugenetriguba@gmail.com>2025-07-12 12:02:55 -0400
commitfc5666c693df9f0ab7a835db680b5bfbe2dffe92 (patch)
tree56895bb6261f37aa0609015b0515bd0ed56db9a6
parentAdd explanation comment for strlcat (diff)
downloadcli-fc5666c693df9f0ab7a835db680b5bfbe2dffe92.zip
Fix word wrapping to next line when the word is longer than the buffer
-rw-r--r--doc/justify.13
-rw-r--r--src/word.c12
-rw-r--r--tests/justify.bats19
-rw-r--r--tests/test_word.c117
4 files changed, 105 insertions, 46 deletions
diff --git a/doc/justify.1 b/doc/justify.1
index ccff202..e37f180 100644
--- a/doc/justify.1
+++ b/doc/justify.1
@@ -44,9 +44,6 @@ An error occurred (e.g., read error).
.SH NOTES
Before appending a word to the current line, the program ensures there is enough space for the word (and a space if not the first word). If not enough space remains, the current line is flushed (justified) and a new line is started.
-.SH LIMITATIONS
-The current implementation does not fully wrap or split words that are longer than the specified line width. Overlong words may be truncated or marked, rather than wrapped across lines.
-
.SH AUTHOR
Eugene Triguba <hello@eugenetriguba.com>
diff --git a/src/word.c b/src/word.c
index 87643d6..e46aedd 100644
--- a/src/word.c
+++ b/src/word.c
@@ -16,11 +16,19 @@ int read_word(FILE *fp, char *word, size_t length) {
while (!isspace(ch) && ch != EOF) {
if (position >= length - 1) {
// Note: -1 to ensure we have space for the null terminator
+ //
// Once we hit the end of our available space, we stop reading,
- // even if we didn't read the whole word.
+ // even if we didn't read the whole word. However, we need to
+ // ensure we put back the character we just read so that it is
+ // retrieved next time we call read_word.
+ int result = ungetc(ch, fp);
+ if (result == EOF) {
+ return READ_WORD_ERR_READ;
+ }
break;
}
- word[position++] = ch;
+ word[position] = ch;
+ position++;
ch = getc(fp);
}
if (length >= 0) {
diff --git a/tests/justify.bats b/tests/justify.bats
index a581c94..73db029 100644
--- a/tests/justify.bats
+++ b/tests/justify.bats
@@ -57,16 +57,15 @@ EOF
assert_output "Hello world"
}
-# TODO: Handle this case properly
-# @test "word longer than buffer is wrapped" {
-# run "${JUSTIFY_BIN}" -w 60 <<EOF
-# 123456789012345678901234567890123456789012345678901234567890EXTRA
-# EOF
-# assert_success
-# expected_output="123456789012345678901234567890123456789012345678901234567890
-# EXTRA"
-# assert_output "$expected_output"
-# }
+@test "word longer than buffer is wrapped" {
+ run "${JUSTIFY_BIN}" -w 60 <<EOF
+123456789012345678901234567890123456789012345678901234567890EXTRA
+EOF
+ assert_success
+ expected_output="123456789012345678901234567890123456789012345678901234567890
+EXTRA"
+ assert_output "$expected_output"
+}
@test "multiple spaces in input are collapsed when on last left-aligned line" {
run "${JUSTIFY_BIN}" <<EOF
diff --git a/tests/test_word.c b/tests/test_word.c
index 2da3589..aa58a34 100644
--- a/tests/test_word.c
+++ b/tests/test_word.c
@@ -17,137 +17,192 @@ FILE *create_tmp_file(const char *data) {
return fp;
}
-void test_read_word_simple(void **state) {
+/**
+ * GIVEN the an input containing a single word
+ * WHEN we read a word from the file
+ * THEN we would expect to read in the single word
+ */
+void test_read_word_single_word(void **state) {
const char *input = "hello";
const char *expected = "hello";
FILE *fp = create_tmp_file(input);
-
char word[100];
+
int result = read_word(fp, word, sizeof(word));
+
assert_int_equal(result, READ_WORD_SUCCESS);
assert_string_equal(word, expected);
-
fclose(fp);
}
+/**
+ * GIVEN an input string with leading spaces
+ * WHEN we read a word from the file
+ * THEN we would expect to have discarded the leading spaces and
+ * read in the following word
+ */
void test_read_word_leading_spaces(void **state) {
const char *input = " leading";
const char *expected = "leading";
FILE *fp = create_tmp_file(input);
-
char word[100];
+
int result = read_word(fp, word, sizeof(word));
+
assert_int_equal(result, READ_WORD_SUCCESS);
assert_string_equal(word, expected);
-
fclose(fp);
}
+/**
+ * GIVEN an input string with trailing spaces
+ * WHEN we read a word from the file
+ * THEN we would expect to read in the word without
+ * the trailing spaces
+ * WHEN we read in another word
+ * THEN we expect to see the remaining spaces simply get
+ * consumed and get back EOF
+ */
void test_read_word_trailing_spaces(void **state) {
const char *input = "trailing ";
const char *expected = "trailing";
FILE *fp = create_tmp_file(input);
-
char word[100];
+
int result = read_word(fp, word, sizeof(word));
assert_int_equal(result, READ_WORD_SUCCESS);
assert_string_equal(word, expected);
+ result = read_word(fp, word, sizeof(word));
+ assert_int_equal(result, READ_WORD_ERR_EOF);
+ assert_string_equal(word, "");
+
fclose(fp);
}
+/**
+ * GIVEN an input string with multiple spaces between words
+ * WHEN we read a word from the file
+ * THEN we expect to read in the first word
+ * WHEN we read in another word from the file
+ * THEN we expect to skip over the multiple spaces
+ * and read in the last word
+ */
void test_read_word_multiple_spaces(void **state) {
const char *input = "multiple spaces";
- const char *expected = "multiple";
FILE *fp = create_tmp_file(input);
-
char word[100];
+
+ char *expected = "multiple";
int result = read_word(fp, word, sizeof(word));
assert_int_equal(result, READ_WORD_SUCCESS);
assert_string_equal(word, expected);
- fclose(fp);
-}
-
-void test_read_word_single_word(void **state) {
- const char *input = "singleword";
- const char *expected = "singleword";
- FILE *fp = create_tmp_file(input);
-
- char word[100];
- int result = read_word(fp, word, sizeof(word));
+ expected = "spaces";
+ result = read_word(fp, word, sizeof(word));
assert_int_equal(result, READ_WORD_SUCCESS);
- assert_string_equal(word, expected);
+ assert_string_equal(word, expected);
fclose(fp);
}
+/**
+ * GIVEN an empty input string
+ * WHEN we read a word from the file
+ * THEN we expect to get EOF on read
+ */
void test_read_word_empty_input(void **state) {
const char *input = "";
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
+ * THEN we expect to get a read error
+ */
void test_read_word_null_fp(void **state) {
char word[100];
int result = read_word(NULL, word, sizeof(word));
assert_int_equal(result, READ_WORD_ERR_READ);
}
+/**
+ * GIVEN a NULL word buffer
+ * WHEN we read a word from the file
+ * THEN we expect to get a read error
+ */
void test_read_word_null_word(void **state) {
FILE *fp = tmpfile();
assert_non_null(fp);
int result = read_word(fp, NULL, 100);
- assert_int_equal(result, READ_WORD_ERR_READ);
+ assert_int_equal(result, READ_WORD_ERR_READ);
fclose(fp);
}
+/**
+ * GIVEN a word buffer with a 0 length
+ * WHEN we read a word from the file
+ * THEN we expect to get EOF
+ */
void test_read_word_zero_length(void **state) {
FILE *fp = tmpfile();
assert_non_null(fp);
-
char word[100];
+
int result = read_word(fp, word, 0);
- assert_int_equal(result, READ_WORD_ERR_EOF);
+ assert_int_equal(result, READ_WORD_ERR_EOF);
fclose(fp);
}
-void test_read_word_buffer_overflow(void **state) {
+/**
+ * GIVEN a word that is too long to fit into the word buffer
+ * WHEN we read a word from the file
+ * THEN we expect to get the partial word back
+ * WHEN we read in another word
+ * THEN we expect to get the remainder of the word
+ */
+void test_read_word_overflow(void **state) {
const char *input = "thisisaverylongword";
- const char *expected = "thisisaver";
FILE *fp = create_tmp_file(input);
+ char word[11]; // Buffer size is 11 to include null terminator
- char word[11]; // Buffer size is 11 including null terminator
+ char *expected = "thisisaver";
int result = read_word(fp, word, sizeof(word));
assert_int_equal(result, READ_WORD_SUCCESS);
assert_string_equal(word, expected);
+ expected = "ylongword";
+ result = read_word(fp, word, sizeof(word));
+ assert_int_equal(result, READ_WORD_SUCCESS);
+ assert_string_equal(word, expected);
+
fclose(fp);
}
int main(void) {
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_read_word_simple),
+ cmocka_unit_test(test_read_word_single_word),
cmocka_unit_test(test_read_word_leading_spaces),
cmocka_unit_test(test_read_word_trailing_spaces),
cmocka_unit_test(test_read_word_multiple_spaces),
- cmocka_unit_test(test_read_word_single_word),
cmocka_unit_test(test_read_word_empty_input),
cmocka_unit_test(test_read_word_null_fp),
cmocka_unit_test(test_read_word_null_word),
cmocka_unit_test(test_read_word_zero_length),
- cmocka_unit_test(test_read_word_buffer_overflow),
+ cmocka_unit_test(test_read_word_overflow),
};
return cmocka_run_group_tests(tests, NULL, NULL);
-} \ No newline at end of file
+}