| #include "../../unity/unity.h" |
| #include <stdlib.h> |
| #include <string.h> |
| #include <ctype.h> |
| #include <stdbool.h> |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| static void reset_translation_state(void) |
| { |
| for (int i = 0; i < 256; i++) |
| trans_table[i] = (unsigned char)i; |
| conversions_mask = 0; |
| translation_needed = false; |
| newline_character = '\n'; |
| space_character = ' '; |
| } |
|
|
| void setUp(void) { |
| reset_translation_state(); |
| } |
|
|
| void tearDown(void) { |
| |
| } |
|
|
| static void assert_trans_table_equals_array_uc(const unsigned char *expected) |
| { |
| for (int i = 0; i < 256; i++) |
| { |
| if (trans_table[i] != expected[i]) |
| { |
| TEST_FAIL_MESSAGE("trans_table mismatch"); |
| } |
| } |
| } |
|
|
| static void assert_trans_table_equals_array_c(const char *expected) |
| { |
| for (int i = 0; i < 256; i++) |
| { |
| unsigned char exp = (unsigned char)expected[i]; |
| if (trans_table[i] != exp) |
| { |
| TEST_FAIL_MESSAGE("trans_table mismatch"); |
| } |
| } |
| } |
|
|
| void test_apply_translations_noop(void) |
| { |
| |
| apply_translations(); |
|
|
| for (int i = 0; i < 256; i++) |
| TEST_ASSERT_EQUAL_UINT8((unsigned char)i, trans_table[i]); |
| TEST_ASSERT_FALSE(translation_needed); |
| TEST_ASSERT_EQUAL_INT('\n', newline_character); |
| TEST_ASSERT_EQUAL_INT(' ', space_character); |
| } |
|
|
| void test_apply_translations_ascii_only(void) |
| { |
| conversions_mask = C_ASCII; |
| apply_translations(); |
|
|
| |
| assert_trans_table_equals_array_c(ebcdic_to_ascii); |
| TEST_ASSERT_TRUE(translation_needed); |
| |
| TEST_ASSERT_EQUAL_INT('\n', newline_character); |
| TEST_ASSERT_EQUAL_INT(' ', space_character); |
| } |
|
|
| void test_apply_translations_ebcdic_only(void) |
| { |
| conversions_mask = C_EBCDIC; |
| apply_translations(); |
|
|
| |
| assert_trans_table_equals_array_c(ascii_to_ebcdic); |
| TEST_ASSERT_TRUE(translation_needed); |
| TEST_ASSERT_EQUAL_INT((unsigned char)ascii_to_ebcdic['\n'], (unsigned char)newline_character); |
| TEST_ASSERT_EQUAL_INT((unsigned char)ascii_to_ebcdic[' '], (unsigned char)space_character); |
| } |
|
|
| void test_apply_translations_ibm_only(void) |
| { |
| conversions_mask = C_IBM; |
| apply_translations(); |
|
|
| |
| assert_trans_table_equals_array_c(ascii_to_ibm); |
| TEST_ASSERT_TRUE(translation_needed); |
| TEST_ASSERT_EQUAL_INT((unsigned char)ascii_to_ibm['\n'], (unsigned char)newline_character); |
| TEST_ASSERT_EQUAL_INT((unsigned char)ascii_to_ibm[' '], (unsigned char)space_character); |
| } |
|
|
| void test_apply_translations_ucase_only(void) |
| { |
| conversions_mask = C_UCASE; |
| apply_translations(); |
|
|
| |
| unsigned char expected[256]; |
| for (int i = 0; i < 256; i++) |
| expected[i] = (unsigned char)toupper((unsigned char)i); |
| assert_trans_table_equals_array_uc(expected); |
|
|
| TEST_ASSERT_TRUE(translation_needed); |
| TEST_ASSERT_EQUAL_INT('\n', newline_character); |
| TEST_ASSERT_EQUAL_INT(' ', space_character); |
| } |
|
|
| void test_apply_translations_lcase_only(void) |
| { |
| conversions_mask = C_LCASE; |
| apply_translations(); |
|
|
| |
| unsigned char expected[256]; |
| for (int i = 0; i < 256; i++) |
| expected[i] = (unsigned char)tolower((unsigned char)i); |
| assert_trans_table_equals_array_uc(expected); |
|
|
| TEST_ASSERT_TRUE(translation_needed); |
| TEST_ASSERT_EQUAL_INT('\n', newline_character); |
| TEST_ASSERT_EQUAL_INT(' ', space_character); |
| } |
|
|
| void test_apply_translations_ucase_then_ebcdic_ordering(void) |
| { |
| |
| conversions_mask = C_UCASE | C_EBCDIC; |
| apply_translations(); |
|
|
| |
| unsigned char expected[256]; |
| for (int i = 0; i < 256; i++) |
| { |
| int up = toupper((unsigned char)i); |
| expected[i] = (unsigned char)ascii_to_ebcdic[up]; |
| } |
| assert_trans_table_equals_array_uc(expected); |
|
|
| TEST_ASSERT_TRUE(translation_needed); |
| TEST_ASSERT_EQUAL_INT((unsigned char)ascii_to_ebcdic['\n'], (unsigned char)newline_character); |
| TEST_ASSERT_EQUAL_INT((unsigned char)ascii_to_ebcdic[' '], (unsigned char)space_character); |
| } |
|
|
| void test_apply_translations_ascii_then_lcase_ordering(void) |
| { |
| |
| conversions_mask = C_ASCII | C_LCASE; |
| apply_translations(); |
|
|
| |
| unsigned char expected[256]; |
| for (int i = 0; i < 256; i++) |
| { |
| unsigned char ea = (unsigned char)ebcdic_to_ascii[i]; |
| expected[i] = (unsigned char)tolower((unsigned char)ea); |
| } |
| assert_trans_table_equals_array_uc(expected); |
|
|
| TEST_ASSERT_TRUE(translation_needed); |
| |
| TEST_ASSERT_EQUAL_INT('\n', newline_character); |
| TEST_ASSERT_EQUAL_INT(' ', space_character); |
| } |
|
|
| int main(void) |
| { |
| UNITY_BEGIN(); |
| RUN_TEST(test_apply_translations_noop); |
| RUN_TEST(test_apply_translations_ascii_only); |
| RUN_TEST(test_apply_translations_ebcdic_only); |
| RUN_TEST(test_apply_translations_ibm_only); |
| RUN_TEST(test_apply_translations_ucase_only); |
| RUN_TEST(test_apply_translations_lcase_only); |
| RUN_TEST(test_apply_translations_ucase_then_ebcdic_ordering); |
| RUN_TEST(test_apply_translations_ascii_then_lcase_ordering); |
| return UNITY_END(); |
| } |