public void CopyToTest() { var set = UnsafeDictionary.Allocate <int, decimal>(10); // Fill set for (int i = 0; i < 10; i++) { UnsafeDictionary.Add <int, decimal>(set, i, i * i * i); } var count = UnsafeDictionary.GetCount(set); var arr = new KeyValuePair <int, decimal> [count]; UnsafeDictionary.CopyTo(set, arr, 0); // Check int num = 0; for (int i = 0; i < count; i++) { Assert.AreEqual(i, arr[num].Key); Assert.AreEqual(i * i * i, arr[num++].Value); } UnsafeDictionary.Free(set); }
public void ClearDictionary() { var map = UnsafeDictionary.Allocate <int, int>(16, fixedSize: false); for (int i = 0; i < 3; i++) { UnsafeDictionary.Add(map, i, 3); } Assert.IsTrue(UnsafeDictionary.ContainsKey(map, 2)); Assert.AreEqual(3, UnsafeDictionary.GetCount(map)); UnsafeDictionary.TryGetValue(map, 2, out int result); Assert.AreEqual(3, result); UnsafeDictionary.Add(map, 3, 1); Assert.AreEqual(4, UnsafeDictionary.GetCount(map)); UnsafeDictionary.Clear(map); Assert.AreEqual(0, UnsafeDictionary.GetCount(map)); Assert.IsFalse(UnsafeDictionary.ContainsKey(map, 2)); UnsafeDictionary.Add(map, 3, 10); Assert.AreEqual(1, UnsafeDictionary.GetCount(map)); Assert.IsTrue(UnsafeDictionary.ContainsKey(map, 3)); UnsafeDictionary.TryGetValue(map, 3, out int result2); Assert.AreEqual(10, result2); UnsafeDictionary.Clear(map); Assert.AreEqual(0, UnsafeDictionary.GetCount(map)); UnsafeDictionary.Free(map); }
public void IteratorTest() { var set = UnsafeDictionary.Allocate <int, decimal>(10); // Fill set for (int i = 0; i < 10; i++) { // Add in reverse order UnsafeDictionary.Add <int, decimal>(set, i, i * i); } var enumerator = UnsafeDictionary.GetEnumerator <int, decimal>(set); for (int i = 0; i < 10; i++) { enumerator.MoveNext(); Assert.AreEqual(i, enumerator.CurrentKey); Assert.AreEqual(i * i, enumerator.CurrentValue); Assert.AreEqual(i, enumerator.Current.Key); Assert.AreEqual(i * i, enumerator.Current.Value); } UnsafeDictionary.Free(set); }
public void TryGetValueTest() { var set = UnsafeDictionary.Allocate <int, int>(10); Assert.IsFalse(UnsafeDictionary.TryGetValue(set, 1, out int value)); UnsafeDictionary.Add(set, 1, 1); UnsafeDictionary.Add(set, 7, 2); UnsafeDictionary.Add(set, 51, 3); UnsafeDictionary.Add(set, 13, 4); Assert.IsFalse(UnsafeDictionary.TryGetValue(set, 3, out value)); Assert.IsTrue(UnsafeDictionary.TryGetValue(set, 1, out value)); Assert.AreEqual(1, value); Assert.IsTrue(UnsafeDictionary.TryGetValue(set, 7, out value)); Assert.AreEqual(2, value); Assert.IsTrue(UnsafeDictionary.TryGetValue(set, 13, out value)); Assert.AreEqual(4, value); Assert.IsTrue(UnsafeDictionary.TryGetValue(set, 51, out value)); Assert.AreEqual(3, value); Assert.IsFalse(UnsafeDictionary.TryGetValue(set, 14, out value)); UnsafeDictionary.Free(set); }
public void ConstructorTest() { var count = UnsafeHashCollection.GetNextPrime(10); var set = UnsafeDictionary.Allocate <int, bool>(10); Assert.AreEqual(0, UnsafeDictionary.GetCount(set)); Assert.AreEqual(count, UnsafeDictionary.GetCapacity(set)); UnsafeDictionary.Free(set); }
public void ContainsValueTest() { var set = UnsafeDictionary.Allocate <int, int>(10); Assert.IsFalse(UnsafeDictionary.ContainsKey <int>(set, 1)); UnsafeDictionary.Add(set, 1, 5); UnsafeDictionary.Add(set, 2, 2); UnsafeDictionary.Add(set, 3, 38); Assert.IsTrue(UnsafeDictionary.ContainsValue <int>(set, 2)); Assert.IsTrue(UnsafeDictionary.ContainsValue <int>(set, 38)); Assert.IsFalse(UnsafeDictionary.ContainsValue <int>(set, 1)); UnsafeDictionary.Free(set); }
public void InvalidTypeTest() { var set = UnsafeDictionary.Allocate <int, bool>(10); // Test key Assert.Catch <AssertException>(() => { UnsafeDictionary.Add <float, bool>(set, 4, true); }); // Test value Assert.Catch <AssertException>(() => { UnsafeDictionary.Add <int, float>(set, 4, 4.1f); }); UnsafeDictionary.Free(set); }
public void ExpandTest() { int count = UnsafeHashCollection.GetNextPrime(8); var set = UnsafeDictionary.Allocate <int, short>(count, fixedSize: false); // Valid adds for (int i = 0; i < 8; i++) { UnsafeDictionary.Add <int, short>(set, i + 1, 2); } Assert.AreEqual(8, UnsafeDictionary.GetCount(set)); Assert.AreEqual(count, UnsafeDictionary.GetCapacity(set)); UnsafeDictionary.Add <int, short>(set, 42, 12); Assert.AreEqual(9, UnsafeDictionary.GetCount(set)); Assert.AreEqual(count, UnsafeDictionary.GetCapacity(set)); UnsafeDictionary.Free(set); }
public void ExpandFailedTest() { var count = UnsafeHashCollection.GetNextPrime(7); var set = UnsafeDictionary.Allocate <int, float>(7, true); // Valid adds for (int i = 0; i < 7; i++) { UnsafeDictionary.Add <int, float>(set, i + 1, i * i); } Assert.AreEqual(7, UnsafeDictionary.GetCount(set)); Assert.AreEqual(7, UnsafeDictionary.GetCapacity(set)); Assert.Throws <InvalidOperationException>(() => { UnsafeDictionary.Add <int, float>(set, 42, 13.3f); }); UnsafeDictionary.Free(set); }
public void RemoveTest() { var set = UnsafeDictionary.Allocate <int, int>(10); Assert.IsFalse(UnsafeDictionary.Remove <int>(set, 1)); UnsafeDictionary.Add(set, 1, 3); UnsafeDictionary.Add(set, 7, 3); UnsafeDictionary.Add(set, 51, 3); UnsafeDictionary.Add(set, 13, 3); Assert.IsFalse(UnsafeDictionary.Remove <int>(set, 3)); Assert.IsTrue(UnsafeDictionary.Remove <int>(set, 1)); Assert.IsTrue(UnsafeDictionary.Remove <int>(set, 7)); Assert.IsTrue(UnsafeDictionary.Remove <int>(set, 13)); Assert.IsTrue(UnsafeDictionary.Remove <int>(set, 51)); Assert.IsFalse(UnsafeDictionary.Remove <int>(set, 13)); UnsafeDictionary.Free(set); }
public void ContainsTest() { var set = UnsafeDictionary.Allocate <int, int>(10); Assert.IsFalse(UnsafeDictionary.ContainsKey <int>(set, 1)); UnsafeDictionary.Add(set, 1, 3); UnsafeDictionary.Add(set, 7, 3); UnsafeDictionary.Add(set, 51, 3); UnsafeDictionary.Add(set, 13, 3); Assert.IsFalse(UnsafeDictionary.ContainsKey <int>(set, 3)); Assert.IsTrue(UnsafeDictionary.ContainsKey <int>(set, 1)); Assert.IsTrue(UnsafeDictionary.ContainsKey <int>(set, 7)); Assert.IsTrue(UnsafeDictionary.ContainsKey <int>(set, 13)); Assert.IsTrue(UnsafeDictionary.ContainsKey <int>(set, 51)); Assert.IsFalse(UnsafeDictionary.ContainsKey <int>(set, 14)); UnsafeDictionary.Free(set); }
public void FreeDynamicDictionary() { var s = UnsafeDictionary.Allocate <int, byte>(2, false); UnsafeDictionary.Free(s); }
public void FreeFixedDictionary() { var s = UnsafeDictionary.Allocate <int, int>(2, true); UnsafeDictionary.Free(s); }