File size: 9,395 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 |
#include "../../unity/unity.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
/* 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();
} |