public void BadAdd() { var rd = new RouletteDictionary <int, int>(); rd.Add(42, 42); // Can't add when key already exists: Assert.Throws <ArgumentException>(() => rd.Add(42, 123)); }
public void ZeroHash() { // Check hashcode of 0, which is a special case. var rd = new RouletteDictionary <string, string>(100, new BadHasher()); rd.Add("foo", "bar"); Assert.Equal("bar", rd["foo"]); Assert.True(rd.Remove("foo")); }
public void RemoveMissing() { var rd = new RouletteDictionary <string, int>(); for (int i = 0; i < 10; i++) { rd.Add(i.ToString(), i); } Assert.False(rd.Remove("12345")); }
public void RemoveAndResize() { var rd = new RouletteDictionary <string, int>(); // Add first 500. for (int i = 0; i < 500; i++) { rd.Add(i.ToString(), i); } // remove everything divisible by 7 for (int i = 0; i < 500; i += 7) { bool removed = rd.Remove(i.ToString()); Assert.True(removed); } // Add 2000 more elements to force resizing. for (int i = 500; i < 2500; i++) { rd.Add(i.ToString(), i); } for (int i = 0; i < 2500; i++) { bool found = rd.TryGetValue(i.ToString(), out int ret); if ((i < 500) && (i % 7 == 0)) { Assert.False(found); } else { Assert.True(found); Assert.Equal(i, ret); } } }
public void ContainsValue() { var rd = new RouletteDictionary <string, int>(); for (int i = 0; i < 100; i++) { rd.Add(i.ToString(), i); } rd.Remove("42"); Assert.True(rd.ContainsValue(66)); Assert.True(rd.Values.Contains(88)); Assert.False(rd.ContainsValue(42)); Assert.False(rd.ContainsValue(543434)); }
public void EnumerateEmpty() { var rd = new RouletteDictionary <string, int>(); for (int i = 0; i < 10; i++) { rd.Add(i.ToString(), i); } rd.Clear(); Assert.Empty(rd); foreach (var kvp in rd) { Assert.True(false, "shouldn't ever get here"); } }
public void EnumerateValues() { var rd = new RouletteDictionary <int, int>(); for (int i = 0; i < 100; i++) { rd.Add(i, i); } rd.Remove(99); bool[] returnedVals = new bool[99]; foreach (var val in rd.Values) { Assert.False(returnedVals[val], "value was returned twice from enumerator"); returnedVals[val] = true; } foreach (bool found in returnedVals) { Assert.True(found, "a value was not returned"); } }
public void EnumerateKeys() { var rd = new RouletteDictionary <int, int>(); for (int i = 0; i < 100; i++) { rd.Add(i, i); } rd.Remove(99); bool[] returnedKeys = new bool[99]; foreach (var key in rd.Keys) { Assert.False(returnedKeys[key], "key was returned twice from enumerator"); returnedKeys[key] = true; } foreach (bool found in returnedKeys) { Assert.True(found, "a key was not returned"); } }
public void CheckEnumerator() { var rd = new RouletteDictionary <string, int>(); for (int i = 0; i < 10; i++) { rd.Add(i.ToString(), i); } bool removed = rd.Remove("9"); Assert.True(removed); int counter = 0; foreach (var kvp in rd) { Assert.True(kvp.Value < 9); counter++; } Assert.Equal(9, counter); }
public void GetRandomWithCondition() { var rd = new RouletteDictionary <string, int>(1000, StringComparer.Ordinal); for (int i = 0; i < 10; i++) { rd[i.ToString()] = i; } rd.Add("foo", 1234); var key = rd.GetRandomKey((v) => v == 1234); Assert.Equal("foo", key); var val = rd.GetRandomValue((v) => v == 1234); Assert.Equal(1234, val); var kvp = rd.GetRandomKeyAndValue((v) => v == 1234); Assert.Equal("foo", kvp.Key); Assert.Equal(1234, kvp.Value); }