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