Exemplo n.º 1
0
        private static void MyHashSetFunctionality()
        {
            var set = new MyHashSet <int>()
            {
                1, 2, 5, 4
            };

            set.Add(64);
            set.Add(0);

            foreach (var item in set)
            {
                Console.WriteLine(item);
            }

            set.Remove(64);
            Console.WriteLine("64 was removed");

            foreach (var item in set)
            {
                Console.WriteLine(item);
            }

            set.Clear();
            Console.WriteLine($"Set was cleared and it's size: {set.Count}");
        }
Exemplo n.º 2
0
        public void Clear()
        {
            hashSet = new MyHashSet <Box>(boxes);

            hashSet.Clear();

            hashSet.Should().BeEmpty();
            hashSet.Count.Should().Be(0);
        }
        public void ClearTest()
        {
            MyHashSet <string> myHashSet = new MyHashSet <string>()
            {
                "abs"
            };

            CollectionAssert.AreEquivalent(new string[] { "abs" }, myHashSet);
            myHashSet.Add("sss");
            CollectionAssert.AreEquivalent(new string[] { "abs", "sss" }, myHashSet);
            myHashSet.Clear();
            CollectionAssert.AreEquivalent(new string[] { }, myHashSet);
        }
Exemplo n.º 4
0
        public static void Print()
        {
            var evenNumbers = new MyHashSet <int>();
            var oddNumbers  = new MyHashSet <int>();

            for (var i = 0; i < 5; i++)
            {
                evenNumbers.Add(i * 2);

                oddNumbers.Add((i * 2) + 1);
            }

            Console.WriteLine("evenNumbers contains {0} elements: ", evenNumbers.Count);
            DisplaySet(evenNumbers);

            Console.WriteLine("oddNumbers contains {0} elements: ", oddNumbers.Count);
            DisplaySet(oddNumbers);

            Console.WriteLine($"oddNumbers contains 1? {oddNumbers.Contains(1)}");
            Console.WriteLine($"oddNumbers contains 2? {oddNumbers.Contains(2)}");

            Console.WriteLine($"evenNumbers contains 1? {evenNumbers.Contains(1)}");
            Console.WriteLine($"evenNumbers contains 2? {evenNumbers.Contains(2)}");

            Console.WriteLine($"\nAdding existing values: ");
            Console.WriteLine($"Add to oddNumbers 1: {oddNumbers.Add(1)}");
            Console.WriteLine($"Add to evenNumbers 2: {evenNumbers.Add(2)}");

            Console.WriteLine("\nClearing and reinitializing");
            evenNumbers.Clear();
            oddNumbers.Clear();
            Console.WriteLine("evenNumbers contains {0} elements: ", evenNumbers.Count);
            DisplaySet(evenNumbers);

            Console.WriteLine("oddNumbers contains {0} elements: ", oddNumbers.Count);
            DisplaySet(oddNumbers);

            for (var i = 0; i < 5; i++)
            {
                evenNumbers.Add(i * 2);

                oddNumbers.Add((i * 2) + 1);
            }

            Console.WriteLine("evenNumbers contains {0} elements: ", evenNumbers.Count);
            DisplaySet(evenNumbers);

            Console.WriteLine("oddNumbers contains {0} elements: ", oddNumbers.Count);
            DisplaySet(oddNumbers);
        }
Exemplo n.º 5
0
        public void ClearTest(List <int> source)
        {
            var set = new MyHashSet <int>();

            foreach (var num in source)
            {
                set.Add(num);
            }

            set.Clear();
            var result = set.Count;

            Assert.That(result, Is.EqualTo(0));
        }
Exemplo n.º 6
0
        public void TestClear()
        {
            MyHashSet <int> set        = new MyHashSet <int>();
            int             itemsCount = 100;

            for (int i = 0; i < itemsCount; i++)
            {
                set.Add(i);
            }

            Assert.AreEqual(100, set.Count);
            Assert.IsTrue(set.Contains(5));

            set.Clear();
            Assert.AreEqual(0, set.Count);
            Assert.IsFalse(set.Contains(5));
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            var set = new MyHashSet<int>();

            set.Add(5);
            set.Add(19);
            set.Add(1513513);
            set.Add(4464);
            Console.WriteLine("Count: {0}", set.Count);


            var anotherSet = new MyHashSet<int>();

            anotherSet.Add(-5);
            anotherSet.Add(19);
            anotherSet.Add(1513513);
            anotherSet.Add(-111);

            set.Union(anotherSet);
            Console.WriteLine("Elements after union: {0}", string.Join(", ", set.Elements()));

            // count gives back as a result 5 because 5 and -5 have got equal hash codes
            Console.WriteLine("Count: {0}", set.Count);

            set.Intersect(anotherSet);
            Console.WriteLine("Elements after intersect: {0}", string.Join(", ", set.Elements()));

            Console.WriteLine("Contains value: {0} -> {1}", 4464, set.Contains(4464));
            Console.WriteLine("Contains value: {0} -> {1}", 4, set.Contains(4));
            Console.WriteLine("Count: {0}", set.Count);

            anotherSet.Clear();
            Console.WriteLine("Another set elements: {0}", string.Join(", ", anotherSet.Elements()));
            // if we add one value more than once exception is thrown because it is not valid in hash set
            // set.Add(5);
        }
Exemplo n.º 8
0
 public void TestClear()
 {
     var hashSet = new MyHashSet<int> { 5, 3, 4 };
     hashSet.Clear();
     Assert.AreEqual(0, hashSet.Count);
 }