| 1 |
/* |
| 2 |
RUNNABLE_PHOBOS_TEST |
| 3 |
PERMUTE_ARGS: |
| 4 |
RUN_OUTPUT: |
| 5 |
--- |
| 6 |
foo() |
| 7 |
foo() 2 |
| 8 |
foo() 3 |
| 9 |
foo() 4 |
| 10 |
Success |
| 11 |
--- |
| 12 |
*/ |
| 13 |
|
| 14 |
extern(C) int printf(const char*, ...); |
| 15 |
|
| 16 |
/************************************************/ |
| 17 |
|
| 18 |
int[string] a; |
| 19 |
|
| 20 |
size_t foo(immutable char [3] s) |
| 21 |
{ |
| 22 |
printf("foo()\n"); |
| 23 |
int[string] b; |
| 24 |
string[] key; |
| 25 |
int[] value; |
| 26 |
printf("foo() 2\n"); |
| 27 |
key = a.keys; |
| 28 |
printf("foo() 3\n"); |
| 29 |
value = a.values; |
| 30 |
printf("foo() 4\n"); |
| 31 |
return a.length + b.length; |
| 32 |
} |
| 33 |
|
| 34 |
void foo2() |
| 35 |
{ |
| 36 |
int[string] c; |
| 37 |
string[] key; |
| 38 |
int[] value; |
| 39 |
int i; |
| 40 |
|
| 41 |
assert(c.length == 0); |
| 42 |
key = c.keys; |
| 43 |
assert(key.length == 0); |
| 44 |
value = c.values; |
| 45 |
assert(value.length == 0); |
| 46 |
|
| 47 |
c["foo"] = 3; |
| 48 |
assert(c["foo"] == 3); |
| 49 |
assert(c.length == 1); |
| 50 |
key = c.keys; |
| 51 |
assert(key.length == 1); |
| 52 |
value = c.values; |
| 53 |
assert(value.length == 1); |
| 54 |
assert(value[0] == 3); |
| 55 |
|
| 56 |
c["bar"] = 4; |
| 57 |
assert(c["bar"] == 4); |
| 58 |
assert(c.length == 2); |
| 59 |
key = c.keys; |
| 60 |
assert(key.length == 2); |
| 61 |
value = c.values; |
| 62 |
assert(value.length == 2); |
| 63 |
|
| 64 |
const fooIndex = key[1] == "foo"; |
| 65 |
assert(key[fooIndex] == "foo" && value[fooIndex] == 3); |
| 66 |
assert(key[1 - fooIndex] == "bar" && value[1 - fooIndex] == 4); |
| 67 |
|
| 68 |
assert("foo" in c); |
| 69 |
c.remove("foo"); |
| 70 |
assert(!("foo" in c)); |
| 71 |
assert(c.length == 1); |
| 72 |
|
| 73 |
assert("bar" in c); |
| 74 |
c.remove("bar"); |
| 75 |
assert(!("bar" in c)); |
| 76 |
assert(c.length == 0); |
| 77 |
} |
| 78 |
|
| 79 |
void testaa() |
| 80 |
{ |
| 81 |
size_t i = foo("abc"); |
| 82 |
assert(i == 0); |
| 83 |
|
| 84 |
foo2(); |
| 85 |
} |
| 86 |
|
| 87 |
/************************************************/ |
| 88 |
|
| 89 |
void test1899() |
| 90 |
{ |
| 91 |
int[3][string] AA; |
| 92 |
int[3] x = [5,4,3]; |
| 93 |
AA["abc"] = x; |
| 94 |
assert(AA["abc"] == x); |
| 95 |
AA["def"] = [1,2,3]; |
| 96 |
assert(AA["def"]==[1,2,3]); |
| 97 |
} |
| 98 |
|
| 99 |
/************************************************/ |
| 100 |
|
| 101 |
void foo4523() |
| 102 |
{ |
| 103 |
int[string] aa = ["test":0, "test2":1]; |
| 104 |
|
| 105 |
bool found = aa.remove("test"); |
| 106 |
assert(found); |
| 107 |
bool notfound = aa.remove("nothing"); |
| 108 |
assert(!notfound); |
| 109 |
} |
| 110 |
|
| 111 |
void test4523() |
| 112 |
{ |
| 113 |
foo4523(); |
| 114 |
static assert({ foo4523(); return true; }()); |
| 115 |
} |
| 116 |
|
| 117 |
/************************************************/ |
| 118 |
// https://issues.dlang.org/show_bug.cgi?id=3825 |
| 119 |
|
| 120 |
import std.math; // necessary for ^^= |
| 121 |
void test3825() |
| 122 |
{ |
| 123 |
// Check for RangeError is thrown |
| 124 |
bool thrown(T)(lazy T cond) |
| 125 |
{ |
| 126 |
import core.exception; |
| 127 |
bool f = false; |
| 128 |
try { |
| 129 |
cond(); |
| 130 |
} catch (RangeError e) { f = true; } |
| 131 |
return f; |
| 132 |
} |
| 133 |
|
| 134 |
int[int] aax; |
| 135 |
int[][int] aay; |
| 136 |
|
| 137 |
aax = null, aay = null; |
| 138 |
assert(thrown(aax[0])); |
| 139 |
assert(thrown(aax[0] = aax[0])); // rhs throws |
| 140 |
assert(thrown(aax[0] += aax[0])); // rhs throws |
| 141 |
assert(thrown(aax[0] ^^= aax[0])); // rhs throws |
| 142 |
assert(thrown(aay[0] ~= aay[0])); // rhs throws |
| 143 |
aax = null; aax[0] = 1; assert(aax[0] == 1); // setting aax[0] is OK |
| 144 |
aax = null; aax[0] += 1; assert(aax[0] == +1); // setting aax[0] to 0 and modify it is OK |
| 145 |
aax = null; aax[0] ^^= 1; assert(aax[0] == 0); // setting aax[0] to 0 and modify it is OK |
| 146 |
aay = null; aay[0] ~= []; assert(aay[0] == []); // setting aay[0] to 0 and modify it is OK |
| 147 |
aax = null; ++aax[0]; assert(aax[0] == +1); // setting aax[0] to 0 and modify it is OK |
| 148 |
aax = null; --aax[0]; assert(aax[0] == -1); // setting aax[0] to 0 and modify it is OK |
| 149 |
|
| 150 |
aax = [0:0], aay = [0:null]; |
| 151 |
assert(thrown(aax[aax[1]] = 1)); // accessing aax[1] in key part throws |
| 152 |
assert(thrown(aax[aax[1]] += 1)); // accessing aax[1] in key part throws |
| 153 |
assert(thrown(aax[aax[1]] ^^= 1)); // accessing aax[1] in key part throws |
| 154 |
assert(thrown(aay[aax[1]] ~= [])); // accessing aax[1] in key part throws |
| 155 |
|
| 156 |
//assert(thrown(aax[( aax[1], 0)] = 0)); |
| 157 |
/* accessing aax[1] in key part, why doesn't throw? |
| 158 |
* Because, in aax[(aax[1], 0)], aax[1] is in lhs of comma expression, and is treated |
| 159 |
* it has no side effect. Then optimizer eliminate it completely, and |
| 160 |
* whole expression succeed to run in runtime. */ |
| 161 |
int n = 0; |
| 162 |
assert(thrown(aax[((){ n=aax[1]; return 0;}())] = 0)); // accessing aax[1] in key part, throws OK |
| 163 |
|
| 164 |
// This works as expected. |
| 165 |
int[int][int] aaa; |
| 166 |
aaa[0][0] = 0; assert(aaa[0][0] == 0); // setting aaa[0][0] is OK |
| 167 |
|
| 168 |
// real test cases |
| 169 |
void bug3825() |
| 170 |
{ |
| 171 |
string[] words = ["how", "are", "you", "are"]; |
| 172 |
|
| 173 |
int[string] aa1; |
| 174 |
foreach (w; words) |
| 175 |
aa1[w] = ((w in aa1) ? (aa1[w] + 1) : 2); |
| 176 |
//writeln(aa1); // Prints: [how:1,you:1,are:2] |
| 177 |
|
| 178 |
int[string] aa2; |
| 179 |
foreach (w; words) |
| 180 |
if (w in aa2) |
| 181 |
aa2[w]++; |
| 182 |
else |
| 183 |
aa2[w] = 2; |
| 184 |
//writeln(aa2); // Prints: [how:2,you:2,are:3] |
| 185 |
|
| 186 |
assert(aa1 == aa2); |
| 187 |
assert(aa1 == ["how":2, "you":2, "are":3]); |
| 188 |
assert(aa2 == ["how":2, "you":2, "are":3]); |
| 189 |
} |
| 190 |
void bug5021() |
| 191 |
{ |
| 192 |
int func() |
| 193 |
{ |
| 194 |
throw new Exception("It's an exception."); |
| 195 |
} |
| 196 |
|
| 197 |
int[string] arr; |
| 198 |
try |
| 199 |
{ |
| 200 |
arr["hello"] = func(); |
| 201 |
} |
| 202 |
catch(Exception e) |
| 203 |
{ |
| 204 |
} |
| 205 |
assert(arr.length == 0); |
| 206 |
} |
| 207 |
void bug7914() |
| 208 |
{ |
| 209 |
size_t[ubyte] aa; |
| 210 |
aa[0] = aa.length; |
| 211 |
assert(aa[0] == 0); |
| 212 |
} |
| 213 |
void bug8070() |
| 214 |
{ |
| 215 |
Object[string] arr; |
| 216 |
|
| 217 |
class A |
| 218 |
{ |
| 219 |
this() |
| 220 |
{ |
| 221 |
// at this point: |
| 222 |
assert("x" !in arr); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
arr["x"] = new A(); |
| 227 |
} |
| 228 |
bug3825(); |
| 229 |
bug5021(); |
| 230 |
bug7914(); |
| 231 |
bug8070(); |
| 232 |
} |
| 233 |
|
| 234 |
void test3825x() |
| 235 |
{ |
| 236 |
return; // depends on AA implementation |
| 237 |
static int ctor, cpctor, dtor; |
| 238 |
|
| 239 |
static struct S |
| 240 |
{ |
| 241 |
this(int) { ++ctor; } |
| 242 |
this(this) { ++cpctor; } |
| 243 |
~this() { ++dtor; } |
| 244 |
} |
| 245 |
|
| 246 |
int[S] aa; |
| 247 |
{ |
| 248 |
auto value = S(1); |
| 249 |
assert(ctor==1 && cpctor==0 && dtor==0); |
| 250 |
|
| 251 |
ref getRef(ref S s = value) { return s; } |
| 252 |
auto getVal() { return value; } |
| 253 |
|
| 254 |
aa[value] = 10; |
| 255 |
assert(ctor==1 && cpctor==1 && dtor==0); |
| 256 |
|
| 257 |
aa[getRef()] += 1; |
| 258 |
assert(ctor==1 && cpctor==1 && dtor==0); |
| 259 |
|
| 260 |
aa[getVal()] += 1; |
| 261 |
assert(ctor==1 && cpctor==2 && dtor==1); |
| 262 |
} |
| 263 |
assert(ctor==1 && cpctor==2 && dtor==2); |
| 264 |
assert(ctor + cpctor - aa.length == dtor); |
| 265 |
} |
| 266 |
|
| 267 |
/************************************************/ |
| 268 |
// https://issues.dlang.org/show_bug.cgi?id=10106 |
| 269 |
|
| 270 |
struct GcPolicy10106 {} |
| 271 |
|
| 272 |
struct Uint24Array10106(SP = GcPolicy10106) |
| 273 |
{ |
| 274 |
this(this) {} |
| 275 |
} |
| 276 |
|
| 277 |
struct InversionList10106(SP = GcPolicy10106) |
| 278 |
{ |
| 279 |
Uint24Array10106!SP data; |
| 280 |
} |
| 281 |
|
| 282 |
alias InversionList10106!GcPolicy10106 CodepointSet10106; |
| 283 |
|
| 284 |
struct PropertyTable10106 |
| 285 |
{ |
| 286 |
CodepointSet10106[string] table; |
| 287 |
} |
| 288 |
|
| 289 |
/************************************************/ |
| 290 |
// strip inout in key and value types |
| 291 |
void testinout() |
| 292 |
{ |
| 293 |
inout(long) func1(inout(long[][int]) aa) |
| 294 |
{ |
| 295 |
return aa[0][0]; |
| 296 |
} |
| 297 |
long[][int] a = [ 0 : [42] ]; |
| 298 |
long b = func1(a); |
| 299 |
assert(b == 42); |
| 300 |
} |
| 301 |
|
| 302 |
/************************************************/ |
| 303 |
// type info generated for enum creation in InExp? |
| 304 |
void testinenum() |
| 305 |
{ |
| 306 |
enum string[string] aa = [ "one" : "un", "two" : "deux" ]; |
| 307 |
assert("one" in aa); |
| 308 |
} |
| 309 |
|
| 310 |
/************************************************/ |
| 311 |
|
| 312 |
int main() |
| 313 |
{ |
| 314 |
testaa(); |
| 315 |
test1899(); |
| 316 |
test4523(); |
| 317 |
test3825(); |
| 318 |
test3825x(); |
| 319 |
testinout(); |
| 320 |
testinenum(); |
| 321 |
|
| 322 |
printf("Success\n"); |
| 323 |
return 0; |
| 324 |
} |