]> git.ozlabs.org Git - ccan/blob - ccan/grab_file/test/run-grab.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / grab_file / test / run-grab.c
1 /* This is test for grab_file() function
2  */
3 #include <ccan/grab_file/grab_file.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <err.h>
7 #include <sys/stat.h>
8 #include <ccan/grab_file/grab_file.c>
9 #include <ccan/tap/tap.h>
10 #include <string.h>
11
12 static char **strsplit(const void *ctx, const char *string, const char *delims)
13 {
14         char **lines = NULL;
15         unsigned int max = 64, num = 0;
16
17         lines = talloc_array(ctx, char *, max+1);
18
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';
24                 string += len;
25                 string += strspn(string, delims) ? 1 : 0;
26                 if (++num == max)
27                         lines = talloc_realloc(ctx, lines, char *, max*=2 + 1);
28         }
29         lines[num] = NULL;
30
31         /* Shrink, so talloc_get_size works */
32         return talloc_realloc(ctx, lines, char *, num+1);
33 }
34
35 int 
36 main(int argc, char *argv[])
37 {
38         unsigned int    i;
39         char            **split, *str;
40         int             length;
41         struct          stat st;
42
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) 
51                 /* FIXME: ditto */
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);
55         talloc_free(str);
56         
57         return 0;
58 }
59
60 /* End of grab_file() test */