Debian Codegraph @

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

raw
1 /*
2 RUN_OUTPUT:
3 ---
4 Success
5 ---
6 */
7
8 extern(C) int printf(const char*, ...);
9
10 enum
11 {
12 T_char,
13 T_wchar,
14 T_dchar,
15 T_bit,
16 T_byte,
17 T_ubyte,
18 T_short,
19 T_ushort,
20 T_int,
21 T_uint,
22 T_long,
23 T_ulong,
24 }
25
26 int dotype(char x) { return T_char; }
27 int dotype(bool x) { return T_bit; }
28 int dotype(byte x) { return T_byte; }
29 int dotype(ubyte x) { return T_ubyte; }
30 int dotype(wchar x) { return T_wchar; }
31 int dotype(short x) { return T_short; }
32 int dotype(ushort x) { return T_ushort; }
33 int dotype(int x) { return T_int; }
34 int dotype(uint x) { return T_uint; }
35 int dotype(long x) { return T_long; }
36 int dotype(ulong x) { return T_ulong; }
37
38 void test1()
39 {
40 /*
41 * 0x7FFF 077777 32767
42 * 0x8000 0100000 32768
43 * 0xFFFF 0177777 65535
44 * 0x10000 0200000 65536
45 * 0x7FFFFFFF 017777777777 2147483647
46 * 0x80000000 020000000000 2147483648
47 * 0xFFFFFFFF 037777777777 4294967295
48 * 0x100000000 040000000000 4294967296
49 * 0x7FFFFFFFFFFFFFFF 0777777777777777777777 9223372036854775807
50 * 0x8000000000000000 01000000000000000000000 9223372036854775808
51 * 0xFFFFFFFFFFFFFFFF 01777777777777777777777 18446744073709551615
52 */
53
54 assert(dotype(1) == T_int);
55
56 /***************** Hexadecimal ***********************/
57
58 assert(dotype(0) == T_int);
59 assert(dotype(0x7FFF) == T_int);
60 assert(dotype(0x8000) == T_int);
61 assert(dotype(0xFFFF) == T_int);
62 assert(dotype(0x10000) == T_int);
63 assert(dotype(0x7FFFFFFF) == T_int);
64 assert(dotype(0x80000000) == T_uint);
65 assert(dotype(0xFFFFFFFF) == T_uint);
66 assert(dotype(0x100000000) == T_long);
67 assert(dotype(0x7FFFFFFFFFFFFFFF) == T_long);
68 assert(dotype(0x8000000000000000) == T_ulong);
69 assert(dotype(0xFFFFFFFFFFFFFFFF) == T_ulong);
70
71 assert(dotype(0u) == T_uint);
72 assert(dotype(0x7FFFu) == T_uint);
73 assert(dotype(0x8000u) == T_uint);
74 assert(dotype(0xFFFFu) == T_uint);
75 assert(dotype(0x10000u) == T_uint);
76 assert(dotype(0x7FFFFFFFu) == T_uint);
77 assert(dotype(0x80000000u) == T_uint);
78 assert(dotype(0xFFFFFFFFu) == T_uint);
79 assert(dotype(0x100000000u) == T_ulong);
80 assert(dotype(0x7FFFFFFFFFFFFFFFu) == T_ulong);
81 assert(dotype(0x8000000000000000u) == T_ulong);
82 assert(dotype(0xFFFFFFFFFFFFFFFFu) == T_ulong);
83
84 assert(dotype(0L) == T_long);
85 assert(dotype(0x7FFFL) == T_long);
86 assert(dotype(0x8000L) == T_long);
87 assert(dotype(0xFFFFL) == T_long);
88 assert(dotype(0x10000L) == T_long);
89 assert(dotype(0x7FFFFFFFL) == T_long);
90 assert(dotype(0x80000000L) == T_long);
91 assert(dotype(0xFFFFFFFFL) == T_long);
92 assert(dotype(0x100000000L) == T_long);
93 assert(dotype(0x7FFFFFFFFFFFFFFFL) == T_long);
94 assert(dotype(0x8000000000000000L) == T_ulong);
95 assert(dotype(0xFFFFFFFFFFFFFFFFL) == T_ulong);
96
97 assert(dotype(0uL) == T_ulong);
98 assert(dotype(0x7FFFuL) == T_ulong);
99 assert(dotype(0x8000uL) == T_ulong);
100 assert(dotype(0xFFFFuL) == T_ulong);
101 assert(dotype(0x10000uL) == T_ulong);
102 assert(dotype(0x7FFFFFFFuL) == T_ulong);
103 assert(dotype(0x80000000uL) == T_ulong);
104 assert(dotype(0xFFFFFFFFuL) == T_ulong);
105 assert(dotype(0x100000000uL) == T_ulong);
106 assert(dotype(0x7FFFFFFFFFFFFFFFuL) == T_ulong);
107 assert(dotype(0x8000000000000000uL) == T_ulong);
108 assert(dotype(0xFFFFFFFFFFFFFFFFuL) == T_ulong);
109
110 /***************** Decimal ***********************/
111
112 assert(dotype(0) == T_int);
113 assert(dotype(32767) == T_int);
114 assert(dotype(32768) == T_int);
115 assert(dotype(65535) == T_int);
116 assert(dotype(65536) == T_int);
117 assert(dotype(2147483647) == T_int);
118 assert(dotype(2147483648) == T_long);
119 assert(dotype(4294967295) == T_long);
120 assert(dotype(4294967296) == T_long);
121 assert(dotype(9223372036854775807) == T_long);
122 //assert(dotype(9223372036854775808) == T_long);
123 //assert(dotype(18446744073709551615) == T_ulong);
124
125 assert(dotype(0u) == T_uint);
126 assert(dotype(32767u) == T_uint);
127 assert(dotype(32768u) == T_uint);
128 assert(dotype(65535u) == T_uint);
129 assert(dotype(65536u) == T_uint);
130 assert(dotype(2147483647u) == T_uint);
131 assert(dotype(2147483648u) == T_uint);
132 assert(dotype(4294967295u) == T_uint);
133 assert(dotype(4294967296u) == T_ulong);
134 assert(dotype(9223372036854775807u) == T_ulong);
135 assert(dotype(9223372036854775808u) == T_ulong);
136 assert(dotype(18446744073709551615u) == T_ulong);
137
138 assert(dotype(0L) == T_long);
139 assert(dotype(32767L) == T_long);
140 assert(dotype(32768L) == T_long);
141 assert(dotype(65535L) == T_long);
142 assert(dotype(65536L) == T_long);
143 assert(dotype(2147483647L) == T_long);
144 assert(dotype(2147483648L) == T_long);
145 assert(dotype(4294967295L) == T_long);
146 assert(dotype(4294967296L) == T_long);
147 assert(dotype(9223372036854775807L) == T_long);
148 //assert(dotype(9223372036854775808L) == T_ulong);
149 //assert(dotype(18446744073709551615L) == T_ulong);
150
151 assert(dotype(0uL) == T_ulong);
152 assert(dotype(32767uL) == T_ulong);
153 assert(dotype(32768uL) == T_ulong);
154 assert(dotype(65535uL) == T_ulong);
155 assert(dotype(65536uL) == T_ulong);
156 assert(dotype(2147483647uL) == T_ulong);
157 assert(dotype(2147483648uL) == T_ulong);
158 assert(dotype(4294967295uL) == T_ulong);
159 assert(dotype(4294967296uL) == T_ulong);
160 assert(dotype(9223372036854775807uL) == T_ulong);
161 assert(dotype(9223372036854775808uL) == T_ulong);
162 assert(dotype(18446744073709551615uL) == T_ulong);
163 }
164
165 void test2()
166 {
167 ulong[] a = [ 2_463_534_242UL ];
168
169 foreach(e; a)
170 assert(e == 2_463_534_242UL);
171 }
172
173 /***************************************************/
174 // https://issues.dlang.org/show_bug.cgi?id=13907
175
176 void f13907_1(wchar[1] a) {}
177 void f13907_2(wchar[2] a) {}
178 void f13907_3(wchar[3] a) {}
179
180 auto f13907_12(char[1]) { return 1; }
181 auto f13907_12(char[2]) { return 2; }
182
183 auto f13907_123(char[1]) { return 1; }
184 auto f13907_123(char[2]) { return 2; }
185 auto f13907_123(char[3]) { return 3; }
186 auto f13907_123(const(char)[]) { return 0; }
187
188 void test13907()
189 {
190 static assert(!__traits(compiles, { f13907_1("\U00010000"w); }));
191 static assert(!__traits(compiles, { f13907_1("\U00010000" ); }));
192 f13907_2("\U00010000"w);
193 f13907_2("\U00010000");
194 f13907_3("\U00010000"w); // Re-enable implicit length extension, from https://issues.dlang.org/show_bug.cgi?id=13999
195 f13907_3("\U00010000" ); // Re-enable implicit length extension, from https://issues.dlang.org/show_bug.cgi?id=13999
196
197 assert(f13907_12("a") == 1);
198 assert(f13907_12("ab") == 2);
199 static assert(!__traits(compiles, { f13907_12("abc"); }));
200
201 assert(f13907_123("a") == 1);
202 assert(f13907_123("ab") == 2);
203 assert(f13907_123("abc") == 3);
204 assert(f13907_123("abcd") == 0);
205
206 // regression tests for the lengthen behavior in initializer
207 enum const(char*) p = "hello world";
208 static assert(!__traits(compiles, { static char[5] a = "hello world"; })); // truncation is not allowed
209 static assert(!__traits(compiles, { static void[20] a = "hello world"; }));
210 static assert(!__traits(compiles, { static int[20] a = "hello world"; }));
211 static assert(!__traits(compiles, { static char[20] a = "hello world"w; }));
212 static assert(!__traits(compiles, { static wchar[20] a = "hello world"d; }));
213 static assert(!__traits(compiles, { static dchar[20] a = "hello world"c; }));
214 static assert(!__traits(compiles, { static char[20] a = p; }));
215 static char[20] csa = "hello world"; // extending is allowed
216 static wchar[20] wsa = "hello world"; // ok
217 static dchar[20] dsa = "hello world"; // ok
218
219 // https://issues.dlang.org/show_bug.cgi?id=13966
220 string[1][] arr;
221 arr ~= ["class"];
222 enum immutable(char[5]) sarrstr = "class";
223 arr ~= [sarrstr];
224
225 // https://issues.dlang.org/show_bug.cgi?id=13999
226 string[dchar[2]] aa13999 = ["あ": "bar"];
227 assert(aa13999["あ"] == "bar");
228 dchar[2] key13999 = "あ";
229 assert(key13999[0] == 'あ');
230 assert(key13999[1] == '\0');
231 assert(aa13999[key13999] == "bar");
232 }
233
234 ulong op12950(ulong v){return v + 12950;}
235
236 void test12950()
237 {
238 assert(0x00_00_00_01.op12950() == 12951);
239 assert(0x00_00_00_01UL.op12950() == 12951);
240 assert(0b00_00_00_01.op12950() == 12951);
241 assert(0b00_00_00_01UL.op12950() == 12951);
242 }
243
244 void testHexstring()
245 {
246 static immutable uint[] x = cast(immutable uint[]) x"FFAADDEE"d;
247 static assert(x[0] == 0xFFAADDEE);
248 assert(x[0] == 0xFFAADDEE);
249
250 static immutable ulong[] y = x"1122334455667788AABBCCDDEEFF0099";
251 static assert(y[0] == 0x1122334455667788);
252 static assert(y[1] == 0xAABBCCDDEEFF0099);
253 assert(y[0] == 0x1122334455667788);
254 assert(y[1] == 0xAABBCCDDEEFF0099);
255
256 immutable long[] c = x"1122334455667788AABBCCDDEEFF0099";
257 assert(c[0] == 0x1122334455667788);
258 assert(c[1] == 0xAABBCCDDEEFF0099);
259
260 // Test that mangling of StringExp with size 8 is the same as array literal mangling:
261 void f(immutable ulong[] a)() {}
262 static assert(f!y.mangleof == f!([0x1122334455667788, 0xAABBCCDDEEFF0099]).mangleof);
263
264 // Test printing StringExp with size 8
265 enum toStr(immutable ulong[] v) = v.stringof;
266 static assert(toStr!y == `x"1122334455667788AABBCCDDEEFF0099"`);
267
268 // Hex string postfixes
269 // https://issues.dlang.org/show_bug.cgi?id=24363
270 wstring wStr = x"AA BB CC DD"w;
271 immutable int[] dStr = x"AA BB CC DD"d;
272 assert(wStr[0] == 0xAABB);
273 assert(wStr[1] == 0xCCDD);
274 assert(dStr[0] == 0xAABBCCDD);
275
276 // Test sliceCmpStringWithArray with size 8
277 static immutable ulong[] z0 = cast(immutable ulong[]) x"1111 1111 1111 1111 0000 000F 0000 0000";
278 static immutable ulong[] z1 = [0x1111_1111_1111_1111, 0x0000_000E_0000_0000];
279 static assert(z0 !is z1);
280
281 // https://github.com/dlang/dmd/issues/20635
282 f20635(cast(ubyte[]) x"00");
283 f20635(cast(const ubyte[]) x"00");
284 f20635(cast(immutable ubyte[]) x"00");
285 }
286
287 void f20635(const ubyte[] value){}
288 void f20635(const string value){}
289
290 /***************************************************/
291
292 int main()
293 {
294 test1();
295 test2();
296 test13907();
297 test12950();
298 testHexstring();
299
300 printf("Success\n");
301 return 0;
302 }