File size: 12,619 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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | #include "../../unity/unity.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
/* The test file is included into the same translation unit as nl.c,
so we can access file-scope static variables and functions directly. */
/* Forward declaration of target (has internal linkage, but visible here
because we are included in the same translation unit). */
static bool nl_file (char const *file);
/* Globals from program, accessible here due to inclusion into same TU. */
extern char const *FORMAT_RIGHT_NOLZ; /* format const from program */
static void reset_lineno(void); /* static in program, visible here by inclusion */
/* Helper: make a print_no_line_fmt based on width and separator. */
static char* make_print_no_line_fmt_buf(int width, const char* sep)
{
size_t seplen = strlen(sep);
char* s = (char*)malloc((size_t)width + seplen + 1);
if (!s) return NULL;
memset(s, ' ', (size_t)width);
memcpy(s + width, sep, seplen);
s[width + seplen] = '\0';
return s;
}
/* Helper: write content to an already-open fd, then rewind position. */
static int write_all(int fd, const char* data, size_t len)
{
size_t off = 0;
while (off < len) {
ssize_t w = write(fd, data + off, len - off);
if (w < 0) return -1;
off += (size_t)w;
}
return 0;
}
/* Helper: create a temp file with given content, return its path (malloc'd).
Caller must free the returned path. */
static char* create_temp_file_with(const char* content)
{
char tmpl[] = "/tmp/nl_test_XXXXXX";
int fd = mkstemp(tmpl);
if (fd < 0) return NULL;
if (content && write_all(fd, content, strlen(content)) < 0) {
close(fd);
unlink(tmpl);
return NULL;
}
/* Rewind the file for reading by nl_file. */
lseek(fd, 0, SEEK_SET);
close(fd);
char* path = strdup(tmpl);
return path;
}
/* Helper: read entire file into malloc'd buffer; returns NUL-terminated string. */
static char* read_entire_file(const char* path)
{
FILE* f = fopen(path, "rb");
if (!f) return NULL;
if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return NULL; }
long sz = ftell(f);
if (sz < 0) { fclose(f); return NULL; }
if (fseek(f, 0, SEEK_SET) != 0) { fclose(f); return NULL; }
char* buf = (char*)malloc((size_t)sz + 1);
if (!buf) { fclose(f); return NULL; }
size_t n = fread(buf, 1, (size_t)sz, f);
buf[n] = '\0';
fclose(f);
return buf;
}
/* Helper: redirect stdout to a temp file, returning saved stdout fd and path to out file. */
static int redirect_stdout_to_temp(char** out_path)
{
fflush(stdout);
char tmpl[] = "/tmp/nl_out_XXXXXX";
int outfd = mkstemp(tmpl);
if (outfd < 0) return -1;
int saved = dup(STDOUT_FILENO);
if (saved < 0) { close(outfd); unlink(tmpl); return -1; }
if (dup2(outfd, STDOUT_FILENO) < 0) {
close(outfd);
close(saved);
unlink(tmpl);
return -1;
}
close(outfd);
*out_path = strdup(tmpl);
return saved;
}
/* Helper: redirect stderr to a temp file, returning saved stderr fd and path. */
static int redirect_stderr_to_temp(char** err_path)
{
fflush(stderr);
char tmpl[] = "/tmp/nl_err_XXXXXX";
int errfd = mkstemp(tmpl);
if (errfd < 0) return -1;
int saved = dup(STDERR_FILENO);
if (saved < 0) { close(errfd); unlink(tmpl); return -1; }
if (dup2(errfd, STDERR_FILENO) < 0) {
close(errfd);
close(saved);
unlink(tmpl);
return -1;
}
close(errfd);
*err_path = strdup(tmpl);
return saved;
}
/* Helper: restore stdout from saved fd. */
static void restore_stdout(int saved_fd)
{
fflush(stdout);
dup2(saved_fd, STDOUT_FILENO);
close(saved_fd);
}
/* Helper: restore stderr from saved fd. */
static void restore_stderr(int saved_fd)
{
fflush(stderr);
dup2(saved_fd, STDERR_FILENO);
close(saved_fd);
}
/* Helper: redirect stdin from a file, returning saved stdin fd. */
static int redirect_stdin_from_file(const char* path)
{
int fd = open(path, O_RDONLY);
if (fd < 0) return -1;
int saved = dup(STDIN_FILENO);
if (saved < 0) { close(fd); return -1; }
if (dup2(fd, STDIN_FILENO) < 0) {
close(fd);
close(saved);
return -1;
}
close(fd);
return saved;
}
extern char const *separator_str;
extern int lineno_width;
extern char const *lineno_format;
extern char *print_no_line_fmt;
extern intmax_t starting_line_number;
extern intmax_t page_incr;
extern bool reset_numbers;
extern char const *body_type;
extern char const *header_type;
extern char const *footer_type;
extern char const *current_type;
extern bool have_read_stdin;
/* Keep track to free the allocated format string per test. */
static char* allocated_print_no_line_fmt = NULL;
/* Initialize core globals before each test. */
void setUp(void) {
/* Defaults tailored for tests */
separator_str = "\t";
lineno_width = 2;
lineno_format = FORMAT_RIGHT_NOLZ;
starting_line_number = 1;
page_incr = 1;
reset_numbers = true;
/* Ensure default numbering style is body "t". */
body_type = "t";
header_type = "n";
footer_type = "n";
current_type = body_type;
/* Build the "no number" prefix: spaces + separator. */
if (allocated_print_no_line_fmt) {
free(allocated_print_no_line_fmt);
allocated_print_no_line_fmt = NULL;
}
allocated_print_no_line_fmt = make_print_no_line_fmt_buf(lineno_width, separator_str);
print_no_line_fmt = allocated_print_no_line_fmt;
/* Set initial line number. */
reset_lineno();
/* Clear have_read_stdin for tests that inspect it. */
have_read_stdin = false;
}
void tearDown(void) {
if (allocated_print_no_line_fmt) {
free(allocated_print_no_line_fmt);
allocated_print_no_line_fmt = NULL;
print_no_line_fmt = NULL;
}
}
/* Test 1: Simple numbering of a regular file with two non-empty lines. */
static void test_nl_file_numbers_simple_file(void)
{
const char* input = "line1\nline2\n";
char* in_path = create_temp_file_with(input);
TEST_ASSERT_NOT_NULL(in_path);
char* out_path = NULL;
int saved_stdout = redirect_stdout_to_temp(&out_path);
TEST_ASSERT_TRUE(saved_stdout >= 0);
TEST_ASSERT_NOT_NULL(out_path);
/* Call under test while stdout is redirected: no Unity asserts here. */
bool ok = nl_file(in_path);
/* Restore stdout before asserting. */
restore_stdout(saved_stdout);
/* Read captured output and assert. */
char* out = read_entire_file(out_path);
TEST_ASSERT_TRUE(ok);
TEST_ASSERT_NOT_NULL(out);
const char* expected = " 1\tline1\n 2\tline2\n";
TEST_ASSERT_EQUAL_STRING(expected, out);
free(out);
unlink(out_path);
free(out_path);
unlink(in_path);
free(in_path);
}
/* Test 2: Behavior with blank lines in 't' mode (number only non-empty lines). */
static void test_nl_file_t_mode_blank_lines(void)
{
const char* input = "\nX\n\n";
char* in_path = create_temp_file_with(input);
TEST_ASSERT_NOT_NULL(in_path);
/* Ensure 't' mode. */
body_type = "t";
current_type = body_type;
reset_lineno();
char* out_path = NULL;
int saved_stdout = redirect_stdout_to_temp(&out_path);
TEST_ASSERT_TRUE(saved_stdout >= 0);
TEST_ASSERT_NOT_NULL(out_path);
bool ok = nl_file(in_path);
restore_stdout(saved_stdout);
char* out = read_entire_file(out_path);
TEST_ASSERT_TRUE(ok);
TEST_ASSERT_NOT_NULL(out);
const char* expected = " \t\n 1\tX\n \t\n";
TEST_ASSERT_EQUAL_STRING(expected, out);
free(out);
unlink(out_path);
free(out_path);
unlink(in_path);
free(in_path);
}
/* Test 3: Reading from stdin ("-") and have_read_stdin side-effect. */
static void test_nl_file_from_stdin_dash(void)
{
const char* input = "a\nb\n";
char* in_path = create_temp_file_with(input);
TEST_ASSERT_NOT_NULL(in_path);
/* Redirect stdin. */
int saved_stdin = redirect_stdin_from_file(in_path);
TEST_ASSERT_TRUE(saved_stdin >= 0);
char* out_path = NULL;
int saved_stdout = redirect_stdout_to_temp(&out_path);
TEST_ASSERT_TRUE(saved_stdout >= 0);
TEST_ASSERT_NOT_NULL(out_path);
/* Call under test while stdout redirected. */
bool ok = nl_file("-");
/* Restore stdout and stdin before assertions. */
restore_stdout(saved_stdout);
dup2(saved_stdin, STDIN_FILENO);
close(saved_stdin);
char* out = read_entire_file(out_path);
TEST_ASSERT_TRUE(ok);
TEST_ASSERT_TRUE(have_read_stdin);
TEST_ASSERT_NOT_NULL(out);
const char* expected = " 1\ta\n 2\tb\n";
TEST_ASSERT_EQUAL_STRING(expected, out);
free(out);
unlink(out_path);
free(out_path);
unlink(in_path);
free(in_path);
}
/* Test 4: Nonexistent file should return false and not write to stdout. */
static void test_nl_file_nonexistent_returns_false(void)
{
const char* bad_path = "/tmp/this_file_should_not_exist_nl_test_XXXX";
/* Capture stdout (should be empty) and stderr (to avoid noisy output). */
char* out_path = NULL;
int saved_stdout = redirect_stdout_to_temp(&out_path);
TEST_ASSERT_TRUE(saved_stdout >= 0);
TEST_ASSERT_NOT_NULL(out_path);
char* err_path = NULL;
int saved_stderr = redirect_stderr_to_temp(&err_path);
TEST_ASSERT_TRUE(saved_stderr >= 0);
TEST_ASSERT_NOT_NULL(err_path);
/* Call under test while redirected. */
bool ok = nl_file(bad_path);
/* Restore I/O before asserting. */
restore_stdout(saved_stdout);
restore_stderr(saved_stderr);
/* stdout should be empty and ok should be false. */
char* out = read_entire_file(out_path);
TEST_ASSERT_FALSE(ok);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_STRING("", out);
free(out);
unlink(out_path);
free(out_path);
unlink(err_path);
free(err_path);
}
/* Test 5: Starting line number and increment respected. */
static void test_nl_file_starting_and_increment(void)
{
starting_line_number = 5;
page_incr = 3;
reset_numbers = true;
reset_lineno();
/* Keep 't' mode. */
body_type = "t";
current_type = body_type;
const char* input = "aa\nbb\n";
char* in_path = create_temp_file_with(input);
TEST_ASSERT_NOT_NULL(in_path);
char* out_path = NULL;
int saved_stdout = redirect_stdout_to_temp(&out_path);
TEST_ASSERT_TRUE(saved_stdout >= 0);
TEST_ASSERT_NOT_NULL(out_path);
bool ok = nl_file(in_path);
restore_stdout(saved_stdout);
char* out = read_entire_file(out_path);
TEST_ASSERT_TRUE(ok);
TEST_ASSERT_NOT_NULL(out);
const char* expected = " 5\taa\n 8\tbb\n";
TEST_ASSERT_EQUAL_STRING(expected, out);
free(out);
unlink(out_path);
free(out_path);
unlink(in_path);
free(in_path);
}
/* Test 6: 'a' mode with blank_join > 1 numbers grouped blank lines appropriately. */
static void test_nl_file_a_mode_blank_join(void)
{
/* Switch to 'a' mode and set blank_join. */
body_type = "a";
current_type = body_type;
extern intmax_t blank_join; /* declared in program */
blank_join = 2;
reset_numbers = true;
starting_line_number = 1;
reset_lineno();
const char* input = "\n\n\nx\n\n";
char* in_path = create_temp_file_with(input);
TEST_ASSERT_NOT_NULL(in_path);
char* out_path = NULL;
int saved_stdout = redirect_stdout_to_temp(&out_path);
TEST_ASSERT_TRUE(saved_stdout >= 0);
TEST_ASSERT_NOT_NULL(out_path);
bool ok = nl_file(in_path);
restore_stdout(saved_stdout);
char* out = read_entire_file(out_path);
TEST_ASSERT_TRUE(ok);
TEST_ASSERT_NOT_NULL(out);
/* Expected:
blank -> no number
second blank -> numbered "1"
third blank -> no number
'x' -> numbered "2"
final blank -> no number
*/
const char* expected =
" \t\n"
" 1\t\n"
" \t\n"
" 2\tx\n"
" \t\n";
TEST_ASSERT_EQUAL_STRING(expected, out);
free(out);
unlink(out_path);
free(out_path);
unlink(in_path);
free(in_path);
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_nl_file_numbers_simple_file);
RUN_TEST(test_nl_file_t_mode_blank_lines);
RUN_TEST(test_nl_file_from_stdin_dash);
RUN_TEST(test_nl_file_nonexistent_returns_false);
RUN_TEST(test_nl_file_starting_and_increment);
RUN_TEST(test_nl_file_a_mode_blank_join);
return UNITY_END();
} |