#include "../../unity/unity.h" #include #include #include #include #include /* Access to program internals (globals/functions) is available because this file is included into the program source. We reference the following: - write_block - struct tspec, spec, n_specs, n_specs_allocated - bytes_per_block, abbreviate_duplicate_blocks, format_address, address_pad_len - enums and helpers: FLOAT_DOUBLE, FLOATING_POINT, print_double, format_address_none - width_bytes[] is used only by write_block; we ensure spec[i].size is valid. */ /* Prototypes from the main program (visible due to inclusion): */ extern void write_block (intmax_t current_offset, idx_t n_bytes, char const *prev_block, char const *curr_block); extern void format_address_none (intmax_t, char); extern void print_double (idx_t fields, idx_t blank, void const *data, const char *fmt, int width, idx_t pad); /* Globals declared in the program; we redeclare as extern only. */ extern struct tspec *spec; extern idx_t n_specs; extern idx_t n_specs_allocated; extern idx_t bytes_per_block; extern bool abbreviate_duplicate_blocks; extern int address_pad_len; extern void (*format_address) (intmax_t, char); /* Helper: configure a spec list where each spec has size FLOAT_DOUBLE (so datum width is usually > bytes_per_block when bytes_per_block is small), print_function set to print_double, and no trailer. */ static void configure_spec_zero_fields(int count) { /* Free any previous spec to avoid leaks if tests reuse. */ if (spec) { free(spec); spec = NULL; } n_specs_allocated = 0; spec = (struct tspec *)calloc((size_t)count, sizeof(*spec)); TEST_ASSERT_NOT_NULL_MESSAGE(spec, "Failed to allocate spec array"); n_specs = (idx_t)count; for (int i = 0; i < count; i++) { spec[i].fmt = FLOATING_POINT; spec[i].size = FLOAT_DOUBLE; /* width_bytes[FLOAT_DOUBLE] exists */ spec[i].print_function = print_double; /* won't be called if fields=0 */ spec[i].field_width = 0; spec[i].pad_width = 0; spec[i].hexl_mode_trailer = false; spec[i].fmt_string[0] = '\0'; } } /* Simple stdout capture utility respecting Unity rule: do not assert while stdout is redirected. */ typedef struct { FILE *cap_file; int saved_fd; int cap_fd; int err; /* 0 ok, else errno-like */ } Capture; static void capture_begin(Capture *c) { memset(c, 0, sizeof(*c)); fflush(stdout); c->cap_file = tmpfile(); if (!c->cap_file) { c->err = errno ? errno : -1; return; } c->cap_fd = fileno(c->cap_file); if (c->cap_fd < 0) { c->err = errno ? errno : -2; return; } c->saved_fd = dup(fileno(stdout)); if (c->saved_fd < 0) { c->err = errno ? errno : -3; return; } if (dup2(c->cap_fd, fileno(stdout)) < 0) { c->err = errno ? errno : -4; /* best-effort restore */ (void)dup2(c->saved_fd, fileno(stdout)); return; } } static char *capture_end(Capture *c) { fflush(stdout); /* Restore stdout first so we can use Unity assertions after this. */ if (c->saved_fd >= 0) (void)dup2(c->saved_fd, fileno(stdout)); if (c->saved_fd >= 0) { close(c->saved_fd); c->saved_fd = -1; } if (!c->cap_file) { return strdup(""); } /* Read captured content */ fseek(c->cap_file, 0, SEEK_END); long len = ftell(c->cap_file); if (len < 0) len = 0; fseek(c->cap_file, 0, SEEK_SET); char *buf = (char *)malloc((size_t)len + 1); if (!buf) { fclose(c->cap_file); return strdup(""); } size_t n = fread(buf, 1, (size_t)len, c->cap_file); buf[n] = '\0'; fclose(c->cap_file); c->cap_file = NULL; return buf; } void setUp(void) { /* Reset easily controllable globals for each test. */ /* We avoid influencing write_block's internal static state here. */ format_address = format_address_none; /* deterministic, no address text */ address_pad_len = 0; /* no extra spaces for later specs */ abbreviate_duplicate_blocks = true; /* tests set as needed */ bytes_per_block = 4; /* small, makes fields=0 with double */ /* Ensure a fresh spec is configured where tests expect it. Some tests will reconfigure as needed. */ configure_spec_zero_fields(1); } void tearDown(void) { /* Free any allocated spec and reset counts. */ if (spec) { free(spec); spec = NULL; } n_specs = 0; n_specs_allocated = 0; /* Do not attempt to reset write_block's internal static state, not accessible. */ } /* Test 1: Duplicate elision sequence. Must run first so write_block's internal 'first' static is true at the start of this sequence. */ void test_write_block_duplicate_elision_sequence(void) { /* Ensure minimal deterministic output: one spec, no address, no fields. */ format_address = format_address_none; address_pad_len = 0; abbreviate_duplicate_blocks = true; bytes_per_block = 4; /* sizeof(double) > 4 on typical platforms */ configure_spec_zero_fields(1); char A[4] = {1,2,3,4}; char B[4] = {9,8,7,6}; Capture cap; capture_begin(&cap); /* No Unity asserts while capturing. */ write_block(0, 4, A, A); /* first call: prints a newline */ write_block(4, 4, A, A); /* identical full block: prints "*\n" */ write_block(8, 4, A, A); /* identical again: suppressed entirely */ write_block(12, 4, A, B); /* different: prints a newline */ char *out = capture_end(&cap); TEST_ASSERT_EQUAL_INT_MESSAGE(0, cap.err, "capture_begin failed"); TEST_ASSERT_NOT_NULL(out); TEST_ASSERT_EQUAL_STRING("\n*\n\n", out); free(out); } /* Test 2: When abbreviate_duplicate_blocks is disabled, even identical full blocks are printed normally (no '*' elision). */ void test_write_block_no_elision_when_disabled(void) { abbreviate_duplicate_blocks = false; format_address = format_address_none; address_pad_len = 0; bytes_per_block = 4; configure_spec_zero_fields(1); char A[4] = {1,1,1,1}; Capture cap; capture_begin(&cap); write_block(0, 4, A, A); write_block(4, 4, A, A); char *out = capture_end(&cap); TEST_ASSERT_EQUAL_INT_MESSAGE(0, cap.err, "capture_begin failed"); TEST_ASSERT_NOT_NULL(out); TEST_ASSERT_EQUAL_STRING("\n\n", out); free(out); } /* Test 3: Partial blocks (n_bytes < bytes_per_block) are never elided, even if identical and abbreviate_duplicate_blocks is true. */ void test_write_block_no_elision_when_partial_block(void) { abbreviate_duplicate_blocks = true; format_address = format_address_none; address_pad_len = 0; bytes_per_block = 4; configure_spec_zero_fields(1); char A[4] = {5,5,5,5}; Capture cap; capture_begin(&cap); write_block(0, 3, A, A); /* partial */ write_block(3, 3, A, A); /* partial and identical */ char *out = capture_end(&cap); TEST_ASSERT_EQUAL_INT_MESSAGE(0, cap.err, "capture_begin failed"); TEST_ASSERT_NOT_NULL(out); TEST_ASSERT_EQUAL_STRING("\n\n", out); free(out); } /* Test 4: Multiple specs produce one line per spec. With zero fields and no address, this should be two newlines. Use different blocks to avoid any chance of elision depending on internal static state. */ void test_write_block_multiple_specs_two_lines(void) { abbreviate_duplicate_blocks = true; format_address = format_address_none; address_pad_len = 0; bytes_per_block = 4; configure_spec_zero_fields(2); char A[4] = {1,2,3,4}; char B[4] = {4,3,2,1}; Capture cap; capture_begin(&cap); write_block(0, 4, A, B); /* different so not elided */ char *out = capture_end(&cap); TEST_ASSERT_EQUAL_INT_MESSAGE(0, cap.err, "capture_begin failed"); TEST_ASSERT_NOT_NULL(out); TEST_ASSERT_EQUAL_STRING("\n\n", out); free(out); } /* Test 5: When there are multiple specs, subsequent specs print address_pad_len spaces before their content; with zero fields and no address, that means spaces then just a newline on spec>=1 lines. */ void test_write_block_address_pad_for_subsequent_specs(void) { abbreviate_duplicate_blocks = true; format_address = format_address_none; address_pad_len = 3; /* expect 3 spaces before second spec's newline */ bytes_per_block = 4; configure_spec_zero_fields(2); char P[4] = {7,7,7,7}; char Q[4] = {8,8,8,8}; /* different to avoid star elision */ Capture cap; capture_begin(&cap); write_block(0, 4, P, Q); char *out = capture_end(&cap); TEST_ASSERT_EQUAL_INT_MESSAGE(0, cap.err, "capture_begin failed"); TEST_ASSERT_NOT_NULL(out); /* First spec: just "\n"; second spec: three spaces then "\n" */ TEST_ASSERT_EQUAL_STRING("\n \n", out); free(out); } int main(void) { UNITY_BEGIN(); RUN_TEST(test_write_block_duplicate_elision_sequence); RUN_TEST(test_write_block_no_elision_when_disabled); RUN_TEST(test_write_block_no_elision_when_partial_block); RUN_TEST(test_write_block_multiple_specs_two_lines); RUN_TEST(test_write_block_address_pad_for_subsequent_specs); return UNITY_END(); }