Debian Codegraph @

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

raw
1 /*
2 RUN_OUTPUT:
3 ---
4 Success
5 ---
6 */
7 extern(C) int printf(const char*, ...);
8
9 template Seq(T...) { alias T Seq; }
10
11 /***************************************************/
12 // https://issues.dlang.org/show_bug.cgi?id=3133
13
14 void test3133()
15 {
16 short[2] x = [1, 2];
17 auto y = cast(int[1])x; // no error
18 }
19
20 /***************************************************/
21 // https://issues.dlang.org/show_bug.cgi?id=7504
22
23 void test7504() pure nothrow @safe
24 {
25 auto n = null;
26 char[] k = n;
27 assert(k.ptr == null);
28 assert(k.length == 0);
29
30 double[] l;
31 l = n;
32 assert(l.ptr == null);
33 assert(l.length == 0);
34
35 immutable(int[]) m = n;
36 assert(m.ptr == null);
37 assert(m.length == 0);
38
39 const(float)[] o;
40 o = n;
41 assert(o.ptr == null);
42 assert(o.length == 0);
43
44 auto c = create7504(null, null);
45 assert(c.k.ptr == null);
46 assert(c.k.length == 0);
47 assert(c.l.ptr == null);
48 assert(c.l.length == 0);
49 }
50
51 class C7504
52 {
53 int[] k;
54 string l;
55 }
56
57 C7504 create7504(T...)(T input)
58 {
59 auto obj = new C7504;
60 obj.tupleof = input;
61 return obj;
62 }
63
64 /***************************************************/
65 // https://issues.dlang.org/show_bug.cgi?id=8119
66
67 struct S8119;
68
69 void test8119()
70 {
71 void* v;
72 auto sp1 = cast(S8119*)v;
73
74 int* i;
75 auto sp2 = cast(S8119*)i;
76
77 S8119* s;
78 auto ip = cast(int*)s;
79 }
80
81 /***************************************************/
82 // https://issues.dlang.org/show_bug.cgi?id=8645
83
84 template TypeTuple8645(TL...)
85 {
86 alias TL TypeTuple8645;
87 }
88
89 void test8645()
90 {
91 alias TypeTuple8645!(int) Foo;
92 int bar;
93 static assert(!is(typeof( cast(Foo)bar )));
94 }
95
96 /***************************************************/
97 // https://issues.dlang.org/show_bug.cgi?id=10497
98
99 struct S10497;
100
101 void test10497(S10497** s)
102 {
103 void* ptr;
104 *s = cast(S10497*)ptr;
105 }
106
107 /***************************************************/
108 // https://issues.dlang.org/show_bug.cgi?id=10793
109
110 struct RealFoo10793
111 {
112 int i;
113 }
114
115 struct Foo10793;
116
117 void test10793()
118 {
119 auto rf = RealFoo10793(10);
120 void* prf = cast(void*)&rf;
121 Foo10793* f = cast(Foo10793*)prf;
122 }
123
124 /***************************************************/
125 // https://issues.dlang.org/show_bug.cgi?id=10834
126
127 void test10834()
128 {
129 struct S { int i; }
130 S s;
131 cast(void)s;
132
133 class C { int i; }
134 C c;
135 cast(void)c;
136
137 enum E { a, b }
138 E e;
139 cast(void)e;
140
141 int[] ia;
142 cast(void)ia;
143 }
144
145 /***************************************************/
146 // https://issues.dlang.org/show_bug.cgi?id=10842
147
148 template Test10842(F, T)
149 {
150 bool res;
151 F from()
152 {
153 res = true;
154 return F.init;
155 }
156 T to()
157 {
158 // The cast operand had incorrectly been eliminated
159 return cast(T)from();
160 }
161 bool test()
162 {
163 res = false;
164 to();
165 return res;
166 }
167 }
168
169 void test10842()
170 {
171 if (!Test10842!(typeof(null), string).test()) // 10842
172 assert(0);
173 }
174
175 /***************************************************/
176 // https://issues.dlang.org/show_bug.cgi?id=11722
177
178 class C11722
179 {
180 T opCast(T)() { assert(0); }
181 }
182
183 void test11722()
184 {
185 C11722 c = new C11722();
186 shared C11722 sc = cast(shared)c;
187 }
188
189 /***************************************************/
190 // https://issues.dlang.org/show_bug.cgi?id=14218
191
192 void test14218()
193 {
194 foreach (To; Seq!( byte, short, int, long,
195 ubyte, ushort, uint, ulong,
196 char, wchar, dchar, bool))
197 {
198 auto x = cast(To)null;
199 assert(x == 0); // false, '0x00'
200 }
201
202 version (DigitalMars)
203 {
204 // Questionable but currently accepted by DMD (but not GDC).
205 foreach (To; Seq!( float, double, real))
206 {
207 auto x = cast(To)null;
208 assert(x == 0); // 0i
209 }
210 }
211 }
212
213 /***************************************************/
214
215 int main()
216 {
217 test3133();
218 test7504();
219 test8119();
220 test8645();
221 test10793();
222 test10834();
223 test10842();
224 test11722();
225 test14218();
226
227 printf("Success\n");
228 return 0;
229 }