コード例 #1
0
        public void RemoveAllCopies()
        {
            var bag1 = new Bag <string>(StringComparer.InvariantCultureIgnoreCase);

            var i = bag1.RemoveAllCopies("Eric");

            Assert.AreEqual(0, i);
            bag1.Add("hello");
            bag1.Add("foo");
            bag1.Add(null);
            bag1.Add(null);
            bag1.Add("hello");
            bag1.Add(null);
            i = bag1.RemoveAllCopies("HELLO");
            Assert.AreEqual(2, i);
            i = bag1.RemoveAllCopies("Hello");
            Assert.AreEqual(0, i);
            i = bag1.RemoveAllCopies(null);
            Assert.AreEqual(3, i);
            bag1.Add("Hello");
            bag1.Add("Eric");
            bag1.Add(null);
            i = bag1.RemoveAllCopies(null);
            Assert.AreEqual(1, i);
            bag1.Add("ERIC");
            i = bag1.RemoveAllCopies("eRic");
            Assert.AreEqual(2, i);
        }
コード例 #2
0
        public void ToArray()
        {
            string[] s_array = { null, "Foo", "Eric", null, "Clapton", "hello", "Clapton", "goodbye", "C#" };
            var      bag1    = new Bag <string>();

            string[] a1 = bag1.ToArray();
            Assert.IsNotNull(a1);
            Assert.AreEqual(0, a1.Length);

            foreach (string s in s_array)
            {
                bag1.Add(s);
            }

            var a2 = bag1.ToArray();

            Array.Sort(s_array);
            Array.Sort(a2);

            Assert.AreEqual(s_array.Length, a2.Length);

            for (var i = 0; i < s_array.Length; ++i)
            {
                Assert.AreEqual(s_array[i], a2[i]);
            }
        }
コード例 #3
0
        public void DistinctItems()
        {
            var bag1 = new Bag <string>(new[] { "foo", null, "Foo", "Eric", "FOO", "eric", "bar" },
                                        StringComparer.InvariantCultureIgnoreCase);

            InterfaceTests.TestEnumerableElementsAnyOrder(bag1.DistinctItems(), new string[] { null, "bar", "Eric", "foo" });

            // Make sure enumeration stops on change.
            int count = 0;

            try {
                foreach (string s in bag1.DistinctItems())
                {
                    if (count == 2)
                    {
                        bag1.Add("zippy");
                    }
                    ++count;
                }
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(3, count);
            }
        }
コード例 #4
0
        public void SerializeStrings()
        {
            Bag <string> d = new Bag <string>(StringComparer.InvariantCultureIgnoreCase);

            d.Add("foo");
            d.Add("world");
            d.Add("hello");
            d.Add("elvis");
            d.Add("ELVIS");
            d.Add(null);
            d.Add("Foo");
            d.AddMany(new string[] { "1", "2", "3", "4", "5", "6" });
            d.AddMany(new string[] { "7", "8", "9", "1", "2", "3" });

            Bag <string> result = (Bag <string>)InterfaceTests.SerializeRoundTrip(d);


            InterfaceTests.TestReadWriteCollectionGeneric(result,
                                                          new string[]
            {
                "1", "2", "3", "4", "5", "6", "elvis", "elvis", "hello", "foo", "foo",
                "WORLD", null, "7", "8", "9", "1", "2", "3"
            }, false,
                                                          StringComparer.InvariantCultureIgnoreCase.Equals);
        }
コード例 #5
0
        public void FailFastEnumerator1()
        {
            var bag1 = new Bag <double>();

            double d = 1.218034;

            for (var i = 0; i < 50; ++i)
            {
                bag1.Add(d);
                d = d * 1.3451 - .31;
            }

            // should throw once the bag is modified.
            foreach (var k in bag1.Where(k => k > 3.0))
            {
                bag1.Add(1.0);
            }
        }
コード例 #6
0
        public void NumberOfCopies()
        {
            var bag1 = new Bag <string>(StringComparer.InvariantCultureIgnoreCase)
            {
                null, "hello", "foo"
            };

            Assert.AreEqual(1, bag1.NumberOfCopies("hello"));
            Assert.AreEqual(1, bag1.NumberOfCopies("FOO"));
            Assert.AreEqual(1, bag1.NumberOfCopies(null));
            bag1.Add(null);
            bag1.Add("HELLO");
            bag1.Add("Hello");
            Assert.AreEqual(3, bag1.NumberOfCopies("hello"));
            Assert.AreEqual(2, bag1.NumberOfCopies(null));
            bag1.Remove(null);
            bag1.Remove(null);
            Assert.AreEqual(0, bag1.NumberOfCopies(null));
        }
コード例 #7
0
        public void AddMany()
        {
            string[] s_array = { "FOO", "x", "elmer", "fudd", "Clapton", null };
            var      bag1    = new Bag <string>(StringComparer.InvariantCultureIgnoreCase)
            {
                "foo", "Eric", "Clapton"
            };

            bag1.AddMany(s_array);

            InterfaceTests.TestReadWriteCollectionGeneric(bag1,
                                                          new[]
                                                          { null, "Clapton", "Clapton", "elmer", "Eric", "foo", "foo", "fudd", "x" },
                                                          false);

            bag1.Clear();
            bag1.Add("foo");
            bag1.Add("Eric");
            bag1.AddMany(bag1);
            InterfaceTests.TestReadWriteCollectionGeneric(bag1, new[] { "Eric", "Eric", "foo", "foo" }, false);
        }
コード例 #8
0
        public void CloneContents()
        {
            var bag1 = new Bag <MyInt>();

            MyInt mi = new MyInt(9);

            bag1.Add(new MyInt(14));
            bag1.Add(new MyInt(143));
            bag1.Add(new MyInt(2));
            bag1.Add(mi);
            bag1.Add(null);
            bag1.Add(new MyInt(14));
            bag1.Add(new MyInt(111));
            bag1.Add(mi);
            Bag <MyInt> bag2 = bag1.CloneContents();

            CompareClones(bag1, bag2);

            var bag3 = new Bag <int>(new int[] { 144, 1, 5, 23, 1, 8 });
            var bag4 = bag3.CloneContents();

            CompareClones(bag3, bag4);

            var bag5 = new Bag <UtilFixture.CloneableStruct>
            {
                new UtilFixture.CloneableStruct(143),
                new UtilFixture.CloneableStruct(1),
                new UtilFixture.CloneableStruct(23),
                new UtilFixture.CloneableStruct(1),
                new UtilFixture.CloneableStruct(8)
            };
            var bag6 = bag5.CloneContents();

            Assert.AreEqual(bag5.Count, bag6.Count);

            // Check that the bags are equal, but not identical (e.g., have been cloned via ICloneable).
            foreach (var item in bag5)
            {
                var found = 0;
                foreach (var other in bag6)
                {
                    if (Equals(item, other))
                    {
                        found += 1;
                        // Assert.IsFalse(item.Identical(other));
                    }
                }
                Assert.AreEqual(bag5.NumberOfCopies(item), found);
            }
        }
コード例 #9
0
        public void GenericICollectionInterface()
        {
            string[] s_array = { "Foo", "hello", "Eric", null, "Clapton", "hello", "goodbye", "C#", null };
            var      bag1    = new Bag <string>();

            foreach (var s in s_array)
            {
                bag1.Add(s);
            }

            Array.Sort(s_array);
            InterfaceTests.TestReadWriteCollectionGeneric(bag1, s_array, false);
        }
コード例 #10
0
        public void Remove()
        {
            var bag1 = new Bag <string>(StringComparer.InvariantCultureIgnoreCase);

            var b = bag1.Remove("Eric");

            Assert.IsFalse(b);
            bag1.Add("hello");
            bag1.Add("foo");
            bag1.Add(null);
            bag1.Add(null);
            bag1.Add("HELLO");
            bag1.Add("Hello");
            b = bag1.Remove("hello");
            Assert.IsTrue(b);
            InterfaceTests.TestCollection(bag1, new string[] { null, null, "foo", "hello", "hello" }, false);
            b = bag1.Remove("Hello");
            Assert.IsTrue(b);
            b = bag1.Remove(null);
            Assert.IsTrue(b);
            b = bag1.Remove(null);
            Assert.IsTrue(b);
            b = bag1.Remove(null);
            Assert.IsFalse(b);
            bag1.Add("Hello");
            bag1.Add("Eric");
            bag1.Add(null);
            b = bag1.Remove(null);
            Assert.IsTrue(b);
            bag1.Add("ERIC");
            b = bag1.Remove("eRic");
            Assert.IsTrue(b);
            b = bag1.Remove("eRic");
            Assert.IsTrue(b);
            bag1.Clear();
            b = bag1.Remove("");
            Assert.IsFalse(b);
        }
コード例 #11
0
        public void RandomAddDelete()
        {
            const int SIZE  = 5000;
            var       count = new int[SIZE];
            var       rand  = new Random(3);
            var       bag1  = new Bag <int>();
            bool      b;

            // Add and delete values at random.
            for (int i = 0; i < SIZE * 100; ++i)
            {
                int v = rand.Next(SIZE);

                Assert.AreEqual(count[v], bag1.NumberOfCopies(v));
                if (count[v] > 0)
                {
                    Assert.IsTrue(bag1.Contains(v));
                }

                if (count[v] == 0 || rand.Next(2) == 1)
                {
                    // Add to the bag.
                    bag1.Add(v);
                    count[v] += 1;
                }
                else
                {
                    // Remove from the bag.
                    b = bag1.Remove(v);
                    Assert.IsTrue(b);
                    count[v] -= 1;
                }
            }

            // Make sure the bag has all the correct values, not necessarily in order.
            int c = count.Sum();

            Assert.AreEqual(c, bag1.Count);

            foreach (var v in bag1)
            {
                --count[v];
            }

            foreach (var x in count)
            {
                Assert.AreEqual(0, x);
            }
        }
コード例 #12
0
        public void RemoveMany()
        {
            var bag1 = new Bag <string>(StringComparer.InvariantCultureIgnoreCase)
            {
                "foo", "Eric", "Clapton", null, "Foo", "fudd", "elmer"
            };

            string[] s_array = { "FOO", "jasmine", "eric", null };
            int      count   = bag1.RemoveMany(s_array);

            Assert.AreEqual(3, count);

            InterfaceTests.TestReadWriteCollectionGeneric(bag1, new[] { "Clapton", "elmer", "foo", "fudd" }, false);

            bag1.Clear();
            bag1.Add("foo");
            bag1.Add("Eric");
            bag1.Add("Clapton");
            bag1.Add(null);
            bag1.Add("Foo");
            count = bag1.RemoveMany(bag1);
            Assert.AreEqual(5, count);
            Assert.AreEqual(0, bag1.Count);
        }
コード例 #13
0
        public void CountAndClear()
        {
            var bag1 = new Bag <string>(StringComparer.InvariantCultureIgnoreCase);

            Assert.AreEqual(0, bag1.Count);
            bag1.Add("hello");
            Assert.AreEqual(1, bag1.Count);
            bag1.Add("foo");
            Assert.AreEqual(2, bag1.Count);
            bag1.Add("");
            Assert.AreEqual(3, bag1.Count);
            bag1.Add("HELLO");
            Assert.AreEqual(4, bag1.Count);
            bag1.Add("foo");
            Assert.AreEqual(5, bag1.Count);
            bag1.Remove("");
            Assert.AreEqual(4, bag1.Count);
            bag1.Add(null);
            Assert.AreEqual(5, bag1.Count);
            bag1.Add("Hello");
            Assert.AreEqual(6, bag1.Count);
            bag1.Add("Eric");
            Assert.AreEqual(7, bag1.Count);
            bag1.RemoveAllCopies("hElLo");
            Assert.AreEqual(4, bag1.Count);
            bag1.Add(null);
            Assert.AreEqual(5, bag1.Count);
            bag1.Clear();
            Assert.AreEqual(0, bag1.Count);

            var found = false;

            foreach (var s in bag1)
            {
                found = true;
            }

            Assert.IsFalse(found);
        }
コード例 #14
0
        public void Clone()
        {
            var bag1 = new Bag <int>(new int[] { 1, 7, 9, 11, 7, 13, 15, -17, 19, -21, 1 });

            var bag2 = bag1.Clone();
            var bag3 = bag1.Clone(); //(Bag<int>) ((ICloneable) bag1).Clone();

            Assert.IsFalse(bag2 == bag1);
            Assert.IsFalse(bag3 == bag1);

            // Modify bag1, make sure bag2, bag3 don't change.
            bag1.Remove(9);
            bag1.Remove(-17);
            bag1.Add(8);

            InterfaceTests.TestReadWriteCollectionGeneric(bag2, new int[] { -21, -17, 1, 1, 7, 7, 9, 11, 13, 15, 19 }, false);
            InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { -21, -17, 1, 1, 7, 7, 9, 11, 13, 15, 19 }, false);

            bag1 = new Bag <int>();
            bag2 = bag1.Clone();
            Assert.IsFalse(bag2 == bag1);
            Assert.IsTrue(bag1.Count == 0 && bag2.Count == 0);
        }