@@ -247,6 +247,9 @@ public void InsertKeysThatForceShiftShouldKeepArraysInSync()
247247 // Now insert a smaller key, which should shift existing elements
248248 store . Add ( 1 , 50 ) ; // Will be inserted at index 0, causes shift right
249249
250+ // Dump internal state for debug (optional)
251+ Trace . WriteLine ( store . ToString ( ) ) ;
252+
250253 // Assert
251254 Assert . AreEqual ( 2 , store . Count , "Store should have two occupied entries." ) ;
252255
@@ -278,12 +281,193 @@ public void InsertRemoveCompactReinsertShouldKeepConsistency()
278281 store . Add ( 1 , 30 ) ; // Reinsert key 1
279282
280283 var keys = store . Keys . ToArray ( ) ;
284+
285+ // Dump internal state for debug (optional)
286+ Trace . WriteLine ( store . ToString ( ) ) ;
287+
281288 CollectionAssert . AreEqual ( new [ ] { 1 , 2 } , keys ) ;
282289
283290 Assert . AreEqual ( 30 , store [ 1 ] ) ;
284291 Assert . AreEqual ( 20 , store [ 2 ] ) ;
285292 }
286293
294+ /// <summary>
295+ /// Inserts the keys starting at one should keep arrays aligned.
296+ /// </summary>
297+ [ TestMethod ]
298+ public void InsertKeysStartingAtOne_ShouldKeepArraysAligned ( )
299+ {
300+ var store = new SortedKvStore ( ) ;
301+
302+ // Insert keys starting from 1 (not 0)
303+ store . Add ( 1 , 100 ) ;
304+ store . Add ( 3 , 300 ) ;
305+ store . Add ( 2 , 200 ) ; // Insert out of order to force shift
306+
307+ // Validate count
308+ Assert . AreEqual ( 3 , store . Count , "Count should be 3 after inserts." ) ;
309+
310+ // Validate keys are sorted
311+ var keys = store . Keys . ToArray ( ) ;
312+ CollectionAssert . AreEqual ( new [ ] { 1 , 2 , 3 } , keys , "Keys should be sorted." ) ;
313+
314+ // Validate values match keys
315+ Assert . AreEqual ( 100 , store [ 1 ] , "Value for key 1 should be 100." ) ;
316+ Assert . AreEqual ( 200 , store [ 2 ] , "Value for key 2 should be 200." ) ;
317+ Assert . AreEqual ( 300 , store [ 3 ] , "Value for key 3 should be 300." ) ;
318+
319+ // Remove key 2 and check
320+ store . Remove ( 2 ) ;
321+ Assert . IsFalse ( store . ContainsKey ( 2 ) , "Key 2 should be removed." ) ;
322+
323+ // Compact to physically remove unoccupied entries
324+ store . Compact ( ) ;
325+
326+ Assert . AreEqual ( 2 , store . Count , "Count should be 2 after removal and compact." ) ;
327+
328+ keys = store . Keys . ToArray ( ) ;
329+ CollectionAssert . AreEqual ( new [ ] { 1 , 3 } , keys , "Keys after removal should be 1 and 3." ) ;
330+
331+ // Reinsert key 2 to check insert after compact works
332+ store . Add ( 2 , 250 ) ;
333+ Assert . IsTrue ( store . ContainsKey ( 2 ) , "Key 2 should exist after reinsertion." ) ;
334+ Assert . AreEqual ( 250 , store [ 2 ] , "Value for key 2 should be updated to 250." ) ;
335+
336+ // Dump internal state for debug (optional)
337+ Trace . WriteLine ( store . ToString ( ) ) ;
338+
339+ // Final keys check to confirm sorted order
340+ keys = store . Keys . ToArray ( ) ;
341+ CollectionAssert . AreEqual ( new [ ] { 1 , 2 , 3 } , keys , "Final keys should be sorted 1, 2, 3." ) ;
342+ }
343+
344+ /// <summary>
345+ /// Removes the many should update count correctly.
346+ /// </summary>
347+ [ TestMethod ]
348+ public void RemoveManyShouldUpdateCountCorrectly ( )
349+ {
350+ var store = new SortedKvStore
351+ {
352+ // Insert 5 keys
353+ { 1 , 100 } ,
354+ { 2 , 200 } ,
355+ { 3 , 300 } ,
356+ { 4 , 400 } ,
357+ { 5 , 500 }
358+ } ;
359+
360+ Assert . AreEqual ( 5 , store . Count ) ;
361+
362+ // Remove keys 2 and 4
363+ var keysToRemove = new int [ ] { 2 , 4 } ;
364+ store . RemoveMany ( keysToRemove ) ;
365+
366+ // Dump internal state for debug (optional)
367+ Trace . WriteLine ( store . ToString ( ) ) ;
368+
369+ // Check keys 2 and 4 are removed
370+ Assert . IsFalse ( store . ContainsKey ( 2 ) ) ;
371+ Assert . IsFalse ( store . ContainsKey ( 4 ) ) ;
372+
373+ // Check keys 1, 3, 5 are still present
374+ Assert . IsTrue ( store . ContainsKey ( 1 ) ) ;
375+ Assert . IsTrue ( store . ContainsKey ( 3 ) ) ;
376+ Assert . IsTrue ( store . ContainsKey ( 5 ) ) ;
377+
378+ // THIS WILL FAIL IF Count IS NOT UPDATED
379+ Assert . AreEqual ( 5 , store . Count , "Count should NOT be updated after RemoveMany" ) ;
380+ }
381+
382+ /// <summary>
383+ /// Removes the many with unsorted keys removes correctly.
384+ /// </summary>
385+ [ TestMethod ]
386+ public void RemoveManyWithUnsortedKeysRemovesCorrectly ( )
387+ {
388+ var store = new SortedKvStore ( ) ;
389+
390+ // Add keys in sorted order
391+ int [ ] keys = { 1 , 3 , 5 , 7 , 9 } ;
392+ foreach ( var key in keys )
393+ {
394+ store . Add ( key , key * 10 ) ;
395+ }
396+
397+ // Confirm initial state
398+ Assert . AreEqual ( keys . Length , store . Count ) ;
399+ foreach ( var key in keys )
400+ {
401+ Assert . IsTrue ( store . ContainsKey ( key ) , $ "Should contain key { key } initially.") ;
402+ }
403+
404+ // Remove keys (unsorted input)
405+ int [ ] keysToRemove = { 7 , 3 } ;
406+ store . RemoveMany ( keysToRemove ) ;
407+
408+ // Dump internal state for debug (optional)
409+ Trace . WriteLine ( store . ToString ( ) ) ;
410+
411+ // Check keys that should remain
412+ int [ ] expectedRemaining = { 1 , 5 , 9 } ;
413+ foreach ( var key in expectedRemaining )
414+ {
415+ Assert . IsTrue ( store . ContainsKey ( key ) , $ "Expected to contain key { key } after RemoveMany.") ;
416+ }
417+
418+ // Check keys that should be removed
419+ foreach ( var key in keysToRemove )
420+ {
421+ Assert . IsFalse ( store . ContainsKey ( key ) , $ "Expected NOT to contain key { key } after RemoveMany.") ;
422+ }
423+ }
424+
425+ /// <summary>
426+ /// Removes the and compact test.
427+ /// </summary>
428+ [ TestMethod ]
429+ public void RemoveAndCompactTest ( )
430+ {
431+ var store = new SortedKvStore
432+ {
433+ // Add some entries
434+ { 1 , 10 } ,
435+ { 3 , 30 } ,
436+ { 5 , 50 } ,
437+ { 7 , 70 } ,
438+ { 9 , 90 }
439+ } ;
440+
441+ Assert . IsTrue ( store . ContainsKey ( 3 ) ) ;
442+ Assert . AreEqual ( 5 , store . Count ) ;
443+
444+ // Remove key 3 logically
445+ store . Remove ( 3 ) ;
446+
447+ // Immediately after remove, key is logically gone
448+ Assert . IsFalse ( store . ContainsKey ( 3 ) ) ;
449+
450+ // Count still includes all, including logically removed
451+ Assert . AreEqual ( 5 , store . Count ) ;
452+
453+ // Compact physically removes unoccupied entries
454+ store . Compact ( ) ;
455+
456+ // After compact, key is definitely gone
457+ Assert . IsFalse ( store . ContainsKey ( 3 ) ) ;
458+
459+ // Count updated to reflect physical removal
460+ Assert . AreEqual ( 4 , store . Count ) ;
461+
462+ // Dump internal state for debug (optional)
463+ Trace . WriteLine ( store . ToString ( ) ) ;
464+
465+ // Optional: check that other keys are still present
466+ Assert . IsTrue ( store . ContainsKey ( 1 ) ) ;
467+ Assert . IsTrue ( store . ContainsKey ( 5 ) ) ;
468+ Assert . IsTrue ( store . ContainsKey ( 7 ) ) ;
469+ Assert . IsTrue ( store . ContainsKey ( 9 ) ) ;
470+ }
287471
288472 }
289473}
0 commit comments