1 // RUNNABLE_PHOBOS_TEST
2 // PERMUTE_ARGS:
3 
magicVariable()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 
main()15 void main()
16 {
17   assert(!__ctfe);
18   assert(magicVariable()==2);
19 }
20 
21 // bug 991 -- invalid.
22 // bug 3500 -- is this related to 2127?
23 
24 // Tests for ^^
25 // TODO: These tests should not require import std.math.
26 
27 import std.math;
28 // Test float ^^ int
29 static assert( 27.0 ^^ 5 == 27.0 * 27.0 * 27.0 * 27.0 * 27.0);
30 static assert( 2.0 ^^ 3 == 8.0);
31 
32 static assert( 2.0 ^^ 4 == 16.0);
33 static assert( 2 ^^ 4 == 16);
34 
35 // Check the typing rules.
36 static assert( is (typeof(2.0^^7) == double));
37 static assert( is (typeof(7^^3) == int));
38 
39 static assert( is (typeof(7L^^3) == long));
40 static assert( is (typeof(7^^3L) == long));
41 enum short POW_SHORT_1 = 3;
42 enum short POW_SHORT_3 = 7;
43 static assert( is (typeof(POW_SHORT_1 * POW_SHORT_1) ==
44 typeof(POW_SHORT_1*POW_SHORT_1)));
45 
46 static assert( is (typeof(7.0^^3) == double));
47 static assert( is (typeof(7.0L^^3) == real));
48 static assert( is (typeof(7.0f^^3) == float));
49 static assert( is (typeof(POW_SHORT_1^^3.1) == double));
50 static assert( is (typeof(POW_SHORT_1^^3.1f) == float));
51 static assert( is (typeof(2.1f ^^ POW_SHORT_1) == float));
52 static assert( is (typeof(7.0f^^3.1) == double));
53 static assert( is (typeof(7.0^^3.1f) == double));
54 static assert( is (typeof(7.0f^^3.1f) == float));
55 static assert( is (typeof(7.0f^^3.1L) == real));
56 static assert( is (typeof(7.0L^^3.1f) == real));
57 // Check typing for special cases
58 static assert( is (typeof(7.0f^^2) == float));
59 static assert( is (typeof(7.0f^^2.0) == double));
60 static assert( is (typeof(7.0f^^8.0) == double));
61 static assert( is (typeof(1^^0.5f) == float));
62 static assert( is (typeof(7^^0.5f) == float));
63 static assert( is (typeof(3L^^0.5) == double));
64 static assert( is (typeof(123^^17.0f) == float));
65 
66 static assert(POW_SHORT_1 ^^ 2 == 9);
67 static assert(4.0 ^^ POW_SHORT_1 == 4.0*4.0*4.0);
68 static assert(4.0 ^^ 7.0 == 4.0*4.0*4.0*4.0*4.0*4.0*4.0);
69 
70 // ^^ has higher precedence than multiply
71 static assert( 2 * 2 ^^ 3 + 1 == 17);
72 static assert( 2 ^^ 3 * 2 + 1 == 17);
73 // ^^ has higher precedence than negate
74 static assert( -2 ^^ 3 * 2 - 1 == -17);
75 
76 // ^^ is right associative
77 static assert( 2 ^^ 3 ^^ 2 == 2 ^^ 9);
78 static assert( 2.0 ^^ -3 ^^ 2 == 2.0 ^^ -9);
79 
80 // 1 ^^ n is always 1, even if n is negative
81 static assert( 1 ^^ -5 == 1);
82 
83 // -1 ^^ n gets transformed into  n & 1 ? -1 : 1
84 // even if n is negative
85 static assert( (-1) ^^ -5 == -1);
86 static assert( (-1) ^^ -4 == 1);
87 static assert( (-1) ^^ 0 == 1);
88 
89 // n ^^ 0 is always 1
90 static assert( (-5) ^^ 0 == 1);
91 
92 // n ^^ 1 is always n
93 static assert( 6.0 ^^ 1 == 6.0);
94 
95 // n ^^ -1.0 gets transformed into 1.0 / n, even if n is negative
96 static assert( (-4) ^^ -1.0 == 1.0 / -4);
97 static assert( 9 ^^ -1.0 == 1.0 / 9);
98 
99 // Other integers raised to negative powers create an error
100 static assert( !is(typeof(2 ^^ -5)));
101 static assert( !is(typeof((-2) ^^ -4)));
102 
103 // Bug 3535
104 struct StructWithCtor
105 {
thisStructWithCtor106     this(int _n) {
107         n = _n; x = 5;
108     }
thisStructWithCtor109     this(int _n, float _x) {
110         n = _n; x = _x;
111     }
112     int n;
113     float x;
114 }
115 
containsAsm()116 int containsAsm()
117 {
118     version (D_InlineAsm_X86)
119         asm { nop; }
120     else version (D_InlineAsm_X86_64)
121         asm { nop; }
122     return 0;
123 }
124 
125 enum A = StructWithCtor(1);
126 enum B = StructWithCtor(7, 2.3);
127 
128 static assert(A.n == 1);
129 static assert(A.x == 5.0);
130 static assert(B.n == 7);
131 static assert(B.x == 2.3);
132 
bazra(int x)133 int bazra(int x)
134 {
135    StructWithCtor p = StructWithCtor(4);
136    return p.n ^^ 3;
137 
138 }
139 
140 static assert(bazra(14)==64);
141 
moreCommaTests()142 void moreCommaTests()
143 {
144    auto k = (containsAsm(), containsAsm());
145    for (int i=0; i< k^^2; i+=StructWithCtor(1).n) {}
146 }
147 
148 // Test copy constructors
149 struct CopyTest {
150    double x;
thisCopyTest151    this(double a) { x = a * 10.0;}
thisCopyTest152    this(this) {  x+=2.0;}
153 }
154 
155 struct CopyTest2
156 {
157    int x; int x1; int x2; int x3;
this(int a)158    this(int a) { x = a * 2; x1 = 3;}
this(this)159    this(this) {  x1+=17;}
160 }
161 
162 
163 const CopyTest z = CopyTest(5.3);
164 /+
165 // TODO: This is not yet supported. But it
166 // generates an error message instead of wrong-code.
167 const CopyTest w = z;
168 static assert(z.x==55.0);
169 +/
170 
copytest1()171 int copytest1()
172 {
173    CopyTest z = CopyTest(3.4);
174    CopyTest w = z;
175    assert(w.x == 36.0);
176    CopyTest2 q = CopyTest2(7);
177    CopyTest2 q2 = q;
178    CopyTest2 q3 = q2;
179    assert(q3.x1 == 37);
180 
181   return 123;
182 }
183 static assert(copytest1()==123);
184 
185 // This must not cause a segfault
186 alias int FILTH;
187 struct Filth
188 {
189      struct Impl
190     {
191         FILTH * handle = null;
thisFilth::Impl192         this(FILTH* h, uint r, string n)
193         {
194             handle = h;
195         }
196     }
197     Impl * p;
198 
199     this(string name, in char[] stdioOpenmode = "rb")
200     {
201     }
202 
~thisFilth203     ~this()
204     {
205         if (!p) return;
206     }
207 
thisFilth208     this(this)
209     {
210         if (!p) return;
211     }
212     }
213     struct InputByChar
214     {
215         private Filth _f;
216 
this(Filth f)217         this(Filth f)
218         {
219             _f = f;
220         }
221 }
222 
223 
224 static int nastyForCtfe=4;
225 
226 // Can't use a global variable
227 static assert(!is(typeof( (){ static assert(0!=nastyForCtfe^^2); })));
228 
anotherPowTest()229 int anotherPowTest()
230 {
231    double x = 5.0;
232    return x^^4 > 2.0 ? 3: 2;
233 }
234