1 module it.c.compile.struct_;
2 
3 import it;
4 
5 @("simple int struct")
6 @safe unittest {
7     shouldCompile(
8         C(
9             q{
10                 struct Foo { int i; };
11             }
12         ),
13         D(
14             q{
15                 auto f = Foo(5);
16                 static assert(f.sizeof == 4, "Wrong sizeof for Foo");
17             }
18         )
19     );
20 }
21 
22 @("simple double struct")
23 @safe unittest {
24     shouldCompile(
25         C(
26             q{
27                 struct Bar { double d; };
28             }
29         ),
30 
31         D(
32             q{
33                 auto b = Bar(33.3);
34                 static assert(b.sizeof == 8, "Wrong sizeof for Bar");
35             }
36         )
37     );
38 }
39 
40 
41 @("Outer struct with Inner")
42 @safe unittest {
43     shouldCompile(
44         C(
45             q{
46                 struct Outer {
47                     struct Inner {
48                         int x;
49                     } inner;
50                 };
51             }
52         ),
53         D(
54             q{
55                 auto o = Outer(Outer.Inner(42));
56                 static assert(o.sizeof == 4, "Wrong sizeof for Outer");
57             }
58         )
59     );
60 }
61 
62 @("typedef struct with name")
63 @safe unittest {
64     shouldCompile(
65         C(
66             q{
67                 typedef struct TypeDefd_ {
68                     int i;
69                     double d;
70                 } TypeDefd;
71             }
72         ),
73         D(
74             q{
75                 {
76                     auto t = TypeDefd_(42, 33.3);
77                     static assert(t.sizeof == 16, "Wrong sizeof for TypeDefd_");
78                 }
79                 {
80                     auto t = TypeDefd(42, 33.3);
81                     static assert(t.sizeof == 16, "Wrong sizeof for TypeDefd");
82                 }
83             }
84         )
85     );
86 }
87 
88 @("typedef struct with no name")
89 @safe unittest {
90     shouldCompile(
91         C(
92             q{
93                 typedef struct {
94                     int x, y, z;
95                 } Nameless1;
96 
97                 typedef struct {
98                     double d;
99                 } Nameless2;
100             }
101         ),
102         D(
103             q{
104                 auto n1 = Nameless1(2, 3, 4);
105                 static assert(n1.sizeof == 12, "Wrong sizeof for Nameless1");
106 
107                 auto n2 = Nameless2(33.3);
108                 static assert(n2.sizeof == 8, "Wrong sizeof for Nameless2");
109             }
110         )
111     );
112 }
113 
114 @("typedef before struct declaration")
115 @safe unittest {
116     shouldCompile(
117         C(
118             q{
119                 typedef struct A B;
120                 struct A { int a; };
121             }
122         ),
123         D(
124             q{
125                 auto a = A(42);
126                 auto b = B(77);
127             }
128         )
129     );
130 }
131 
132 @("fsid_t")
133 @safe unittest {
134     shouldCompile(
135         C(
136             `
137                 #define __FSID_T_TYPE struct { int __val[2]; }
138                 typedef  __FSID_T_TYPE __fsid_t;
139                 typedef __fsid_t fsid_t;
140             `
141         ),
142         D(
143             q{
144                 fsid_t foo;
145                 foo.__val[0] = 2;
146                 foo.__val[1] = 3;
147             }
148         )
149     );
150 }
151 
152 
153 @("fd_set")
154 @safe unittest {
155 
156     with(immutable IncludeSandbox()) {
157 
158         writeFile("system.h",
159                   `
160                       #define __FD_SETSIZE 1024
161                       typedef long int __fd_mask;
162                       #define __NFDBITS (8 * (int) sizeof (__fd_mask))
163 
164                       typedef struct
165                       {
166                        #ifdef __USE_XOPEN
167                           __fd_mask fds_bits[__FD_SETSIZE / __NFDBITS];
168                        # define __FDS_BITS(set) ((set)->fds_bits)
169                        #else
170                           __fd_mask __fds_bits[__FD_SETSIZE / __NFDBITS];
171                        # define __FDS_BITS(set) ((set)->__fds_bits)
172                        #endif
173                       } fd_set;
174                   `);
175 
176 
177         writeFile("header.h",
178                   `
179                       #include "system.h"
180                   `);
181 
182         const dppFileName = "foo.dpp";
183         writeFile("foo.dpp",
184                   `
185                       #include "header.h"
186                       void func() {
187                           fd_set foo;
188                           foo.__fds_bits[0] = 5;
189                       }
190                   `);
191 
192 
193         runPreprocessOnly("foo.dpp");
194         shouldCompile("foo.d");
195     }
196 }
197 
198 @("multiple declarations")
199 @safe unittest {
200     shouldCompile(
201         C(
202             q{
203                 struct Struct;
204                 struct Struct;
205                 struct OtherStruct;
206                 struct Struct { int x, y, z; };
207             }
208         ),
209         D(
210             q{
211                 Struct s;
212                 s.x = 42;
213                 s.y = 33;
214                 s.z = 77;
215                 static assert(!__traits(compiles, OtherStruct()));
216             }
217         )
218     );
219 }
220 
221 @("anonymous struct var")
222 @safe unittest {
223     shouldCompile(
224         C(`struct { int i; } var;`),
225         D(
226             q{
227                 var.i = 42;
228             }
229         )
230     );
231 }
232 
233 @("typedef anonymous struct var")
234 @safe unittest {
235     shouldCompile(
236         C(`
237               typedef struct { int i; } mystruct;
238               mystruct var;
239           `),
240         D(
241             q{
242                 var.i = 42;
243             }
244         )
245     );
246 }