1 /* This is test for grab_file() function
3 #include <ccan/grab_file/grab_file.h>
8 #include <ccan/grab_file/grab_file.c>
9 #include <ccan/tap/tap.h>
12 static char **strsplit(const void *ctx, const char *string, const char *delims)
15 unsigned int max = 64, num = 0;
17 lines = talloc_array(ctx, char *, max+1);
19 while (*string != '\0') {
20 unsigned int len = strcspn(string, delims);
21 lines[num] = talloc_array(lines, char, len + 1);
22 memcpy(lines[num], string, len);
23 lines[num][len] = '\0';
25 string += strspn(string, delims) ? 1 : 0;
27 lines = talloc_realloc(ctx, lines, char *, max*=2 + 1);
31 /* Shrink, so talloc_get_size works */
32 return talloc_realloc(ctx, lines, char *, num+1);
36 main(int argc, char *argv[])
43 str = grab_file(NULL, "test/run-grab.c", NULL);
44 split = strsplit(str, str, "\n");
45 length = strlen(split[0]);
46 ok1(!strcmp(split[0], "/* This is test for grab_file() function"));
47 for (i = 1; split[i]; i++)
48 length += strlen(split[i]);
49 ok1(!strcmp(split[i-1], "/* End of grab_file() test */"));
50 if (stat("test/run-grab.c", &st) != 0)
52 if (stat("ccan/grab_file/test/run-grab.c", &st) != 0)
53 err(1, "Could not stat self");
54 ok1(st.st_size == length + i);
60 /* End of grab_file() test */