1 // RUNNABLE_PHOBOS_TEST 2 // PERMUTE_ARGS: 3 magicVariable()4int 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()15void 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()116int 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)133int 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()142void moreCommaTests() 143 { 144 (containsAsm(), containsAsm()); 145 auto k = containsAsm(); 146 for (int i=0; i< k^^2; i+=StructWithCtor(1).n) {} 147 } 148 149 // Test copy constructors 150 struct CopyTest { 151 double x; thisCopyTest152 this(double a) { x = a * 10.0;} thisCopyTest153 this(this) { x+=2.0;} 154 } 155 156 struct CopyTest2 157 { 158 int x; int x1; int x2; int x3; this(int a)159 this(int a) { x = a * 2; x1 = 3;} this(this)160 this(this) { x1+=17;} 161 } 162 163 164 const CopyTest z = CopyTest(5.3); 165 /+ 166 // TODO: This is not yet supported. But it 167 // generates an error message instead of wrong-code. 168 const CopyTest w = z; 169 static assert(z.x==55.0); 170 +/ 171 copytest1()172int copytest1() 173 { 174 CopyTest z = CopyTest(3.4); 175 CopyTest w = z; 176 assert(w.x == 36.0); 177 CopyTest2 q = CopyTest2(7); 178 CopyTest2 q2 = q; 179 CopyTest2 q3 = q2; 180 assert(q3.x1 == 37); 181 182 return 123; 183 } 184 static assert(copytest1()==123); 185 186 // This must not cause a segfault 187 alias int FILTH; 188 struct Filth 189 { 190 struct Impl 191 { 192 FILTH * handle = null; thisFilth::Impl193 this(FILTH* h, uint r, string n) 194 { 195 handle = h; 196 } 197 } 198 Impl * p; 199 200 this(string name, in char[] stdioOpenmode = "rb") 201 { 202 } 203 ~thisFilth204 ~this() 205 { 206 if (!p) return; 207 } 208 thisFilth209 this(this) 210 { 211 if (!p) return; 212 } 213 } 214 struct InputByChar 215 { 216 private Filth _f; 217 this(Filth f)218 this(Filth f) 219 { 220 _f = f; 221 } 222 } 223 224 225 static int nastyForCtfe=4; 226 227 // Can't use a global variable 228 static assert(!is(typeof( (){ static assert(0!=nastyForCtfe^^2); }))); 229 anotherPowTest()230int anotherPowTest() 231 { 232 double x = 5.0; 233 return x^^4 > 2.0 ? 3: 2; 234 } 235