1 module betterc_test;
2 
3 import bcaa;
4 import bcaa: Mallocator;
5 
6 import core.stdc.stdio;
7 import core.stdc.time;
8 
9 extern(C) void main() @nogc
10 {
11   clock_t begin = clock();
12   {
13     Bcaa!(int, int) aa0;
14     scope(exit) aa0.free;
15 
16     foreach (i; 0..1000_000){
17       aa0[i] = i;
18     }
19 
20     foreach (i; 2000..1000_000){
21       aa0.remove(i);
22     }
23 
24     printf("%d \n", aa0[1000]);
25   }
26   clock_t end = clock(); printf("Elapsed time: %f \n", cast(double)(end - begin) / CLOCKS_PER_SEC);
27 
28   {
29     Bcaa!(string, string) aa1;
30     scope(exit) aa1.free;
31 
32     aa1["Stevie"] = "Ray Vaughan";
33     aa1["Asım Can"] = "Gündüz";
34     aa1["Dan"] = "Patlansky";
35     aa1["İlter"] = "Kurcala";
36     aa1["Ferhat"] = "Kurtulmuş";
37 
38     foreach(pair; aa1){
39       printf("%s -> %s", (*pair.keyp).ptr, (*pair.valp).ptr);
40     }
41 
42     if (auto valptr = "Dan" in aa1)
43       printf("%s exists!!!!\n", (*valptr).ptr );
44     else
45       printf("does not exist!!!!\n".ptr);
46 
47     /+
48     assert(aa1.remove("Ferhat") == true);
49     assert(aa1["Ferhat"] == null);
50     assert(aa1.remove("Foe") == false);
51     assert(aa1["İlter"] =="Kurcala");
52     +/
53     aa1.rehash();
54 
55     printf("%s\n",aa1["Stevie"].ptr);
56     printf("%s\n",aa1["Asım Can"].ptr);
57     printf("%s\n",aa1["Dan"].ptr);
58     //printf("%s\n",aa1["Ferhat"].ptr);
59 
60     auto keys = aa1.keys;
61     scope(exit) Mallocator.instance.dispose(keys);
62     foreach(key; keys)
63       printf("%s -> %s \n", key.ptr, aa1[key].ptr);
64 
65     struct Guitar {
66       string brand;
67     }
68 
69     Bcaa!(int, Guitar) guitars;
70     scope(exit) guitars.free;
71 
72     guitars[0] = Guitar("Fender");
73     guitars[3] = Guitar("Gibson");
74     guitars[356] = Guitar("Stagg");
75 
76     //assert(guitars[3].brand == "Gibson");
77 
78     printf("%s \n", guitars[356].brand.ptr);
79 
80     if(auto valPtr = 3 in guitars)
81       printf("%s \n", (*valPtr).brand.ptr);
82   }
83 
84   // Test "in" works for AA without allocated storage.
85   {
86     Bcaa!(int, int) emptyMap;
87     //assert(0 !in emptyMap);
88 
89   }
90 
91   // Try to force a memory leak - issue #5
92   {
93     struct S {
94       int x;
95       int y;
96       string txt;
97     }
98 
99     Bcaa!(int, S) aas;
100     scope(exit) aas.free;
101 
102     for(int i = 1024; i < 2048; i++) {
103       aas[i] = S(i, i*2, "caca");
104     }
105     aas[100] = S(10, 20, "caca");
106 
107     printf(".x=%d .y%d %s\n", aas[100].x, aas[100].y, aas[100].txt.ptr);
108 
109     for(int i = 1024; i < 2048; i++) {
110       aas.remove(i);
111     }
112   }
113 }
114