|
|
#include "../../unity/unity.h" |
|
|
#include <string.h> |
|
|
#include <stdlib.h> |
|
|
#include <stdio.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void reset_words_range(int from, int to_exclusive) { |
|
|
|
|
|
memset(&word[from], 0, (size_t)(to_exclusive - from) * sizeof(word[0])); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void prepare_words(const int *lengths, const int *spaces, int count, int sentinel_len) { |
|
|
|
|
|
reset_words_range(0, count + 1); |
|
|
|
|
|
for (int i = 0; i < count; i++) { |
|
|
word[i].length = lengths[i]; |
|
|
word[i].space = spaces ? spaces[i] : 1; |
|
|
word[i].paren = 0; |
|
|
word[i].period = 0; |
|
|
word[i].punct = 0; |
|
|
word[i].final = 0; |
|
|
word[i].text = NULL; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
word_limit = &word[count]; |
|
|
|
|
|
word_limit->length = sentinel_len; |
|
|
|
|
|
word_limit->best_cost = 0; |
|
|
word_limit->next_break = NULL; |
|
|
word_limit->line_length = 0; |
|
|
} |
|
|
|
|
|
void setUp(void) { |
|
|
|
|
|
last_line_length = 0; |
|
|
first_indent = 0; |
|
|
other_indent = 0; |
|
|
max_width = 0; |
|
|
goal_width = 0; |
|
|
} |
|
|
|
|
|
void tearDown(void) { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void test_fmt_paragraph_forced_wrap(void) { |
|
|
|
|
|
const int lengths[] = {3, 3}; |
|
|
const int spaces[] = {1, 1}; |
|
|
prepare_words(lengths, spaces, 2, 123); |
|
|
|
|
|
max_width = 5; |
|
|
goal_width = 5; |
|
|
first_indent = 0; |
|
|
other_indent = 0; |
|
|
last_line_length = 0; |
|
|
|
|
|
fmt_paragraph(); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_PTR(&word[1], word[0].next_break); |
|
|
TEST_ASSERT_EQUAL_INT(3, word[0].line_length); |
|
|
|
|
|
TEST_ASSERT_EQUAL_PTR(word_limit, word[1].next_break); |
|
|
TEST_ASSERT_EQUAL_INT(3, word[1].line_length); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(123, word_limit->length); |
|
|
} |
|
|
|
|
|
|
|
|
static void test_fmt_paragraph_two_words_single_line_with_indent(void) { |
|
|
const int lengths[] = {3, 3}; |
|
|
const int spaces[] = {1, 1}; |
|
|
prepare_words(lengths, spaces, 2, 99); |
|
|
|
|
|
max_width = 20; |
|
|
goal_width = 20; |
|
|
first_indent = 2; |
|
|
other_indent = 4; |
|
|
|
|
|
fmt_paragraph(); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_PTR(word_limit, word[0].next_break); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(9, word[0].line_length); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_PTR(word_limit, word[1].next_break); |
|
|
TEST_ASSERT_EQUAL_INT(other_indent + 3, word[1].line_length); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(99, word_limit->length); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void test_fmt_paragraph_three_words_forced_each_line(void) { |
|
|
const int lengths[] = {3, 3, 3}; |
|
|
const int spaces[] = {1, 1, 1}; |
|
|
prepare_words(lengths, spaces, 3, 77); |
|
|
|
|
|
max_width = 5; |
|
|
goal_width = 5; |
|
|
first_indent = 0; |
|
|
other_indent = 0; |
|
|
|
|
|
fmt_paragraph(); |
|
|
|
|
|
TEST_ASSERT_EQUAL_PTR(&word[1], word[0].next_break); |
|
|
TEST_ASSERT_EQUAL_INT(3, word[0].line_length); |
|
|
|
|
|
TEST_ASSERT_EQUAL_PTR(&word[2], word[1].next_break); |
|
|
TEST_ASSERT_EQUAL_INT(3, word[1].line_length); |
|
|
|
|
|
TEST_ASSERT_EQUAL_PTR(word_limit, word[2].next_break); |
|
|
TEST_ASSERT_EQUAL_INT(3, word[2].line_length); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(77, word_limit->length); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void test_fmt_paragraph_raggedness_with_last_line_length(void) { |
|
|
const int lengths[] = {3, 3}; |
|
|
const int spaces[] = {1, 1}; |
|
|
prepare_words(lengths, spaces, 2, 55); |
|
|
|
|
|
max_width = 20; |
|
|
goal_width = 5; |
|
|
first_indent = 0; |
|
|
other_indent = 0; |
|
|
last_line_length = 10; |
|
|
|
|
|
fmt_paragraph(); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_PTR(word_limit, word[0].next_break); |
|
|
TEST_ASSERT_EQUAL_INT(7, word[0].line_length); |
|
|
|
|
|
TEST_ASSERT_EQUAL_PTR(word_limit, word[1].next_break); |
|
|
TEST_ASSERT_EQUAL_INT(3, word[1].line_length); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(55, word_limit->length); |
|
|
} |
|
|
|
|
|
int main(void) { |
|
|
UNITY_BEGIN(); |
|
|
RUN_TEST(test_fmt_paragraph_forced_wrap); |
|
|
RUN_TEST(test_fmt_paragraph_two_words_single_line_with_indent); |
|
|
RUN_TEST(test_fmt_paragraph_three_words_forced_each_line); |
|
|
RUN_TEST(test_fmt_paragraph_raggedness_with_last_line_length); |
|
|
return UNITY_END(); |
|
|
} |