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