⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multidictionarytests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 4 页
字号:
            dict1.Add("foo", 6);
            dict1.Add("z", 3);
            dict1.Add("bar", 8);
            dict1.Add("z", 3);
            dict1.Add("foo", 1);
            dict1.Add("bill", 9);

            dict1.ReplaceMany("bill", new int[0]);
            dict1.ReplaceMany("foo", new int[] { 13, 4 });
            dict1.ReplaceMany("z", new int[] { 19 });
            dict1.ReplaceMany("hello", new int[] { 193, -11, 193 });
            dict1.ReplaceMany("goodbye", new int[0]);
            dict1.ReplaceMany("foo", new int[] { 123, 0, 4 });
            dict1.Add("foo", 29);

            CheckMultiDictionaryContents(dict1,
                new string[] { "bar", "foo", "hello", "z" },
                new int[][] { new int[] { 7, 8 }, new int[] { 0, 4, 29, 123 }, new int[] { -11, 193 }, new int[] { 19 } },
                "sailor", 19921, null, null);
        }

        [Test]
        public void RemoveKey()
        {
            MultiDictionary<string, int> dict1 = new MultiDictionary<string, int>(true);

            dict1.Add("foo", 4);
            dict1.Add("bar", 7);
            dict1.Add("foo", 6);
            dict1.Add("z", 3);
            dict1.Add("bar", 8);
            dict1.Add("z", 10);
            dict1.Add("z", 3);
            dict1.Add("foo", 4);
            dict1.Add("bill", 9);

            Assert.IsTrue(dict1.ContainsKey("bill"));
            Assert.IsTrue(dict1.ContainsKey("foo"));
            Assert.IsTrue(dict1.ContainsKey("z"));

            Assert.IsTrue(dict1.Remove("bill"));
            Assert.IsFalse(dict1.Remove("bill"));
            Assert.IsFalse(dict1.Remove("smell"));
            Assert.IsTrue(dict1.Remove("foo"));

            CheckMultiDictionaryContents(dict1,
                new string[] { "bar", "z" },
                new int[][] { new int[] { 7, 8 }, new int[] { 3, 3, 10 } },
                "sailor", 19921, null, null);
        }

        [Test]
        public void RemoveManyKeys()
        {
            MultiDictionary<string, int> dict1 = new MultiDictionary<string, int>(true);

            dict1.Add("foo", 4);
            dict1.Add("bar", 7);
            dict1.Add("foo", 6);
            dict1.Add("z", 3);
            dict1.Add("bar", 8);
            dict1.Add("z", 10);
            dict1.Add("z", 3);
            dict1.Add("foo", 4);
            dict1.Add("bill", 9);

            Assert.IsTrue(dict1.ContainsKey("bill"));
            Assert.IsTrue(dict1.ContainsKey("foo"));
            Assert.IsTrue(dict1.ContainsKey("z"));

            Assert.AreEqual(2, dict1.RemoveMany(new string[] { "bill", "smell", "foo", "bill" }));

            CheckMultiDictionaryContents(dict1,
                new string[] { "bar", "z" },
                new int[][] { new int[] { 7, 8 }, new int[] { 3, 3, 10 } },
                "sailor", 19921, null, null);
        }

        [Test]
        public void Remove()
        {
            MultiDictionary<string, int> dict1 = new MultiDictionary<string, int>(true);

            dict1.Add("foo", 4);
            dict1.Add("bar", 7);
            dict1.Add("foo", 6);
            dict1.Add("z", 3);
            dict1.Add("bar", 8);
            dict1.Add("z", 10);
            dict1.Add("z", 3);
            dict1.Add("foo", 4);
            dict1.Add("bill", 9);
            dict1.Add("foo", 4);

            Assert.IsTrue(dict1.Remove("foo", 4));
            Assert.IsTrue(dict1.Remove("foo", 4));
            Assert.IsTrue(dict1.Remove("z", 10));
            Assert.IsFalse(dict1.Remove("z", 10));
            Assert.IsFalse(dict1.Remove("foo", 11));
            Assert.IsFalse(dict1.Remove(null, 0));
            Assert.IsTrue(dict1.Remove("bill", 9));

            CheckMultiDictionaryContents(dict1,
                new string[] { "bar", "foo", "z" },
                new int[][] { new int[] { 7, 8 }, new int[] { 4, 6 }, new int[] { 3, 3 } },
                "sailor", 19921, null, null);
        }

        [Test]
        public void RemoveMany1()
        {
            MultiDictionary<string, int> dict1 = new MultiDictionary<string, int>(true);

            dict1.Add("bill", 7);
            dict1.Add("foo", 4);
            dict1.Add("bar", 7);
            dict1.Add("foo", 6);
            dict1.Add("z", 3);
            dict1.Add("bar", 8);
            dict1.Add("z", 10);
            dict1.Add("z", 3);
            dict1.Add("foo", 4);
            dict1.Add("bill", 9);
            dict1.Add("foo", 4);

            Assert.AreEqual(2, dict1.RemoveMany("foo", new int[] { 4, 11, 4 }));
            Assert.AreEqual(1, dict1.RemoveMany("z", new int[] { 9, 2, 10 }));
            Assert.AreEqual(0, dict1.RemoveMany("z", new int[] { 10, 16, 144, 10 }));
            Assert.AreEqual(0, dict1.RemoveMany("foo", new int[0]));
            Assert.AreEqual(0, dict1.RemoveMany(null, new int[2] { 1, 2 }));
            Assert.AreEqual(2, dict1.RemoveMany("bill", new int[] { 9, 7 }));

            CheckMultiDictionaryContents(dict1,
                new string[] { "bar", "foo", "z" },
                new int[][] { new int[] { 7, 8 }, new int[] { 4, 6 }, new int[] { 3, 3 } },
                "sailor", 19921, null, null);
        }

        [Test]
        public void Clear()
        {
            MultiDictionary<string, int> dict1 = new MultiDictionary<string, int>(true);

            dict1.Add("foo", 4);
            dict1.Add("bill", 7);
            dict1.Add("foo", 4);
            dict1.Add("bar", 7);
            dict1.Add("foo", 6);
            dict1.Add("z", 3);
            dict1.Add("bar", 8);
            dict1.Add("z", 10);
            dict1.Add(null, 3);
            dict1.Add("foo", 4);
            dict1.Add("bill", 9);
            dict1.Add("foo", 4);

            dict1.Clear();

            Assert.AreEqual(0, dict1.Count);
            Assert.IsFalse(dict1.ContainsKey("foo"));
            Assert.IsFalse(dict1.ContainsKey("z"));
            Assert.IsFalse(dict1.ContainsKey(null));
            Assert.AreEqual(0, Algorithms.Count(dict1.Keys));
            Assert.AreEqual(0, Algorithms.Count(dict1.Values));
            Assert.AreEqual(0, Algorithms.Count(dict1.KeyValuePairs));

            CheckMultiDictionaryContents(dict1, new string[0], new int[0][], "foo", 4, null, null);
        }

        [Test]
        public void Count()
        {
            MultiDictionary<string, int> dict1 = new MultiDictionary<string, int>(true);

            dict1.Add("foo", 4);
            dict1.Add(null, 7);
            dict1.Add("bar", 11);
            dict1.Add("foo", 7);
            dict1.Add(null, 7);
            dict1.Add("hello", 11);
            dict1.Add("foo", 4);
            Assert.AreEqual(4, dict1.Count);

            MultiDictionary<string, int> dict2 = new MultiDictionary<string, int>(false);

            dict2.Add("foo", 4);
            dict2.Add(null, 7);
            dict2.Add("bar", 11);
            dict2.Add("foo", 7);
            dict2.Add(null, 7);
            dict2.Add("hello", 11);
            dict2.Add("foo", 4);
            Assert.AreEqual(4, dict2.Count);

            dict2.Remove("foo");
            Assert.AreEqual(3, dict2.Count);

            dict2.Clear();
            Assert.AreEqual(0, dict2.Count);
        }

        [Test]
        public void ContainsKey()
        {
            MultiDictionary<string, int> dict1 = new MultiDictionary<string, int>(true);

            dict1.Add("foo", 4);
            dict1.Add(null, 7);
            dict1.Add("bar", 11);
            dict1.Add("foo", 7);
            dict1.Add(null, 7);
            dict1.Add("hello", 11);
            dict1.Add("foo", 4);

            Assert.IsTrue(dict1.ContainsKey(null));
            Assert.IsTrue(dict1.ContainsKey("foo"));
            Assert.IsTrue(dict1.ContainsKey("bar"));
            Assert.IsTrue(dict1.ContainsKey("hello"));
            dict1.Remove("hello", 11);
            Assert.IsFalse(dict1.ContainsKey("hello"));
            dict1.Remove(null, 7);
            Assert.IsTrue(dict1.ContainsKey(null));
            dict1.Remove(null, 7);
            Assert.IsFalse(dict1.ContainsKey(null));
        }

        [Test]
        public void Contains()
        {
            MultiDictionary<string, int> dict1 = new MultiDictionary<string, int>(true);

            dict1.Add("foo", 4);
            dict1.Add(null, 7);
            dict1.Add("bar", 11);
            dict1.Add("foo", 7);
            dict1.Add(null, 7);
            dict1.Add("hello", 11);
            dict1.Add("foo", 4);

            Assert.IsTrue(dict1.Contains(null, 7));
            Assert.IsTrue(dict1.Contains("foo", 4));
            Assert.IsTrue(dict1.Contains("bar", 11));
            Assert.IsTrue(dict1.Contains("hello", 11));
            Assert.IsFalse(dict1.Contains("HELLO", 11));
            Assert.IsFalse(dict1.Contains("bar", 12));
            Assert.IsFalse(dict1.Contains("foo", 0));
            dict1.Remove("hello", 11);
            Assert.IsFalse(dict1.Contains("hello", 11));
            dict1.Remove(null, 7);
            Assert.IsTrue(dict1.Contains(null, 7));
            dict1.Remove(null, 7);
            Assert.IsFalse(dict1.Contains(null, 7));
        }

        [Test]
        public void KeysCollection()
        {
            MultiDictionary<string, int> dict1 = new MultiDictionary<string, int>(false, StringComparer.InvariantCultureIgnoreCase);

            dict1.Add("foo", 4);
            dict1.Add(null, 2);
            dict1.Add("bar", 3);
            dict1.Add("sailor", 0);
            dict1.Add("FOO", 9);
            dict1.Add("b", 7);
            dict1.Add("Foo", -1);
            dict1.Add("BAR", 3);
            dict1.Remove("b", 7);

            InterfaceTests.TestReadonlyCollectionGeneric<string>(dict1.Keys, new string[] { null, "bar", "foo", "sailor" }, false, null);

            Assert.IsTrue(dict1.Keys.Contains("foo"));
            Assert.IsTrue(dict1.Keys.Contains("Foo"));
            Assert.IsTrue(dict1.Keys.Contains(null));
            Assert.IsTrue(dict1.Keys.Contains("Sailor"));
            Assert.IsFalse(dict1.Keys.Contains("banana"));

            MultiDictionary<string, int> dict2 = new MultiDictionary<string, int>(false, StringComparer.InvariantCultureIgnoreCase);
            InterfaceTests.TestEnumerableElementsAnyOrder(dict2.Keys, new string[] { });
        }

        [Test]
        public void ValuesCollection1()
        {
            MultiDictionary<double, string> dict = new MultiDictionary<double, string>(false, EqualityComparer<double>.Default, StringComparer.InvariantCultureIgnoreCase);

            dict.Add(7, "Gizzle");
            dict.Add(4, "foo");
            dict.Add(6, "Foo");
            dict.Add(3, "FOO");
            dict.Add(3, "baz");
            dict.Add(3, "bar");
            dict.Add(4, "FOo");
            dict.Add(3, "BAZ");
            dict.Add(5, "bAZ");
            dict.Add(7, "hello");
            dict.Add(7, "foo");

            ICollection<string> vals = dict.Values;

            string[] expected = {
                "bar", "BAZ", "FOO", "FOo", "bAZ", "Foo", "foo", "Gizzle", "hello"};

            InterfaceTests.TestReadonlyCollectionGeneric<string>(vals, expected, false, null);

            Assert.IsTrue(vals.Contains("gizzle"));
            Assert.IsTrue(vals.Contains("FOO"));
            Assert.IsTrue(vals.Contains("fOO"));
            Assert.IsTrue(vals.Contains("hello"));
            Assert.IsTrue(vals.Contains("bar"));
            Assert.IsTrue(vals.Contains("BAR"));
            Assert.IsFalse(vals.Contains("qatar"));
        }

        [Test]
        public void ValuesCollection2()
        {
            MultiDictionary<double, string> dict = new MultiDictionary<double, string>(true, EqualityComparer<double>.Default, StringComparer.InvariantCultureIgnoreCase);

            dict.Add(7, "Gizzle");
            dict.Add(4, "foo");
            dict.Add(6, "Foo");
            dict.Add(3, "FOO");
            dict.Add(3, "baz");
            dict.Add(3, "bar");
            dict.Add(4, "FOo");
            dict.Add(3, "BAZ");
            dict.Add(5, "bAZ");
            dict.Add(7, "hello");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -