Esempio n. 1
0
        /// <summary>
        /// Tos the chunks.
        /// </summary>
        /// <returns>The chunks.</returns>
        /// <param name="array">Array.</param>
        public static NSList <string> ToChunks(char[] array)
        {
            int i          = 0;
            int _ChunkSize = 2;
            var tmp        = new NSList <string>();


            while (!(i == array.Length - 2 && _ChunkSize == 3))
            {
                //Console.WriteLine("Index: {0}, Length: {1}", i, _ChunkSize);
                var _Chunk = array.SubArray(i, _ChunkSize);

                tmp.Add(new string(_Chunk));

                if (_ChunkSize == 2)
                {
                    _ChunkSize = 3;
                }
                else
                {
                    _ChunkSize = 2;
                    i++;
                }
            }

            return(tmp);
        }
Esempio n. 2
0
        public void EnumeratorTest()
        {
            // Testing with some easy cases like integers.
            intList = new NSList <int> {
                1, 2, 3, 4, 5
            };
            int expected = 1;

            foreach (int number in intList)
            {
                Assert.AreEqual(expected, number);
                expected++;
            }

            // Testing that the foreach can reach the objects inside the collection.
            stringList = new NSList <string>()
            {
                "a", "b", "c", "d", "e"
            };
            var resList = new NSList <string>()
            {
                "A", "B", "C", "D", "E"
            };
            int i = 0;

            foreach (string str in stringList)
            {
                Assert.AreEqual(resList[i], str.ToUpper());
                i++;
            }
        }
Esempio n. 3
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));
        }
Esempio n. 4
0
        /// <summary>
        /// Map the specified en and f.
        /// </summary>
        /// <returns>The map.</returns>
        /// <param name="en">En.</param>
        /// <param name="f">F.</param>
        /// <typeparam name="T1">The 1st type parameter.</typeparam>
        /// <typeparam name="T2">The 2nd type parameter.</typeparam>
        public static IEnumerable <T2> Map <T1, T2>(this IEnumerable <T1> en, Func <T1, T2> f)
        {
            var temp = new NSList <T2>();

            foreach (var e in en)
            {
                temp.Add(f(e));
            }
            return(temp);
        }
Esempio n. 5
0
        /// <summary>
        /// Filter the specified f and e.
        /// </summary>
        /// <returns>The filter.</returns>
        /// <param name="f">F.</param>
        /// <param name="e">E.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public static IEnumerable <T> Filter <T>(this IEnumerable <T> e, Predicate <T> f)
        {
            var temp = new NSList <T>();

            foreach (var el in e)
            {
                if (f(el))
                {
                    temp.Add(el);
                }
            }
            return(temp);
        }
Esempio n. 6
0
        /// <summary>
        /// Inverse the specified en.
        /// </summary>
        /// <returns>The inverse.</returns>
        /// <param name="en">En.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public static IEnumerable <T> Inverse <T>(this IEnumerable <T> en)
        {
            var inversed = new NSList <T>();
            var stack    = new Stack <T>();

            Console.WriteLine("-- Slow version of the inverse executing --");

            foreach (var el in en)
            {
                stack.Push(el);
            }

            foreach (var el in stack)
            {
                inversed.Add(el);
            }

            return(inversed);
        }
Esempio n. 7
0
        public void InverseTest()
        {
            list = new NSList <int> {
                1, 2, 3, 4
            };

            var expected = 4;

            foreach (var el in list.Inverse())
            {
                Assert.AreEqual(expected, el);
                expected--;
            }
            var result = new int[] { 0, 1, 2, 3, 4 };

            expected = 4;
            foreach (var el in result.Inverse())
            {
                Console.WriteLine(el);
                Assert.AreEqual(expected, el);
                expected--;
            }
        }
Esempio n. 8
0
        public void ContainsPerformanceTest()
        {
            list = new NSList <int>();
            for (int i = 0; i < 100000; i++)
            {
                list.Add(i);
            }

            var StartTime = DateTime.Now;

            for (int i = (100000 - 1); i >= 0; i--)
            {
                Assert.AreEqual(true, list.Contains(i));
            }

            list[0] = -222;
            Assert.AreEqual(true, list.Contains(-222));
            list.Remove(-222);
            Assert.AreEqual(false, list.Contains(-222));
            list[1] = 10;
            Assert.AreEqual(1, list.IndexOf(10));
            Console.WriteLine("Time to check that contains all 100000 elements: {0}", (DateTime.Now - StartTime).TotalSeconds);
        }
Esempio n. 9
0
        public void AddTest()
        {
            // Checking that we begin the test with empty lists.
            Assert.AreEqual(0, intList.Count);
            Assert.AreEqual(0, stringList.Count);

            // Testing the base case of adding one single element.
            intList.Add(1);
            Assert.AreEqual(1, intList.Count);
            Assert.AreEqual(true, intList.Contains(1));
            Assert.AreEqual(0, intList.IndexOf(1));

            // Testing the base case of adding one single element that is a variable.
            var t = 2;

            intList.Add(t);
            Assert.AreEqual(2, intList.Count);
            Assert.AreEqual(true, intList.Contains(2));
            Assert.AreEqual(1, intList.IndexOf(2));
            Assert.AreEqual(true, intList.Contains(t));
            Assert.AreEqual(1, intList.IndexOf(t));

            // Testing the base case of adding a repeated element as fas a 1 already belongs to the intList.
            intList.Add(1);
            Assert.AreEqual(3, intList.Count);
            Assert.AreEqual(true, intList.Contains(1));
            Assert.AreEqual(0, intList.IndexOf(1));


            // Same for strings but checking the nullable condition.
            // Testing the base case of adding one single element.
            stringList.Add("a");
            Assert.AreEqual(1, stringList.Count);
            Assert.AreEqual(true, stringList.Contains("a"));
            Assert.AreEqual(0, stringList.IndexOf("a"));

            // Testing the base case of adding one single element that is a variable.
            var s = "b";

            stringList.Add(s);
            Assert.AreEqual(2, stringList.Count);
            Assert.AreEqual(true, stringList.Contains(s));
            Assert.AreEqual(1, stringList.IndexOf(s));

            // Testing the base case for nullable objects of adding a null value to the list.
            stringList.Add(null);
            Assert.AreEqual(3, stringList.Count);
            Assert.AreEqual(true, stringList.Contains(null));
            Assert.AreEqual(2, stringList.IndexOf(null));

            // Testing the base case of adding a repeated element as fas a 's' already belongs to the stringList.
            stringList.Add(s);
            Assert.AreEqual(4, stringList.Count);
            Assert.AreEqual(true, stringList.Contains(s));
            Assert.AreEqual(1, stringList.IndexOf(s));


            // Testing for open lists that accept any kind of object.
            var objectList = new NSList <object>();

            // Adding an int to a object type list.
            objectList.Add(1);
            Assert.AreEqual(1, objectList.Count);
            Assert.AreEqual(true, objectList.Contains(1));
            Assert.AreEqual(0, objectList.IndexOf(1));

            // Ading an string to an object type list that already contains an int.
            objectList.Add("a");
            Assert.AreEqual(2, objectList.Count);
            Assert.AreEqual(true, objectList.Contains("a"));
            Assert.AreEqual(1, objectList.IndexOf("a"));

            // Adding a null value to an object list type.
            objectList.Add(null);
            Assert.AreEqual(3, objectList.Count);
            Assert.AreEqual(true, objectList.Contains(null));
            Assert.AreEqual(2, objectList.IndexOf(null));

            // Adding a repeated element to an object list type. Notice that the repeated element it's also null.
            objectList.Add(null);
            Assert.AreEqual(4, objectList.Count);
            Assert.AreEqual(true, objectList.Contains(null));
            Assert.AreEqual(2, objectList.IndexOf(null));

            // Clearing our list to some performance test.
            intList.Clear();
            stringList.Clear();

            // Adding some values to our lists to check that can add multiple values with no problem.
            for (int i = 0; i < 100000; i++)
            {
                intList.Add(i);
                stringList.Add(RAWordGenerator.GenerateRandomWord());
                Assert.AreEqual(i + 1, intList.Count);
                Assert.AreEqual(i + 1, stringList.Count);
            }
        }
Esempio n. 10
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));
        }
Esempio n. 11
0
 public void BeforeEachTest()
 {
     list      = new NSList <int>();
     employees = RWEmployeeFactory.CreateEmployees(100);
 }
Esempio n. 12
0
 public void BeforeTest()
 {
     list = new NSList <int>();
 }