|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using DataStructures.Hashing; |
| 4 | +using NUnit.Framework; |
| 5 | + |
| 6 | +namespace DataStructures.Tests.Hashing |
| 7 | +{ |
| 8 | + [TestFixture] |
| 9 | + public class HashTableTests |
| 10 | + { |
| 11 | + [Test] |
| 12 | + public void Add_ThrowsException_WhenKeyIsNull() |
| 13 | + { |
| 14 | + var hashTable = new HashTable<string, int>(); |
| 15 | + |
| 16 | + Assert.Throws<ArgumentNullException>(() => hashTable.Add(null, 1)); |
| 17 | + } |
| 18 | + |
| 19 | + [Test] |
| 20 | + public void Add_ThrowsException_WhenKeyAlreadyExists() |
| 21 | + { |
| 22 | + var hashTable = new HashTable<string, int>(); |
| 23 | + |
| 24 | + hashTable.Add("a", 1); |
| 25 | + |
| 26 | + Assert.Throws<ArgumentException>(() => hashTable.Add("a", 2)); |
| 27 | + } |
| 28 | + |
| 29 | + [Test] |
| 30 | + public void Add_IncreasesCount_WhenKeyDoesNotExist() |
| 31 | + { |
| 32 | + var hashTable = new HashTable<string, int>(); |
| 33 | + |
| 34 | + hashTable.Add("a", 1); |
| 35 | + |
| 36 | + Assert.AreEqual(1, hashTable.Count); |
| 37 | + } |
| 38 | + |
| 39 | + [Test] |
| 40 | + public void Add_DoesNotIncreaseCount_WhenKeyAlreadyExists() |
| 41 | + { |
| 42 | + var hashTable = new HashTable<string, int>(); |
| 43 | + |
| 44 | + hashTable.Add("a", 1); |
| 45 | + try |
| 46 | + { |
| 47 | + hashTable.Add("a", 2); |
| 48 | + } |
| 49 | + catch (ArgumentException) |
| 50 | + { |
| 51 | + Console.WriteLine("ArgumentException"); |
| 52 | + } |
| 53 | + Assert.AreEqual(1, hashTable.Count); |
| 54 | + } |
| 55 | + |
| 56 | + [Test] |
| 57 | + public void Add_ThrowsException_WhenValueIsNull() |
| 58 | + { |
| 59 | + var hashTable = new HashTable<string, string>(); |
| 60 | + |
| 61 | + Assert.Throws<ArgumentNullException>(() => hashTable.Add("a", null)); |
| 62 | + } |
| 63 | + |
| 64 | + [Test] |
| 65 | + public void Add_IncreasesCount_WhenValueDoesNotExist() |
| 66 | + { |
| 67 | + var hashTable = new HashTable<string, int>(); |
| 68 | + |
| 69 | + hashTable.Add("a", 1); |
| 70 | + |
| 71 | + Assert.AreEqual(1, hashTable.Count); |
| 72 | + } |
| 73 | + |
| 74 | + [Test] |
| 75 | + public void Add_DoesNotIncreaseCount_WhenValueAlreadyExists() |
| 76 | + { |
| 77 | + var hashTable = new HashTable<string, int>(); |
| 78 | + |
| 79 | + hashTable.Add("a", 1); |
| 80 | + |
| 81 | + try |
| 82 | + { |
| 83 | + hashTable.Add("b", 1); |
| 84 | + } |
| 85 | + catch (ArgumentException) |
| 86 | + { |
| 87 | + Console.WriteLine("ArgumentException"); |
| 88 | + } |
| 89 | + |
| 90 | + Assert.AreEqual(2, hashTable.Count); |
| 91 | + } |
| 92 | + |
| 93 | + [Test] |
| 94 | + public void Add_IncreasesCount_WhenValueIsNull() |
| 95 | + { |
| 96 | + var hashTable = new HashTable<string, string>(); |
| 97 | + |
| 98 | + try |
| 99 | + { |
| 100 | + hashTable.Add("a", null); |
| 101 | + } |
| 102 | + catch (ArgumentNullException) |
| 103 | + { |
| 104 | + Console.WriteLine("ArgumentNullException"); |
| 105 | + } |
| 106 | + Assert.AreEqual(0, hashTable.Count); |
| 107 | + } |
| 108 | + |
| 109 | + [Test] |
| 110 | + public void Add_IncreasesCount_WhenValueAlreadyExists() |
| 111 | + { |
| 112 | + var hashTable = new HashTable<string, int>(); |
| 113 | + |
| 114 | + hashTable.Add("a", 1); |
| 115 | + hashTable.Add("b", 1); |
| 116 | + Assert.AreEqual(2, hashTable.Count); |
| 117 | + } |
| 118 | + |
| 119 | + [Test] |
| 120 | + public void Remove_ThrowsException_WhenKeyIsNull() |
| 121 | + { |
| 122 | + var hashTable = new HashTable<string, int>(); |
| 123 | + |
| 124 | + Assert.Throws<ArgumentNullException>(() => hashTable.Remove(null)); |
| 125 | + } |
| 126 | + |
| 127 | + [Test] |
| 128 | + public void Remove_ReturnsFalse_WhenKeyDoesNotExist() |
| 129 | + { |
| 130 | + var hashTable = new HashTable<string, int>(); |
| 131 | + |
| 132 | + Assert.IsFalse(hashTable.Remove("a")); |
| 133 | + } |
| 134 | + |
| 135 | + [Test] |
| 136 | + public void Remove_ReturnsTrue_WhenKeyExists() |
| 137 | + { |
| 138 | + var hashTable = new HashTable<string, int>(); |
| 139 | + |
| 140 | + hashTable.Add("a", 1); |
| 141 | + |
| 142 | + Assert.IsTrue(hashTable.Remove("a")); |
| 143 | + } |
| 144 | + |
| 145 | + [Test] |
| 146 | + public void Remove_DecreasesCount_WhenKeyExists() |
| 147 | + { |
| 148 | + var hashTable = new HashTable<string, int>(); |
| 149 | + |
| 150 | + hashTable.Add("a", 1); |
| 151 | + hashTable.Remove("a"); |
| 152 | + |
| 153 | + Assert.AreEqual(0, hashTable.Count); |
| 154 | + } |
| 155 | + |
| 156 | + [Test] |
| 157 | + public void Remove_DoesNotDecreaseCount_WhenKeyDoesNotExist() |
| 158 | + { |
| 159 | + var hashTable = new HashTable<string, int>(); |
| 160 | + |
| 161 | + hashTable.Remove("a"); |
| 162 | + |
| 163 | + Assert.AreEqual(0, hashTable.Count); |
| 164 | + } |
| 165 | + |
| 166 | + [Test] |
| 167 | + public void ContainsValue_ReturnsFalse_WhenValueDoesNotExist() |
| 168 | + { |
| 169 | + var hashTable = new HashTable<string, int>(); |
| 170 | + |
| 171 | + Assert.IsFalse(hashTable.ContainsValue(1)); |
| 172 | + } |
| 173 | + |
| 174 | + [Test] |
| 175 | + public void ContainsValue_ReturnsTrue_WhenValueExists() |
| 176 | + { |
| 177 | + var hashTable = new HashTable<string, int>(); |
| 178 | + |
| 179 | + hashTable.Add("a", 1); |
| 180 | + |
| 181 | + Assert.IsTrue(hashTable.ContainsValue(1)); |
| 182 | + } |
| 183 | + |
| 184 | + [Test] |
| 185 | + public void ContainsValue_ReturnsFalse_WhenValueIsNull() |
| 186 | + { |
| 187 | + var hashTable = new HashTable<string, string>(); |
| 188 | + |
| 189 | + Assert.Throws<ArgumentNullException>(() => hashTable.ContainsValue(null)); |
| 190 | + } |
| 191 | + |
| 192 | + [Test] |
| 193 | + public void ContainsKey_ReturnsFalse_WhenKeyDoesNotExist() |
| 194 | + { |
| 195 | + var hashTable = new HashTable<string, int>(); |
| 196 | + |
| 197 | + Assert.IsFalse(hashTable.ContainsKey("a")); |
| 198 | + } |
| 199 | + |
| 200 | + [Test] |
| 201 | + public void ContainsKey_ReturnsTrue_WhenKeyExists() |
| 202 | + { |
| 203 | + var hashTable = new HashTable<string, int>(); |
| 204 | + |
| 205 | + hashTable.Add("a", 1); |
| 206 | + |
| 207 | + Assert.IsTrue(hashTable.ContainsKey("a")); |
| 208 | + } |
| 209 | + |
| 210 | + [Test] |
| 211 | + public void ContainsKey_ReturnsFalse_WhenKeyIsNull() |
| 212 | + { |
| 213 | + var hashTable = new HashTable<string, int>(); |
| 214 | + |
| 215 | + Assert.Throws<ArgumentNullException>(() => hashTable.ContainsKey(null)); |
| 216 | + } |
| 217 | + |
| 218 | + [Test] |
| 219 | + public void Clear_SetsCountToZero() |
| 220 | + { |
| 221 | + var hashTable = new HashTable<string, int>(); |
| 222 | + |
| 223 | + hashTable.Add("a", 1); |
| 224 | + hashTable.Clear(); |
| 225 | + |
| 226 | + Assert.AreEqual(0, hashTable.Count); |
| 227 | + } |
| 228 | + |
| 229 | + [Test] |
| 230 | + public void Clear_RemovesAllElements() |
| 231 | + { |
| 232 | + var hashTable = new HashTable<string, int>(); |
| 233 | + |
| 234 | + hashTable.Add("a", 1); |
| 235 | + hashTable.Clear(); |
| 236 | + |
| 237 | + Assert.IsFalse(hashTable.ContainsKey("a")); |
| 238 | + } |
| 239 | + |
| 240 | + [Test] |
| 241 | + public void Resize_IncreasesCapacity() |
| 242 | + { |
| 243 | + var hashTable = new HashTable<string, int>(4); |
| 244 | + |
| 245 | + hashTable.Add("one", 1); |
| 246 | + hashTable.Add("two", 2); |
| 247 | + hashTable.Add("three", 3); |
| 248 | + hashTable.Add("four", 4); |
| 249 | + hashTable.Add("humour", 5); |
| 250 | + |
| 251 | + /// Next Prime number after 4 is 5 |
| 252 | + /// Capacity should be 5 |
| 253 | + /// After resizing, the capacity should be 10 |
| 254 | + Assert.AreEqual(10, hashTable.Capacity); |
| 255 | + } |
| 256 | + [Test] |
| 257 | + public void LoadFactor_ReturnsCorrectValue() |
| 258 | + { |
| 259 | + var hashTable = new HashTable<string, int>(4); |
| 260 | + |
| 261 | + hashTable.Add("one", 1); |
| 262 | + hashTable.Add("two", 2); |
| 263 | + hashTable.Add("three", 3); |
| 264 | + hashTable.Add("four", 4); |
| 265 | + hashTable.Add("humour", 5); |
| 266 | + Assert.AreEqual(0.75f, hashTable.LoadFactor); |
| 267 | + } |
| 268 | + |
| 269 | + [Test] |
| 270 | + public void Keys_ReturnsCorrectKeys() |
| 271 | + { |
| 272 | + var hashTable = new HashTable<int, string>(); |
| 273 | + hashTable.Add(1, "one"); |
| 274 | + hashTable.Add(2, "two"); |
| 275 | + hashTable.Add(3, "three"); |
| 276 | + |
| 277 | + var keys = new List<int> { 1,2,3 }; |
| 278 | + |
| 279 | + CollectionAssert.AreEquivalent(keys, hashTable.Keys); |
| 280 | + } |
| 281 | + |
| 282 | + [Test] |
| 283 | + public void Values_ReturnsCorrectValues() |
| 284 | + { |
| 285 | + var hashTable = new HashTable<int, string>(); |
| 286 | + hashTable.Add(1, "one"); |
| 287 | + hashTable.Add(2, "two"); |
| 288 | + hashTable.Add(3, "three"); |
| 289 | + |
| 290 | + var values = new List<string> { "one", "two", "three" }; |
| 291 | + |
| 292 | + CollectionAssert.AreEquivalent(values, hashTable?.Values); |
| 293 | + } |
| 294 | + |
| 295 | + [Test] |
| 296 | + public void Constructor_ThrowsException_WhenCapacityIsZero() |
| 297 | + { |
| 298 | + Assert.Throws<ArgumentOutOfRangeException>(() => new HashTable<string, int>(0)); |
| 299 | + } |
| 300 | + |
| 301 | + [Test] |
| 302 | + public void Constructor_ThrowsException_WhenLoadFactorIsZero() |
| 303 | + { |
| 304 | + Assert.Throws<ArgumentOutOfRangeException>(() => new HashTable<string, int>(4, 0)); |
| 305 | + } |
| 306 | + |
| 307 | + [Test] |
| 308 | + public void Constructor_ThrowsException_WhenLoadFactorIsLessThanZero() |
| 309 | + { |
| 310 | + Assert.Throws<ArgumentOutOfRangeException>(() => new HashTable<string, int>(4, -1)); |
| 311 | + } |
| 312 | + |
| 313 | + [Test] |
| 314 | + public void Constructor_ThrowsException_WhenLoadFactorIsGreaterThanOne() |
| 315 | + { |
| 316 | + Assert.Throws<ArgumentOutOfRangeException>(() => new HashTable<string, int>(4, 2)); |
| 317 | + } |
| 318 | + |
| 319 | + [Test] |
| 320 | + public void GetIndex_ThrowsException_WhenKeyIsNull() |
| 321 | + { |
| 322 | + var hashTable = new HashTable<string, int>(4); |
| 323 | + Assert.Throws<ArgumentNullException>(() => hashTable.GetIndex(null)); |
| 324 | + } |
| 325 | + |
| 326 | + [Test] |
| 327 | + public void FindEntry_ThrowsException_WhenKeyIsNull() |
| 328 | + { |
| 329 | + var hashTable = new HashTable<string, int>(4); |
| 330 | + Assert.Throws<ArgumentNullException>(() => hashTable.FindEntry(null)); |
| 331 | + } |
| 332 | + |
| 333 | + [Test] |
| 334 | + public void This_Get_ThrowsException_WhenKeyIsNull() |
| 335 | + { |
| 336 | + var hashTable = new HashTable<string, int>(4); |
| 337 | + Assert.Throws<ArgumentNullException>(() => |
| 338 | + { |
| 339 | + var value = hashTable[null]; |
| 340 | + Console.WriteLine(value); |
| 341 | + }); |
| 342 | + } |
| 343 | + |
| 344 | + [Test] |
| 345 | + public void This_Set_ThrowsException_WhenKeyIsNull() |
| 346 | + { |
| 347 | + var hashTable = new HashTable<string, int>(4); |
| 348 | + Assert.Throws<ArgumentNullException>(() => hashTable[null] = 1); |
| 349 | + } |
| 350 | + |
| 351 | + [Test] |
| 352 | + public void This_Get_ReturnsCorrectValue() |
| 353 | + { |
| 354 | + var hashTable = new HashTable<string, int>(4); |
| 355 | + hashTable.Add("one", 1); |
| 356 | + Assert.AreEqual(1, hashTable["one"]); |
| 357 | + } |
| 358 | + |
| 359 | + [Test] |
| 360 | + public void This_Set_UpdatesValue() |
| 361 | + { |
| 362 | + var hashTable = new HashTable<string, int>(4); |
| 363 | + hashTable.Add("one", 1); |
| 364 | + hashTable["one"] = 2; |
| 365 | + Assert.AreEqual(2, hashTable["one"]); |
| 366 | + } |
| 367 | + |
| 368 | + [Test] |
| 369 | + public void This_Set_KeyNotFoundException_WhenKeyDoesNotExist() |
| 370 | + { |
| 371 | + var hashTable = new HashTable<string, int>(4); |
| 372 | + Assert.Throws<KeyNotFoundException>(() => hashTable["one"] = 2); |
| 373 | + } |
| 374 | + |
| 375 | + [Test] |
| 376 | + public void This_Get_KeyNotFoundException_WhenKeyDoesNotExist() |
| 377 | + { |
| 378 | + var hashTable = new HashTable<string, int>(4); |
| 379 | + Assert.Throws<KeyNotFoundException>(() => { |
| 380 | + var value = hashTable["one"]; |
| 381 | + Console.WriteLine(value); |
| 382 | + }); |
| 383 | + } |
| 384 | + } |
| 385 | +} |
0 commit comments