]> git.ozlabs.org Git - ccan/blobdiff - ccan/string/test/run.c
test case for grab_file
[ccan] / ccan / string / test / run.c
index fded0e49a3ebde3044ea9270cd02688055a7f0c3..f68e7ec56c5c886293be9b5f8e4c086e6076ebfb 100644 (file)
@@ -1,12 +1,15 @@
+#include "string/string.h"
 #include <stdlib.h>
 #include <stdio.h>
-#include "string/string.h"
+#include "string/string.c"
 #include "tap/tap.h"
 
 /* FIXME: ccanize */
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
 
-static char *substrings[] = { "far", "bar", "baz", "b", "ba", "z", "ar" };
+static char *substrings[] = { "far", "bar", "baz", "b", "ba", "z", "ar", NULL };
+
+#define NUM_SUBSTRINGS (ARRAY_SIZE(substrings) - 1)
 
 static char *strdup_rev(const char *s)
 {
@@ -21,11 +24,15 @@ static char *strdup_rev(const char *s)
 int main(int argc, char *argv[])
 {
        unsigned int i, j, n;
-       char *strings[ARRAY_SIZE(substrings) * ARRAY_SIZE(substrings)];
+       char **split, *str;
+       void *ctx;
+       char *strings[NUM_SUBSTRINGS * NUM_SUBSTRINGS];
+       int length;
+       struct stat st;
 
        n = 0;
-       for (i = 0; i < ARRAY_SIZE(substrings); i++) {
-               for (j = 0; j < ARRAY_SIZE(substrings); j++) {
+       for (i = 0; i < NUM_SUBSTRINGS; i++) {
+               for (j = 0; j < NUM_SUBSTRINGS; j++) {
                        strings[n] = malloc(strlen(substrings[i])
                                            + strlen(substrings[j]) + 1);
                        sprintf(strings[n++], "%s%s",
@@ -33,7 +40,7 @@ int main(int argc, char *argv[])
                }
        }
 
-       plan_tests(n * n * 5);
+       plan_tests(n * n * 5 + 19);
        for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                        unsigned int k, identical = 0;
@@ -73,5 +80,53 @@ int main(int argc, char *argv[])
                        }
                }
        }
+
+       split = strsplit(NULL, "hello  world", " ", &n);
+       ok1(n == 3);
+       ok1(streq(split[0], "hello"));
+       ok1(streq(split[1], ""));
+       ok1(streq(split[2], "world"));
+       ok1(split[3] == NULL);
+       talloc_free(split);
+
+       split = strsplit(NULL, "hello  world", " ", NULL);
+       ok1(streq(split[0], "hello"));
+       ok1(streq(split[1], ""));
+       ok1(streq(split[2], "world"));
+       ok1(split[3] == NULL);
+       talloc_free(split);
+
+       split = strsplit(NULL, "hello  world", "o ", NULL);
+       ok1(streq(split[0], "hell"));
+       ok1(streq(split[1], ""));
+       ok1(streq(split[2], ""));
+       ok1(streq(split[3], "w"));
+       ok1(streq(split[4], "rld"));
+       ok1(split[5] == NULL);
+
+       ctx = split;
+       split = strsplit(ctx, "hello  world", "o ", NULL);
+       ok1(talloc_parent(split) == ctx);
+       talloc_free(ctx);
+
+       str = strjoin(NULL, substrings, ", ");
+       ok1(streq(str, "far, bar, baz, b, ba, z, ar, "));
+       ctx = str;
+       str = strjoin(ctx, substrings, "");
+       ok1(streq(str, "farbarbazbbazar"));
+       ok1(talloc_parent(str) == ctx);
+       talloc_free(ctx);
+
+       str = grab_file(NULL, "ccan/string/test/run-grab.c");
+       split = strsplit(NULL, str, "\n", NULL);
+       length = strlen(split[0]);
+       ok1(streq(split[0], "/* This is test for grab_file() function */"));
+       for(i = 1; split[i]; i++)       
+               length += strlen(split[i]);
+       ok1(streq(split[i-1], "/* End of grab_file() test */"));
+       if (stat("ccan/string/test/run-grab.c", &st) != 0) 
+               err(1, "Could not stat self");
+       ok1(st.st_size == length);
+               
        return exit_status();
 }