Debian Codegraph @

gcc/testsuite/gdc.test/runnable/template3.d

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