]> pd.if.org Git - lice/blob - tests/init.c
autocommit for files dated 2014-11-17 20:13:36
[lice] / tests / init.c
1 // initializers
2
3 void testi(int *recieved, int *wanted, int length) {
4     for (int i = 0; i < length; i++)
5         expecti(recieved[i], wanted[i]);
6 }
7
8 void tests(short *recieved, short *wanted, int length) {
9     for (int i = 0; i < length; i++)
10         expects(recieved[i], wanted[i]);
11 }
12
13 void testarray(void) {
14     int a[]     = { 1, 2, 3 };
15     int b[]     = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
16     int c[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
17     int d[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
18     int e[]     = { 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0 };
19     int f[4][3]  = { { 1 }, { 2 }, { 3 }, { 4 } };
20
21     expecti(a[0], 1);
22     expecti(a[1], 2);
23     expecti(a[2], 3);
24
25     testi(b, c, 9);
26     testi(c, d, 9);
27     testi(e, f, 12);
28
29     short g[] = {
30         1, 0, 0, 0, 0,
31         0, 2, 3, 0, 0,
32         0, 0, 4, 5, 6
33     };
34
35     short h[4][3][2] = {
36         { 1 },
37         { 2, 3 },
38         { 4, 5, 6 }
39     };
40
41     tests(g, h, 15);
42 }
43
44 void teststruct(void) {
45     int a[] = {
46         1, 0, 0, 0,
47         2, 0, 0, 0
48     };
49
50     struct {
51         int a[3];
52         int b;
53     } data[] = { { 1 }, 2 };
54
55     testi(&data, a, 8);
56 }
57
58 void testnesting(void) {
59     struct {
60         struct {
61             struct {
62                 int a;
63                 int b;
64             } a;
65
66             struct {
67                 char a[8];
68             } b;
69         } a;
70     } data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
71
72     expecti(data.a.a.a,    1);
73     expecti(data.a.a.b,    2);
74     expecti(data.a.b.a[0], 3);
75     expecti(data.a.b.a[1], 4);
76     expecti(data.a.b.a[2], 5);
77     expecti(data.a.b.a[3], 6);
78     expecti(data.a.b.a[4], 7);
79     expecti(data.a.b.a[5], 8);
80     expecti(data.a.b.a[6], 9);
81     expecti(data.a.b.a[7], 10);
82 }
83
84 void testdesignated(void) {
85     struct { int a, b, c; } a = {
86         .a = 1024,
87         .b = 2048,
88         .c = 4096,
89     };
90
91     struct { int a, b, c; } b = {
92         .c = 4096,
93         .b = 2048,
94         .a = 1024
95     };
96
97     struct { int a, b, c; } c = {
98         .b = 2048,
99         4096, // should be c
100         .a = 1024
101     };
102
103     expecti(a.a, 1024);
104     expecti(a.b, 2048);
105     expecti(a.c, 4096);
106     expecti(b.a, 1024);
107     expecti(b.b, 2048);
108     expecti(b.c, 4096);
109     expecti(c.a, 1024);
110     expecti(c.b, 2048);
111     expecti(c.c, 4096);
112
113     // array designated initializers
114     int array[3] = { [1] = 1024 };
115     expecti(array[0], 0);
116     expecti(array[1], 1024);
117     expecti(array[2], 0);
118
119     // for structs too
120     struct {
121         int a, b;
122     } data[2] = { [1] = { 1, 2 } };
123
124     expecti(data[1].a, 1);
125     expecti(data[1].b, 2);
126     expecti(data[0].a, 0);
127     expecti(data[0].b, 0);
128
129     // splatted too
130     struct {
131         int a, b;
132     } data2[3] = { [1] = 10, 20, 30, 40 };
133     expecti(data2[0].a, 0);
134     expecti(data2[0].b, 0);
135     expecti(data2[1].a, 10);
136     expecti(data2[1].b, 20);
137     expecti(data2[2].a, 30);
138     expecti(data2[2].b, 40);
139
140     struct {
141         struct {
142             int a, b;
143         } c[3];
144     } again[] = {
145         [1].c[0].b = 10, 20, 30, 40, 50,
146         [0].c[2].b = 60, 70
147     };
148     expecti(10, again[1].c[0].b);
149     expecti(20, again[1].c[1].a);
150     expecti(30, again[1].c[1].b);
151     expecti(40, again[1].c[2].a);
152     expecti(50, again[1].c[2].b);
153     expecti(60, again[0].c[2].b);
154     expecti(70, again[1].c[0].a);
155
156     int A[][3] = { [0][0] = 10, [1][0] = 20 };
157     struct { int a, b[3]; } B = { .a = 10, .b[0] = 20, .b[1] = 30 };
158
159     expecti(A[0][0], 10);
160     expecti(A[1][0], 20);
161     expecti(B.a, 10);
162     expecti(B.b[0], 20);
163     expecti(B.b[1], 30);
164     expecti(B.b[2], 0);
165 }
166
167 void testorder(void) {
168     int order[4] = { [2] = 30, [0] = 10, 20 };
169
170     expecti(order[0], 10);
171     expecti(order[1], 20);
172     expecti(order[2], 30);
173 }
174
175 void testzero(void) {
176     struct { int a, b; } a = { 1024 };
177     struct { int a, b; } b = { .b = 1024 };
178
179     expecti(a.a, 1024);
180     expecti(a.b, 0);
181     expecti(b.a, 0);
182     expecti(b.b, 1024);
183 }
184
185 void testtypedef(void) {
186     typedef int array[];
187     array a = { 1, 2, 3 };
188     array b = { 4, 5, 6 };
189
190     expecti(sizeof(a) / sizeof(*a), 3);
191     expecti(sizeof(b) / sizeof(*b), 3);
192
193     expecti(a[0], 1);
194     expecti(a[1], 2);
195     expecti(a[2], 3);
196     expecti(b[0], 4);
197     expecti(b[1], 5);
198     expecti(b[2], 6);
199 }
200
201 int main(void) {
202     testarray();
203     testnesting();
204     teststruct();
205     testdesignated();
206     testzero();
207     testtypedef();
208     testorder();
209
210     int primitive = { 1024 };
211     expecti(primitive, 1024);
212
213     // test odd string initializers
214     char f[] = "abc";     expectstr(f, "abc");
215     char g[] = { "cba" }; expectstr(g, "cba");
216
217     return 0;
218 }