File size: 7,814 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
#include "../../unity/unity.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>

/* External/internal symbols from the program are visible here,
   since this file is included into the same translation unit. */

/* Forward declarations to silence potential warnings (they are defined above). */
static void split_file (void);

/* Test helpers state */
static int start_files_created = 0;
static char test_prefix[64] = {0};

/* Helper: create temp input file with given content and return strdup'ed path */
static char* create_temp_input_file(const char* content) {
  char templ[] = "/tmp/csplit_test_input_XXXXXX";
  int fd = mkstemp(templ);
  if (fd < 0) {
    perror("mkstemp");
    return NULL;
  }
  size_t len = strlen(content);
  if (len > 0) {
    ssize_t w = write(fd, content, len);
    if (w < 0 || (size_t)w != len) {
      close(fd);
      unlink(templ);
      perror("write temp input");
      return NULL;
    }
  }
  if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
    close(fd);
    unlink(templ);
    perror("lseek");
    return NULL;
  }
  close(fd);
  return strdup(templ);
}

/* Helper: read entire file into newly malloc'd buffer (null-terminated).
   Returns NULL on error. */
static char* read_file_contents(const char* path) {
  struct stat st;
  if (stat(path, &st) != 0)
    return NULL;
  size_t sz = (size_t)st.st_size;
  FILE* f = fopen(path, "rb");
  if (!f)
    return NULL;
  char* buf = (char*)malloc(sz + 1);
  if (!buf) {
    fclose(f);
    return NULL;
  }
  size_t r = fread(buf, 1, sz, f);
  fclose(f);
  if (r != sz) {
    free(buf);
    return NULL;
  }
  buf[sz] = '\0';
  return buf;
}

/* Helper: build filename for file index i using program's make_filename.
   Returns pointer to internal filename_space. */
static const char* outfile_name_for_index(int i) {
  /* make_filename writes into filename_space and returns it */
  return make_filename(i);
}

/* Reset input buffer state and related globals between tests */
static void reset_input_state(void) {
  /* Free any hold area from previous run */
  if (hold_area) {
    free(hold_area);
    hold_area = NULL;
  }
  hold_count = 0;
  have_read_eof = false;
  head = NULL;
  last_line_number = 0;
  current_line = 0;
}

/* Prepare environment before each test call to split_file */
static void prepare_environment(void) {
  /* Ensure signals set is empty for critical sections */
  sigemptyset(&caught_signals);

  /* Avoid printing byte counts to stdout */
  suppress_count = true;

  /* Do not auto-remove files on error or elide empty files in tests */
  remove_files = false;
  elide_empty_files = false;
  suppress_matched = false;

  /* Use numeric suffix with default digits */
  digits = 2;
  suffix = NULL;

  /* Ensure filename_space exists and is large enough */
  if (!filename_space) {
    filename_space = (char*)malloc(1024);
  }

  /* Use a stable test prefix unique(ish) to this process */
  if (test_prefix[0] == '\0') {
    snprintf(test_prefix, sizeof(test_prefix), "utcs_%ld_", (long)getpid());
  }
  prefix = test_prefix;

  /* Reset input stream state */
  reset_input_state();

  /* Start tracking created files from current state */
  start_files_created = files_created;
}

/* Cleanup files created during the test and restore files_created */
static void cleanup_created_files(void) {
  int end = files_created;
  for (int i = start_files_created; i < end; i++) {
    const char* name = outfile_name_for_index(i);
    unlink(name);
  }
  files_created = start_files_created;
}

/* Unity required hooks */
void setUp(void) {
  prepare_environment();
}

void tearDown(void) {
  cleanup_created_files();
}

/* Test 1: Numeric line-count split: single control "3"
   Input: a\nb\nc\nd\ne\n
   Expect: two files:
     00: "a\nb\n"
     01: "c\nd\ne\n"
*/
void test_split_file_linecount_basic(void) {
  const char* input = "a\nb\nc\nd\ne\n";
  char* path = create_temp_input_file(input);
  TEST_ASSERT_NOT_NULL_MESSAGE(path, "Failed to create temp input");

  set_input_file(path);

  /* Build controls: one numeric control lines_required=3 */
  control_used = 0;
  struct control* p = new_control_record();
  p->lines_required = 3; /* split before line 3 */
  p->argnum = 1; /* arbitrary */

  /* Execute */
  split_file();

  /* Verify two files created */
  int created = files_created - start_files_created;
  TEST_ASSERT_EQUAL_INT_MESSAGE(2, created, "Expected exactly 2 output files");

  const char* f0 = outfile_name_for_index(start_files_created + 0);
  const char* f1 = outfile_name_for_index(start_files_created + 1);

  char* c0 = read_file_contents(f0);
  char* c1 = read_file_contents(f1);

  TEST_ASSERT_NOT_NULL_MESSAGE(c0, "Failed to read first output file");
  TEST_ASSERT_NOT_NULL_MESSAGE(c1, "Failed to read second output file");

  TEST_ASSERT_EQUAL_STRING("a\nb\n", c0);
  TEST_ASSERT_EQUAL_STRING("c\nd\ne\n", c1);

  free(c0);
  free(c1);

  unlink(path);
  free(path);
}

/* Test 2: Regexp basic split: control '/^c$/'
   Input: a\nb\nc\nd\ne\n
   Expect: two files:
     00: "a\nb\n"
     01: "c\nd\ne\n"
*/
void test_split_file_regexp_basic(void) {
  const char* input = "a\nb\nc\nd\ne\n";
  char* path = create_temp_input_file(input);
  TEST_ASSERT_NOT_NULL_MESSAGE(path, "Failed to create temp input");

  set_input_file(path);

  /* Build controls: one regexp control '/^c$/' */
  control_used = 0;
  struct control* p = extract_regexp(1, false, "/^c$/");
  (void)p; /* silence unused warning in case */

  /* Execute */
  split_file();

  /* Verify two files created */
  int created = files_created - start_files_created;
  TEST_ASSERT_EQUAL_INT_MESSAGE(2, created, "Expected exactly 2 output files");

  const char* f0 = outfile_name_for_index(start_files_created + 0);
  const char* f1 = outfile_name_for_index(start_files_created + 1);

  char* c0 = read_file_contents(f0);
  char* c1 = read_file_contents(f1);

  TEST_ASSERT_NOT_NULL_MESSAGE(c0, "Failed to read first output file");
  TEST_ASSERT_NOT_NULL_MESSAGE(c1, "Failed to read second output file");

  TEST_ASSERT_EQUAL_STRING("a\nb\n", c0);
  TEST_ASSERT_EQUAL_STRING("c\nd\ne\n", c1);

  free(c0);
  free(c1);

  unlink(path);
  free(path);
}

/* Test 3: Regexp with suppress_matched: '/^c$/'
   suppress_matched = true
   Input: a\nb\nc\nd\ne\n
   Expect: two files:
     00: "a\nb\n"
     01: "d\ne\n"
   (matching line 'c' is suppressed)
*/
void test_split_file_regexp_suppress_matched(void) {
  const char* input = "a\nb\nc\nd\ne\n";
  char* path = create_temp_input_file(input);
  TEST_ASSERT_NOT_NULL_MESSAGE(path, "Failed to create temp input");

  set_input_file(path);

  /* Build controls: one regexp control '/^c$/' */
  control_used = 0;
  struct control* p = extract_regexp(1, false, "/^c$/");
  (void)p;

  /* Suppress matched lines */
  suppress_matched = true;

  /* Execute */
  split_file();

  /* Verify two files created */
  int created = files_created - start_files_created;
  TEST_ASSERT_EQUAL_INT_MESSAGE(2, created, "Expected exactly 2 output files");

  const char* f0 = outfile_name_for_index(start_files_created + 0);
  const char* f1 = outfile_name_for_index(start_files_created + 1);

  char* c0 = read_file_contents(f0);
  char* c1 = read_file_contents(f1);

  TEST_ASSERT_NOT_NULL_MESSAGE(c0, "Failed to read first output file");
  TEST_ASSERT_NOT_NULL_MESSAGE(c1, "Failed to read second output file");

  TEST_ASSERT_EQUAL_STRING("a\nb\n", c0);
  TEST_ASSERT_EQUAL_STRING("d\ne\n", c1);

  free(c0);
  free(c1);

  unlink(path);
  free(path);
}

int main(void) {
  UNITY_BEGIN();
  RUN_TEST(test_split_file_linecount_basic);
  RUN_TEST(test_split_file_regexp_basic);
  RUN_TEST(test_split_file_regexp_suppress_matched);
  return UNITY_END();
}