]> git.ozlabs.org Git - ccan/blob - ccan/darray/test/testLits.h.template
tdb2: fix tdb_summary reports
[ccan] / ccan / darray / test / testLits.h.template
1 static int testdarray_from_lit(void);
2 static int testdarray_append_lit(void);
3 static int testdarray_prepend_lit(void);
4
5 static void testLits(void) {
6         testing(darray_from_lit);
7         ok1(testdarray_from_lit());
8         
9         testing(testdarray_append_lit);
10         ok1(testdarray_append_lit());
11         
12         testing(testdarray_prepend_lit);
13         ok1(testdarray_prepend_lit());
14 }
15
16 static int testdarray_from_lit(void) {
17         darray_char a = darray_new();
18         size_t testsPassed = 0;
19         size_t len = 0;
20         
21         @forEachRandomString
22                 /* Test @i */
23                 darray_from_lit(a, @str);
24                 len = strlen(@str);
25                 if (len != sizeof(@str)-1)
26                         goto end;
27                 if (a.size != len)
28                         goto end;
29                 if (a.size > a.alloc)
30                         goto end;
31                 if (strcmp(a.item, @str))
32                         goto end;
33                 darray_free(a);
34                 darray_init(a);
35                 testsPassed++;
36         @end
37         
38 end:
39         darray_free(a);
40         return testsPassed == @amount;
41 }
42
43 typedef struct {
44         char *item;
45         size_t size;
46 } testLits_string;
47
48 static int testdarray_append_lit(void) {
49         darray_char a = darray_new();
50         darray(testLits_string) strings = darray_new();
51         testLits_string *i;
52         size_t testsPassed = 0;
53         size_t oldSize;
54         testLits_string append;
55         size_t offs = 0;
56         
57         @forEachRandomString
58                 /* Test @i */
59                 append.size = sizeof(@str)-1;
60                 oldSize = a.size;
61                 darray_append_lit(a, @str);
62                 if (a.size != oldSize+append.size)
63                         goto end;
64                 if (a.size > a.alloc)
65                         goto end;
66                 if (a.item[a.size])
67                         goto end;
68                 if (memcmp(a.item+oldSize, @str, a.size-oldSize))
69                         goto end;
70                 append.item = strdup(@str);
71                 darray_append(strings, append);
72                 testsPassed++;
73         @end
74         
75         if (strings.size != @amount)
76                 goto end;
77         
78         darray_foreach(i, strings) {
79                 if (a.size-offs < i->size)
80                         goto end;
81                 if (memcmp(a.item+offs, i->item, i->size))
82                         goto end;
83                 offs += i->size;
84         };
85         
86         if (offs != a.size)
87                 goto end;
88         if (a.item[offs])
89                 goto end;
90         testsPassed++;
91         
92 end:
93         darray_free(a);
94         darray_foreach(i, strings)
95                 free(i->item);
96         darray_free(strings);
97         return testsPassed == @amount+1;
98 }
99
100 static int testdarray_prepend_lit(void) {
101         darray_char a = darray_new();
102         darray(testLits_string) strings = darray_new();
103         testLits_string *i;
104         size_t testsPassed = 0;
105         size_t oldSize;
106         testLits_string append;
107         size_t offs;
108         
109         @forEachRandomString
110                 /* Test @i */
111                 append.size = sizeof(@str)-1;
112                 oldSize = a.size;
113                 darray_prepend_lit(a, @str);
114                 if (a.size != oldSize+append.size)
115                         goto end;
116                 if (a.size > a.alloc)
117                         goto end;
118                 if (a.item[a.size])
119                         goto end;
120                 if (memcmp(a.item, @str, a.size-oldSize))
121                         goto end;
122                 append.item = strdup(@str);
123                 darray_append(strings, append);
124                 testsPassed++;
125         @end
126         
127         offs = a.size;
128         if (a.item[offs])
129                 goto end;
130         if (strings.size != @amount)
131                 goto end;
132         darray_foreach(i, strings) {
133                 if (offs < i->size)
134                         goto end;
135                 offs -= i->size;
136                 if (memcmp(a.item+offs, i->item, i->size))
137                         goto end;
138         };
139         if (offs)
140                 goto end;
141         testsPassed++;
142         
143 end:
144         darray_free(a);
145         darray_foreach(i, strings)
146                 free(i->item);
147         darray_free(strings);
148         return testsPassed == @amount+1;
149 }