File size: 7,809 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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
#include "../../unity/unity.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
/* We rely on internal symbols from fmt.c:
- static void set_prefix(char *p);
- static int copy_rest(FILE *f, int c);
- static bool tabs;
- static int in_column;
- static int next_prefix_indent;
- static char const *prefix;
- static int prefix_length;
These are available since this test file is included into the same
translation unit. */
/* Prototypes to silence any compiler warnings (available in this TU). */
static void set_prefix (char *p);
static int copy_rest (FILE *f, int c);
/* Globals from the program (declared in the same TU) */
extern bool tabs;
extern int in_column;
extern int next_prefix_indent;
/* Helper: create an input FILE* containing 'content' and rewind it. */
static FILE* make_input_stream(const char* content)
{
FILE* f = tmpfile();
if (!f)
return NULL;
size_t len = content ? strlen(content) : 0;
if (len > 0)
{
if (fwrite(content, 1, len, f) != len)
{
fclose(f);
return NULL;
}
}
rewind(f);
return f;
}
/* Helper: capture stdout while invoking copy_rest(fin, c).
- Returns a heap-allocated string with the captured output (caller frees).
- Writes the return value of copy_rest into *ret_c.
- Returns NULL on failure (allocation or tmpfile issues).
Note: No Unity assertions while stdout is redirected. */
static char* capture_copy_rest_output(FILE* fin, int c, int* ret_c)
{
if (!ret_c)
return NULL;
fflush(stdout);
int saved_fd = dup(fileno(stdout));
if (saved_fd == -1)
return NULL;
FILE* cap = tmpfile();
if (!cap)
{
close(saved_fd);
return NULL;
}
int cap_fd = fileno(cap);
if (dup2(cap_fd, fileno(stdout)) == -1)
{
fclose(cap);
close(saved_fd);
return NULL;
}
int r = copy_rest(fin, c);
fflush(stdout);
fflush(cap);
/* Restore stdout */
if (dup2(saved_fd, fileno(stdout)) == -1)
{
fclose(cap);
close(saved_fd);
return NULL;
}
close(saved_fd);
/* Read captured output */
if (fseek(cap, 0, SEEK_END) != 0)
{
fclose(cap);
return NULL;
}
long sz = ftell(cap);
if (sz < 0)
{
fclose(cap);
return NULL;
}
if (fseek(cap, 0, SEEK_SET) != 0)
{
fclose(cap);
return NULL;
}
char* buf = (char*)malloc((size_t)sz + 1);
if (!buf)
{
fclose(cap);
return NULL;
}
size_t nread = fread(buf, 1, (size_t)sz, cap);
buf[nread] = '\0';
fclose(cap);
*ret_c = r;
return buf;
}
void setUp(void)
{
/* Ensure deterministic spacing (no tabs in output) */
tabs = false;
}
void tearDown(void)
{
/* Nothing to clean */
}
/* Test: c == '\n' with no prefix progress; expect no output and return '\n'. */
static void test_copy_rest_blank_line_no_output(void)
{
char pfx[] = "";
set_prefix(pfx);
next_prefix_indent = 0;
in_column = 0;
FILE* fin = make_input_stream("");
TEST_ASSERT_NOT_NULL(fin);
int retc = 0;
char* out = capture_copy_rest_output(fin, '\n', &retc);
fclose(fin);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_INT('\n', retc);
TEST_ASSERT_EQUAL_UINT(0, strlen(out));
free(out);
}
/* Test: c == '\n' with some indentation and partial prefix print. */
static void test_copy_rest_newline_with_partial_prefix(void)
{
char pfx[] = "ABC";
set_prefix(pfx);
next_prefix_indent = 2; /* two leading spaces */
in_column = 3; /* matched 'A' before newline */
FILE* fin = make_input_stream("");
TEST_ASSERT_NOT_NULL(fin);
int retc = 0;
char* out = capture_copy_rest_output(fin, '\n', &retc);
fclose(fin);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_INT('\n', retc);
/* Expect: " A" (2 spaces + 'A') */
TEST_ASSERT_EQUAL_STRING(" A", out);
free(out);
}
/* Test: mismatch (c not '\n'/EOF) with no extra padding beyond trimmed prefix. */
static void test_copy_rest_mismatch_no_padding(void)
{
char pfx[] = "ABC";
set_prefix(pfx);
next_prefix_indent = 2;
in_column = 3; /* matched 'A' */
/* The remainder of the line (after 'X') will be "YZ\n" */
FILE* fin = make_input_stream("YZ\n");
TEST_ASSERT_NOT_NULL(fin);
int retc = 0;
char* out = capture_copy_rest_output(fin, 'X', &retc);
fclose(fin);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_INT('\n', retc);
/* Expect: " A" + 'X' + "YZ" = " AXYZ" */
TEST_ASSERT_EQUAL_STRING(" AXYZ", out);
free(out);
}
/* Test: mismatch with in_column beyond trimmed prefix -> extra padding spaces added. */
static void test_copy_rest_mismatch_with_padding_beyond_trimmed_prefix(void)
{
char pfx[] = "AB "; /* trimmed prefix becomes "AB", prefix_length = 2 */
set_prefix(pfx);
next_prefix_indent = 1;
in_column = next_prefix_indent + 4; /* 1 (indent) + 4 matched columns */
/* After printing indent + "AB", we need (4 - 2) = 2 padding spaces. */
FILE* fin = make_input_stream("T\n");
TEST_ASSERT_NOT_NULL(fin);
int retc = 0;
char* out = capture_copy_rest_output(fin, 'X', &retc);
fclose(fin);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_INT('\n', retc);
/* Expect: " " + "AB" + " " + 'X' + "T" = " AB XT" */
TEST_ASSERT_EQUAL_STRING(" AB XT", out);
free(out);
}
/* Test: EOF where in_column >= next_prefix_indent + prefix_length -> emits newline. */
static void test_copy_rest_eof_emits_newline_after_full_prefix(void)
{
char pfx[] = "HELLO";
set_prefix(pfx);
next_prefix_indent = 3;
in_column = next_prefix_indent + (int)strlen("HELLO"); /* full trimmed prefix matched */
FILE* fin = make_input_stream("");
TEST_ASSERT_NOT_NULL(fin);
int retc = 0;
char* out = capture_copy_rest_output(fin, EOF, &retc);
fclose(fin);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_INT(EOF, retc);
/* Expect: " HELLO\n" */
TEST_ASSERT_EQUAL_STRING(" HELLO\n", out);
free(out);
}
/* Test: EOF where no progress beyond indent -> no output. */
static void test_copy_rest_eof_no_output_when_no_progress(void)
{
char pfx[] = "HELLO";
set_prefix(pfx);
next_prefix_indent = 2;
in_column = 2; /* no progress beyond indent */
FILE* fin = make_input_stream("");
TEST_ASSERT_NOT_NULL(fin);
int retc = 0;
char* out = capture_copy_rest_output(fin, EOF, &retc);
fclose(fin);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_INT(EOF, retc);
TEST_ASSERT_EQUAL_UINT(0, strlen(out));
free(out);
}
/* Test: No prefix context (empty prefix), c not newline/EOF. */
static void test_copy_rest_no_prefix_context_non_newline(void)
{
char pfx[] = "";
set_prefix(pfx);
next_prefix_indent = 0;
in_column = 0;
FILE* fin = make_input_stream("RS\n"); /* tail after 'Q' */
TEST_ASSERT_NOT_NULL(fin);
int retc = 0;
char* out = capture_copy_rest_output(fin, 'Q', &retc);
fclose(fin);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_INT('\n', retc);
/* Expect: 'Q' + "RS" */
TEST_ASSERT_EQUAL_STRING("QRS", out);
free(out);
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_copy_rest_blank_line_no_output);
RUN_TEST(test_copy_rest_newline_with_partial_prefix);
RUN_TEST(test_copy_rest_mismatch_no_padding);
RUN_TEST(test_copy_rest_mismatch_with_padding_beyond_trimmed_prefix);
RUN_TEST(test_copy_rest_eof_emits_newline_after_full_prefix);
RUN_TEST(test_copy_rest_eof_no_output_when_no_progress);
RUN_TEST(test_copy_rest_no_prefix_context_non_newline);
return UNITY_END();
} |