File size: 4,877 Bytes
78d2150 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
#include "../../unity/unity.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/* Unity setup/teardown */
void setUp(void) {
/* Reset key globals to a known state before each test */
in_column = 0;
tabs = false;
}
void tearDown(void) {
/* Nothing to cleanup */
}
/* Helper to create a tmp file with content s, rewound for reading */
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;
}
/* 1) Non-blank initial character: should return c, not change in_column or tabs, and not consume further. */
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);
/* Next char should be EOF because only 'A' was present and already consumed before call */
int next = getc(f);
TEST_ASSERT_EQUAL_INT(EOF, next);
fclose(f);
}
/* 2) Spaces then non-blank: should consume spaces, update in_column, return first non-blank, and consume it. */
static void test_get_space_spaces_then_nonblank(void) {
FILE* f = make_tmp_with(" XZ"); /* 4 spaces, 'X', 'Z' */
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);
/* X has been consumed by get_space; next should be 'Z' */
int next = getc(f);
TEST_ASSERT_EQUAL_INT('Z', next);
fclose(f);
}
/* 3) Tabs and spaces mixed with non-zero initial in_column: verify tab stops and tabs flag. */
static void test_get_space_tabs_and_spaces_mixed_initial_incol(void) {
FILE* f = make_tmp_with("\t\t XZ"); /* tab, tab, space, space, 'X', 'Z' */
int first = getc(f);
TEST_ASSERT_EQUAL_INT('\t', first);
in_column = 3; /* Start not on tab stop */
tabs = false;
int r = get_space(f, first);
TEST_ASSERT_EQUAL_INT('X', r);
/* Computation:
start 3
'\t' -> 8
'\t' -> 16
' ' -> 17
' ' -> 18
*/
TEST_ASSERT_EQUAL_INT(18, in_column);
TEST_ASSERT_TRUE(tabs);
/* 'X' consumed; next should be 'Z' */
int next = getc(f);
TEST_ASSERT_EQUAL_INT('Z', next);
fclose(f);
}
/* 4) Only spaces to EOF: should return EOF and update in_column accordingly, tabs remains false. */
static void test_get_space_only_spaces_to_eof(void) {
FILE* f = make_tmp_with(" "); /* three spaces, then EOF */
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);
/* Stream should now be at EOF */
int next = getc(f);
TEST_ASSERT_EQUAL_INT(EOF, next);
fclose(f);
}
/* 5) Initial EOF: should return EOF without changing state or reading. */
static void test_get_space_initial_eof(void) {
FILE* f = make_tmp_with(""); /* empty file */
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);
}
/* 6) Newline treated as non-blank: should return '\n' immediately, no changes to in_column or tabs. */
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);
}
/* 7) Tab when at a tab stop boundary: should advance to next tab stop correctly. */
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; /* at a tab stop already */
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); /* after 'Y', file ends */
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();
} |