File size: 8,550 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 |
#include "../../unity/unity.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
/* We rely on the program's static globals and the target function being
visible here due to textual inclusion into the same translation unit. */
/* Helper to construct a line with given fields. Allocates memory for the
fields and their contents. */
static void build_line(struct line *ln, const char *const *vals, const idx_t *lens, idx_t n)
{
memset(ln, 0, sizeof(*ln));
ln->nfields = n;
ln->nfields_allocated = n;
if (n == 0) {
ln->fields = NULL;
return;
}
ln->fields = (struct field *)malloc(sizeof(struct field) * n);
for (idx_t i = 0; i < n; ++i) {
idx_t L = lens ? lens[i] : (idx_t)strlen(vals[i]);
char *buf = (char *)malloc((size_t)L);
if (L > 0) {
memcpy(buf, vals[i], (size_t)L);
}
ln->fields[i].beg = buf;
ln->fields[i].len = L;
}
}
/* Helper to free memory allocated by build_line. */
static void free_line(struct line *ln)
{
if (!ln) return;
for (idx_t i = 0; i < ln->nfields; ++i) {
free(ln->fields[i].beg);
ln->fields[i].beg = NULL;
ln->fields[i].len = 0;
}
free(ln->fields);
ln->fields = NULL;
ln->nfields = 0;
ln->nfields_allocated = 0;
}
/* Capture stdout output produced by calling prfields(line, join_field, autocount).
Returns a malloc'd NUL-terminated string on success, or NULL on failure. */
static char *capture_prfields_output(struct line const *line, idx_t join_field, idx_t autocount)
{
char *result = NULL;
FILE *tmp = tmpfile();
if (!tmp) {
return NULL;
}
int out_fd = fileno(stdout);
int saved_fd = dup(out_fd);
if (saved_fd < 0) {
fclose(tmp);
return NULL;
}
int tmp_fd = fileno(tmp);
/* Redirect stdout to tmp. */
fflush(stdout);
if (dup2(tmp_fd, out_fd) < 0) {
close(saved_fd);
fclose(tmp);
return NULL;
}
/* Call the target function while stdout is redirected. */
prfields(line, join_field, autocount);
/* Flush and read back the captured output. */
fflush(stdout);
long size = 0;
if (fseek(tmp, 0, SEEK_END) == 0) {
long endpos = ftell(tmp);
if (endpos >= 0) {
size = endpos;
(void)fseek(tmp, 0, SEEK_SET);
}
}
if (size < 0) size = 0;
result = (char *)malloc((size_t)size + 1);
if (!result) {
/* Restore stdout before returning. */
dup2(saved_fd, out_fd);
close(saved_fd);
fclose(tmp);
return NULL;
}
if (size > 0) {
size_t rd = fread(result, 1, (size_t)size, tmp);
result[rd] = '\0';
if (rd != (size_t)size) {
/* Restore stdout and report failure. */
dup2(saved_fd, out_fd);
close(saved_fd);
fclose(tmp);
free(result);
return NULL;
}
} else {
result[0] = '\0';
}
/* Restore stdout. */
dup2(saved_fd, out_fd);
close(saved_fd);
fclose(tmp);
return result;
}
void setUp(void) {
/* Default settings before each test. */
autoformat = false;
output_separator = " ";
output_seplen = 1;
empty_filler = NULL;
}
void tearDown(void) {
/* Nothing persistent to clean up across tests. */
}
/* Test: basic behavior, skip the join field in the middle. */
static void test_prfields_basic_skip_join_middle(void)
{
const char *vals[] = { "A", "B", "C" };
struct line ln;
build_line(&ln, vals, NULL, 3);
output_separator = ",";
output_seplen = (idx_t)strlen(output_separator);
autoformat = false;
empty_filler = NULL;
char *out = capture_prfields_output(&ln, (idx_t)1, (idx_t)0);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_STRING(",A,C", out);
free(out);
free_line(&ln);
}
/* Test: join_field out of range (greater than number of fields) -> no skip, all printed. */
static void test_prfields_join_field_out_of_range_high(void)
{
const char *vals[] = { "A", "B", "C" };
struct line ln;
build_line(&ln, vals, NULL, 3);
output_separator = ",";
output_seplen = 1;
autoformat = false;
char *out = capture_prfields_output(&ln, (idx_t)5, (idx_t)0);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_STRING(",A,B,C", out);
free(out);
free_line(&ln);
}
/* Test: no fields present -> no output. */
static void test_prfields_no_fields_no_output(void)
{
struct line ln;
build_line(&ln, NULL, NULL, 0);
output_separator = ",";
output_seplen = 1;
autoformat = false;
char *out = capture_prfields_output(&ln, (idx_t)0, (idx_t)0);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_STRING("", out);
free(out);
free_line(&ln);
}
/* Test: empty field replaced by empty_filler when printed (non-join field). */
static void test_prfields_empty_field_with_empty_filler(void)
{
const char *vals[] = { "A", "", "C" };
idx_t lens[] = { 1, 0, 1 }; /* explicit lengths to create an empty field */
struct line ln;
build_line(&ln, vals, lens, 3);
output_separator = ",";
output_seplen = 1;
autoformat = false;
empty_filler = "<E>";
/* Use join_field = 0 so fields 1 and 2 are printed. */
char *out = capture_prfields_output(&ln, (idx_t)0, (idx_t)0);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_STRING(",<E>,C", out);
free(out);
free_line(&ln);
}
/* Test: autoformat extends beyond existing fields and uses empty_filler for missing ones. */
static void test_prfields_autoformat_extends_and_fills(void)
{
const char *vals[] = { "A", "B" };
struct line ln;
build_line(&ln, vals, NULL, 2);
output_separator = ",";
output_seplen = 1;
autoformat = true;
empty_filler = "<E>";
/* join_field = 0 -> print indices 1..3; ln has only index 1, so 2 and 3 use filler */
char *out = capture_prfields_output(&ln, (idx_t)0, (idx_t)4);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_STRING(",B,<E>,<E>", out);
free(out);
free_line(&ln);
}
/* Test: autoformat limits output to autocount even if there are more fields; join field skipped. */
static void test_prfields_autoformat_limits(void)
{
const char *vals[] = { "A", "B", "C", "D" };
struct line ln;
build_line(&ln, vals, NULL, 4);
output_separator = ",";
output_seplen = 1;
autoformat = true;
empty_filler = NULL;
/* nfields considered = 3 (autocount). join_field = 2 => print 0 and 1 only. */
char *out = capture_prfields_output(&ln, (idx_t)2, (idx_t)3);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_STRING(",A,B", out);
free(out);
free_line(&ln);
}
/* Test: custom multi-character separator honors output_seplen. */
static void test_prfields_custom_separator_multichar(void)
{
const char *vals[] = { "X", "Y" };
struct line ln;
build_line(&ln, vals, NULL, 2);
output_separator = "::";
output_seplen = (idx_t)strlen(output_separator);
autoformat = false;
/* join_field = 1 => print only field 0 */
char *out = capture_prfields_output(&ln, (idx_t)1, (idx_t)0);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_STRING("::X", out);
free(out);
free_line(&ln);
}
/* Test: autoformat with missing fields and no empty_filler prints only separators for those fields. */
static void test_prfields_autoformat_no_empty_filler_only_separators(void)
{
const char *vals[] = { "A" };
struct line ln;
build_line(&ln, vals, NULL, 1);
output_separator = ",";
output_seplen = 1;
autoformat = true;
empty_filler = NULL;
/* join_field = 0; autocount=3 -> attempt to print fields 1 and 2 (both missing):
separators appear, but no filler text. */
char *out = capture_prfields_output(&ln, (idx_t)0, (idx_t)3);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_STRING(",,", out);
free(out);
free_line(&ln);
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_prfields_basic_skip_join_middle);
RUN_TEST(test_prfields_join_field_out_of_range_high);
RUN_TEST(test_prfields_no_fields_no_output);
RUN_TEST(test_prfields_empty_field_with_empty_filler);
RUN_TEST(test_prfields_autoformat_extends_and_fills);
RUN_TEST(test_prfields_autoformat_limits);
RUN_TEST(test_prfields_custom_separator_multichar);
RUN_TEST(test_prfields_autoformat_no_empty_filler_only_separators);
return UNITY_END();
} |