1 module tests.containers.array; 2 3 import unit_threaded; 4 import evael.lib.containers.array; 5 6 @Name("Array is correctly initialized without capacity") 7 unittest 8 { 9 auto arr = Array!int(); 10 11 arr.length.shouldEqual(0); 12 arr.capacity.shouldEqual(0); 13 } 14 15 @Name("Array is correctly initialized with capacity") 16 unittest 17 { 18 auto arr = Array!int(1337); 19 20 arr.length.shouldEqual(0); 21 arr.capacity.shouldEqual(1337); 22 } 23 24 @Name("Array access fails with invalid index") 25 unittest 26 { 27 auto arr = Array!int(1337); 28 29 arr[1].shouldThrow!Error(); 30 } 31 32 @Name("Array access success with valid index") 33 unittest 34 { 35 auto arr = Array!int(1337); 36 arr.insert(50); 37 38 arr[0].shouldEqual(50); 39 } 40 41 @Name("Array inserts value") 42 unittest 43 { 44 auto arr = Array!int(); 45 arr.insert(50); 46 47 arr[0].shouldEqual(50); 48 arr.length.shouldEqual(1); 49 arr.capacity.shouldEqual(32); 50 } 51 52 @Name("Array removes back value") 53 unittest 54 { 55 auto arr = Array!int(); 56 arr.insert(50); 57 arr.removeBack(); 58 59 arr[0].shouldThrow!Error(); 60 arr.length.shouldEqual(0); 61 arr.capacity.shouldEqual(32); 62 } 63 64 @Name("Array removes value at specific index") 65 unittest 66 { 67 auto arr = Array!int(); 68 arr.insert(50); 69 arr.removeAt(0); 70 71 arr[0].shouldThrow!Error(); 72 arr.length.shouldEqual(0); 73 arr.capacity.shouldEqual(32); 74 } 75 76 @Name("Array removes value at invalid index") 77 unittest 78 { 79 auto arr = Array!int(); 80 arr.insert(50); 81 arr.removeAt(1).shouldThrow!Error(); 82 } 83 84 85 @Name("Array index assignment") 86 unittest 87 { 88 auto arr = Array!int(); 89 arr.insert(1); 90 arr[0] = 50; 91 92 arr[0].shouldEqual(50); 93 arr.length.shouldEqual(1); 94 arr.capacity.shouldEqual(32); 95 } 96 97 @Name("Array slice assignment") 98 unittest 99 { 100 auto arr = Array!int(5, 1); 101 arr[0..5] = 12345; 102 103 for (int i = 0; i < arr.length; i++) 104 { 105 arr[i].shouldEqual(12345); 106 } 107 } 108 109 @Name("Array slice assignment2") 110 unittest 111 { 112 auto arr = Array!int(5, 1); 113 arr[] = 1337; 114 115 for (int i = 0; i < arr.length; i++) 116 { 117 arr[i].shouldEqual(1337); 118 } 119 } 120 121 @Name("Array slice") 122 unittest 123 { 124 auto arr = Array!int(5, 1); 125 126 arr[].shouldEqual([1, 1, 1, 1, 1]); 127 } 128 129 @Name("Array slice2") 130 unittest 131 { 132 auto arr = Array!int(5, 1); 133 134 arr[0..5].shouldEqual([1, 1, 1, 1, 1]); 135 } 136 137 @Name("Array of array") 138 unittest 139 { 140 auto arr = Array!(Array!bool)(); 141 142 arr.insert(Array!bool(3, false)); 143 144 arr[0][].shouldEqual([false, false, false]); 145 } 146 147 @Name("Array foreach loop") 148 unittest 149 { 150 auto arr = Array!int(5, 1); 151 152 int counter; 153 154 foreach (i; arr) 155 { 156 counter++; 157 i.shouldEqual(1); 158 } 159 160 counter.shouldEqual(arr.length); 161 } 162 163 @Name("Array foreach loop with index") 164 unittest 165 { 166 auto arr = Array!int(5, 1); 167 168 int counter; 169 170 foreach (index, i; arr) 171 { 172 i.shouldEqual(1); 173 index.shouldEqual(counter); 174 counter++; 175 } 176 177 counter.shouldEqual(arr.length); 178 }