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