]>
Commit | Line | Data |
---|---|---|
5fee5ec3 | 1 | /* |
7da827c9 IB |
2 | TEST_OUTPUT: |
3 | --- | |
4 | hello | |
5 | hello | |
6 | ||
7 | ||
8 | ||
9 | hello | |
10 | hello | |
11 | --- | |
12 | ||
13 | RUN_OUTPUT: | |
14 | --- | |
15 | 31 | |
16 | 42 | |
17 | 53 | |
18 | 64 | |
19 | 75 | |
20 | 86 | |
21 | 97 | |
22 | 108 | |
23 | 119 | |
24 | 1210 | |
25 | 5 | |
26 | test4 | |
27 | Constructor | |
28 | Inside Scope | |
29 | Destructor | |
30 | hey | |
31 | Success | |
32 | --- | |
33 | */ | |
5fee5ec3 | 34 | import core.stdc.stdio; |
b4c522fa IB |
35 | |
36 | /*********************************************/ | |
37 | ||
38 | int foo(int x) | |
39 | { | |
40 | return mixin("x + 1"w); | |
41 | } | |
42 | ||
43 | void test1() | |
44 | { | |
45 | assert(foo(3) == 4); | |
46 | } | |
47 | ||
48 | /*********************************************/ | |
49 | ||
50 | void test2() | |
51 | { | |
52 | int j; | |
53 | mixin(" | |
54 | int x = 3; | |
55 | for (int i = 0; i < 10; i++) | |
5fee5ec3 | 56 | printf(\"%d%d\n\", x + i, ++j); |
b4c522fa IB |
57 | "); |
58 | assert(j == 10); | |
59 | } | |
60 | ||
61 | /*********************************************/ | |
62 | ||
63 | mixin("int abc3 = 5;"); | |
64 | ||
65 | void test3() | |
66 | { | |
5fee5ec3 | 67 | printf("%d\n", abc3); |
b4c522fa IB |
68 | assert(abc3 == 5); |
69 | } | |
70 | ||
71 | /*********************************************/ | |
72 | ||
73 | mixin(" | |
74 | void test4() | |
75 | { | |
5fee5ec3 | 76 | printf(\"test4\n\"); |
b4c522fa IB |
77 | " ~ "}"); |
78 | ||
79 | /*********************************************/ | |
80 | ||
81 | int x5; | |
82 | ||
5fee5ec3 | 83 | class Foo5 |
b4c522fa IB |
84 | { |
85 | this () | |
86 | { | |
5fee5ec3 | 87 | printf ("Constructor\n"); |
b4c522fa IB |
88 | assert(x5 == 0); |
89 | x5++; | |
90 | } | |
91 | ~this () | |
92 | { | |
5fee5ec3 | 93 | printf ("Destructor\n"); |
b4c522fa IB |
94 | assert(x5 == 2); |
95 | x5++; | |
96 | } | |
97 | } | |
98 | ||
99 | void test5() | |
100 | { | |
101 | { | |
102 | mixin ("scope Foo5 f = new Foo5;\n"); | |
5fee5ec3 | 103 | printf (" Inside Scope\n"); |
b4c522fa IB |
104 | assert(x5 == 1); |
105 | x5++; | |
106 | } | |
107 | assert(x5 == 3); | |
108 | } | |
109 | ||
110 | /*********************************************/ | |
111 | ||
112 | void test6() | |
113 | { | |
114 | static const b = "printf(`hey\n`);"; | |
115 | ||
116 | if (true) | |
117 | mixin(b); | |
118 | } | |
119 | ||
120 | /*********************************************/ | |
121 | ||
122 | template Foo7(alias f) | |
123 | { | |
124 | } | |
125 | ||
126 | class Bar7 | |
127 | { | |
128 | mixin Foo7!( function {} ); | |
129 | } | |
130 | ||
131 | void test7() | |
132 | { | |
133 | } | |
134 | ||
135 | /*********************************************/ | |
136 | ||
137 | template TupleDecls(T, R ...) { | |
138 | T value; | |
139 | static if (R.length) | |
140 | mixin TupleDecls!(R) Inner; | |
141 | } | |
142 | ||
143 | struct TupleStruct(T ...) { mixin TupleDecls!(T); } | |
144 | ||
145 | void test8() { | |
146 | alias TupleStruct!(char[], char[]) twoStrings; | |
147 | } | |
148 | ||
149 | /*********************************************/ | |
150 | ||
151 | template Magic() | |
152 | { | |
153 | void* magic = null; | |
154 | } | |
155 | ||
156 | struct Item | |
157 | { | |
158 | mixin Magic A; | |
159 | } | |
160 | ||
161 | struct Foo9(alias S) | |
162 | { | |
163 | } | |
164 | ||
165 | void test9() | |
166 | { | |
167 | Foo9!(Item.A) bar; | |
168 | } | |
169 | ||
170 | /*********************************************/ | |
171 | ||
172 | pragma(msg, "hello"); | |
173 | pragma(msg, ['h', 'e', 'l', 'l', 'o']); | |
174 | pragma(msg, ""); | |
175 | pragma(msg, []); | |
176 | pragma(msg, null); | |
177 | mixin("string hello;"); | |
178 | mixin(['i', 'n', 't', ' ', 't', 'e', 's', 't', '1', '0', 'x', ';']); | |
179 | mixin(""); | |
180 | mixin([]); | |
181 | mixin(null); | |
182 | void test10() | |
183 | { | |
184 | pragma(msg, "hello"); | |
185 | pragma(msg, ['h', 'e', 'l', 'l', 'o']); | |
186 | pragma(msg, ""); | |
187 | pragma(msg, []); | |
188 | pragma(msg, null); | |
189 | mixin("string hello;"); | |
190 | mixin(['i', 'n', 't', ' ', 'a', ';']); | |
191 | mixin(""); | |
192 | mixin([]); | |
193 | mixin(null); | |
194 | } | |
195 | ||
196 | /*********************************************/ | |
5fee5ec3 | 197 | // https://issues.dlang.org/show_bug.cgi?id=7560 |
b4c522fa IB |
198 | |
199 | class Base7560 | |
200 | { | |
201 | template getter(T) | |
202 | { | |
203 | void get(ref T[] i, uint n) {} | |
204 | } | |
205 | mixin getter!uint; | |
206 | mixin getter!char; | |
207 | } | |
208 | ||
209 | class Derived7560 : Base7560 | |
210 | { | |
211 | alias Base7560.get get; | |
212 | void get(ref char[] x) {} | |
213 | } | |
214 | ||
215 | /*********************************************/ | |
5fee5ec3 | 216 | // https://issues.dlang.org/show_bug.cgi?id=10577 |
b4c522fa IB |
217 | |
218 | enum sync10577; | |
219 | ||
220 | public template get_sync10577(size_t I, A...) | |
221 | { | |
222 | static if (I == A.length) enum bool get_sync10577 = false; | |
223 | else static if (is(A[I] == sync10577)) enum bool get_sync10577 = true; | |
224 | else enum bool get_sync10577 = get_sync!10577(I+1, A); | |
225 | } | |
226 | ||
227 | template add_sync10577(T, size_t ITER=0) | |
228 | { | |
229 | static if (ITER == (__traits(derivedMembers, T).length)) | |
230 | { | |
231 | enum string add_sync10577 = ""; | |
232 | } | |
233 | else | |
234 | { | |
235 | enum string mem = __traits(derivedMembers, T)[ITER]; | |
236 | enum string add_sync10577 = | |
237 | "static if (! __traits(isVirtualMethod, " ~ mem ~ ")) {" ~ | |
238 | "mixin(add_sync10577!(get_sync10577!(0, __traits(getAttributes, " | |
239 | ~ mem ~ ")), \"" ~ mem ~ "\"));} " ~ add_sync10577!(T, ITER+1); | |
240 | } | |
241 | } | |
242 | ||
243 | template add_sync10577(bool A, string M) | |
244 | { | |
245 | static if (A) | |
246 | { | |
247 | enum string add_sync10577 = " auto " ~ M[1..$] ~ | |
248 | "() { synchronized(this) return " ~ M ~ "; }"; | |
249 | } | |
250 | else | |
251 | { | |
252 | enum string add_sync10577 = ""; | |
253 | } | |
254 | } | |
255 | ||
256 | class base10577 | |
257 | { | |
258 | public void foo() {} | |
259 | } | |
260 | ||
261 | class derived10577 : base10577 | |
262 | { | |
263 | mixin(add_sync10577!(derived10577)); | |
264 | @sync10577 private bool _bar; | |
265 | ||
266 | public override void foo() {} | |
267 | } | |
268 | ||
269 | /*********************************************/ | |
5fee5ec3 | 270 | // https://issues.dlang.org/show_bug.cgi?id=10583 |
b4c522fa IB |
271 | |
272 | enum sync10583; | |
273 | ||
274 | public template get_sync10583(size_t I, A...) | |
275 | { | |
276 | static if (I == A.length) | |
277 | enum bool get_sync10583 = false; | |
278 | else static if (is(A[I] == sync10583)) | |
279 | enum bool get_sync10583 = true; | |
280 | else | |
281 | enum bool get_sync10583 = get_sync10583!(I+1, A); | |
282 | } | |
283 | ||
284 | template add_sync10583(T, size_t ITER = 0) | |
285 | { | |
286 | static if (ITER == (__traits(derivedMembers, T).length)) | |
287 | { | |
288 | enum string add_sync10583 = ""; | |
289 | } | |
290 | else | |
291 | { | |
292 | enum string mem = __traits(derivedMembers, T)[ITER]; | |
293 | enum string add_sync10583 = | |
294 | "mixin(add_sync10583!(get_sync10583!(0, __traits(getAttributes, " | |
295 | ~ mem ~ ")), __traits(getProtection, " | |
296 | ~ mem ~ "), \"" ~ mem ~ "\"));\n" ~ add_sync10583!(T, ITER+1); | |
297 | } | |
298 | } | |
299 | ||
300 | template add_sync10583(bool A, string P, string M) | |
301 | { | |
302 | static if (A) | |
303 | { | |
304 | enum string add_sync10583 = " auto " ~ M[1..$] ~ | |
305 | "() { synchronized(this) return " ~ M ~ "; }"; | |
306 | } | |
307 | else | |
308 | enum string add_sync10583 = ""; | |
309 | } | |
310 | ||
311 | class derived10583 | |
312 | { | |
313 | mixin(add_sync10583!(derived10583)); | |
314 | @sync10583 private bool _bar; | |
315 | void frop() {} | |
316 | } | |
317 | ||
318 | /*********************************************/ | |
319 | ||
320 | string rep(string s, int n) | |
321 | { | |
322 | return n > 1 ? s ~ rep(s, n-1) : s; | |
323 | } | |
324 | ||
325 | void test7156() | |
326 | { | |
327 | int i; | |
328 | mixin(rep("++i;", 200)); | |
329 | } | |
330 | ||
331 | /*********************************************/ | |
5fee5ec3 | 332 | // https://issues.dlang.org/show_bug.cgi?id=7553 |
b4c522fa IB |
333 | |
334 | template Foo7553() | |
335 | { | |
336 | struct Range7553 {} | |
337 | } | |
338 | template Biz7553() | |
339 | { | |
340 | struct Range7553 {} | |
341 | } | |
342 | ||
343 | class Bar7553 | |
344 | { | |
345 | mixin Foo7553!() index0; | |
346 | mixin Biz7553!() index1; | |
347 | ||
348 | auto to_range(Range)(Range r) | |
349 | { | |
350 | return r; | |
351 | } | |
352 | ||
353 | } | |
354 | ||
355 | void test7553() | |
356 | { | |
357 | auto r2 = new Bar7553().to_range(1); | |
358 | } | |
359 | ||
360 | /*********************************************/ | |
5fee5ec3 | 361 | // https://issues.dlang.org/show_bug.cgi?id=13479 |
b4c522fa IB |
362 | |
363 | mixin template F13479() | |
364 | { | |
365 | void t()() | |
366 | { | |
367 | } | |
368 | ||
369 | alias t!() a; | |
370 | } | |
371 | ||
372 | void test13479() | |
373 | { | |
374 | mixin F13479!(); | |
375 | a(); | |
376 | } | |
377 | ||
378 | /*********************************************/ | |
379 | ||
380 | void main() | |
381 | { | |
382 | test1(); | |
383 | test2(); | |
384 | test3(); | |
385 | test4(); | |
386 | test5(); | |
387 | test6(); | |
388 | test7(); | |
389 | test8(); | |
390 | test9(); | |
391 | test10(); | |
392 | test7156(); | |
393 | test13479(); | |
394 | ||
5fee5ec3 | 395 | printf("Success\n"); |
b4c522fa | 396 | } |