-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContiguousMapTests.cs
More file actions
580 lines (485 loc) · 13.8 KB
/
Copy pathContiguousMapTests.cs
File metadata and controls
580 lines (485 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// Copyright (c) 2023-2026 ktsu-dev contributors
namespace ktsu.Containers.Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class ContiguousMapTests
{
[TestMethod]
public void Constructor_WithoutParameters_CreatesEmptyMap()
{
// Arrange & Act
ContiguousMap<int, string> map = [];
// Assert
Assert.AreEqual(0, map.Count);
Assert.IsFalse(map.IsReadOnly, "Map should not be read-only");
}
[TestMethod]
public void Constructor_WithCapacity_CreatesMapWithCapacity()
{
// Arrange & Act
ContiguousMap<int, string> map = new(10);
// Assert
Assert.AreEqual(0, map.Count);
}
[TestMethod]
public void Constructor_WithKeyValuePairs_AddsUniqueKeys()
{
// Arrange
KeyValuePair<int, string>[] items =
[
new(3, "three"),
new(1, "one"),
new(4, "four"),
new(1, "duplicate"), // Duplicate key
new(2, "two"),
];
// Act
ContiguousMap<int, string> map = [];
foreach (KeyValuePair<int, string> item in items)
{
map.TryAdd(item.Key, item.Value);
}
// Assert
Assert.AreEqual(4, map.Count); // Only unique keys
Assert.AreEqual("one", map[1]); // First value for key 1
Assert.AreEqual("two", map[2]);
Assert.AreEqual("three", map[3]);
Assert.AreEqual("four", map[4]);
}
[TestMethod]
public void Add_NewKeyValuePair_AddsSuccessfully()
{
// Arrange
ContiguousMap<int, string> map = [];
// Act
map.Add(5, "five");
// Assert
Assert.AreEqual(1, map.Count);
Assert.AreEqual("five", map[5]);
Assert.IsTrue(map.ContainsKey(5), "Map should contain added key");
}
[TestMethod]
public void Add_DuplicateKey_ThrowsArgumentException()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(5, "five");
// Act & Assert
Assert.ThrowsExactly<ArgumentException>(() => map.Add(5, "another five"));
}
[TestMethod]
public void TryAdd_NewKey_ReturnsTrue()
{
// Arrange
ContiguousMap<int, string> map = [];
// Act
bool result = map.TryAdd(5, "five");
// Assert
Assert.IsTrue(result, "TryAdd should return true for new key");
Assert.AreEqual(1, map.Count);
Assert.AreEqual("five", map[5]);
}
[TestMethod]
public void TryAdd_DuplicateKey_ReturnsFalse()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(5, "five");
// Act
bool result = map.TryAdd(5, "another five");
// Assert
Assert.IsFalse(result, "TryAdd should return false for duplicate key");
Assert.AreEqual(1, map.Count);
Assert.AreEqual("five", map[5]); // Original value remains
}
[TestMethod]
public void Indexer_ExistingKey_ReturnsValue()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(1, "one");
map.Add(2, "two");
// Act & Assert
Assert.AreEqual("one", map[1]);
Assert.AreEqual("two", map[2]);
}
[TestMethod]
public void Indexer_NonExistingKey_ThrowsKeyNotFoundException()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(1, "one");
// Act & Assert
Assert.ThrowsExactly<KeyNotFoundException>(() => map[2]);
}
[TestMethod]
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Major Bug",
"S4143:Collection elements should not be replaced unconditionally",
Justification = "<Pending>"
)]
public void Indexer_Set_UpdatesValue()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(1, "one");
// Act
map[1] = "ONE";
// Assert
Assert.AreEqual("ONE", map[1]);
Assert.AreEqual(1, map.Count); // Count should not change
}
[TestMethod]
public void Indexer_SetNewKey_AddsKeyValuePair()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(1, "one");
// Act
map[2] = "two";
// Assert
Assert.AreEqual("two", map[2]);
Assert.AreEqual(2, map.Count);
}
[TestMethod]
public void ContainsKey_ExistingKey_ReturnsTrue()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(1, "one");
map.Add(2, "two");
// Act & Assert
Assert.IsTrue(map.ContainsKey(1), "Map should contain key 1");
Assert.IsTrue(map.ContainsKey(2), "Map should contain key 2");
}
[TestMethod]
public void ContainsKey_NonExistingKey_ReturnsFalse()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(1, "one");
// Act & Assert
Assert.IsFalse(map.ContainsKey(2), "Map should not contain key 2");
Assert.IsFalse(map.ContainsKey(0), "Map should not contain key 0");
}
[TestMethod]
public void TryGetValue_ExistingKey_ReturnsTrueWithValue()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(1, "one");
// Act
bool result = map.TryGetValue(1, out string? value);
// Assert
Assert.IsTrue(result, "TryGetValue should return true for existing key");
Assert.AreEqual("one", value);
}
[TestMethod]
public void TryGetValue_NonExistingKey_ReturnsFalseWithDefault()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(1, "one");
// Act
bool result = map.TryGetValue(2, out string? value);
// Assert
Assert.IsFalse(result, "TryGetValue should return false for non-existing key");
Assert.IsNull(value);
}
[TestMethod]
public void Remove_ExistingKey_RemovesAndReturnsTrue()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(1, "one");
map.Add(2, "two");
map.Add(3, "three");
// Act
bool result = map.Remove(2);
// Assert
Assert.IsTrue(result, "Remove should return true for existing key");
Assert.AreEqual(2, map.Count);
Assert.IsFalse(map.ContainsKey(2), "Map should not contain removed key");
Assert.IsTrue(map.ContainsKey(1), "Map should still contain key 1");
Assert.IsTrue(map.ContainsKey(3), "Map should still contain key 3");
}
[TestMethod]
public void Remove_NonExistingKey_ReturnsFalse()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(1, "one");
// Act
bool result = map.Remove(2);
// Assert
Assert.IsFalse(result, "Remove should return false for non-existing key");
Assert.AreEqual(1, map.Count);
}
[TestMethod]
public void Clear_WithElements_RemovesAllElements()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(1, "one");
map.Add(2, "two");
map.Add(3, "three");
// Act
map.Clear();
// Assert
Assert.AreEqual(0, map.Count);
Assert.IsFalse(map.ContainsKey(1), "Map should not contain key 1 after clear");
Assert.IsFalse(map.ContainsKey(2), "Map should not contain key 2 after clear");
Assert.IsFalse(map.ContainsKey(3), "Map should not contain key 3 after clear");
}
[TestMethod]
public void Keys_ReturnsAllKeys()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(3, "three");
map.Add(1, "one");
map.Add(4, "four");
map.Add(2, "two");
// Act
HashSet<int> keys = [.. map.Keys];
// Assert
Assert.HasCount(4, keys);
Assert.Contains(1, keys);
Assert.Contains(2, keys);
Assert.Contains(3, keys);
Assert.Contains(4, keys);
}
[TestMethod]
public void Values_ReturnsAllValues()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(3, "three");
map.Add(1, "one");
map.Add(4, "four");
map.Add(2, "two");
// Act
HashSet<string> values = [.. map.Values];
// Assert
Assert.HasCount(4, values);
Assert.Contains("one", values);
Assert.Contains("two", values);
Assert.Contains("three", values);
Assert.Contains("four", values);
}
[TestMethod]
public void GetEnumerator_IteratesAllPairs()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(3, "three");
map.Add(1, "one");
map.Add(4, "four");
map.Add(2, "two");
// Act
Dictionary<int, string> enumerated = [];
foreach (KeyValuePair<int, string> kvp in map)
{
enumerated.Add(kvp.Key, kvp.Value);
}
// Assert
Assert.HasCount(4, enumerated);
Assert.AreEqual("one", enumerated[1]);
Assert.AreEqual("two", enumerated[2]);
Assert.AreEqual("three", enumerated[3]);
Assert.AreEqual("four", enumerated[4]);
}
[TestMethod]
public void Contains_ExistingKeyValuePair_ReturnsTrue()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(1, "one");
map.Add(2, "two");
// Act & Assert
Assert.IsTrue(map.Contains(new KeyValuePair<int, string>(1, "one")), "Map should contain key-value pair (1, 'one')");
Assert.IsTrue(map.Contains(new KeyValuePair<int, string>(2, "two")), "Map should contain key-value pair (2, 'two')");
}
[TestMethod]
public void Contains_NonExistingKeyValuePair_ReturnsFalse()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(1, "one");
// Act & Assert
Assert.IsFalse(map.Contains(new KeyValuePair<int, string>(1, "ONE")), "Map should not contain key-value pair with wrong value");
Assert.IsFalse(map.Contains(new KeyValuePair<int, string>(2, "two")), "Map should not contain key-value pair with wrong key");
}
[TestMethod]
public void CopyTo_ValidArray_CopiesElements()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(3, "three");
map.Add(1, "one");
KeyValuePair<int, string>[] array = new KeyValuePair<int, string>[4];
// Act
map.CopyTo(array, 1);
// Assert
Assert.AreEqual(default, array[0]); // Unchanged
// Verify that the two pairs are in the array (order may vary)
HashSet<KeyValuePair<int, string>> copied = [array[1], array[2]];
Assert.Contains(new KeyValuePair<int, string>(1, "one"), copied);
Assert.Contains(new KeyValuePair<int, string>(3, "three"), copied);
Assert.AreEqual(default, array[3]); // Unchanged
}
[TestMethod]
public void GetKeysSpan_ReturnsCorrectSpan()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(3, "three");
map.Add(1, "one");
map.Add(4, "four");
map.Add(2, "two");
// Act
ReadOnlySpan<int> keysSpan = map.GetKeysSpan();
// Assert
Assert.HasCount(4, keysSpan);
// Verify all keys are in the span
HashSet<int> spanKeys = [];
for (int i = 0; i < keysSpan.Length; i++)
{
spanKeys.Add(keysSpan[i]);
}
Assert.HasCount(4, spanKeys);
Assert.Contains(1, spanKeys);
Assert.Contains(2, spanKeys);
Assert.Contains(3, spanKeys);
Assert.Contains(4, spanKeys);
}
[TestMethod]
public void GetValuesSpan_ReturnsCorrectSpan()
{
// Arrange
ContiguousMap<int, string> map = [];
map.Add(3, "three");
map.Add(1, "one");
map.Add(4, "four");
map.Add(2, "two");
// Act
ReadOnlySpan<string> valuesSpan = map.GetValuesSpan();
// Assert
Assert.HasCount(4, valuesSpan);
// Verify all values are in the span
HashSet<string> spanValues = [];
for (int i = 0; i < valuesSpan.Length; i++)
{
spanValues.Add(valuesSpan[i]);
}
Assert.HasCount(4, spanValues);
Assert.Contains("one", spanValues);
Assert.Contains("two", spanValues);
Assert.Contains("three", spanValues);
Assert.Contains("four", spanValues);
}
[TestMethod]
public void ContiguousMemoryLayout_OptimalForCachePerformance()
{
// Arrange
ContiguousMap<int, string> map = [];
// Act - Add many key-value pairs
for (int i = 0; i < 1000; i++)
{
map.Add(i, $"value_{i}");
}
// Assert - Verify all elements are accessible
Assert.AreEqual(1000, map.Count);
for (int i = 0; i < 1000; i++)
{
Assert.IsTrue(map.ContainsKey(i), $"Map should contain key {i}");
Assert.AreEqual($"value_{i}", map[i]);
}
// Test span access (only possible with contiguous memory)
ReadOnlySpan<int> keysSpan = map.GetKeysSpan();
ReadOnlySpan<string> valuesSpan = map.GetValuesSpan();
Assert.HasCount(1000, keysSpan);
Assert.HasCount(1000, valuesSpan);
// Verify all keys are in the span
HashSet<int> spanKeys = [];
for (int i = 0; i < keysSpan.Length; i++)
{
spanKeys.Add(keysSpan[i]);
}
Assert.HasCount(1000, spanKeys);
}
[TestMethod]
public void WorksWithCustomTypes()
{
// Arrange
ContiguousMap<string, int> map = [];
// Act
map.Add("charlie", 3);
map.Add("alpha", 1);
map.Add("bravo", 2);
// Assert
Assert.AreEqual(3, map.Count);
Assert.AreEqual(1, map["alpha"]);
Assert.AreEqual(2, map["bravo"]);
Assert.AreEqual(3, map["charlie"]);
}
[TestMethod]
public void HandlesNullValues()
{
// Arrange
ContiguousMap<int, string?> map = [];
// Act
map.Add(1, null);
map.Add(2, "two");
// Assert
Assert.AreEqual(2, map.Count);
Assert.IsNull(map[1]);
Assert.AreEqual("two", map[2]);
Assert.IsTrue(map.TryGetValue(1, out string? value), "TryGetValue should return true for key with null value");
Assert.IsNull(value);
}
[TestMethod]
public void Entry_Equals_SameKeyAndValue_ReturnsTrue()
{
// Arrange
ContiguousMap<int, string>.Entry left = new(1, "one");
ContiguousMap<int, string>.Entry right = new(1, "one");
// Act & Assert
Assert.IsTrue(left.Equals(right), "Entries with the same key and value should be equal");
Assert.IsTrue(left.Equals((object)right), "Equals(object) should agree with Equals(Entry)");
Assert.IsTrue(left == right, "operator == should report equality");
Assert.IsFalse(left != right, "operator != should report equality");
Assert.AreEqual(left.GetHashCode(), right.GetHashCode());
}
[TestMethod]
public void Entry_Equals_DifferentKeyOrValue_ReturnsFalse()
{
// Arrange
ContiguousMap<int, string>.Entry entry = new(1, "one");
ContiguousMap<int, string>.Entry differentKey = new(2, "one");
ContiguousMap<int, string>.Entry differentValue = new(1, "two");
// Act & Assert
Assert.IsFalse(entry.Equals(differentKey), "Entries with different keys should not be equal");
Assert.IsFalse(entry.Equals(differentValue), "Entries with different values should not be equal");
Assert.IsTrue(entry != differentKey, "operator != should report inequality");
Assert.IsFalse(entry == differentValue, "operator == should report inequality");
}
[TestMethod]
public void Entry_Equals_NonEntryObject_ReturnsFalse()
{
// Arrange
ContiguousMap<int, string>.Entry entry = new(1, "one");
// Act & Assert
Assert.IsFalse(entry.Equals(null), "An entry should not equal null");
Assert.IsFalse(entry.Equals("not an entry"), "An entry should not equal an unrelated type");
}
[TestMethod]
public void Entry_KeyAndValue_ExposeConstructorArguments()
{
// Arrange & Act
ContiguousMap<int, string>.Entry entry = new(7, "seven");
// Assert
Assert.AreEqual(7, entry.Key);
Assert.AreEqual("seven", entry.Value);
}
}