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