1 // PERMUTE_ARGS: -fPIC
2 
3 /* Test associative arrays */
4 
5 extern(C) int printf(const char*, ...);
6 extern(C) int memcmp(const void *s1, const void *s2, size_t n);
7 
8 import core.memory;  // for GC.collect
9 import std.random;   // for uniform random numbers
10 
11 /************************************************/
12 
13 int nametable[char[]];
14 
insert(string name,int value)15 void insert(string name, int value)
16 {
17     nametable[name] = value;
18 }
19 
retrieve(string name)20 int retrieve(string name)
21 {
22     return nametable[name];
23 }
24 
test1()25 void test1()
26 {   int v;
27 
28     printf("test1.a\n");
29     insert("hello", 1);
30     printf("test1.b\n");
31     insert("world", 2);
32     printf("test1.c\n");
33     v = retrieve("hello");
34     assert(v == 1);
35     v = retrieve("world");
36     assert(v == 2);
37     v = retrieve("world");
38     assert(v == 2);
39 
40     nametable.rehash;
41     v = retrieve("world");
42     assert(v == 2);
43 }
44 
45 /************************************************/
46 
47 
test2()48 void test2()
49 {
50     int[string] aa;
51     string[] keys;
52     int[] values;
53 
54     printf("test2()\n");
55 
56     /*************/
57 
58     assert(aa == null);
59     assert(aa.length == 0);
60 
61     keys = aa.keys;
62     assert(keys.length == 0);
63 
64     values = aa.values;
65     assert(values.length == 0);
66 
67     aa.rehash;
68     assert(aa.length == 0);
69 
70     /*************/
71 
72     aa["hello"] = 3;
73     assert(aa["hello"] == 3);
74     aa["hello"]++;
75     assert(aa["hello"] == 4);
76 
77     assert(aa.length == 1);
78 
79     keys = aa.keys;
80     assert(keys.length == 1);
81     assert(memcmp(keys[0].ptr, cast(char*)"hello", 5) == 0);
82 
83     values = aa.values;
84     assert(values.length == 1);
85     assert(values[0] == 4);
86 
87     aa.rehash;
88     assert(aa.length == 1);
89     assert(aa["hello"] == 4);
90 }
91 
92 /************************************************/
93 
test4()94 void test4()
95 {
96     int[const(ubyte)[]] b;
97     const(ubyte)[] x;
98     b[x] = 3;
99     assert(b[x] == 3);
100 }
101 
102 /************************************************/
103 
test5()104 void test5()
105 {
106     int[immutable(short)[]] b;
107     immutable(short)[] x;
108     b[x] = 3;
109     assert(b[x] == 3);
110 }
111 
112 /************************************************/
113 
test6()114 void test6()
115 {
116     int[const(int)[]] b;
117     const(int)[] x;
118     b[x] = 3;
119     assert(b[x] == 3);
120 }
121 
122 /************************************************/
123 
test7()124 void test7()
125 {
126     int[immutable(uint)[]] b;
127     immutable(uint)[] x;
128     b[x] = 3;
129     assert(b[x] == 3);
130 }
131 
132 /************************************************/
133 
test8()134 void test8()
135 {
136     int[immutable(long)[]] b;
137     immutable(long)[] x;
138     b[x] = 3;
139     assert(b[x] == 3);
140 }
141 
142 /************************************************/
143 
test9()144 void test9()
145 {
146     int[immutable(ulong)[]] b;
147     immutable(ulong)[] x;
148     b[x] = 3;
149     assert(b[x] == 3);
150 }
151 
152 /************************************************/
153 
154 class A10 {}
155 
156 int[immutable(A10)[]] foo10;
157 
test10()158 void test10()
159 {
160     auto key = new immutable(A10)[2];
161 
162     cast()(key[0]) = new A10();
163     foo10[key] = 0;
164     assert(key in foo10);
165     assert(!(key !in foo10));
166 }
167 
168 
169 /************************************************/
170 
171 struct Value
172 {
173     uint x,y,z,t;
174 }
175 
176 struct Key
177 {
178     int a,b,c,d;
179 
180     static int hash, cmp, equals;
181 
toHash()182     size_t toHash() const
183     {
184         hash = 1;
185         return a + b + c + d;
186     }
187 
opCmp(ref const Key s)188     int opCmp(ref const Key s) const
189     {
190         cmp = 1;
191         int x;
192 
193         x = a - s.a;
194         if (x == 0)
195         {   x = b - s.b;
196             if (x == 0)
197             {   x = c - s.c;
198                 if (x == 0)
199                     x = d - s.d;
200             }
201         }
202         return x;
203     }
204 
opEquals(ref const Key s)205     bool opEquals(ref const Key s) const
206     {
207         printf("opEquals()\n");
208         equals = 1;
209         return (a == s.a && b == s.b && c == s.c && d == s.d);
210     }
211 }
212 
test11()213 void test11()
214 {
215     Value[Key] table;
216 
217     Value* p;
218     Value v;
219     Value r;
220     Key k;
221 
222     v.x = 7;
223     v.y = 8;
224     v.z = 9;
225     v.t = 10;
226 
227     k.a = 1;
228     k.b = 2;
229     k.c = 3;
230     k.d = 4;
231 
232     p = k in table;
233     assert(!p);
234 
235     table[k] = v;
236     p = k in table;
237     assert(p);
238 
239     table.rehash;
240     p = k in table;
241     assert(p);
242 
243     r = table[k];
244     assert(v == r);
245 
246     table.remove(k);
247     assert(!(k in table));
248 
249     printf("Key.hash = %d\n", Key.hash);
250     assert(Key.hash == 1);
251     printf("Key.cmp = %d\n", Key.cmp);
252     printf("Key.equals = %d\n", Key.equals);
253     assert(Key.cmp == 1 && !Key.equals || !Key.cmp && Key.equals == 1);
254 }
255 
256 
257 /************************************************/
258 
259 struct S12
260 {
261     byte number;
262     char[] description;
263     char[] font_face;
264     byte font_size;
265     ushort flags;
266     int colour_back;
267     int colour_fore;
268     byte charset;
269 }
270 
test12()271 void test12()
272 {
273     S12[] x;
274     printf("size %d\n",S12.sizeof);
275     printf("align %d\n",S12.alignof);
276     printf("offset %d\n",S12.description.offsetof);
277 
278     for (int i=0;i<3;i++) {
279         S12 s;
280         s.font_face="font face".dup;
281         x ~= s;
282     }
283 
284 /* works fine
285     S12 s;
286     s.font_face="font face".dup;
287     x ~= s;
288     s.font_face="font face".dup;
289     x ~= s;
290     s.font_face="font face".dup;
291     x ~= s;
292     s.font_face="font face".dup;
293     x ~= s;
294 */
295     GC.collect();
296     printf("%.*s\n",x[0].font_face.length,x[0].font_face.ptr);
297     printf("%.*s\n",x[1].font_face.length,x[1].font_face.ptr);
298 }
299 
300 
301 /************************************************/
302 
test13()303 void test13()
304 {
305     int[string] array;
306     array["eins"]=1;
307     array["zwei"]=2;
308     array["drei"]=3;
309 
310     assert(array.length==3);
311 
312     int[string] rehashed=array.rehash;
313     assert(rehashed is array);
314 
315     string[] key = array.keys;
316     assert(key.length==3);
317 
318     bool have[3];
319 
320     assert(!have[0]);
321     assert(!have[1]);
322     assert(!have[2]);
323 
324     foreach(string value; key){
325         switch(value){
326             case "eins":{
327                 have[0]=true;
328                 break;
329             }case "zwei":{
330                 have[1]=true;
331                 break;
332             }case "drei":{
333                 have[2]=true;
334                 break;
335             }default:{
336                 assert(0);
337             }
338         }
339     }
340 
341     assert(have[0]);
342     assert(have[1]);
343     assert(have[2]);
344 }
345 
346 /************************************************/
347 
test14()348 void test14()
349 {
350     int[char[]] aa;
351 
352     aa["hello"] = 3;
353     assert(aa["hello"] == 3);
354     assert("hello" in aa);
355     //delete aa["hello"];
356     aa.remove("hello");
357     assert(!("hello" in aa));
358 }
359 
360 /************************************************/
361 
362 class SomeClass
363 {
this(char value)364     this(char value)
365     {
366         printf("class created\n");
367         _value = value;
368     }
369 
~this()370     ~this()
371     {
372         printf("class killed (%d)\n", _value);
373     }
374 
value()375     char value()
376     {
377         return _value;
378     }
379 
380     private
381     {
382         char _value;
383     }
384 }
385 
386 char[] allChars = [ 'a', 'b', 'c', 'e', 'z', 'q', 'x' ];
387 
388 SomeClass[char] _chars;
389 
_realLoad()390 void _realLoad()
391 {
392     printf("Loading...\n");
393     foreach(char ch; allChars)
394     {
395         _chars[ch] = new SomeClass(ch);
396     }
397 }
398 
399 
400 
test15()401 void test15()
402 {
403     _realLoad();
404     int j;
405 
406     for (int i = 0; i < 10000; i++)
407     {
408         foreach(char ch; allChars)
409         {
410             SomeClass obj = _chars[ch];
411             j += obj.value;
412         }
413         GC.collect();
414     }
415     printf("j = %d\n", j);
416     assert(j == 7500000);
417 }
418 
419 
420 /************************************************/
421 
test16()422 void test16()
423 {
424     int[int] aa;
425 
426     Random gen;
427     for (int i = 0; i < 50000; i++)
428     {
429         int key = uniform(0, int.max, gen);
430         int value = uniform(0, int.max, gen);
431 
432         aa[key] = value;
433     }
434 
435     int[] keys = aa.keys;
436     assert(keys.length == aa.length);
437 
438     int j;
439     foreach (k; keys)
440     {
441         assert(k in aa);
442         j += aa[k];
443     }
444     printf("test16 = %d\n", j);
445 
446     int m;
447     foreach (k, v; aa)
448     {
449         assert(k in aa);
450         assert(aa[k] == v);
451         m += v;
452     }
453     assert(j == m);
454 
455     m = 0;
456     foreach (v; aa)
457     {
458         m += v;
459     }
460     assert(j == m);
461 
462     int[] values = aa.values;
463     assert(values.length == aa.length);
464 
465     foreach(k; keys)
466     {
467         aa.remove(k);
468     }
469     assert(aa.length == 0);
470 
471     for (int i = 0; i < 1000; i++)
472     {
473         int key2 = uniform(0, int.max, gen);
474         int value2 = uniform(0, int.max, gen);
475 
476         aa[key2] = value2;
477     }
478     foreach(k; aa)
479     {
480         if (k < 1000)
481             break;
482     }
483     foreach(k, v; aa)
484     {
485         if (k < 1000)
486             break;
487     }
488 }
489 
490 /************************************************/
491 
dummy17()492 void dummy17()
493 {
494 }
495 
496 int bb17[string];
497 
foo17()498 int foo17()
499 {
500     foreach(string s, int i; bb17)
501     {
502         dummy17();
503     }
504 
505     bb17["a"] = 1;
506 
507     foreach(int b; bb17)
508     {
509         try{
510             throw new Error("foo");
511         }catch(Error e){
512             assert(e);
513             return 0;
514         }catch{
515             assert(0);
516         }
517         assert(0);
518     }
519 
520     assert(0);
521 }
522 
test17()523 void test17()
524 {
525     int i = foo17();
526     printf("foo17 = %d\n", i);
527     assert(i == 0);
528 }
529 
530 /************************************************/
531 
test18()532 void test18()
533 {
534     int[uint] aa;
535 
536     aa[1236448822] = 0;
537     aa[2716102924] = 1;
538     aa[ 315901071] = 2;
539 
540     aa.remove(1236448822);
541     printf("%d\n", aa[2716102924]);
542     assert(aa[2716102924] == 1);
543 }
544 
545 
546 /************************************************/
547 
test19()548 void test19()
549 {
550     immutable(char[5])[int] aa = ([3:"hello", 4:"betty"]);
551 
552     assert(aa[3] == "hello");
553     assert(aa[4] == "betty");
554 
555     auto keys = aa.keys;
556     printf("%d\n", keys[0]);
557     printf("%d\n", keys[1]);
558 
559     auto vs = aa.values;
560     printf("%.*s\n", vs[0].length, vs[0].ptr);
561     printf("%.*s\n", vs[1].length, vs[1].ptr);
562 
563     string aavalue_typeid = typeid(typeof(aa.values)).toString();
564     printf("%.*s\n", aavalue_typeid.length, aavalue_typeid.ptr);
565 
566     printf("%.*s\n", aa[3].length, aa[3].ptr);
567     printf("%.*s\n", aa[4].length, aa[4].ptr);
568 }
569 
570 /************************************************/
571 
test20()572 void test20()
573 {
574     string[int] aa = ([3:"hello", 4:"betty"]);
575 
576     assert(aa[3] == "hello");
577     assert(aa[4] == "betty");
578 
579     auto keys = aa.keys;
580     printf("%d\n", keys[0]);
581     printf("%d\n", keys[1]);
582 
583     auto values = aa.values;
584     printf("%.*s\n", values[0].length, values[0].ptr);
585     printf("%.*s\n", values[1].length, values[1].ptr);
586 
587     string aavalue_typeid = typeid(typeof(aa.values)).toString();
588     printf("%.*s\n", aavalue_typeid.length, aavalue_typeid.ptr);
589 
590     printf("%.*s\n", aa[3].length, aa[3].ptr);
591     printf("%.*s\n", aa[4].length, aa[4].ptr);
592 }
593 
594 /************************************************/
595 
test21()596 void test21()
597 {
598     ushort[20] key = 23;
599     int[ushort[20]] aa;
600     aa[key] = 42;
601     auto x = aa[key];
602     assert(x == 42);
603     printf("foo\n");
604 }
605 
606 /************************************************/
607 
test22()608 void test22()
609 {
610     int[string] stopWords = [ "abc"[]:1 ];
611     assert("abc"[] in stopWords);
612 }
613 
614 /************************************************/
615 
test23()616 void test23()
617 {
618     uint[char[]][] fractal;
619     fractal.length = 10;
620 }
621 
622 /************************************************/
623 
test24()624 void test24()
625 {
626     int[string] x;
627     char[] y;
628     if (y in x)
629     {
630         int z = x[y];
631     }
632 }
633 
634 /************************************************/
635 
test25()636 void test25()
637 {
638     string[string] aa;
639     foreach (k,v; aa)
640     {
641     }
642 }
643 
644 /************************************************/
645 
646 class Tag
647 {
648     string[string] attr;
649 }
650 
foo26(const (Tag)tag_)651 void foo26(const(Tag) tag_)
652 {
653     foreach(k,v;tag_.attr) { }
654 }
655 
test26()656 void test26()
657 {
658 }
659 
660 /************************************************/
661 
test27()662 void test27()
663 {
664     int[int] s;
665     s = s.init;
666 }
667 
668 /************************************************/
669 
test28()670 void test28()
671 {
672     auto a1 = [ 1:10.0, 2:20, 3:15 ];
673     auto a2 = [ 1:10.0, 2:20, 3:15 ];
674     assert(a1 !is a2);
675     assert(a1 == a2);
676     a2[7] = 23;
677     assert(a1 != a2);
678     a2.remove(7);
679     assert(a1 == a2);
680     a1.rehash;
681     assert(a1 == a2);
682     a2[2] = 18;
683     assert(a1 != a2);
684 }
685 
686 /************************************************/
687 
test29()688 void test29()
689 {
690     auto gammaFunc = [-1.5:2.363, -0.5:-3.545, 0.5:1.772];
691 
692     // write all keys
693     foreach (k; gammaFunc.byKey()) {
694        printf("%f\n", k);
695     }
696 
697     // write all values
698     foreach (v; gammaFunc.byValue()) {
699        printf("%f\n", v);
700     }
701 }
702 
703 /************************************************/
704 
toString(int value)705 string toString(int value)
706 {
707     char[] result = new char[12];
708 
709     uint ndigits = 0;
710     do
711     {
712         const c = cast(char) ((value % 10) + '0');
713         value /= 10;
714         ndigits++;
715         result[$ - ndigits] = c;
716     }
717     while (value);
718     return cast(string) result[$ - ndigits .. $];
719 }
720 
test30()721 void test30()
722 {
723     int[string] aa;
724     for(int i = 0; i < 100000; i++)
725     {
726         string s = toString(i);
727         aa[s] = i;
728     }
729 }
730 
731 /************************************************/
732 
test31()733 void test31()
734 {
735     int[int] test;
736     test[0] = 0;
737     test[1] = 1;
738     test[2] = 2;
739 
740     bool flag = false;
741     foreach( k, v; test){
742         //printf("loop: %d %d\n", k, v);
743         assert(!flag);
744         flag = true;
745         break;
746     }
747 }
748 
749 /************************************************/
750 
test32()751 void test32()
752 {
753     uint[ushort] aa;
754     aa[1] = 1;
755     aa[2] = 2;
756     aa[3] = 3;
757     aa[4] = 4;
758     aa[5] = 5;
759     foreach(v; aa)
760     {
761         printf("%x\n", v);
762         assert(v >= 1 && v <= 5);
763     }
764 }
765 
766 /************************************************/
767 
768 template ICE3996(T : V[K], K, V) {}
769 
770 struct Bug3996 {}
771 
772 static assert(!is( ICE3996!(Bug3996) ));
773 
774 /************************************************/
775 
bug4826c(T)776 void bug4826c(T)(int[int] value, T x) {}
777 
test4826c()778 void test4826c()
779 {
780     AssociativeArray!(int, int) z;
781     bug4826c(z,1);
782 }
783 
784 /************************************************/
785 // 5131
786 
787 struct ICE5131
788 {
thisICE5131789     this(int n) {}
opAssignICE5131790     ICE5131 opAssign(int x) { return this; }
791 }
792 
test5131()793 void test5131()
794 {
795     ICE5131[string] a;
796     a["ICE?"] = 1;  // call ctor
797     a["ICE?"] = 1;  // call opAssign
798 }
799 
800 /************************************************/
801 // 6178
802 
test6178a()803 bool test6178a()
804 {
805     // AA value setting through identity opAssign
806 
807     int assign = 0;
808     struct S
809     {
810         int value = 10;
811 
812         void opAssign(S rhs)
813         {
814             ++assign;
815             assert(value == 10);
816         }
817     }
818 
819     int count = 0;
820     int makeKey() { return ++count; }
821 
822     S[int] aa;
823     assert(aa.length == 0);
824 
825     aa[makeKey()] = S();
826     assert(assign == 0);
827     assert(aa.length == 1 && 1 in aa);
828 
829     aa[1] = S();
830     assert(assign == 1);
831     assert(aa.length == 1 && 1 in aa);
832 
833     return true;
834 }
835 
test6178b()836 bool test6178b()
837 {
838     // AA value setting through implicit ctor call + non-identity opAssign
839 
840     int ctor = 0;
841     int assign = 0;
842     struct S
843     {
844         int value = 10;
845 
846         @disable this();
847 
848         this(int n)
849         {
850             ++ctor;
851             assert(value == 10);
852             value = 20;
853         }
854         void opAssign(int rhs)
855         {
856             ++assign;
857             assert(value == 20);
858             assert(rhs == 30);
859             value = rhs;
860         }
861     }
862 
863     int count = 0;
864     int makeKey() { return ++count; }
865 
866     S[int] aa;
867     assert(aa.length == 0);
868 
869     aa[makeKey()] = 20;
870     assert(assign == 0 && ctor == 1 && count == 1);
871     assert(aa.length == 1 && (1 in aa));
872 
873     aa[1] = 30;
874     assert(assign == 1 && ctor == 1);
875     assert(aa.length == 1 && 1 in aa);
876 
877     return true;
878 }
879 
test6178c()880 bool test6178c()
881 {
882     // AA value setting through non-identity opAssign
883 
884     struct S
885     {
886         //this(int) {}
887         // not possible to perform implicit ctor call
888         void opAssign(int) {}
889     }
890 
891     S[int] aa;
892     assert(aa.length == 0);
893 
894     if (!__ctfe)
895     {
896         // currently CTFE does not support throwing RangeError
897         import core.exception : RangeError;
898         try { aa[1] = 1; assert(0); } catch (RangeError) {}
899 
900         // The above line is exactly same as:
901         try { aa[1].opAssign(1); assert(0); } catch (RangeError) {}
902     }
903     assert(aa.length == 0);
904 
905     aa[1] = S();
906     aa[1] = 1;
907     assert(aa.length == 1);
908 
909     return true;
910 }
911 
test6178d()912 bool test6178d()
913 {
914     // AA value setting through implicit ctor call + alias this
915 
916     int ctor;
917     struct S
918     {
919         this(int n) { ++ctor; value = n; }
920 
921         int value;
922         alias value this;
923     }
924 
925     S[int] aa;
926     assert(ctor == 0);
927     assert(aa.length == 0);
928 
929     aa[1] = 0;      // implicit ctor call + blit assign
930     assert(aa[1].value == 0 && ctor == 1);
931     assert(aa.length == 1);
932 
933     aa[1] = 1;      // set through alias this
934     assert(aa[1].value == 1 && ctor == 1);
935     assert(aa.length == 1);
936 
937     return true;
938 }
939 
test6178e()940 bool test6178e()
941 {
942     // AA value setting through alias this
943 
944     struct S
945     {
946         int value;
947         alias value this;
948     }
949 
950     S[int] aa;
951     assert(aa.length == 0);
952 
953     if (!__ctfe)
954     {
955         // currently CTFE does not support throwing RangeError
956         import core.exception : RangeError;
957         try { aa[1] = 1; assert(0); } catch (RangeError) {}
958 
959         // The above line is exactly same as:
960         try { aa[1].value = 1; assert(0); } catch (RangeError) {}
961     }
962     assert(aa.length == 0);
963 
964     aa[1] = S(0);   // construct + blit assign
965     assert(aa[1].value == 0 && aa.length == 1);
966 
967     aa[1] = 1;      // set through alias this
968     assert(aa[1].value == 1 && aa.length == 1);
969 
970     return true;
971 }
972 
test6178()973 void test6178()
974 {
975     static assert(test6178a()); // ctfe check
976     test6178a();                // runtime test
977 
978     static assert(test6178b());
979     test6178b();
980 
981     static assert(test6178c());
982     test6178c();
983 
984     static assert(test6178d());
985     test6178d();
986 
987     static assert(test6178e());
988     test6178e();
989 }
990 
test6178x()991 void test6178x()
992 {
993     return; // depends on AA implementation
994     static int ctor, cpctor, dtor;
995 
996     static struct S
997     {
998         this(int)  { ++ctor;   printf("ctor\n");   }
999         this(this) { ++cpctor; printf("cpctor\n"); }
1000         ~this()    { ++dtor;   printf("dtor\n");   }
1001     }
1002     static struct X
1003     {
1004         this(int) {}
1005         void opAssign(int) {}
1006     }
1007 
1008     X[S] aa1;
1009     S[int] aa2;
1010 
1011     {
1012         auto value = S(1);
1013         assert(ctor==1 && cpctor==0 && dtor==0);
1014 
1015         ref getRef(ref S s = value) { return s; }
1016         auto getVal() { return value; }
1017 
1018         aa1[value] = 10;
1019         assert(ctor==1 && cpctor==1 && dtor==0); //call copy ctor when we putting 'value' to aa1
1020 
1021         aa1[getRef()] = 20;
1022         assert(ctor==1 && cpctor==1 && dtor==0); //copy ctor wasn't called because we didn't create a new entry in aa, using an existing key
1023 
1024         aa1[getVal()] = 20;
1025         assert(ctor==1 && cpctor==2 && dtor==1); //call copy ctor and dtor, because we pass key by value
1026 
1027         aa2[1] = value;
1028         assert(ctor==1 && cpctor==3 && dtor==1); //call copy ctor when we putting `value` to aa2[1]
1029 
1030         aa2[2] = getRef();
1031         assert(ctor==1 && cpctor==4 && dtor==1); //call copy ctor when we putting `value` to aa2[2]
1032     }
1033     assert(ctor==1 && cpctor==4 && dtor==2);     //We've got 3 "S" instances that aren't destroyed yet: the key in aa1, aa2[1], aa2[2].
1034     assert(ctor + cpctor - aa2.length - aa1.length == dtor);
1035 }
1036 
1037 /************************************************/
1038 // 10595
1039 
1040 struct S10595
1041 {
1042     bool b = true;
1043 
testS105951044     bool test()
1045     {
1046         if (!b)  // note: must be a check, not 'return b;'
1047             return false;
1048 
1049         return true;
1050     }
1051 }
1052 
1053 struct Wrap10595
1054 {
1055     int i;
1056     alias i this;
1057     S10595 s;
1058 }
1059 
test10595()1060 void test10595()
1061 {
1062     {
1063         Wrap10595[int] wrap;
1064 
1065         wrap[0] = Wrap10595();
1066         wrap[0].i = 0;
1067 
1068         assert(wrap[0].s.test());  // ok
1069     }
1070 
1071     {
1072         Wrap10595[int] wrap;
1073 
1074         wrap[0] = Wrap10595();
1075         wrap[0] = 0;  // note: using 'alias this' to assign
1076 
1077         assert(wrap[0].s.test());  // failure
1078     }
1079 }
1080 
1081 /************************************************/
1082 // 10970
1083 
RefCounted10970(T)1084 struct RefCounted10970(T) //if (!is(T == class))
1085 {
1086     struct RefCountedStore
1087     {
1088     }
1089     RefCountedStore _refCounted;
1090 
1091     this(this) {}
1092 
1093     ~this() {}
1094 }
1095 
1096 struct Array10970(T) if (!is(T : const(bool)))
1097 {
1098     struct Payload
1099     {
1100     }
1101     RefCounted10970!Payload _data;
1102 }
1103 
1104 class C10970
1105 {
this(string name)1106     this(string name)
1107     {
1108         m[name] = Arr();
1109     }
1110 
1111     alias Array10970!C10970 Arr;
1112     Arr[string] m;
1113 }
1114 
test10970()1115 void test10970()
1116 {
1117     C10970 c = new C10970("test");
1118 }
1119 
1120 /************************************************/
1121 // 6433
1122 
test6433()1123 void test6433()
1124 {
1125     int[int] aa;
1126     static assert(aa.sizeof != 0);
1127     static assert(aa.alignof != 0);
1128     static assert(is(typeof(aa.init) == int[int]));
1129     static assert(typeof(aa).mangleof == "Hii");
1130     static assert(typeof(aa).stringof == "int[int]");
1131     static struct AA { int[int] aa; }
1132     static assert(AA.aa.offsetof == 0);
1133 
1134     aa = aa.init;
1135     aa[0] = 1;
1136     assert(aa.length == 1 && aa[0] == 1);
1137 }
1138 
1139 /************************************************/
1140 // 6612
1141 
test6612()1142 void test6612()
1143 {
1144     auto aa1 = [1: 2]; // OK
1145     auto aa2 = [4: 5]; // OK
1146     int[int[int]] aa3 = [aa1:3, aa2:6]; // OK
1147     int[int[int]] aa4 = [[1:2]:3, [4:5]:6]; // error
1148     int[int[string]] aa5 = [["a":1]:2, ["b":3]:4];
1149 }
1150 
1151 /************************************************/
1152 // 7365
1153 
1154 struct TickDuration
1155 {
opEqualsTickDuration1156     bool opEquals(ref const TickDuration rhs) const
1157     {
1158         return true;
1159     }
1160 }
1161 
test7365()1162 void test7365()
1163 {
1164     TickDuration[Object] aa;
1165     aa.keys;
1166 }
1167 
1168 /************************************************/
1169 
1170 enum aa5520 = [5 : "hello"];
1171 
test5520()1172 void test5520()
1173 {
1174     auto a = aa5520.values;
1175 }
1176 
1177 /************************************************/
1178 
1179 enum size_t N6655 = 1;
1180 int[bar6655.length] foo6655;
1181 int[N6655] bar6655;
1182 
1183 /************************************************/
1184 
1185 struct ChunkLoc {}
1186 
Get()1187 ChunkLoc Get()
1188 {
1189     return ChunkLoc();
1190 }
1191 
test6799()1192 void test6799()
1193 {
1194     int[ChunkLoc] aa;
1195     aa.remove(Get());
1196 }
1197 
1198 /************************************************/
1199 // 11359
1200 
test11359()1201 void test11359()
1202 {
1203     class Bar {}
1204     static Bar[string] aa;
1205     static ref fun() { return aa; }
1206 
1207     string key = "test";
1208 
1209     fun[key] = new Bar;
1210     assert(aa.length == 1);
1211     Bar bar = fun[key];
1212 }
1213 
1214 /************************************************/
1215 // 11730
1216 
1217 struct SysTime11730
1218 {
opAssignSysTime117301219     ref SysTime11730 opAssign(SysTime11730 rhs)
1220     {
1221         assert(0);
1222     }
1223 }
1224 
Nullable11730(T)1225 struct Nullable11730(T)
1226 {
1227     T _value;
1228 
1229     void opAssign()(T value)
1230     {
1231         assert(0);
1232     }
1233 
1234     @property ref inout(T) get() inout
1235     {
1236         assert(0);
1237     }
1238     alias get this;
1239 }
1240 
test11730()1241 void test11730()
1242 {
1243     Nullable11730!SysTime11730[string] map;
1244     map["foo"] = Nullable11730!SysTime11730();
1245 }
1246 
1247 /************************************************/
1248 // 14089
1249 
1250 struct S14089
1251 {
1252     int num;
opAssignS140891253     S14089 opAssign(S14089 val) { return this; }
1254 }
1255 
test14089()1256 void test14089()
1257 {
1258     S14089[int] aa;
1259     S14089 b = aa[1] = S14089(0);
1260     assert(aa[1].num == 0);
1261     assert(b.num == 0);
1262 }
1263 
1264 /************************************************/
1265 // 14144
1266 
1267 struct JSON14144
1268 {
1269     union
1270     {
1271         double _floating;
1272     }
1273 
thisJSON141441274     this(typeof(null))
1275     {
1276     }
1277 
typeofJSON141441278     @trusted pure nothrow typeof(null) opAssign(typeof(null) nothing)
1279     {
1280         return null;
1281     }
1282 }
1283 
test14144()1284 void test14144()
1285 {
1286     JSON14144[string] x;
1287     x["wat"] = null;
1288     assert(x.length == 1);
1289     assert("wat" in x);
1290 }
1291 
1292 /************************************************/
1293 // 14321
1294 
test14321()1295 void test14321()
1296 {
1297     struct Foo
1298     {
1299         static char[8] buf;
1300         static char[] op;
1301 
1302         this(int id) { buf[op.length] = 'c'; op = buf[0..op.length + 1]; }
1303         this(this) { buf[op.length] = 'p'; op = buf[0..op.length + 1]; }
1304         ~this() { buf[op.length] = 'd'; op = buf[0..op.length + 1]; }
1305     }
1306     Foo[string] foos;
1307     assert(Foo.op == "");
1308     foos["test"] = Foo(42);     // initialization
1309     assert(Foo.op == "c");
1310     foos["test"] = Foo(42);     // assignment
1311     assert(Foo.op == "ccd");
1312 
1313     struct Bar
1314     {
1315         static char[8] buf;
1316         static char[] op;
1317 
1318         int id;
1319         //this(int id) { op ~= "c"; }
1320         this(this) { buf[op.length] = 'p'; op = buf[0..op.length + 1]; }
1321         ~this() { buf[op.length] = 'd'; op = buf[0..op.length + 1]; }
1322     }
1323     Bar[string] bars;
1324     assert(Bar.op == "");
1325     bars["test"] = Bar(42);     // initialization
1326     assert(Bar.op == "");
1327     bars["test"] = Bar(42);     // assignment
1328     assert(Bar.op == "d");
1329 }
1330 
1331 /************************************************/
1332 
main()1333 int main()
1334 {
1335     printf("before test 1\n");   test1();
1336     printf("before test 2\n");   test2();
1337     printf("before test 4\n");   test4();
1338     printf("before test 5\n");   test5();
1339     printf("before test 6\n");   test6();
1340     printf("before test 7\n");   test7();
1341     printf("before test 8\n");   test8();
1342     printf("before test 9\n");   test9();
1343     printf("before test 10\n");   test10();
1344     printf("before test 11\n");   test11();
1345     printf("before test 12\n");   test12();
1346     printf("before test 13\n");   test13();
1347     printf("before test 14\n");   test14();
1348     printf("before test 15\n");   test15();
1349     printf("before test 16\n");   test16();
1350     printf("before test 17\n");   test17();
1351     printf("before test 18\n");   test18();
1352     printf("before test 19\n");   test19();
1353     printf("before test 20\n");   test20();
1354     printf("before test 21\n");   test21();
1355     printf("before test 22\n");   test22();
1356     printf("before test 23\n");   test23();
1357     printf("before test 24\n");   test24();
1358     printf("before test 25\n");   test25();
1359     printf("before test 26\n");   test26();
1360     printf("before test 27\n");   test27();
1361     printf("before test 28\n");   test28();
1362     printf("before test 29\n");   test29();
1363     printf("before test 30\n");   test30();
1364     printf("before test 31\n");   test31();
1365     printf("before test 32\n");   test32();
1366 
1367     test4826c();
1368     test5131();
1369     test6178();
1370     test6178x();
1371     test10595();
1372     test10970();
1373     test6433();
1374     test6612();
1375     test7365();
1376     test5520();
1377     test6799();
1378     test11359();
1379     test11730();
1380     test14089();
1381     test14321();
1382 
1383     printf("Success\n");
1384     return 0;
1385 }
1386