]> git.ozlabs.org Git - ccan/blob - ccan/string/test/run-grab.c
merge
[ccan] / ccan / string / test / run-grab.c
1 /* This is test for grab_file() function
2  *
3  * Example:
4  * 
5  * void *grab_file(const void *ctx, const char *filename)
6  *      {
7  *      int fd; 
8  *      char *buffer;
9  * 
10  *      if (streq(filename, "-"))
11  *              fd = dup(STDIN_FILENO);
12  *      else
13  *              fd = open(filename, O_RDONLY, 0); 
14  *
15  *      if (fd < 0)
16  *              return NULL; 
17  *
18  *      buffer = grab_fd(ctx, fd);
19  *      close_noerr(fd);
20  *      return buffer;
21  *      }
22  */
23
24 #include        <stdlib.h>
25 #include        <stdio.h>
26 #include        <err.h>
27 #include        <sys/stat.h>
28 #include        "string/string.h"
29 #include        "string/string.c"
30 #include        "tap/tap.h"
31
32 int 
33 main(int argc, char *argv[])
34 {
35         unsigned int    i;
36         char            **split, *str;
37         int             length;
38         struct          stat st;
39
40         str = grab_file(NULL, "ccan/string/test/run-grab.c");
41         split = strsplit(NULL, str, "\n", NULL);
42         length = strlen(split[0]);
43         ok1(streq(split[0], "/* This is test for grab_file() function"));
44         for (i = 1; split[i]; i++)      
45                 length += strlen(split[i]);
46         ok1(streq(split[i-1], "/* End of grab_file() test */"));
47         if (stat("ccan/string/test/run-grab.c", &st) != 0) 
48                 err(1, "Could not stat self");
49         ok1(st.st_size == length + i);
50         
51         return 0;
52 }
53
54 /* End of grab_file() test */