Skip to content

Commit 8702ad8

Browse files
author
LoneWandererProductions
committed
add a test
1 parent 18477f6 commit 8702ad8

2 files changed

Lines changed: 27 additions & 35 deletions

File tree

CommonExtendedObjectsTests/SortedKvStoreTests.cs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ public class SortedKvStoreTests
2828
/// </summary>
2929
private const int ItemCount = 100_000;
3030

31-
private SortedKvStore _store;
32-
33-
private int _entryCount;
34-
3531
/// <summary>
3632
/// Adds the and try get works.
3733
/// </summary>
@@ -477,32 +473,28 @@ public void RemoveAndCompactTest()
477473
Assert.IsTrue(store.ContainsKey(9));
478474
}
479475

480-
public void TestCase()
476+
/// <summary>
477+
/// Removes the and compact test.
478+
/// </summary>
479+
[TestMethod]
480+
public void BinarySearch()
481481
{
482-
const int size = 1024;
483-
var count = 10;
482+
var store = new SortedKvStore(128);
484483

484+
for (int i = 0; i < 10; i++)
485+
{
486+
store.Add(i, i);
487+
}
485488

486-
_store = new SortedKvStore(128);
487-
488-
for (var cycle = 0; cycle < 5; cycle++)
489+
for (int i = 5; i < 10; i++)
489490
{
490-
var handles = new List<int>(count);
491-
for (var i = 0; i < count; i++)
492-
handles[i] = Allocate(size);
493-
for (var i = 0; i < count; i++)
494-
Free(handles[i]);
491+
store.Remove(i);
495492
}
496-
}
497493

498-
private void Free(int memoryHandle)
499-
{
500-
//_store[id] = _entryCount;
501-
}
494+
Trace.WriteLine(store.ToString());
495+
var position = store.BinarySearch(9);
502496

503-
private int Allocate(int size)
504-
{
505-
return 1;
497+
Assert.AreEqual(9, position, "Wrong position.");
506498
}
507499
}
508500
}

ExtendedSystemObjects/SortedKvStore.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,18 @@ public void Compact()
385385
ArrayPool<int>.Shared.Return(rented);
386386
}
387387

388+
/// <summary>
389+
/// Performs a binary search for the specified key.
390+
/// </summary>
391+
/// <param name="key">The key to locate.</param>
392+
/// <returns>
393+
/// The index of the key if found; otherwise, the bitwise complement of the index at which the key should be inserted.
394+
/// </returns>
395+
public int BinarySearch(int key)
396+
{
397+
return Utility.BinarySearch(_keys.AsSpan(), Count, key);
398+
}
399+
388400
/// <summary>
389401
/// Removes all entries from the store.
390402
/// </summary>
@@ -433,17 +445,5 @@ private void EnsureCapacity()
433445
_values.EnsureCapacity(Count + 1);
434446
_occupied.EnsureCapacity(Count + 1);
435447
}
436-
437-
/// <summary>
438-
/// Performs a binary search for the specified key.
439-
/// </summary>
440-
/// <param name="key">The key to locate.</param>
441-
/// <returns>
442-
/// The index of the key if found; otherwise, the bitwise complement of the index at which the key should be inserted.
443-
/// </returns>
444-
private int BinarySearch(int key)
445-
{
446-
return Utility.BinarySearch(_keys.AsSpan(), Count, key);
447-
}
448448
}
449449
}

0 commit comments

Comments
 (0)