Ejemplo n.º 1
0
        public void ContainsTest()
        {
            // Checking thet when we add one nnumber the list contains and only contains that number.
            intList.Add(1);
            Assert.AreEqual(false, intList.Contains(item: -1));
            Assert.AreEqual(false, intList.Contains(item: 0));
            Assert.AreEqual(true, intList.Contains(item: 1));
            Assert.AreEqual(false, intList.Contains(item: 2));

            // Same for strings.
            stringList.Add("a");
            Assert.AreEqual(false, stringList.Contains(item: "A"));
            Assert.AreEqual(false, stringList.Contains(item: "b"));
            Assert.AreEqual(true, stringList.Contains(item: "a"));
            Assert.AreEqual(false, stringList.Contains(item: "c"));

            // Some performance test.
            for (int i = 2; i < 10000; i++)
            {
                intList.Add(i);
                Assert.AreEqual(true, intList.Contains(item: i));
            }

            // Checking that the stringList does not contains any null value.
            Assert.AreEqual(false, stringList.Contains(item: null));

            // Adding some strings to the stringList.
            stringList.Add("b");
            stringList.Add("c");
            stringList.Add("d");

            // Setting one of the values to null to test the contains with null.
            stringList[2] = null;
            Assert.AreEqual(true, stringList.Contains(item: null));

            // Quick check of the indexOf.
            Assert.AreEqual(2, stringList.IndexOf(item: null));

            // Checking with non-primitive objects.
            var peopleList = new NSList <RWPerson>();

            // Creating some people.
            var john       = new RWPerson("John", 29);
            var johnClone  = new RWPerson("John", 29);       // A clon of john.
            var johnFather = new RWPerson("John", 64);       // John's father.
            var mary       = new RWPerson("Mary", 36);       // Ramdom person.

            // Adding john to the list.
            peopleList.Add(john);

            // With the following assertions we're checking that the contains uses the default Equals or (if) the overrided one.
            Assert.AreEqual(true, peopleList.Contains(john));
            Assert.AreEqual(true, peopleList.Contains(johnClone));
            Assert.AreEqual(true, peopleList.Contains(new RWPerson("John", 29)));
            Assert.AreEqual(true, peopleList.Contains(new RWPerson("jOhN", 29)));
            Assert.AreEqual(false, peopleList.Contains(johnFather));
            Assert.AreEqual(false, peopleList.Contains(mary));
        }
Ejemplo n.º 2
0
        public void RemoveTest()
        {
            // Checking that we begin the test with empty lists.
            Assert.AreEqual(0, intList.Count);
            Assert.AreEqual(0, stringList.Count);

            // Trying to remove an element that does not exists in the list.
            Assert.AreEqual(false, intList.Remove(1));

            // Adding a repeated element to the list {1, 1}
            intList.Add(1);
            intList.Add(1);

            // Removing the first appearence of 1. And Checking that the second one passes to the index of the first one.
            Assert.AreEqual(true, intList.Remove(1));
            Assert.AreEqual(1, intList[0]);
            Assert.AreEqual(1, intList.Count);

            // Checking with non-primitive objects.
            var peopleList = new NSList <RWPerson>();

            // Creating some people.
            var john       = new RWPerson("John", 29);
            var johnClone  = new RWPerson("John", 29);       // A clon of john.
            var johnFather = new RWPerson("John", 64);       // John's father.
            var mary       = new RWPerson("Mary", 36);       // Ramdom person.

            // Adding some people to the list.
            peopleList.Add(john);
            peopleList.Add(johnClone);
            peopleList.Add(johnFather);
            peopleList.Add(mary);

            Assert.AreEqual(true, peopleList.Remove(johnClone));

            // Notice that because of the equals of person and that the list contains a clone we can still check
            // that the list contains john and jhonClone.
            Assert.AreEqual(true, peopleList.Contains(johnClone));
            Assert.AreEqual(true, peopleList.Contains(john));

            // We have still one john in the list
            Assert.AreEqual(true, peopleList.Remove(johnClone));

            // No more jhons should be in the list.
            Assert.AreEqual(false, peopleList.Remove(johnClone));
            Assert.AreEqual(false, peopleList.Remove(john));

            // Checking that we can also remove by using the equals of the object.
            Assert.AreEqual(true, peopleList.Remove(new RWPerson("Mary", 36)));

            // Clearing the list for some performance tests.
            intList.Clear();
            for (int i = 0; i < 1000; i++)
            {
                intList.Add(i);
            }
            Assert.AreEqual(1000, intList.Count);
            for (int i = 999; i > -1; i--)
            {
                intList.Remove(i);
            }
            Assert.AreEqual(0, intList.Count);

            intList.Add(321);
            Assert.AreEqual(1, intList.Count);
            Assert.AreEqual(true, intList.Contains(item: 321));
            intList.Remove(321);
            Assert.AreEqual(false, intList.Contains(item: 321));
        }