Exemplo n.º 1
0
        public void Remove()
        {
            var rd = new RouletteDictionary <string, int>();

            for (int i = 0; i < 10; i++)
            {
                rd[i.ToString()] = i;
            }
            Assert.Equal(10, rd.Count);

            // remove evens.
            for (int i = 0; i < 10; i += 2)
            {
                rd.Remove(i.ToString());
            }
            Assert.Equal(5, rd.Count);

            for (int i = 0; i < 10; i++)
            {
                bool found = rd.TryGetValue(i.ToString(), out int ret);
                if (i % 2 == 0)
                {
                    Assert.False(found);
                }
                else
                {
                    Assert.True(found);
                    Assert.Equal(i, ret);
                }
            }
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        public void CustomComparer()
        {
            var rd = new RouletteDictionary <string, string>(comparer: StringComparer.OrdinalIgnoreCase);

            rd["hello"] = "world";
            Assert.Equal("world", rd["HELLO"]);
            Assert.Throws <KeyNotFoundException>(() => rd["foo"]);
        }
Exemplo n.º 4
0
        public void OneItem()
        {
            var rd = new RouletteDictionary <string, string>(1);

            rd["hello"] = "world";
            Assert.Equal("world", rd["hello"]);
            Assert.Throws <KeyNotFoundException>(() => rd["foo"]);
        }
Exemplo n.º 5
0
        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"));
        }
Exemplo n.º 6
0
        public void GetRandomUnsatisfied()
        {
            var rd = new RouletteDictionary <string, int>(1000, StringComparer.Ordinal);

            rd["foo"] = 42;
            Assert.Throws <InvalidOperationException>(() =>
            {
                rd.GetRandomValue(v => v == 24);
            });
        }
Exemplo n.º 7
0
        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"));
        }
Exemplo n.º 8
0
        public void RemoveAndGet()
        {
            var rd = new RouletteDictionary <string, string>(100);

            rd["foo"] = "bar";

            var removedKVP = rd.RemoveRandomAndGet();

            Assert.Equal("foo", removedKVP.Key);
            Assert.Equal("bar", removedKVP.Value);
            Assert.Empty(rd);
        }
Exemplo n.º 9
0
        public void UpdateItem()
        {
            var rd = new RouletteDictionary <string, int>();

            for (int i = 0; i < 10; i++)
            {
                rd[i.ToString()] = i;
            }

            rd["5"] = 42;
            Assert.Equal(42, rd["5"]);
            Assert.Equal(3, rd["3"]);
        }
Exemplo n.º 10
0
        public void SetAndMaintainCount()
        {
            var rd = new RouletteDictionary <string, int>(1000, StringComparer.Ordinal);

            for (int i = 0; i < 10; i++)
            {
                rd[i.ToString()] = i;
            }

            rd.SetAndMaintainCount("foo", 42);
            Assert.Equal(10, rd.Count);
            Assert.True(rd.ContainsKey("foo"));
        }
Exemplo n.º 11
0
        public void RemoveRandom()
        {
            var rd = new RouletteDictionary <string, int>();

            for (int i = 0; i < 10; i++)
            {
                rd[i.ToString()] = i;
            }

            bool removed = rd.RemoveRandom();

            Assert.True(removed);
            Assert.Equal(9, rd.Count);
        }
Exemplo n.º 12
0
        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));
        }
Exemplo n.º 13
0
        public void LotsOfCollisions()
        {
            // Make sure the dictionary still works, even when there are lots of collisions
            var rd = new RouletteDictionary <string, int>(comparer: new BadHasher());

            for (int i = 0; i < 1_000; i++)
            {
                rd[i.ToString()] = i;
            }

            Assert.Equal(1_000, rd.Count);

            for (int i = 0; i < 1_000; i++)
            {
                Assert.Equal(i, rd[i.ToString()]);
            }
        }
Exemplo n.º 14
0
        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");
            }
        }
Exemplo n.º 15
0
        public void TenThousand()
        {
            // start with low capacity to exercise resizing.
            var rd = new RouletteDictionary <string, int>(1);

            for (int i = 0; i < 10_000; i++)
            {
                rd[i.ToString()] = i;
            }

            Assert.Equal(10_000, rd.Count);

            for (int i = 0; i < 10_000; i++)
            {
                Assert.Equal(i, rd[i.ToString()]);
            }
        }
Exemplo n.º 16
0
        public void GetRandom()
        {
            var rd = new RouletteDictionary <string, string>(42);

            rd["hello"] = "world";

            var key = rd.GetRandomKey();

            Assert.Equal("hello", key);

            var val = rd.GetRandomValue();

            Assert.Equal("world", val);

            var kvp = rd.GetRandomKeyAndValue();

            Assert.Equal("hello", kvp.Key);
            Assert.Equal("world", kvp.Value);
        }
Exemplo n.º 17
0
        public void Trim()
        {
            var rd = new RouletteDictionary <string, int>();

            for (int i = 0; i < 1234; i++)
            {
                rd[i.ToString()] = i;
            }

            int initialCap = rd.Capacity;

            Assert.True(initialCap > 1234);

            rd.Trim();
            Assert.Equal(500, rd["500"]);


            rd.Clear();
            rd.Trim();
            Assert.Equal(8, rd.Capacity);
        }
Exemplo n.º 18
0
        public void RemoveRandomWithCondition()
        {
            var rd = new RouletteDictionary <string, int>();

            for (int i = 0; i < 10; i++)
            {
                rd[i.ToString()] = i;
            }

            // remove all 5 even numbers using RemoveRandom
            for (int i = 0; i < 5; i++)
            {
                bool removed = rd.RemoveRandom((val) => val % 2 == 0);
                Assert.True(removed);
            }

            // make sure everything left is odd.
            Assert.Equal(5, rd.Count);
            foreach (int val in rd.Values)
            {
                Assert.True(val % 2 == 1);
            }
        }
Exemplo n.º 19
0
        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");
            }
        }
Exemplo n.º 20
0
        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);
        }
Exemplo n.º 21
0
        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");
            }
        }
Exemplo n.º 22
0
        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);
                }
            }
        }
Exemplo n.º 23
0
        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);
        }
Exemplo n.º 24
0
        public void RemoveAddAgain()
        {
            var rd = new RouletteDictionary <string, int>();

            for (int i = 0; i < 10; i++)
            {
                rd[i.ToString()] = i;
            }

            rd.Remove("7");

            Assert.Equal(9, rd.Count);
            Assert.False(rd.ContainsKey("7"));
            Assert.True(rd.ContainsKey("8"));

            // put it back in, make sure everything's still ok:
            rd["7"] = 7;

            Assert.Equal(10, rd.Count);
            for (int i = 0; i < 10; i++)
            {
                Assert.Equal(i, rd[i.ToString()]);
            }
        }
Exemplo n.º 25
0
        public void EmptyDict()
        {
            var rd = new RouletteDictionary <int, int>();

            Assert.Throws <KeyNotFoundException>(() => rd[42]);
        }