]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gdc.test/runnable/template3.d
Add D front-end, libphobos library, and D2 testsuite.
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / runnable / template3.d
1
2 import core.stdc.stdio;
3
4 /*********************************************************/
5
6 template Foo(T)
7 {
8 static if (is(T : int))
9 alias T t1;
10
11 static if (T.sizeof == 4)
12 alias T t2;
13
14 static if (is(T U : int))
15 alias U t3;
16
17 static if (is(T* V : V*))
18 alias V t4;
19
20 static if (is(T W))
21 alias W t5;
22 else
23 alias char t5;
24
25 static if (is(T* X : X*))
26 {
27 }
28 }
29
30 void test1()
31 {
32 Foo!(int).t1 x1;
33 assert(typeid(typeof(x1)) == typeid(int));
34
35 Foo!(int).t2 x2;
36 assert(typeid(typeof(x2)) == typeid(int));
37
38 Foo!(int).t3 x3;
39 assert(typeid(typeof(x3)) == typeid(int));
40
41 Foo!(int*).t4 x4;
42 assert(typeid(typeof(x4)) == typeid(int*));
43
44 Foo!(int).t5 x5;
45 assert(typeid(typeof(x5)) == typeid(int));
46
47 Foo!(int).X x6;
48 assert(typeid(typeof(x6)) == typeid(int));
49 }
50
51 /*********************************************************/
52
53
54 void test2()
55 {
56 alias int T;
57
58 static if (is(T : int))
59 alias T t1;
60
61 static if (T.sizeof == 4)
62 alias T t2;
63
64 static if (is(T U : int))
65 alias U t3;
66
67 static if (is(T* V : V*))
68 alias V t4;
69
70 static if (is(T W))
71 alias W t5;
72 else
73 alias char t5;
74
75 static if (is(T* X : X*))
76 {
77 }
78
79 t1 x1;
80 assert(typeid(typeof(x1)) == typeid(int));
81
82 t2 x2;
83 assert(typeid(typeof(x2)) == typeid(int));
84
85 t3 x3;
86 assert(typeid(typeof(x3)) == typeid(int));
87
88 t4 x4;
89 assert(typeid(typeof(x4)) == typeid(int));
90
91 t5 x5;
92 assert(typeid(typeof(x5)) == typeid(int));
93
94 X x6;
95 assert(typeid(typeof(x6)) == typeid(int));
96 }
97
98 /*********************************************************/
99
100 void test3()
101 {
102 static if (is(short : int))
103 { printf("1\n");
104 }
105 else
106 assert(0);
107 static if (is(short == int))
108 assert(0);
109 static if (is(int == int))
110 { printf("3\n");
111 }
112 else
113 assert(0);
114 }
115
116 /*********************************************************/
117
118 template TValue(int i:1)
119 {
120 pragma(msg,"last instantiation!!!");
121 const int TValue = 1;
122 }
123
124 template TValue(int i)
125 {
126 pragma(msg,"instantiating...");
127 const int TValue = i * TValue!(i-1);
128 }
129
130 void test4()
131 {
132 assert(TValue!(3) == 6);
133 }
134
135 /*********************************************************/
136
137 template Reverse(string s: "") {
138 const char[] Reverse = "";
139 }
140
141 template Reverse(string s) {
142 const char[] Reverse = Reverse!(s[1..$]) ~ s[0];
143 }
144
145 void test5()
146 {
147 assert(Reverse!("Recursive string template") == "etalpmet gnirts evisruceR");
148 }
149
150 /*********************************************************/
151
152 template foo6(alias V)
153 {
154 int foo6()
155 {
156 return V;
157 }
158 }
159
160 class bar6(alias V)
161 {
162 int abc()
163 {
164 return V;
165 }
166 }
167
168 void test6()
169 {
170 int j = 3;
171 int k = 4;
172
173 int i = foo6!(j)();
174 i += foo6!(j)();
175
176 i += foo6!(k)();
177
178 bar6!(j) b = new bar6!(j);
179 i -= b.abc();
180
181 assert(i == 7);
182 }
183
184 /*********************************************************/
185
186 template Bind7(alias dg)
187 {
188 int Bind7()
189 {
190 dg('c');
191 return 0;
192 }
193 }
194
195 void test7()
196 {
197 char[] v;
198
199 void foo(char c) { v ~= c; }
200
201 alias Bind7!(foo) intv;
202 intv();
203 assert(v[0] == 'c');
204 }
205
206 /*********************************************************/
207
208 template sum8(real x)
209 {
210 static if (x <= 1.0L){
211 const real sum8 = x;
212 }else{
213 const real sum8 = x + sum8!(x - 1.0L);
214 }
215 }
216
217 void test8()
218 {
219 real x = sum8!(3.0L);
220
221 if(x != 6.0L){
222 assert(0);
223 }
224 }
225
226 /*********************************************************/
227
228 int main()
229 {
230 test1();
231 test2();
232 test3();
233 test4();
234 test5();
235 test6();
236 test7();
237 test8();
238
239 printf("Success\n");
240 return 0;
241 }