| #include "../../unity/unity.h" |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <errno.h> |
|
|
| |
| void setUp(void) { |
| |
| in_column = 0; |
| tabs = false; |
| } |
|
|
| void tearDown(void) { |
| |
| } |
|
|
| |
| static FILE* make_tmp_with(const char* s) { |
| FILE* f = tmpfile(); |
| TEST_ASSERT_NOT_NULL(f); |
| if (s && *s) { |
| size_t len = strlen(s); |
| TEST_ASSERT_EQUAL_size_t(len, fwrite(s, 1, len, f)); |
| } |
| rewind(f); |
| return f; |
| } |
|
|
| |
| static void test_get_space_nonblank_initial(void) { |
| FILE* f = make_tmp_with("A"); |
| int first = getc(f); |
| TEST_ASSERT_EQUAL_INT('A', first); |
|
|
| in_column = 5; |
| tabs = false; |
|
|
| int r = get_space(f, first); |
| TEST_ASSERT_EQUAL_INT('A', r); |
| TEST_ASSERT_EQUAL_INT(5, in_column); |
| TEST_ASSERT_FALSE(tabs); |
|
|
| |
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT(EOF, next); |
|
|
| fclose(f); |
| } |
|
|
| |
| static void test_get_space_spaces_then_nonblank(void) { |
| FILE* f = make_tmp_with(" XZ"); |
| int first = getc(f); |
| TEST_ASSERT_EQUAL_INT(' ', first); |
|
|
| in_column = 0; |
| tabs = false; |
|
|
| int r = get_space(f, first); |
| TEST_ASSERT_EQUAL_INT('X', r); |
| TEST_ASSERT_EQUAL_INT(4, in_column); |
| TEST_ASSERT_FALSE(tabs); |
|
|
| |
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT('Z', next); |
|
|
| fclose(f); |
| } |
|
|
| |
| static void test_get_space_tabs_and_spaces_mixed_initial_incol(void) { |
| FILE* f = make_tmp_with("\t\t XZ"); |
| int first = getc(f); |
| TEST_ASSERT_EQUAL_INT('\t', first); |
|
|
| in_column = 3; |
| tabs = false; |
|
|
| int r = get_space(f, first); |
| TEST_ASSERT_EQUAL_INT('X', r); |
|
|
| |
| |
| |
| |
| |
| |
| |
| TEST_ASSERT_EQUAL_INT(18, in_column); |
| TEST_ASSERT_TRUE(tabs); |
|
|
| |
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT('Z', next); |
|
|
| fclose(f); |
| } |
|
|
| |
| static void test_get_space_only_spaces_to_eof(void) { |
| FILE* f = make_tmp_with(" "); |
| int first = getc(f); |
| TEST_ASSERT_EQUAL_INT(' ', first); |
|
|
| in_column = 10; |
| tabs = false; |
|
|
| int r = get_space(f, first); |
| TEST_ASSERT_EQUAL_INT(EOF, r); |
| TEST_ASSERT_EQUAL_INT(13, in_column); |
| TEST_ASSERT_FALSE(tabs); |
|
|
| |
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT(EOF, next); |
|
|
| fclose(f); |
| } |
|
|
| |
| static void test_get_space_initial_eof(void) { |
| FILE* f = make_tmp_with(""); |
|
|
| in_column = 7; |
| tabs = false; |
|
|
| int r = get_space(f, EOF); |
| TEST_ASSERT_EQUAL_INT(EOF, r); |
| TEST_ASSERT_EQUAL_INT(7, in_column); |
| TEST_ASSERT_FALSE(tabs); |
|
|
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT(EOF, next); |
|
|
| fclose(f); |
| } |
|
|
| |
| static void test_get_space_newline_nonblank(void) { |
| FILE* f = make_tmp_with("\nA"); |
| int first = getc(f); |
| TEST_ASSERT_EQUAL_INT('\n', first); |
|
|
| in_column = 2; |
| tabs = false; |
|
|
| int r = get_space(f, first); |
| TEST_ASSERT_EQUAL_INT('\n', r); |
| TEST_ASSERT_EQUAL_INT(2, in_column); |
| TEST_ASSERT_FALSE(tabs); |
|
|
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT('A', next); |
|
|
| fclose(f); |
| } |
|
|
| |
| static void test_get_space_tab_at_tabstop(void) { |
| FILE* f = make_tmp_with("\tY"); |
| int first = getc(f); |
| TEST_ASSERT_EQUAL_INT('\t', first); |
|
|
| in_column = 8; |
| tabs = false; |
|
|
| int r = get_space(f, first); |
| TEST_ASSERT_EQUAL_INT('Y', r); |
| TEST_ASSERT_EQUAL_INT(16, in_column); |
| TEST_ASSERT_TRUE(tabs); |
|
|
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT(EOF, next); |
|
|
| fclose(f); |
| } |
|
|
| int main(void) { |
| UNITY_BEGIN(); |
|
|
| RUN_TEST(test_get_space_nonblank_initial); |
| RUN_TEST(test_get_space_spaces_then_nonblank); |
| RUN_TEST(test_get_space_tabs_and_spaces_mixed_initial_incol); |
| RUN_TEST(test_get_space_only_spaces_to_eof); |
| RUN_TEST(test_get_space_initial_eof); |
| RUN_TEST(test_get_space_newline_nonblank); |
| RUN_TEST(test_get_space_tab_at_tabstop); |
|
|
| return UNITY_END(); |
| } |