示例#1
0
        public void TestGet()
        {
            Assert.AreEqual(2, _dict["TWO"]);
            int i;

            Assert.IsFalse(_dict.TryGetValue("Thousand", out i));
            Assert.IsTrue(_dict.TryGetValue("two", out i));
        }
        public void RemoveAll()
        {         //****************************************
            var Random  = Initialise();
            var Records = new StringKeyDictionary <int>();

            var Dictionary = new Dictionary <string, int>(1024);

            //****************************************

            foreach (var Pair in YieldRandom(Random, 1024))
            {
                Dictionary.Add(Pair.Key, Pair.Value);
                Records.Add(Pair.Key, Pair.Value);
            }

            //****************************************

            Records.RemoveAll((pair) => { if (Random.Next() > int.MaxValue / 2)
                                          {
                                              return(Dictionary.Remove(pair.Key));
                                          }
                                          return(false); });

            //****************************************

            CollectionAssert.AreEquivalent(Dictionary, Records, "Collections don't match");

            foreach (var MyPair in Dictionary)
            {
                Assert.IsTrue(Records.TryGetValue(MyPair.Key, out var Value));
                Assert.AreEqual(MyPair.Value, Value);
            }

            Thread.Sleep(1);
        }
        public void PrePopulate()
        {         //****************************************
            var Random = Initialise();

            var Dictionary = new Dictionary <string, int>(1024);

            //****************************************

            foreach (var Pair in YieldRandom(Random, 1024))
            {
                Dictionary.Add(Pair.Key, Pair.Value);
            }

            //****************************************

            var Records = new StringKeyDictionary <int>(Dictionary);

            //****************************************

            Assert.AreEqual(1024, Records.Count, "Count incorrect");

            CollectionAssert.AreEquivalent(Dictionary, Records, "Collections don't match");

            foreach (var MyPair in Dictionary)
            {
                Assert.IsTrue(Records.TryGetValue(MyPair.Key, out var Value));
                Assert.AreEqual(MyPair.Value, Value);
            }

            Thread.Sleep(1);
        }
        public void AddRangePrePopulated()
        {         //****************************************
            var Random  = Initialise();
            var Records = new StringKeyDictionary <int>();

            var Dictionary = new Dictionary <string, int>(1024);
            var SecondSet  = new List <KeyValuePair <string, int> >(512);

            //****************************************

            foreach (var Pair in YieldRandom(Random, 1024))
            {
                Dictionary.Add(Pair.Key, Pair.Value);

                if (Records.Count < 512)
                {
                    Records.Add(Pair.Key, Pair.Value);
                }
                else
                {
                    SecondSet.Add(Pair);
                }
            }

            //****************************************

            Records.AddRange(SecondSet);

            //****************************************

            Assert.AreEqual(1024, Records.Count, "Count incorrect");

            CollectionAssert.AreEquivalent(Dictionary, Records, "Collections don't match");

            foreach (var MyPair in Dictionary)
            {
                Assert.IsTrue(Records.TryGetValue(MyPair.Key, out var Value));
                Assert.AreEqual(MyPair.Value, Value);
            }

            Thread.Sleep(1);
        }
        public void RemoveAt()
        {         //****************************************
            var Random  = Initialise();
            var Records = new StringKeyDictionary <int>();

            var Dictionary = new Dictionary <string, int>(1024);

            //****************************************

            foreach (var Pair in YieldRandom(Random, 1024))
            {
                Dictionary.Add(Pair.Key, Pair.Value);
                Records.Add(Pair.Key, Pair.Value);
            }

            //****************************************

            for (var Index = 0; Index < 512; Index++)
            {
                var InnerIndex = Random.Next(Records.Count);

                var Key = Records.Keys[InnerIndex];

                Records.RemoveAt(InnerIndex);
                Assert.IsTrue(Dictionary.Remove(Key));
            }

            //****************************************

            Assert.AreEqual(512, Records.Count, "Count incorrect");

            CollectionAssert.AreEquivalent(Dictionary, Records, "Collections don't match");

            foreach (var MyPair in Dictionary)
            {
                Assert.IsTrue(Records.TryGetValue(MyPair.Key, out var Value));
                Assert.AreEqual(MyPair.Value, Value);
            }

            Thread.Sleep(1);
        }
        public void RemoveRange()
        {         //****************************************
            var Random  = Initialise();
            var Records = new StringKeyDictionary <int>();

            var Dictionary = new Dictionary <string, int>(1024);

            //****************************************

            foreach (var Pair in YieldRandom(Random, 1024))
            {
                Dictionary.Add(Pair.Key, Pair.Value);
                Records.Add(Pair.Key, Pair.Value);
            }

            //****************************************

            foreach (var Key in Enumerable.Skip <string>(Records.Keys, 256).Take(256))
            {
                Dictionary.Remove(Key);
            }

            Records.RemoveRange(256, 256);

            //****************************************

            CollectionAssert.AreEquivalent(Dictionary, Records, "Collections don't match");

            foreach (var MyPair in Dictionary)
            {
                Assert.IsTrue(Records.TryGetValue(MyPair.Key, out var Value));
                Assert.AreEqual(MyPair.Value, Value);
            }

            Thread.Sleep(1);
        }