d: Merge dmd. druntime e770945277, phobos 6d6e0b9b9
[official-gcc.git] / gcc / testsuite / gdc.test / runnable / literal.d
blob69971240d9487ce0d46aec70d5939ea50c383eb7
1 /*
2 RUN_OUTPUT:
3 ---
4 Success
5 ---
6 */
8 extern(C) int printf(const char*, ...);
10 enum
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,
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; }
38 void test1()
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
54 assert(dotype(1) == T_int);
56 /***************** Hexadecimal ***********************/
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);
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);
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);
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);
110 /***************** Decimal ***********************/
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);
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);
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);
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);
165 void test2()
167 ulong[] a = [ 2_463_534_242UL ];
169 foreach(e; a)
170 assert(e == 2_463_534_242UL);
173 /***************************************************/
174 // https://issues.dlang.org/show_bug.cgi?id=13907
176 void f13907_1(wchar[1] a) {}
177 void f13907_2(wchar[2] a) {}
178 void f13907_3(wchar[3] a) {}
180 auto f13907_12(char[1]) { return 1; }
181 auto f13907_12(char[2]) { return 2; }
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; }
188 void test13907()
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
197 assert(f13907_12("a") == 1);
198 assert(f13907_12("ab") == 2);
199 static assert(!__traits(compiles, { f13907_12("abc"); }));
201 assert(f13907_123("a") == 1);
202 assert(f13907_123("ab") == 2);
203 assert(f13907_123("abc") == 3);
204 assert(f13907_123("abcd") == 0);
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
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];
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");
234 ulong op12950(ulong v){return v + 12950;}
236 void test12950()
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);
244 void testHexstring()
246 static immutable uint[] x = cast(immutable uint[]) x"FFAADDEE";
247 static assert(x[0] == 0xFFAADDEE);
248 assert(x[0] == 0xFFAADDEE);
250 static immutable ulong[] y = cast(immutable ulong[]) x"1122334455667788AABBCCDDEEFF0099";
251 static assert(y[0] == 0x1122334455667788);
252 static assert(y[1] == 0xAABBCCDDEEFF0099);
253 assert(y[0] == 0x1122334455667788);
254 assert(y[1] == 0xAABBCCDDEEFF0099);
256 // Test that mangling of StringExp with size 8 is the same as array literal mangling:
257 void f(immutable ulong[] a)() {}
258 static assert(f!y.mangleof == f!([0x1122334455667788, 0xAABBCCDDEEFF0099]).mangleof);
260 // Test printing StringExp with size 8
261 enum toStr(immutable ulong[] v) = v.stringof;
262 static assert(toStr!y == `x"88776655443322119900FFEEDDCCBBAA"`);
265 /***************************************************/
267 int main()
269 test1();
270 test2();
271 test13907();
272 test12950();
273 testHexstring();
275 printf("Success\n");
276 return 0;