Debian Codegraph @

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

raw
1 // RUNNABLE_PHOBOS_TEST
2 // PERMUTE_ARGS:
3
4 int magicVariable()
5 {
6 if (__ctfe)
7 return 3;
8
9 shared int var = 2;
10 return var;
11 }
12
13 static assert(magicVariable()==3);
14
15 /************************************/
16 // https://issues.dlang.org/show_bug.cgi?id=11159
17
18 void test11159()
19 {
20 import std.math : pow;
21 enum ulong
22 e_2_pow_64 = 2uL^^64,
23 e_10_pow_19 = 10uL^^19,
24 e_10_pow_20 = 10uL^^20;
25 assert(e_2_pow_64 == pow(2uL, 64));
26 assert(e_10_pow_19 == pow(10uL, 19));
27 assert(e_10_pow_20 == pow(10uL, 20));
28 }
29
30 // https://issues.dlang.org/show_bug.cgi?id=991 -- invalid.
31 // https://issues.dlang.org/show_bug.cgi?id=3500 -- is this related to 2127?
32
33 // Tests for ^^
34 // TODO: These tests should not require import std.math.
35
36 import std.math;
37 // Test float ^^ int
38 static assert( 27.0 ^^ 5 == 27.0 * 27.0 * 27.0 * 27.0 * 27.0);
39 static assert( 2.0 ^^ 3 == 8.0);
40
41 static assert( 2.0 ^^ 4 == 16.0);
42 static assert( 2 ^^ 4 == 16);
43
44 static assert((2 ^^ 8) == 256);
45 static assert((3 ^^ 8.0) == 6561);
46 static assert((4.0 ^^ 8) == 65536);
47 static assert((5.0 ^^ 8.0) == 390625);
48
49 static assert((0.5 ^^ 3) == 0.125);
50 static assert((1.5 ^^ 3.0) == 3.375);
51 static assert((2.5 ^^ 3) == 15.625);
52 static assert((3.5 ^^ 3.0) == 42.875);
53
54 static assert(((-2) ^^ -5.0) == -0.031250);
55 static assert(((-2.0) ^^ -6) == 0.015625);
56 static assert(((-2.0) ^^ -7.0) == -0.0078125);
57
58 static assert((144 ^^ 0.5) == 12);
59 static assert((1089 ^^ 0.5) == 33);
60 static assert((1764 ^^ 0.5) == 42);
61 static assert((650.25 ^^ 0.5) == 25.5);
62
63 // Check the typing rules.
64 static assert( is (typeof(2.0^^7) == double));
65 static assert( is (typeof(7^^3) == int));
66
67 static assert( is (typeof(7L^^3) == long));
68 static assert( is (typeof(7^^3L) == long));
69 enum short POW_SHORT_1 = 3;
70 enum short POW_SHORT_3 = 7;
71 static assert( is (typeof(POW_SHORT_1 * POW_SHORT_1) ==
72 typeof(POW_SHORT_1*POW_SHORT_1)));
73
74 static assert( is (typeof(7.0^^3) == double));
75 static assert( is (typeof(7.0L^^3) == real));
76 static assert( is (typeof(7.0f^^3) == float));
77 static assert( is (typeof(POW_SHORT_1^^3.1) == double));
78 static assert( is (typeof(POW_SHORT_1^^3.1f) == float));
79 static assert( is (typeof(2.1f ^^ POW_SHORT_1) == float));
80 static assert( is (typeof(7.0f^^3.1) == double));
81 static assert( is (typeof(7.0^^3.1f) == double));
82 static assert( is (typeof(7.0f^^3.1f) == float));
83 static assert( is (typeof(7.0f^^3.1L) == real));
84 static assert( is (typeof(7.0L^^3.1f) == real));
85 // Check typing for special cases
86 static assert( is (typeof(7.0f^^2) == float));
87 static assert( is (typeof(7.0f^^2.0) == double));
88 static assert( is (typeof(7.0f^^8.0) == double));
89 static assert( is (typeof(1^^0.5f) == float));
90 static assert( is (typeof(7^^0.5f) == float));
91 static assert( is (typeof(3L^^0.5) == double));
92 static assert( is (typeof(123^^17.0f) == float));
93
94 static assert(POW_SHORT_1 ^^ 2 == 9);
95 static assert(4.0 ^^ POW_SHORT_1 == 4.0*4.0*4.0);
96 static assert(4.0 ^^ 7.0 == 4.0*4.0*4.0*4.0*4.0*4.0*4.0);
97
98 // ^^ has higher precedence than multiply
99 static assert( 2 * 2 ^^ 3 + 1 == 17);
100 static assert( 2 ^^ 3 * 2 + 1 == 17);
101 // ^^ has higher precedence than negate
102 static assert( -2 ^^ 3 * 2 - 1 == -17);
103
104 // ^^ is right associative
105 static assert( 2 ^^ 3 ^^ 2 == 2 ^^ 9);
106 static assert( 2.0 ^^ -3 ^^ 2 == 2.0 ^^ -9);
107
108 // 1 ^^ n is always 1, even if n is negative
109 static assert( 1 ^^ -5.0 == 1);
110
111 // -1.0 ^^ n is either 1 or -1 if n is integral.
112 static assert( (-1.0) ^^ -5 == -1);
113 static assert( (-1.0) ^^ -4 == 1);
114 static assert( (-1.0) ^^ 0 == 1);
115 // -1.0 ^^ n is otherwise always NaN.
116 static assert( (-1.0) ^^ -5.5 is double.nan);
117 static assert( (-1.0) ^^ -4.4 is double.nan);
118 static assert( (-1.0) ^^ -0.1 is double.nan);
119
120 // n ^^ 0 is always 1
121 static assert( (-5) ^^ 0 == 1);
122
123 // n ^^ 1 is always n
124 static assert( 6.0 ^^ 1 == 6.0);
125
126 // n ^^ -1.0 gets transformed into 1.0 / n, even if n is negative
127 static assert( (-4) ^^ -1.0 == 1.0 / -4);
128 static assert( 9 ^^ -1.0 == 1.0 / 9);
129
130 // Other integers raised to negative powers create an error
131 static assert( !is(typeof(2 ^^ -5)));
132 static assert( !is(typeof((-2) ^^ -4)));
133
134 // https://issues.dlang.org/show_bug.cgi?id=3535
135 struct StructWithCtor
136 {
137 this(int _n) {
138 n = _n; x = 5;
139 }
140 this(int _n, float _x) {
141 n = _n; x = _x;
142 }
143 int n;
144 float x;
145 }
146
147 int containsAsm()
148 {
149 version (D_InlineAsm_X86)
150 asm { nop; }
151 else version (D_InlineAsm_X86_64)
152 asm { nop; }
153 return 0;
154 }
155
156 enum A = StructWithCtor(1);
157 enum B = StructWithCtor(7, 2.3);
158
159 static assert(A.n == 1);
160 static assert(A.x == 5.0);
161 static assert(B.n == 7);
162 static assert(B.x == 2.3);
163
164 int bazra(int x)
165 {
166 StructWithCtor p = StructWithCtor(4);
167 return p.n ^^ 3;
168
169 }
170
171 static assert(bazra(14)==64);
172
173 void moreCommaTests()
174 {
175 (containsAsm(), containsAsm());
176 auto k = containsAsm();
177 for (int i=0; i< k^^2; i+=StructWithCtor(1).n) {}
178 }
179
180 // Test copy constructors
181 struct CopyTest {
182 double x;
183 this(double a) { x = a * 10.0;}
184 this(this) { x+=2.0;}
185 }
186
187 struct CopyTest2
188 {
189 int x; int x1; int x2; int x3;
190 this(int a) { x = a * 2; x1 = 3;}
191 this(this) { x1+=17;}
192 }
193
194
195 const CopyTest z = CopyTest(5.3);
196 /+
197 // TODO: This is not yet supported. But it
198 // generates an error message instead of wrong-code.
199 const CopyTest w = z;
200 static assert(z.x==55.0);
201 +/
202
203 int copytest1()
204 {
205 CopyTest z = CopyTest(3.4);
206 CopyTest w = z;
207 assert(w.x == 36.0);
208 CopyTest2 q = CopyTest2(7);
209 CopyTest2 q2 = q;
210 CopyTest2 q3 = q2;
211 assert(q3.x1 == 37);
212
213 return 123;
214 }
215 static assert(copytest1()==123);
216
217 // This must not cause a segfault
218 alias int FILTH;
219 struct Filth
220 {
221 struct Impl
222 {
223 FILTH * handle = null;
224 this(FILTH* h, uint r, string n)
225 {
226 handle = h;
227 }
228 }
229 Impl * p;
230
231 this(string name, in char[] stdioOpenmode = "rb")
232 {
233 }
234
235 ~this()
236 {
237 if (!p) return;
238 }
239
240 this(this)
241 {
242 if (!p) return;
243 }
244 }
245 struct InputByChar
246 {
247 private Filth _f;
248
249 this(Filth f)
250 {
251 _f = f;
252 }
253 }
254
255
256 static int nastyForCtfe=4;
257
258 // Can't use a global variable
259 static assert(!is(typeof( (){ static assert(0!=nastyForCtfe^^2); })));
260
261 int anotherPowTest()
262 {
263 double x = 5.0;
264 return x^^4 > 2.0 ? 3: 2;
265 }
266
267 /************************************/
268
269 void main()
270 {
271 assert(!__ctfe);
272 assert(magicVariable()==2);
273 test11159();
274 }