public void CrashRdx_RemoveRange_Argument() { var rd = new RankedDictionary <int, int>(); rd.Add(3, 33); rd.Add(5, 55); rd.RemoveRange(1, 2); }
static void Main() { var dary1 = new RankedDictionary <string, int> (StringComparer.InvariantCultureIgnoreCase); dary1.Add("AAA", 0); dary1.Add("bbb", 1); dary1.Add("CCC", 2); dary1.Add("ddd", 3); Console.WriteLine("Comparer is case insensitive:"); foreach (System.Collections.Generic.KeyValuePair <string, int> pair in dary1) { Console.WriteLine(pair.Key); } Console.WriteLine(); var dary2 = new RankedDictionary <string, int> (StringComparer.Ordinal); dary2.Add("AAA", 0); dary2.Add("bbb", 2); dary2.Add("CCC", 1); dary2.Add("ddd", 3); Console.WriteLine("Comparer is case sensitive:"); foreach (System.Collections.Generic.KeyValuePair <string, int> pair in dary2) { Console.WriteLine(pair.Key); } }
public void CrashRd_Ctor1NoComparer_InvalidOperation() { var comp0 = (System.Collections.Generic.Comparer <Person>)null; #if TEST_BCL var d1 = new SortedDictionary <Person, int> (comp0); #else var d1 = new RankedDictionary <Person, int> (comp0); #endif d1.Add(new Person("Zed"), 1); d1.Add(new Person("Macron"), 2); }
public void UnitRdx_ElementsBetweenIndexes() { int n = 30; var rd = new RankedDictionary <int, int> { Capacity = 4 }; for (int ii = 0; ii < n; ++ii) { rd.Add(ii, -ii); } for (int p1 = 0; p1 < n; ++p1) { for (int p2 = p1; p2 < n; ++p2) { int actual = 0; foreach (var pair in rd.ElementsBetweenIndexes(p1, p2)) { actual += pair.Key; } int expected = (p2 - p1 + 1) * (p1 + p2) / 2; Assert.AreEqual(expected, actual); } } }
public void UnitRdx_IndexOfKey() { var rd = new RankedDictionary <int, int> { Capacity = 5 }; for (int ii = 0; ii < 500; ii += 2) { rd.Add(ii, ii + 1000); } for (int ii = 0; ii < 500; ii += 2) { int ix = rd.IndexOfKey(ii); Assert.AreEqual(ii / 2, ix); } int iw = rd.IndexOfKey(-1); Assert.AreEqual(~0, iw); int iy = rd.IndexOfKey(500); Assert.AreEqual(~250, iy); }
public void CrashRdx_RemoveAtA_ArgumentOutOfRange() { var rd = new RankedDictionary <int, int>(); rd.Add(42, 24); rd.RemoveAt(-1); }
public void UnitRdx_RemoveAt() { var rd = new RankedDictionary <int, int>(); for (int ii = 0; ii < 5000; ++ii) { rd.Add(ii, -ii); } for (int i2 = 4900; i2 >= 0; i2 -= 100) { rd.RemoveAt(i2); } for (int i2 = 0; i2 < 5000; ++i2) { if (i2 % 100 == 0) { Assert.IsFalse(rd.ContainsKey(i2)); } else { Assert.IsTrue(rd.ContainsKey(i2)); } } }
public void UnitRd_TryGetValueForUnfoundKeyString() { #if TEST_BCL var sd = new SortedDictionary <string, int> (StringComparer.Ordinal); #else var sd = new RankedDictionary <string, int> (StringComparer.Ordinal) { Capacity = 4 }; #endif for (char c = 'A'; c <= 'Z'; ++c) { sd.Add(c.ToString(), (int)c); } bool result1 = sd.TryGetValue("M", out int val1); bool result2 = sd.TryGetValue("U", out int val2); bool result3 = sd.TryGetValue("$", out int val3); Assert.AreEqual(val1, 'M'); Assert.AreEqual(val2, 'U'); Assert.IsTrue(result1); Assert.IsTrue(result2); Assert.IsFalse(result3); }
public void UnitRdx_ElementsFromMissingVal() { var rd = new RankedDictionary <int, int>() { Capacity = 8 }; #if STRESS int n = 1000; #else int n = 10; #endif for (int i = 0; i < n; i += 2) { rd.Add(i, -i); } for (int i = 1; i < n - 1; i += 2) { foreach (var x in rd.ElementsFrom(i)) { Assert.AreEqual(i + 1, x.Key, "Incorrect key value"); break; } } }
public void UnitRdx_ElementsBetweenA() { var rd = new RankedDictionary <string, int>(); var pc = (ICollection <KeyValuePair <string, int> >)rd; rd.Add("Alpha", 1); rd.Add("Beta", 2); rd.Add("Omega", 24); pc.Add(new KeyValuePair <string, int> (null, 0)); int actual = 0; foreach (var kv in rd.ElementsBetween(null, "C")) { ++actual; } Assert.AreEqual(3, actual); }
static void Main() { IFormatter formatter = new BinaryFormatter(); var set1 = new RankedDictionary <Person, string> (new PersonComparer()); set1.Add(new Person("Hugh", "Mann"), "B+"); set1.Add(new Person("Hammond", "Egger"), "C-"); SerializePersons("Persons.bin", set1, formatter); Console.WriteLine($"Wrote {set1.Count} key/value pairs."); Console.WriteLine(); RankedDictionary <Person, string> set2 = DeserializePersons("Persons.bin", formatter); Console.WriteLine("Read back:"); foreach (var kv in set2) { Console.WriteLine(kv); } }
public void UnitRd_Ctor1A2() { #if TEST_BCL var tree = new SortedDictionary <string, int> (StringComparer.Ordinal); #else var tree = new RankedDictionary <string, int> (StringComparer.Ordinal); #endif tree.Add("AAA", 0); tree.Add("bbb", 2); tree.Add("CCC", 1); tree.Add("ddd", 3); int actualPosition = 0; foreach (KeyValuePair <string, int> pair in tree) { Assert.AreEqual(actualPosition, pair.Value); ++actualPosition; } Assert.AreEqual(4, actualPosition); }
public void UnitRdx_TryGet() { var rd = new RankedDictionary <string, int> (StringComparer.InvariantCultureIgnoreCase); rd.Add("AAA", 1); rd.Add("bbb", 2); rd.Add("ccc", 3); bool got1 = rd.Keys.TryGet("aaa", out string actual1); Assert.IsTrue(got1); Assert.AreEqual("AAA", actual1); bool got2 = rd.Keys.TryGet("bb", out string actual2); Assert.IsFalse(got2); bool got3 = rd.Keys.TryGet("CCC", out string actual3); Assert.IsTrue(got3); Assert.AreEqual("ccc", actual3); }
public void UnitRdvx_Indexer() { var rd = new RankedDictionary <string, int> { Capacity = 4 }; foreach (var kv in greek) { rd.Add(kv.Key, kv.Value); } Assert.AreEqual(11, rd.Values[7]); }
public void UnitRdx_IndexOfValue() { var rd = new RankedDictionary <int, int>(); for (int ii = 0; ii < 500; ++ii) { rd.Add(ii, ii + 1000); } var ix1 = rd.IndexOfValue(1400); Assert.AreEqual(400, ix1); var ix2 = rd.IndexOfValue(88888); Assert.AreEqual(-1, ix2); }
static void Main(string[] args) { foreach (var reps in new int[] { 100, 1000, 10000, 100000, 1000000, 10000000, 20000000, 40000000 }) { tree = new RankedDictionary <Guid, int>(); for (int ii = 0; ii < reps; ++ii) { tree.Add(Guid.NewGuid(), ii); } Console.WriteLine(reps); #if DEBUG Console.WriteLine(tree.GetTreeStatsText()); #endif } }
public void UnitRdx_RemoveWhereB() { int n = 2000; var rd = new RankedDictionary <int, int> { Capacity = 7 }; for (int ix = 0; ix < n; ++ix) { rd.Add(ix, -ix); } int removed = rd.RemoveWhere(IsAlways); Assert.AreEqual(n, removed); Assert.AreEqual(0, rd.Count); }
static void Main() { int reps = 5000000; var sd = new SortedDictionary <Guid, int>(); Console.Write($"Loading SortedDictionary with {reps} elements:\n\nLoad time = "); Stopwatch watch1 = new Stopwatch(); watch1.Reset(); watch1.Start(); for (int i = 0; i < reps; ++i) { var guid = Guid.NewGuid(); sd.Add(guid, i); } var time11 = watch1.ElapsedMilliseconds; Console.WriteLine(time11 + "ms"); for (int order = 32; order <= 256; order += 16) { var bt = new RankedDictionary <Guid, int>() { Capacity = order }; Console.Write($"\nLoading BtreeDictionary (order={order}) with {reps} elements:\n\nLoad time = "); Stopwatch watch2 = new Stopwatch(); watch2.Reset(); watch2.Start(); for (int i = 0; i < reps; ++i) { var guid = Guid.NewGuid(); bt.Add(guid, i); } var time21 = watch2.ElapsedMilliseconds; Console.WriteLine($"{time21}ms"); } }
public void UnitRdx_RemoveWhereA() { var rd = new RankedDictionary <int, int>(); for (int ix = 0; ix < 1000; ++ix) { rd.Add(ix, ix + 1000); } int c0 = rd.Count; int removed = rd.RemoveWhere(IsEven); Assert.AreEqual(500, removed); foreach (int key in rd.Keys) { Assert.IsTrue(key % 2 != 0); } }
public void UnitRdx_RemoveWhereElement() { var rd = new RankedDictionary <int, int>(); for (int ix = 0; ix < 1000; ++ix) { rd.Add(ix, -ix); } int c0 = rd.Count; int removed = rd.RemoveWhereElement(IsPairEven); Assert.AreEqual(500, removed); foreach (int val in rd.Values) { Assert.IsTrue(val % 2 != 0); } }
public void UnitRdx_ElementsFromPassedEnd() { var rd = new RankedDictionary <int, int>(); for (int i = 0; i < 1000; ++i) { rd.Add(i, -i); } int iterations = 0; foreach (var x in rd.ElementsFrom(2000)) { ++iterations; } Assert.AreEqual(0, iterations, "SkipUntilKey shouldn't find anything"); }
public void UnitRdvx_IndexOf() { var rd = new RankedDictionary <int, int> { Capacity = 5 }; for (int ii = 0; ii < 900; ++ii) { rd.Add(ii, ii + 1000); } var ix1 = rd.Values.IndexOf(1500); Assert.AreEqual(500, ix1); var ix2 = rd.Values.IndexOf(77777); Assert.AreEqual(-1, ix2); }
public void UnitRdx_Reverse() { var rd = new RankedDictionary <int, int> { Capacity = 5 }; int expected = 500; for (int ii = 1; ii <= expected; ++ii) { rd.Add(ii, -ii); } foreach (var actual in rd.Reverse()) { Assert.AreEqual(expected, actual.Key); Assert.AreEqual(-expected, actual.Value); --expected; } Assert.AreEqual(0, expected); }
public void UnitRdx_RemoveRange() { var rd = new RankedDictionary <int, int> { Capacity = 7 }; for (int ii = 0; ii < 20; ++ii) { rd.Add(ii, -ii); } rd.RemoveRange(20, 0); Assert.AreEqual(20, rd.Count); rd.RemoveRange(12, 4); Assert.AreEqual(16, rd.Count); #if DEBUG rd.SanityCheck(); #endif }
public void UnitRdx_ElementsBetweenPassedEnd() { var rd = new RankedDictionary <int, int>(); for (int i = 0; i < 1000; ++i) { rd.Add(i, -i); } int iterations = 0; int sumVals = 0; foreach (KeyValuePair <int, int> e in rd.ElementsBetween(500, 1500)) { ++iterations; sumVals += e.Value; } Assert.AreEqual(500, iterations); Assert.AreEqual(-374750, sumVals, "Sum of values not correct"); }
public void UnitRdx_ElementsBetweenB() { var rd = new RankedDictionary <int, int> { Capacity = 4 }; for (int i = 90; i >= 0; i -= 10) { rd.Add(i, -100 - i); } int iterations = 0; int sumVals = 0; foreach (var kv in rd.ElementsBetween(35, 55)) { ++iterations; sumVals += kv.Value; } Assert.AreEqual(2, iterations); Assert.AreEqual(-290, sumVals); }
public void UnitRdx_MinMax() { var rd = new RankedDictionary <int, int> { Capacity = 4 }; int min0 = rd.MinKey; int max0 = rd.MaxKey; Assert.AreEqual(default(int), min0); Assert.AreEqual(default(int), max0); for (int i1 = 1; i1 <= 99; ++i1) { rd.Add(i1, i1 + 100); } int min = rd.MinKey; int max = rd.MaxKey; Assert.AreEqual(1, min); Assert.AreEqual(99, max); }
public void UnitRdx_TryGetValueIndex() { var rd = new RankedDictionary <int, int>(); rd.Capacity = 5; for (int ii = 0; ii < 500; ii += 2) { rd.Add(ii, ii + 1000); } for (int ii = 0; ii < 500; ii += 2) { bool isOk = rd.TryGetValueAndIndex(ii, out int v1, out int i1); Assert.IsTrue(isOk); Assert.AreEqual(ii / 2, i1); Assert.AreEqual(ii + 1000, v1); } bool isOkNot = rd.TryGetValueAndIndex(111, out int v2, out int i2); Assert.IsFalse(isOkNot); }
public void UnitRdx_ElementsFromB() { var rd = new RankedDictionary <int, int>(); for (int i = 1; i <= 1000; ++i) { rd.Add(i, -i); } int firstKey = -1; int iterations = 0; foreach (var e in rd.ElementsFrom(501)) { if (iterations == 0) { firstKey = e.Key; } ++iterations; } Assert.AreEqual(501, firstKey); Assert.AreEqual(500, iterations); }
public void UnitRdx_Capacity() { var rd = new RankedDictionary <int, int>(); var initial = rd.Capacity; rd.Capacity = 0; Assert.AreEqual(initial, rd.Capacity); rd.Capacity = 3; Assert.AreEqual(initial, rd.Capacity); rd.Capacity = 257; Assert.AreEqual(initial, rd.Capacity); rd.Capacity = 4; Assert.AreEqual(4, rd.Capacity); rd.Capacity = 256; Assert.AreEqual(256, rd.Capacity); rd.Add(1, 11); rd.Capacity = 128; Assert.AreEqual(256, rd.Capacity); }