Debian Codegraph @

libphobos/testsuite/libphobos.aa/test_aa.d

raw
1 void main()
2 {
3 testKeysValues1();
4 testKeysValues2();
5 testGet1();
6 testGet2();
7 testRequire1();
8 testRequire2();
9 testRequire3();
10 testUpdate1();
11 testUpdate2();
12 testByKey1();
13 testByKey2();
14 testByKey3();
15 testByKey4();
16 issue5842();
17 issue5842Expanded();
18 issue5925();
19 issue8583();
20 issue9052();
21 issue9119();
22 issue9852();
23 issue10381();
24 issue10720();
25 issue11761();
26 issue13078();
27 issue14104();
28 issue14626();
29 issue15290();
30 issue15367();
31 issue16974();
32 issue18071();
33 issue20440();
34 issue21442();
35 testIterationWithConst();
36 testStructArrayKey();
37 miscTests1();
38 miscTests2();
39 testRemove();
40 testZeroSizedValue();
41 testTombstonePurging();
42 testClear();
43 testTypeInfoCollect();
44 }
45
46 void testKeysValues1()
47 {
48 static struct T
49 {
50 byte b;
51 static size_t count;
52 this(this) { ++count; }
53 }
54 T[int] aa;
55 T t;
56 aa[0] = t;
57 aa[1] = t;
58 assert(T.count == 2);
59 auto vals = aa.values;
60 assert(vals.length == 2);
61 assert(T.count == 4);
62
63 T.count = 0;
64 int[T] aa2;
65 aa2[t] = 0;
66 assert(T.count == 1);
67 aa2[t] = 1;
68 assert(T.count == 1);
69 auto keys = aa2.keys;
70 assert(keys.length == 1);
71 assert(T.count == 2);
72 }
73
74 void testKeysValues2() nothrow pure
75 {
76 int[string] aa;
77
78 assert(aa.keys.length == 0);
79 assert(aa.values.length == 0);
80
81 aa["hello"] = 3;
82 assert(aa["hello"] == 3);
83 aa["hello"]++;
84 assert(aa["hello"] == 4);
85
86 assert(aa.length == 1);
87
88 string[] keys = aa.keys;
89 assert(keys.length == 1);
90 assert(keys[0] == "hello");
91
92 int[] values = aa.values;
93 assert(values.length == 1);
94 assert(values[0] == 4);
95
96 aa.rehash;
97 assert(aa.length == 1);
98 assert(aa["hello"] == 4);
99
100 aa["foo"] = 1;
101 aa["bar"] = 2;
102 aa["batz"] = 3;
103
104 assert(aa.keys.length == 4);
105 assert(aa.values.length == 4);
106
107 foreach (a; aa.keys)
108 {
109 assert(a.length != 0);
110 assert(a.ptr != null);
111 }
112
113 foreach (v; aa.values)
114 {
115 assert(v != 0);
116 }
117 }
118
119 void testGet1() @safe
120 {
121 int[string] aa;
122 int a;
123 foreach (val; aa.byKeyValue)
124 {
125 ++aa[val.key];
126 a = val.value;
127 }
128 }
129
130 void testGet2()
131 {
132 static class T
133 {
134 static size_t count;
135 this() { ++count; }
136 }
137
138 T[string] aa;
139
140 auto a = new T;
141 aa["foo"] = a;
142 assert(T.count == 1);
143 auto b = aa.get("foo", new T);
144 assert(T.count == 1);
145 assert(b is a);
146 auto c = aa.get("bar", new T);
147 assert(T.count == 2);
148 assert(c !is a);
149
150 //Obviously get doesn't add.
151 assert("bar" !in aa);
152 }
153
154 void testRequire1()
155 {
156 static class T
157 {
158 static size_t count;
159 this() { ++count; }
160 }
161
162 T[string] aa;
163
164 auto a = new T;
165 aa["foo"] = a;
166 assert(T.count == 1);
167 auto b = aa.require("foo", new T);
168 assert(T.count == 1);
169 assert(b is a);
170 auto c = aa.require("bar", null);
171 assert(T.count == 1);
172 assert(c is null);
173 assert("bar" in aa);
174 auto d = aa.require("bar", new T);
175 assert(d is null);
176 auto e = aa.require("baz", new T);
177 assert(T.count == 2);
178 assert(e !is a);
179
180 assert("baz" in aa);
181
182 bool created = false;
183 auto f = aa.require("qux", { created = true; return new T; }());
184 assert(created == true);
185
186 T g;
187 auto h = aa.require("qux", { g = new T; return g; }());
188 assert(g !is h);
189 }
190
191 void testRequire2()
192 {
193 static struct S
194 {
195 int value;
196 }
197
198 S[string] aa;
199
200 aa.require("foo").value = 1;
201 assert(aa == ["foo" : S(1)]);
202
203 aa["bar"] = S(2);
204 auto a = aa.require("bar", S(3));
205 assert(a == S(2));
206
207 auto b = aa["bar"];
208 assert(b == S(2));
209
210 S* c = &aa.require("baz", S(4));
211 assert(c is &aa["baz"]);
212 assert(*c == S(4));
213
214 assert("baz" in aa);
215
216 auto d = aa["baz"];
217 assert(d == S(4));
218 }
219
220 void testRequire3() pure
221 {
222 string[string] aa;
223
224 auto a = aa.require("foo", "bar");
225 assert("foo" in aa);
226 }
227
228
229 void testUpdate1()
230 {
231 static class C {}
232 C[string] aa;
233
234 C orig = new C;
235 aa["foo"] = orig;
236
237 C newer;
238 C older;
239
240 void test(string key)
241 {
242 aa.update(key, {
243 newer = new C;
244 return newer;
245 }, (ref C c) {
246 older = c;
247 newer = new C;
248 return newer;
249 });
250 }
251
252 test("foo");
253 assert(older is orig);
254 assert(newer is aa["foo"]);
255
256 test("bar");
257 assert(newer is aa["bar"]);
258 }
259
260 void testUpdate2()
261 {
262 static class C {}
263 C[string] aa;
264
265 auto created = false;
266 auto updated = false;
267
268 class Creator
269 {
270 C opCall()
271 {
272 created = true;
273 return new C();
274 }
275 }
276
277 class Updater
278 {
279 C opCall(ref C)
280 {
281 updated = true;
282 return new C();
283 }
284 }
285
286 aa.update("foo", new Creator, new Updater);
287 assert(created);
288 aa.update("foo", new Creator, new Updater);
289 assert(updated);
290 }
291
292 void testByKey1() @safe
293 {
294 static struct BadValue
295 {
296 int x;
297 this(this) @system { *(cast(ubyte*)(null) + 100000) = 5; } // not @safe
298 alias x this;
299 }
300
301 BadValue[int] aa;
302
303 // FIXME: Should be @system because of the postblit
304 if (false)
305 auto x = aa.byKey.front;
306 }
307
308 void testByKey2() nothrow pure
309 {
310 int[int] a;
311 foreach (i; a.byKey)
312 {
313 assert(false);
314 }
315 foreach (i; a.byValue)
316 {
317 assert(false);
318 }
319 }
320
321 void testByKey3() /*nothrow*/ pure
322 {
323 auto a = [ 1:"one", 2:"two", 3:"three" ];
324 auto b = a.dup;
325 assert(b == [ 1:"one", 2:"two", 3:"three" ]);
326
327 int[] c;
328 foreach (k; a.byKey)
329 {
330 c ~= k;
331 }
332
333 assert(c.length == 3);
334 assert(c[0] == 1 || c[1] == 1 || c[2] == 1);
335 assert(c[0] == 2 || c[1] == 2 || c[2] == 2);
336 assert(c[0] == 3 || c[1] == 3 || c[2] == 3);
337 }
338
339 void testByKey4() nothrow pure
340 {
341 string[] keys = ["a", "b", "c", "d", "e", "f"];
342
343 // Test forward range capabilities of byKey
344 {
345 int[string] aa;
346 foreach (key; keys)
347 aa[key] = 0;
348
349 auto keyRange = aa.byKey();
350 auto savedKeyRange = keyRange.save;
351
352 // Consume key range once
353 size_t keyCount = 0;
354 while (!keyRange.empty)
355 {
356 aa[keyRange.front]++;
357 keyCount++;
358 keyRange.popFront();
359 }
360
361 foreach (key; keys)
362 {
363 assert(aa[key] == 1);
364 }
365 assert(keyCount == keys.length);
366
367 // Verify it's possible to iterate the range the second time
368 keyCount = 0;
369 while (!savedKeyRange.empty)
370 {
371 aa[savedKeyRange.front]++;
372 keyCount++;
373 savedKeyRange.popFront();
374 }
375
376 foreach (key; keys)
377 {
378 assert(aa[key] == 2);
379 }
380 assert(keyCount == keys.length);
381 }
382
383 // Test forward range capabilities of byValue
384 {
385 size_t[string] aa;
386 foreach (i; 0 .. keys.length)
387 {
388 aa[keys[i]] = i;
389 }
390
391 auto valRange = aa.byValue();
392 auto savedValRange = valRange.save;
393
394 // Consume value range once
395 int[] hasSeen;
396 hasSeen.length = keys.length;
397 while (!valRange.empty)
398 {
399 assert(hasSeen[valRange.front] == 0);
400 hasSeen[valRange.front]++;
401 valRange.popFront();
402 }
403
404 foreach (sawValue; hasSeen) { assert(sawValue == 1); }
405
406 // Verify it's possible to iterate the range the second time
407 hasSeen = null;
408 hasSeen.length = keys.length;
409 while (!savedValRange.empty)
410 {
411 assert(!hasSeen[savedValRange.front]);
412 hasSeen[savedValRange.front] = true;
413 savedValRange.popFront();
414 }
415
416 foreach (sawValue; hasSeen) { assert(sawValue); }
417 }
418 }
419
420 void issue5842() pure nothrow
421 {
422 string[string] test = null;
423 test["test1"] = "test1";
424 test.remove("test1");
425 test.rehash;
426 test["test3"] = "test3"; // causes divide by zero if rehash broke the AA
427 }
428
429 /// expanded test for 5842: increase AA size past the point where the AA
430 /// stops using binit, in order to test another code path in rehash.
431 void issue5842Expanded() pure nothrow
432 {
433 int[int] aa;
434 foreach (int i; 0 .. 32)
435 aa[i] = i;
436 foreach (int i; 0 .. 32)
437 aa.remove(i);
438 aa.rehash;
439 aa[1] = 1;
440 }
441
442 void issue5925() nothrow pure
443 {
444 const a = [4:0];
445 const b = [4:0];
446 assert(a == b);
447 }
448
449 /// test for bug 8583: ensure Slot and aaA are on the same page wrt value alignment
450 void issue8583() nothrow pure
451 {
452 string[byte] aa0 = [0: "zero"];
453 string[uint[3]] aa1 = [[1,2,3]: "onetwothree"];
454 ushort[uint[3]] aa2 = [[9,8,7]: 987];
455 ushort[uint[4]] aa3 = [[1,2,3,4]: 1234];
456 string[uint[5]] aa4 = [[1,2,3,4,5]: "onetwothreefourfive"];
457
458 assert(aa0.byValue.front == "zero");
459 assert(aa1.byValue.front == "onetwothree");
460 assert(aa2.byValue.front == 987);
461 assert(aa3.byValue.front == 1234);
462 assert(aa4.byValue.front == "onetwothreefourfive");
463 }
464
465 void issue9052() nothrow pure
466 {
467 static struct Json {
468 Json[string] aa;
469 void opAssign(Json) {}
470 size_t length() const { return aa.length; }
471 // This length() instantiates AssociativeArray!(string, const(Json)) to call AA.length(), and
472 // inside ref Slot opAssign(Slot p); (which is automatically generated by compiler in Slot),
473 // this.value = p.value would actually fail, because both side types of the assignment
474 // are const(Json).
475 }
476 }
477
478 void issue9119()
479 {
480 int[string] aa;
481 assert(aa.byKeyValue.empty);
482
483 aa["a"] = 1;
484 aa["b"] = 2;
485 aa["c"] = 3;
486
487 auto pairs = aa.byKeyValue;
488
489 auto savedPairs = pairs.save;
490 size_t count = 0;
491 while (!pairs.empty)
492 {
493 assert(pairs.front.key in aa);
494 assert(pairs.front.value == aa[pairs.front.key]);
495 count++;
496 pairs.popFront();
497 }
498 assert(count == aa.length);
499
500 // Verify that saved range can iterate over the AA again
501 count = 0;
502 while (!savedPairs.empty)
503 {
504 assert(savedPairs.front.key in aa);
505 assert(savedPairs.front.value == aa[savedPairs.front.key]);
506 count++;
507 savedPairs.popFront();
508 }
509 assert(count == aa.length);
510 }
511
512 void issue9852() nothrow pure
513 {
514 // Original test case (revised, original assert was wrong)
515 int[string] a;
516 a["foo"] = 0;
517 a.remove("foo");
518 assert(a == null); // should not crash
519
520 int[string] b;
521 assert(b is null);
522 assert(a == b); // should not deref null
523 assert(b == a); // ditto
524
525 int[string] c;
526 c["a"] = 1;
527 assert(a != c); // comparison with empty non-null AA
528 assert(c != a);
529 assert(b != c); // comparison with null AA
530 assert(c != b);
531 }
532
533 void issue10381()
534 {
535 alias II = int[int];
536 II aa1 = [0 : 1];
537 II aa2 = [0 : 1];
538 II aa3 = [0 : 2];
539 assert(aa1 == aa2); // Passes
540 assert(typeid(II).equals(&aa1, &aa2));
541 assert(!typeid(II).equals(&aa1, &aa3));
542 }
543
544 void issue10720() nothrow pure
545 {
546 static struct NC
547 {
548 @disable this(this) { }
549 }
550
551 NC[string] aa;
552 static assert(!is(aa.nonExistingField));
553 }
554
555 /// bug 11761: test forward range functionality
556 void issue11761() pure nothrow
557 {
558 auto aa = ["a": 1];
559
560 void testFwdRange(R, T)(R fwdRange, T testValue)
561 {
562 assert(!fwdRange.empty);
563 assert(fwdRange.front == testValue);
564 static assert(is(typeof(fwdRange.save) == typeof(fwdRange)));
565
566 auto saved = fwdRange.save;
567 fwdRange.popFront();
568 assert(fwdRange.empty);
569
570 assert(!saved.empty);
571 assert(saved.front == testValue);
572 saved.popFront();
573 assert(saved.empty);
574 }
575
576 testFwdRange(aa.byKey, "a");
577 testFwdRange(aa.byValue, 1);
578 //testFwdRange(aa.byPair, tuple("a", 1));
579 }
580
581 void issue13078() nothrow pure
582 {
583 shared string[][string] map;
584 map.rehash;
585 }
586
587 void issue14104()
588 {
589 alias K = const(ubyte)*;
590 size_t[K] aa;
591 immutable key = cast(K)(cast(size_t) uint.max + 1);
592 aa[key] = 12;
593 assert(key in aa);
594 }
595
596 void issue14626()
597 {
598 static struct S
599 {
600 string[string] aa;
601 inout(string) key() inout { return aa.byKey().front; }
602 inout(string) val() inout { return aa.byValue().front; }
603 auto keyval() inout { return aa.byKeyValue().front; }
604 }
605
606 S s = S(["a":"b"]);
607 assert(s.key() == "a");
608 assert(s.val() == "b");
609 assert(s.keyval().key == "a");
610 assert(s.keyval().value == "b");
611
612 void testInoutKeyVal(inout(string) key)
613 {
614 inout(string)[typeof(key)] aa;
615
616 foreach (i; aa.byKey()) {}
617 foreach (i; aa.byValue()) {}
618 foreach (i; aa.byKeyValue()) {}
619 }
620
621 const int[int] caa;
622 static assert(is(typeof(caa.byValue().front) == const int));
623 }
624
625 /// test duplicated keys in AA literal
626 /// https://issues.dlang.org/show_bug.cgi?id=15290
627 void issue15290()
628 {
629 string[int] aa = [ 0: "a", 0: "b" ];
630 assert(aa.length == 1);
631 assert(aa.keys == [ 0 ]);
632 }
633
634 void issue15367()
635 {
636 void f1() {}
637 void f2() {}
638
639 // TypeInfo_Delegate.getHash
640 int[void delegate()] aa;
641 assert(aa.length == 0);
642 aa[&f1] = 1;
643 assert(aa.length == 1);
644 aa[&f1] = 1;
645 assert(aa.length == 1);
646
647 auto a1 = [&f2, &f1];
648 auto a2 = [&f2, &f1];
649
650 // TypeInfo_Delegate.equals
651 for (auto i = 0; i < 2; i++)
652 assert(a1[i] == a2[i]);
653 assert(a1 == a2);
654
655 // TypeInfo_Delegate.compare
656 for (auto i = 0; i < 2; i++)
657 assert(a1[i] <= a2[i]);
658 assert(a1 <= a2);
659 }
660
661 /// test AA as key
662 /// https://issues.dlang.org/show_bug.cgi?id=16974
663 void issue16974()
664 {
665 int[int] a = [1 : 2], a2 = [1 : 2];
666
667 assert([a : 3] == [a : 3]);
668 assert([a : 3] == [a2 : 3]);
669
670 assert(typeid(a).getHash(&a) == typeid(a).getHash(&a));
671 assert(typeid(a).getHash(&a) == typeid(a).getHash(&a2));
672 }
673
674 /// test safety for alias-this'd AA that have unsafe opCast
675 /// https://issues.dlang.org/show_bug.cgi?id=18071
676 void issue18071()
677 {
678 static struct Foo
679 {
680 int[int] aa;
681 auto opCast() pure nothrow @nogc
682 {
683 *cast(uint*)0xdeadbeef = 0xcafebabe;// unsafe
684 return null;
685 }
686 alias aa this;
687 }
688
689 Foo f;
690 () @safe { assert(f.byKey.empty); }();
691 }
692
693 /// Test that `require` works even with types whose opAssign
694 /// doesn't return a reference to the receiver.
695 /// https://issues.dlang.org/show_bug.cgi?id=20440
696 void issue20440() @safe
697 {
698 static struct S
699 {
700 int value;
701 auto opAssign(S s) {
702 this.value = s.value;
703 return this;
704 }
705 }
706 S[S] aa;
707 assert(aa.require(S(1), S(2)) == S(2));
708 assert(aa[S(1)] == S(2));
709 }
710
711 ///
712 void issue21442()
713 {
714 import core.memory;
715
716 size_t[size_t] glob;
717
718 class Foo
719 {
720 size_t count;
721
722 this (size_t entries) @safe
723 {
724 this.count = entries;
725 foreach (idx; 0 .. entries)
726 glob[idx] = idx;
727 }
728
729 ~this () @safe
730 {
731 foreach (idx; 0 .. this.count)
732 glob.remove(idx);
733 }
734 }
735
736 void bar () @safe
737 {
738 Foo f = new Foo(16);
739 }
740
741 bar();
742 GC.collect(); // Needs to happen from a GC collection
743 }
744
745 /// Verify iteration with const.
746 void testIterationWithConst()
747 {
748 auto aa = [1:2, 3:4];
749 foreach (const t; aa.byKeyValue)
750 {
751 auto k = t.key;
752 auto v = t.value;
753 }
754 }
755
756 void testStructArrayKey() @safe
757 {
758 struct S
759 {
760 int i;
761 const @safe nothrow:
762 hash_t toHash() { return 0; }
763 bool opEquals(const S) { return true; }
764 int opCmp(const S) { return 0; }
765 }
766
767 int[S[]] aa = [[S(11)] : 13];
768 assert(aa[[S(12)]] == 13);
769 }
770
771 void miscTests1() pure nothrow
772 {
773 string[int] key1 = [1 : "true", 2 : "false"];
774 string[int] key2 = [1 : "false", 2 : "true"];
775 string[int] key3;
776
777 // AA lits create a larger hashtable
778 int[string[int]] aa1 = [key1 : 100, key2 : 200, key3 : 300];
779
780 // Ensure consistent hash values are computed for key1
781 assert((key1 in aa1) !is null);
782
783 // Manually assigning to an empty AA creates a smaller hashtable
784 int[string[int]] aa2;
785 aa2[key1] = 100;
786 aa2[key2] = 200;
787 aa2[key3] = 300;
788
789 assert(aa1 == aa2);
790
791 // Ensure binary-independence of equal hash keys
792 string[int] key2a;
793 key2a[1] = "false";
794 key2a[2] = "true";
795
796 assert(aa1[key2a] == 200);
797 }
798
799 void miscTests2()
800 {
801 int[int] aa;
802 foreach (k, v; aa)
803 assert(false);
804 foreach (v; aa)
805 assert(false);
806 assert(aa.byKey.empty);
807 assert(aa.byValue.empty);
808 assert(aa.byKeyValue.empty);
809
810 size_t n;
811 aa = [0 : 3, 1 : 4, 2 : 5];
812 foreach (k, v; aa)
813 {
814 n += k;
815 assert(k >= 0 && k < 3);
816 assert(v >= 3 && v < 6);
817 }
818 assert(n == 3);
819 n = 0;
820
821 foreach (v; aa)
822 {
823 n += v;
824 assert(v >= 3 && v < 6);
825 }
826 assert(n == 12);
827
828 n = 0;
829 foreach (k, v; aa)
830 {
831 ++n;
832 break;
833 }
834 assert(n == 1);
835
836 n = 0;
837 foreach (v; aa)
838 {
839 ++n;
840 break;
841 }
842 assert(n == 1);
843 }
844
845 void testRemove()
846 {
847 int[int] aa;
848 assert(!aa.remove(0));
849 aa = [0 : 1];
850 assert(aa.remove(0));
851 assert(!aa.remove(0));
852 aa[1] = 2;
853 assert(!aa.remove(0));
854 assert(aa.remove(1));
855
856 assert(aa.length == 0);
857 assert(aa.byKey.empty);
858 }
859
860 /// test zero sized value (hashset)
861 void testZeroSizedValue()
862 {
863 alias V = void[0];
864 auto aa = [0 : V.init];
865 assert(aa.length == 1);
866 assert(aa.byKey.front == 0);
867 assert(aa.byValue.front == V.init);
868 aa[1] = V.init;
869 assert(aa.length == 2);
870 aa[0] = V.init;
871 assert(aa.length == 2);
872 assert(aa.remove(0));
873 aa[0] = V.init;
874 assert(aa.length == 2);
875 assert(aa == [0 : V.init, 1 : V.init]);
876 }
877
878 void testTombstonePurging()
879 {
880 int[int] aa;
881 foreach (i; 0 .. 6)
882 aa[i] = i;
883 foreach (i; 0 .. 6)
884 assert(aa.remove(i));
885 foreach (i; 6 .. 10)
886 aa[i] = i;
887 assert(aa.length == 4);
888 foreach (i; 6 .. 10)
889 assert(i in aa);
890 }
891
892 void testClear()
893 {
894 int[int] aa;
895 assert(aa.length == 0);
896 foreach (i; 0 .. 100)
897 aa[i] = i * 2;
898 assert(aa.length == 100);
899 auto aa2 = aa;
900 assert(aa2.length == 100);
901 aa.clear();
902 assert(aa.length == 0);
903 assert(aa2.length == 0);
904
905 aa2[5] = 6;
906 assert(aa.length == 1);
907 assert(aa[5] == 6);
908 }
909
910 // https://github.com/dlang/dmd/issues/17503
911 void testTypeInfoCollect()
912 {
913 import core.memory;
914
915 static struct S
916 {
917 int x;
918 ~this() {}
919 }
920
921 static struct AAHolder
922 {
923 S[int] aa;
924 }
925
926 static S* getBadS()
927 {
928 auto aaholder = new AAHolder;
929 aaholder.aa[0] = S();
930 auto s = 0 in aaholder.aa; // keep a pointer to the entry
931 GC.free(aaholder); // but not a pointer to the AA.
932 return s;
933 }
934
935 static void stackStomp()
936 {
937 import core.stdc.string : memset;
938 ubyte[4 * 4096] x;
939 memset(x.ptr, 0, x.sizeof);
940 }
941
942 auto s = getBadS();
943 stackStomp(); // destroy any stale references to the AA or s except in the current frame;
944 GC.collect(); // BUG: this used to invalidate the fake type info, should no longer do this.
945 foreach(i; 0 .. 1000) // try to reallocate the freed type info
946 auto p = new void*[1];
947 s = null; // clear any reference to the entry
948 GC.collect(); // used to segfault.
949 }