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 ExampleTest4()
    {
        MyHashSet myHashSet = new MyHashSet();

        myHashSet.Add(1);                   // set = [1]
        myHashSet.Add(2);                   // set = [1, 2]
        var actual = myHashSet.Contains(3); // return False, (never added)

        Assert.False(actual);
    }
Exemplo n.º 3
0
    public void ExampleTest1()
    {
        MyHashSet myHashSet = new MyHashSet();

        myHashSet.Add(1);                   // set = [1]
        myHashSet.Add(2);                   // set = [1, 2]
        var actual = myHashSet.Contains(1); // return True

        Assert.True(actual);
    }
Exemplo n.º 4
0
 public void BaseTest()
 {
     // Act & Assert
     hashSet.Add(1);
     hashSet.Add(2);
     Assert.IsTrue(hashSet.Contains(1));                // returns true
     Assert.IsFalse(hashSet.Contains(3));               // returns false (not found)
     hashSet.Add(2);
     Assert.IsTrue(hashSet.Contains(2));                // returns true
     hashSet.Remove(2);
     Assert.IsFalse(hashSet.Contains(2));               // returns false (already removed)
 }
Exemplo n.º 5
0
    public void ExampleTest3()
    {
        MyHashSet myHashSet = new MyHashSet();

        myHashSet.Add(1);                   // set = [1]
        myHashSet.Add(2);                   // set = [1, 2]
        myHashSet.Add(2);                   // set = [1, 2]
        myHashSet.Remove(2);                // set = [1]
        var actual = myHashSet.Contains(2); // return False, (already removed)

        Assert.False(actual);
    }
        public void BalancingTest()
        {
            MyHashSet <int> myHashSet = new MyHashSet <int>(2);

            Assert.AreEqual(0, myHashSet.Balancing);
            myHashSet.Add(0);
            Assert.AreEqual(1, myHashSet.Balancing);
            myHashSet.Add(1);
            Assert.AreEqual(0, myHashSet.Balancing);
            myHashSet.Add(2);
            Assert.AreEqual(1.0 / 3.0, myHashSet.Balancing, double.Epsilon);
        }
Exemplo n.º 7
0
        public void ContainsTest()
        {
            MyHashSet hashSet = new MyHashSet();

            hashSet.Add(1);
            hashSet.Add(2);
            Assert.IsTrue(hashSet.Contains(1));    // 返回 true
            Assert.IsFalse(hashSet.Contains(3));   // 返回 false (未找到)
            hashSet.Add(2);
            Assert.IsTrue(hashSet.Contains(2));    // 返回 true
            hashSet.Remove(2);
            Assert.IsFalse(hashSet.Contains(2));   // 返回  false (已经被删除)
        }
Exemplo n.º 8
0
        public void AddCountTest(List <int> source, List <int> addNumbers, List <bool> expectedList)
        {
            var set = new MyHashSet <int>();

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

            var result = addNumbers.Select(item => set.Add(item)).ToList();

            Assert.That(result, Is.EqualTo(expectedList));
        }
        public void AddTest()
        {
            MyHashSet <string> myHashSet = new MyHashSet <string>()
            {
                "abs"
            };

            CollectionAssert.AreEquivalent(new string[] { "abs" }, myHashSet);
            Assert.IsTrue(myHashSet.Add("sss"));
            CollectionAssert.AreEquivalent(new string[] { "abs", "sss" }, myHashSet);
            Assert.IsFalse(myHashSet.Add("sss"));
            CollectionAssert.AreEquivalent(new string[] { "abs", "sss" }, myHashSet);
        }
Exemplo n.º 10
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.º 11
0
    // Start is called before the first frame update
    void Start()
    {
        MyHashSet hashSet = new MyHashSet();

        hashSet.Add(1);
        hashSet.Add(2);
        Debug.Log(hashSet.Contains(1));     // 返回 true
        Debug.Log(hashSet.Contains(3));     // 返回 false (未找到)
        hashSet.Add(2);

        Debug.Log(hashSet.Contains(2));    // 返回 true
        hashSet.Remove(2);

        Debug.Log(hashSet.Contains(2));    // 返回  false (已经被删除)
    }
Exemplo n.º 12
0
        public void TestGetEnumerator()
        {
            MyHashSet <int> set        = new MyHashSet <int>();
            int             itemsCount = 5;

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

            List <int> items = new List <int>();

            foreach (var item in set)
            {
                items.Add(item);
            }

            items.Sort();

            string        expected = "01234";
            StringBuilder actual   = new StringBuilder();

            foreach (var item in items)
            {
                actual.Append(item);
            }

            Assert.AreEqual(expected, actual.ToString());
        }
Exemplo n.º 13
0
        public void TestUnionWith_BothArentEmpty()
        {
            MyHashSet <int> firstSet   = new MyHashSet <int>();
            int             itemsCount = 5;

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

            MyHashSet <int> secondSet = new MyHashSet <int>();

            for (int i = 3; i < 7; i++)
            {
                secondSet.Add(i);
            }

            // 0,1,2,3,4 U 3,4,5,6 = 0,1,2,3,4,5,6
            firstSet.UnionWith(secondSet);

            Assert.AreEqual(7, firstSet.Count);
            Assert.IsTrue(firstSet.Contains(0));
            Assert.IsTrue(firstSet.Contains(1));
            Assert.IsTrue(firstSet.Contains(2));
            Assert.IsTrue(firstSet.Contains(3));
            Assert.IsTrue(firstSet.Contains(4));
            Assert.IsTrue(firstSet.Contains(5));
            Assert.IsTrue(firstSet.Contains(6));
        }
Exemplo n.º 14
0
        public void Test1()
        {
            MyHashSet obj = new MyHashSet();

            obj.Add(1);
            obj.Add(2);
            obj.Add(2);
            var a  = obj.Contains(1);
            var a1 = obj.Contains(3);
            var a2 = obj.Contains(2);

            obj.Remove(2);
            var a3 = obj.Contains(2);

            Assert.Pass();
        }
Exemplo n.º 15
0
        public void ShouldAddItem()
        {
            hashSet = new MyHashSet <Box>();

            hashSet.Add(boxes[0]).Should().BeTrue();
            hashSet.Should().BeEquivalentTo(boxes[0]);
            hashSet.Count.Should().Be(1);
        }
Exemplo n.º 16
0
        public void Add_SingleItem_CanAdd(int input, bool expected)
        {
            var sut = new MyHashSet();

            sut.Add(input);
            var actual = sut.Contains(input);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 17
0
        public void ReturnFalse_After_TryToAddItem_ThatItAlreadyContain()
        {
            hashSet = new MyHashSet <Box> {
                boxes[0]
            };

            hashSet.Add(boxes[0]).Should().BeFalse();
            hashSet.Should().BeEquivalentTo(boxes[0]);
            hashSet.Count.Should().Be(1);
        }
Exemplo n.º 18
0
        public void Contains(int input, int isInHash, bool expected)
        {
            var sut = new MyHashSet();

            sut.Add(input);

            var actual = sut.Contains(isInHash);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 19
0
        public void Test()
        {
            var set = new MyHashSet();

            set.Add(1);
            set.Add(31);
            set.Add(81);
            set.Add(1011);

            Assert.AreEqual(true, set.Contains(1));
            Assert.AreEqual(true, set.Contains(31));
            Assert.AreEqual(true, set.Contains(81));
            Assert.AreEqual(true, set.Contains(1011));

            Assert.AreEqual(false, set.Contains(2));

            set.Remove(1);
            Assert.AreEqual(false, set.Contains(1));
        }
Exemplo n.º 20
0
        public void TestAdd_50000Items()
        {
            MyHashSet <int> set        = new MyHashSet <int>();
            int             itemsCount = 50000;

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

            Assert.AreEqual(itemsCount, set.Count);
        }
Exemplo n.º 21
0
        public void NullExceptionTest(string value, Type expectedEx)
        {
            var set = new MyHashSet <string>();

            void Result1() => set.Add(value);
            void Result2() => set.Remove(value);
            void Result3() => set.Contains(value);

            Assert.Throws(expectedEx, Result1);
            Assert.Throws(expectedEx, Result2);
            Assert.Throws(expectedEx, Result3);
        }
Exemplo n.º 22
0
        private static void Main()
        {
            var hashSet = new MyHashSet<int>();
            var otherSet = new MyHashSet<int>();

            for (int i = 0; i < 20; i++)
            {
                hashSet.Add(i);
                otherSet.Add(i + 1);
            }

            Console.WriteLine("Test intersect:");
            otherSet.IntersectWith(hashSet);
            Console.WriteLine(otherSet);

            otherSet.Add(-1);
            otherSet.Add(1000);
            Console.WriteLine("Test union:");
            otherSet.UnionWith(hashSet);
            Console.WriteLine(otherSet);
        }
Exemplo n.º 23
0
        public void AddItem()
        {
            var a = new int[10];

            var set = new MyHashSet <int>();

            set.Add(1);

            Assert.IsTrue(set.Contains(1));
            Assert.IsFalse(set.Contains(2));
            Assert.IsFalse(set.Contains(3));
        }
Exemplo n.º 24
0
        public static Graph GenerateGraph(int vertexes, int edges, int weightFrom, int weightTo)
        {
            var result = GenerateTree(vertexes);

            var alredyGeneratedEdges = new MyHashSet(vertexes);

            foreach (var edge in result.Edges)
            {
                alredyGeneratedEdges.Add(Tuple.Create(edge.From, edge.To));
            }

            while (result.Edges.Count != edges)
            {
                var from = random.Next(vertexes);
                var to   = random.Next(vertexes);

                if (from == to)
                {
                    continue;
                }

                if (alredyGeneratedEdges.Contains(Tuple.Create(from, to)))
                {
                    var newPair = NeedNewPair(alredyGeneratedEdges, Tuple.Create(from, to), vertexes);
                    from = newPair.Item1;
                    to   = newPair.Item2;
                }

                alredyGeneratedEdges.Add(Tuple.Create(from, to));

                result.Edges.Add(new Edge <int>(from, to, 0));
            }

            foreach (var edge in result.Edges)
            {
                edge.Weight = random.Next(weightFrom, weightTo + 1);
            }

            return(result);
        }
Exemplo n.º 25
0
        public void Add_AddCollisions_ReturnsFalseIfDoesNotContain(int[] arrange, int input, bool expected)
        {
            var sut = new MyHashSet();

            foreach (var val in arrange)
            {
                sut.Add(val);
            }

            var actual = sut.Contains(input);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 26
0
        public void AddItem_WithUsingComparer()
        {
            var boxComparer = new BoxSameVolumeComparer();

            hashSet = new MyHashSet <Box>(boxComparer)
            {
                boxes[0], boxes[2]
            };

            hashSet.Add(boxes[1]).Should().BeFalse();
            hashSet.Should().BeEquivalentTo(boxes[0], boxes[2]);
            hashSet.Count.Should().Be(2);
        }
        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);
        }
        public void RemoveTest()
        {
            MyHashSet <string> myHashSet = new MyHashSet <string>()
            {
                "abs",
                "aaa",
                "bbb",
                "asdfsad"
            };

            CollectionAssert.AreEquivalent(new string[] { "abs", "aaa", "bbb", "asdfsad" }, myHashSet);
            Assert.IsFalse(myHashSet.Remove(""));
            CollectionAssert.AreEquivalent(new string[] { "abs", "aaa", "bbb", "asdfsad" }, myHashSet);
            Assert.IsFalse(myHashSet.Remove(null));
            CollectionAssert.AreEquivalent(new string[] { "abs", "aaa", "bbb", "asdfsad" }, myHashSet);
            Assert.IsFalse(myHashSet.Remove("ork2p3okr"));
            CollectionAssert.AreEquivalent(new string[] { "abs", "aaa", "bbb", "asdfsad" }, myHashSet);
            Assert.IsFalse(myHashSet.Remove("\0"));
            CollectionAssert.AreEquivalent(new string[] { "abs", "aaa", "bbb", "asdfsad" }, myHashSet);
            Assert.IsTrue(myHashSet.Remove("abs"));
            CollectionAssert.AreEquivalent(new string[] { "aaa", "bbb", "asdfsad" }, myHashSet);
            Assert.IsTrue(myHashSet.Remove("aaa"));
            CollectionAssert.AreEquivalent(new string[] { "bbb", "asdfsad" }, myHashSet);
            Assert.IsTrue(myHashSet.Remove("asdfsad"));
            CollectionAssert.AreEquivalent(new string[] { "bbb" }, myHashSet);
            Assert.IsTrue(myHashSet.Remove("bbb"));
            CollectionAssert.AreEquivalent(new string[] { }, myHashSet);
            Assert.IsFalse(myHashSet.Remove(""));
            CollectionAssert.AreEquivalent(new string[] { }, myHashSet);
            Assert.IsFalse(myHashSet.Remove(null));
            CollectionAssert.AreEquivalent(new string[] { }, myHashSet);
            Assert.IsFalse(myHashSet.Remove("ork2p3okr"));
            CollectionAssert.AreEquivalent(new string[] { }, myHashSet);
            Assert.IsFalse(myHashSet.Remove("\0"));
            CollectionAssert.AreEquivalent(new string[] { }, myHashSet);
            Assert.IsFalse(myHashSet.Remove("abs"));
            CollectionAssert.AreEquivalent(new string[] { }, myHashSet);
            Assert.IsFalse(myHashSet.Remove("aaa"));
            CollectionAssert.AreEquivalent(new string[] { }, myHashSet);
            Assert.IsFalse(myHashSet.Remove("asdfsad"));
            CollectionAssert.AreEquivalent(new string[] { }, myHashSet);
            Assert.IsFalse(myHashSet.Remove("bbb"));
            CollectionAssert.AreEquivalent(new string[] { }, myHashSet);
            Assert.IsTrue(myHashSet.Add("bbb"));
            CollectionAssert.AreEquivalent(new string[] { "bbb" }, myHashSet);
            Assert.IsTrue(myHashSet.Remove("bbb"));
            CollectionAssert.AreEquivalent(new string[] { }, myHashSet);
            Assert.IsFalse(myHashSet.Remove("bbb"));
            CollectionAssert.AreEquivalent(new string[] { }, myHashSet);
        }
Exemplo n.º 29
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.º 30
0
        public void Remove_SingleItem_CanRemove()
        {
            var sut      = new MyHashSet();
            var input    = 1;
            var expected = false;

            sut.Add(input);

            sut.Remove(input);

            var actual = sut.Contains(input);

            Assert.AreEqual(expected, actual);
        }
        public static void Main()
        {
            var firstSet = new MyHashSet<int>();
            var secondSet = new MyHashSet<int>();

            for (int i = 0; i < 15; i++)
            {
                int valueToAdd = i * 5;
                firstSet.Add(valueToAdd);
                if (i % 3 == 0)
                {
                    secondSet.Add(valueToAdd);
                }
            }

            secondSet.Add(13);
            secondSet.Add(19);

            Console.WriteLine("First set count of elements = {0}", firstSet.Count);

            firstSet.Remove(20);
            firstSet.Remove(5);
            Console.WriteLine("First set count of elements = {0}", firstSet.Count);

            int ten = firstSet.Find(10);
            Console.WriteLine("Is ten in the first set {0}", ten == 10 ? "YES" : "NO");

            Console.WriteLine("First set = {0}", firstSet);
            Console.WriteLine("Second set = {0}", secondSet);

            var unionSet = firstSet.Union(secondSet);
            Console.WriteLine("Union set = {0}", unionSet);

            var intersectSet = firstSet.Intersect(secondSet);
            Console.WriteLine("Intersect set = {0}", intersectSet);
        }
Exemplo n.º 32
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.º 33
0
        public void Add_AddCollisions_ReturnValue(int[] inputs)
        {
            var sut = new MyHashSet();

            foreach (var input in inputs)
            {
                sut.Add(input);
            }

            foreach (var input in inputs)
            {
                var doesContain = sut.Contains(input);
                Assert.IsTrue(doesContain);
            }
        }