| #include "../../unity/unity.h" |
| #include <stdlib.h> |
| #include <string.h> |
|
|
| |
| extern int goal_width; |
| extern struct Word *word_limit; |
|
|
| |
| static int saved_goal_width; |
| static struct Word *saved_word_limit; |
|
|
| void setUp(void) { |
| saved_goal_width = goal_width; |
| saved_word_limit = word_limit; |
| } |
|
|
| void tearDown(void) { |
| goal_width = saved_goal_width; |
| word_limit = saved_word_limit; |
| } |
|
|
| |
| static void zero_words(WORD *arr, size_t n) { |
| memset(arr, 0, n * sizeof(*arr)); |
| } |
|
|
| |
| void test_line_cost_next_is_word_limit_returns_zero(void) { |
| WORD arr[2]; |
| zero_words(arr, 2); |
|
|
| goal_width = 70; |
| word_limit = &arr[1]; |
|
|
| COST c = line_cost(word_limit, 123); |
| TEST_ASSERT_EQUAL_INT(0, (int)c); |
| } |
|
|
| |
| void test_line_cost_base_only_exact_goal_width_zero(void) { |
| WORD arr[2]; |
| zero_words(arr, 2); |
|
|
| goal_width = 50; |
| word_limit = &arr[1]; |
|
|
| WORD *next = &arr[0]; |
| next->next_break = word_limit; |
| next->line_length = 0; |
|
|
| int len = 50; |
| COST expected = SHORT_COST(goal_width - len); |
| COST c = line_cost(next, len); |
| TEST_ASSERT_EQUAL_INT((int)expected, (int)c); |
| } |
|
|
| |
| void test_line_cost_base_only_under_goal(void) { |
| WORD arr[2]; |
| zero_words(arr, 2); |
|
|
| goal_width = 50; |
| word_limit = &arr[1]; |
|
|
| WORD *next = &arr[0]; |
| next->next_break = word_limit; |
|
|
| int len = 45; |
| COST expected = SHORT_COST(goal_width - len); |
| COST c = line_cost(next, len); |
| TEST_ASSERT_EQUAL_INT((int)expected, (int)c); |
| } |
|
|
| |
| void test_line_cost_base_only_over_goal(void) { |
| WORD arr[2]; |
| zero_words(arr, 2); |
|
|
| goal_width = 50; |
| word_limit = &arr[1]; |
|
|
| WORD *next = &arr[0]; |
| next->next_break = word_limit; |
|
|
| int len = 60; |
| COST expected = SHORT_COST(goal_width - len); |
| COST c = line_cost(next, len); |
| TEST_ASSERT_EQUAL_INT((int)expected, (int)c); |
| } |
|
|
| |
| void test_line_cost_includes_ragged_positive_difference(void) { |
| WORD arr[3]; |
| zero_words(arr, 3); |
|
|
| goal_width = 50; |
| word_limit = &arr[2]; |
|
|
| WORD *next = &arr[0]; |
| next->next_break = &arr[1]; |
| next->line_length = 43; |
|
|
| int len = 48; |
| COST expected = SHORT_COST(goal_width - len) + RAGGED_COST(len - next->line_length); |
| COST c = line_cost(next, len); |
| TEST_ASSERT_EQUAL_INT((int)expected, (int)c); |
| } |
|
|
| |
| void test_line_cost_includes_ragged_negative_difference(void) { |
| WORD arr[3]; |
| zero_words(arr, 3); |
|
|
| goal_width = 50; |
| word_limit = &arr[2]; |
|
|
| WORD *next = &arr[0]; |
| next->next_break = &arr[1]; |
| next->line_length = 45; |
|
|
| int len = 40; |
| COST expected = SHORT_COST(goal_width - len) + RAGGED_COST(len - next->line_length); |
| COST c = line_cost(next, len); |
| TEST_ASSERT_EQUAL_INT((int)expected, (int)c); |
| } |
|
|
| |
| void test_line_cost_no_ragged_when_next_break_is_word_limit(void) { |
| WORD arr[2]; |
| zero_words(arr, 2); |
|
|
| goal_width = 52; |
| word_limit = &arr[1]; |
|
|
| WORD *next = &arr[0]; |
| next->next_break = word_limit; |
| next->line_length = 10; |
|
|
| int len = 49; |
| COST expected = SHORT_COST(goal_width - len); |
| COST c = line_cost(next, len); |
| TEST_ASSERT_EQUAL_INT((int)expected, (int)c); |
| } |
|
|
| |
| void test_line_cost_zero_when_at_goal_and_no_ragged_diff(void) { |
| WORD arr[3]; |
| zero_words(arr, 3); |
|
|
| goal_width = 80; |
| word_limit = &arr[2]; |
|
|
| WORD *next = &arr[0]; |
| next->next_break = &arr[1]; |
| next->line_length = 80; |
|
|
| int len = 80; |
| COST expected = SHORT_COST(0) + RAGGED_COST(0); |
| COST c = line_cost(next, len); |
| TEST_ASSERT_EQUAL_INT((int)expected, (int)c); |
| } |
|
|
| int main(void) { |
| UNITY_BEGIN(); |
| RUN_TEST(test_line_cost_next_is_word_limit_returns_zero); |
| RUN_TEST(test_line_cost_base_only_exact_goal_width_zero); |
| RUN_TEST(test_line_cost_base_only_under_goal); |
| RUN_TEST(test_line_cost_base_only_over_goal); |
| RUN_TEST(test_line_cost_includes_ragged_positive_difference); |
| RUN_TEST(test_line_cost_includes_ragged_negative_difference); |
| RUN_TEST(test_line_cost_no_ragged_when_next_break_is_word_limit); |
| RUN_TEST(test_line_cost_zero_when_at_goal_and_no_ragged_diff); |
| return UNITY_END(); |
| } |