1 // REQUIRED_ARGS:
2 
3 extern(C) int printf(const char*, ...);
4 
5 /************************************************/
6 // These seem to be the original tests for $ (originally 'length').
7 
8 int x;
9 
bar(int[]a)10 int[] bar(int[] a)
11 {
12     x++;
13     return a[0 .. $ - 1];
14 }
15 
test1()16 void test1()
17 {
18   {
19     int[4] foo;
20 
21     foo[$ - 2] = 4;
22     assert(foo[0] == 0);
23     assert(foo[1] == 0);
24     assert(foo[2] == 4);
25     assert(foo[3] == 0);
26 
27     foo[3] = 5;
28     assert(foo[$ - 1] == 5);
29 
30     x = 0;
31     bar(foo)[$ - 3] = 6;
32     assert(x == 1);
33     assert(foo[0] == 6);
34 
35     assert(bar(foo)[$ * 2 - 1 - $] == 4);
36     assert(x == 2);
37 
38     foo[0 .. $] = 1;
39     assert(foo[0] == 1);
40     assert(foo[1] == 1);
41     assert(foo[2] == 1);
42     assert(foo[3] == 1);
43 
44     x = 0;
45     bar(foo)[1 .. $ * 3 - $ - $] = 2;
46     assert(x == 1);
47     assert(foo[0] == 1);
48     assert(foo[1] == 2);
49     assert(foo[2] == 2);
50     assert(foo[3] == 1);
51 
52     int[] a = new int[3];
53     a[0..$] = foo[0..$-1];
54     assert(a[0] == 1);
55     assert(a[1] == 2);
56     assert(a[2] == 2);
57     a[] = 4;
58     a[0..$] = bar(foo)[0..$];
59     assert(x == 2);
60     assert(a[0] == 1);
61     assert(a[1] == 2);
62     assert(a[2] == 2);
63   }
64 
65   {
66     int[4] f; int[] foo = f;
67 
68     foo[$ - 2] = 4;
69     assert(foo[0] == 0);
70     assert(foo[1] == 0);
71     assert(foo[2] == 4);
72     assert(foo[3] == 0);
73 
74     foo[3] = 5;
75     assert(foo[$ - 1] == 5);
76 
77     x = 0;
78     bar(foo)[$ - 3] = 6;
79     assert(x == 1);
80     assert(foo[0] == 6);
81 
82     assert(bar(foo)[$ * 2 - 1 - $] == 4);
83     assert(x == 2);
84 
85     foo[0 .. $] = 1;
86     assert(foo[0] == 1);
87     assert(foo[1] == 1);
88     assert(foo[2] == 1);
89     assert(foo[3] == 1);
90 
91     x = 0;
92     bar(foo)[1 .. $ * 3 - $ - $] = 2;
93     assert(x == 1);
94     assert(foo[0] == 1);
95     assert(foo[1] == 2);
96     assert(foo[2] == 2);
97     assert(foo[3] == 1);
98 
99     int[] a = new int[3];
100     a[0..$] = foo[0..$-1];
101     assert(a[0] == 1);
102     assert(a[1] == 2);
103     assert(a[2] == 2);
104     a[] = 4;
105     a[0..$] = bar(foo)[0..$];
106     assert(x == 2);
107     assert(a[0] == 1);
108     assert(a[1] == 2);
109     assert(a[2] == 2);
110   }
111 }
112 
113 /************************************************/
114 
115 struct ICONINFO
116 {
117     bool fIcon;
118 }
119 
test2()120 void test2()
121 {
122      ICONINFO info;
123      info.fIcon = true;
124      assert(info.fIcon == true);
125 }
126 
127 
128 /************************************************/
129 
130 class A3
131 {
foo()132     void foo()
133     {
134         printf("A.foo \n" );
135     }
136 }
137 
138 class B3 : A3
139 {
140 }
141 
142 class C3 : B3
143 {
foo()144     override void foo()
145     {
146         printf("C.foo \n" );
147         super.foo();
148     }
149 }
150 
test3()151 void test3()
152 {
153     C3 c = new C3();
154     c.foo();
155 }
156 
157 
158 /************************************************/
159 
test4()160 void test4()
161 {
162     int function (int i) x = function int (int i) { return i * 2; };
163     int function (int i) y = function int (int i) { return i / 2; };
164 
165     int k;
166     k = x(2);
167     assert(k == 4);
168     k = y(3);
169     assert(k == 1);
170 }
171 
172 
173 /************************************************/
174 
175 class Parser
176 {
next(ref int test)177     void next(ref int test)
178     {
179         void work (int input)
180         {
181             printf("work(%d, %d)\n", input, test);
182             test = 2;
183         }
184 
185         test = 3;
186         work(4);
187     }
188 }
189 
190 
test5()191 void test5()
192 {
193     Parser parser = new Parser();
194     int test;
195 
196     parser.next (test);
197     printf("test %d\n", test);
198     assert(test == 2);
199 }
200 
201 
202 /************************************************/
203 
foo6(out int bar)204 void foo6(out int bar)
205 {
206 }
207 
test6()208 void test6()
209 {
210     int bar = 3;
211     foo6(bar);
212     printf("%d", bar );
213     assert(bar == 0);
214 //    return 0;
215 }
216 
217 /************************************************/
218 
test7()219 void test7()
220 {
221    char ch = ' ';
222    char[] u;
223    u.length = 3;
224 
225    int i = 2;
226 
227    printf("a\n");
228    u[0..2] = ch;
229    printf("b\n");
230    u[0..i] = ch;
231    printf("c\n");
232    assert(u[0] == 0x20);
233    assert(u[1] == 0x20);
234 }
235 
236 
237 /************************************************/
238 
239 struct X8
240 {
241     bool flag;
242 }
243 
test8()244 void test8()
245 {
246     X8 x;
247     x.flag = 0 != 0;
248 }
249 
250 
251 /************************************************/
252 
foo9(float x)253 void foo9(float x)
254 {
255     assert(x == 0.0f);
256 }
257 
len9(float x,float y,float z,float t)258 void len9(float x, float y, float z, float t)
259 {
260     foo9(x*x+y*y+z*z);
261 }
262 
test9()263 void test9()
264 {
265     float[4] a;
266     a[0] = a[1] = a[2] = a[3] = 0.0f;
267 
268     for (int y = 0; y < 7; ++y)
269     {
270         len9(a[0], a[1], a[2], a[3]);
271 
272         float justOne() { return 1.0f; }
273 
274         float dot = justOne();
275         if (dot < 0.0f)
276             dot = 0.0f;
277     }
278 }
279 
280 /************************************************/
281 
282 ubyte[4] arr10;
283 
foo10()284 void foo10()
285 {
286   *cast(float*)(&arr10[0]) = 3.25;
287 }
288 
bar10()289 uint bar10()
290 {
291   uint result = *cast(uint*)&arr10[0];
292   return result;
293 }
294 
baz10()295 float baz10()
296 {
297   uint result = bar10();
298   return *cast(float*)&result;
299 }
300 
test10()301 void test10()
302 {
303   foo10();
304   float x = baz10();
305 
306   assert(x == 3.25);
307 }
308 
309 
310 /************************************************/
311 
312 interface I11
313 {
314   void M ();
315 }
316 
317 interface J11 : I11
318 {
319   void N ();
320 }
321 
322 class A11 : I11
323 {
M()324   void M () { printf("A.M()\n"); }
325 }
326 
327 class B11 : A11, J11
328 {
N()329   void N () { printf("B.N()\n"); }
330 }
331 
test11()332 void test11()
333 {
334   I11 f = new B11 ();
335 
336   f.M();
337 }
338 
339 
340 /************************************************/
341 
342 int x12;
343 
test12()344 void test12()
345 {
346     static class S
347     {
348         static this()
349         {
350             printf ("static constructor\n");
351             x12 += 1;
352         }
353 
354         this()
355         {
356             printf ("class constructor\n");
357             x12 += 10;
358         }
359     }
360 
361     assert(x12 == 1);
362     new S;
363     assert(x12 == 11);
364 }
365 
366 
367 /************************************************/
368 
main()369 int main()
370 {
371     test1();
372     test2();
373     test3();
374     test4();
375     test5();
376     test6();
377     test7();
378     test8();
379     test9();
380     test10();
381     test11();
382     test12();
383 
384     printf("Success\n");
385     return 0;
386 }
387