Example #1
0
        public void TestDistinctWhenThrowingExceptions()
        {
            string[] employeesNames = null;

            var result = LINQFunctions.Distinct(employeesNames, new EqualityComparer <string>());

            Assert.Throws <ArgumentNullException>(() => result.Count());
        }
Example #2
0
        public void TestAllWhenFalse()
        {
            var array = new int[] { 2, 4, 3 };

            Func <int, bool> myFunc = (x) => { return(x % 2 == 0); };

            Assert.False(LINQFunctions.All(array, p => myFunc(p)));
        }
Example #3
0
        public void TestSelectMany()
        {
            var employees = Employee.GetEmployees();

            Func <Employee, List <Department> > myFunc = (x) => x.Departments;
            var selectedEmployees = LINQFunctions.SelectMany(employees, p => myFunc(p));

            Assert.Equal(12, selectedEmployees.Count());
        }
Example #4
0
        public void TestWhere()
        {
            var employees = Employee.GetEmployees();
            Func <Employee, bool> myFunc = (x) => x.FirstName.StartsWith('P');

            var selectedEmployees = LINQFunctions.Where(employees, p => myFunc(p));

            Assert.Equal(2, selectedEmployees.Count());
        }
Example #5
0
        public void TestExceptWhenThrowingExceptions()
        {
            string[] firstArray  = { "Andreea", "Maria", "Ioana" };
            string[] secondArray = null;

            var result = LINQFunctions.Except(firstArray, secondArray, new EqualityComparer <string>());

            Assert.Throws <ArgumentNullException>(() => result.Count());
        }
Example #6
0
        public void TestZip()
        {
            int[]    numbers = { 1, 2, 3, 4 };
            string[] words   = { "one", "two", "three", "nine", "six" };

            var result = LINQFunctions.Zip(numbers, words, (first, second) => first + " " + second);

            Assert.True(result.Contains("3 three"));
        }
Example #7
0
        public void TestUnionWhenThrowingExceptions()
        {
            string[] oldEmployees = null;
            string[] newEmployees = { "Andreea", "George", "Andrei", "Ionut" };

            var unionComparer = new EqualityComparer <string>();

            Assert.Throws <ArgumentNullException>(() => LINQFunctions.Union(oldEmployees, newEmployees, unionComparer));
        }
Example #8
0
        public void TestAnyThrowingExceptions()
        {
            string[] array = null;

            Func <string, bool> myFunc = (x) => { return(x.Length == 5); };


            Assert.Throws <ArgumentNullException>(() => LINQFunctions.Any(array, p => myFunc(p)));
        }
Example #9
0
        public void TestSelectManyWhenThrowingExceptions()
        {
            List <Employee> employees = null;

            Func <Employee, List <Department> > myFunc = (x) => x.Departments;
            var selectedEmployees = LINQFunctions.SelectMany(employees, p => myFunc(p));

            Assert.Throws <ArgumentNullException>(() => selectedEmployees.Count());
        }
Example #10
0
        public void TestZipWhenThrowingExceptions()
        {
            int[]    numbers = null;
            string[] words   = { "one", "two", "three", "nine", "six" };

            var result = LINQFunctions.Zip(numbers, words, (first, second) => first + " " + second);

            Assert.Throws <ArgumentNullException>(() => result.Count());
        }
Example #11
0
        public void TestAggregate()
        {
            int[] array = { 1, 2, 4, 5 };

            Func <int, int, int> myFunc = (x, z) => x * z;

            var result = LINQFunctions.Aggregate(array, 5, (a, b) => myFunc(a, b));

            Assert.Equal(200, result);
        }
Example #12
0
        public void TestWhereWhenThrowingExceptions()
        {
            List <Employee> employees = null
            ;
            Func <Employee, bool> myFunc = (x) => x.FirstName.StartsWith('P');

            var selectedEmployees = LINQFunctions.Where(employees, p => myFunc(p));

            Assert.Throws <ArgumentNullException>(() => selectedEmployees.Count());
        }
Example #13
0
        public void TestDistinct()
        {
            string[] employeesNames           = { "Mara", "Mara", "Ana", "Andreea" };
            var      distinctEqualityComparer = new EqualityComparer <string>();


            var result = LINQFunctions.Distinct(employeesNames, distinctEqualityComparer);

            Assert.Equal(3, result.Count());
        }
Example #14
0
        public void TestFirstWhenExists()
        {
            var array = new int[] { 2, 4, 3 };

            Func <int, bool> myFunc = (x) => { return(x == 4); };

            var result = LINQFunctions.First(array, p => myFunc(p));

            Assert.Equal(4, result);
        }
Example #15
0
        public void TestExcept()
        {
            string[] firstArray  = { "Andreea", "Maria", "Ioana" };
            string[] secondArray = { "Andreea", "Maria", "Andrei", "Ionut" };


            var exceptComparer = new EqualityComparer <string>();
            var result         = LINQFunctions.Except(firstArray, secondArray, exceptComparer);

            Assert.Equal(1, result.Count());
        }
Example #16
0
        public void TestAllThrowningExceptions()
        {
            int[] array = null;

            Func <int, bool> myFunc = (x) => { return(x % 2 == 0); };

            Assert.Throws <ArgumentNullException>(() => LINQFunctions.All(array, p => myFunc(p)));

            array = new int[] { 2, 4, 6 };

            Assert.Throws <ArgumentNullException>(() => LINQFunctions.All(array, null));
        }
Example #17
0
        public void TestIntersectWhenThrowingExceptions()
        {
            List <Employee> oldEmployees = null;

            List <Employee> newEmployees = new List <Employee>
            {
                new Employee {
                    ID          = 104,
                    FirstName   = "George",
                    LastName    = "Petrescu",
                    Salary      = 90000,
                    Departments = { new Department {
                                        Name = "Marketing"
                                    }, new Department{
                                        Name = "Sales"
                                    } }
                },

                new Employee {
                    ID          = 105,
                    FirstName   = "Ionut",
                    LastName    = "Ghita",
                    Salary      = 100000,
                    Departments = { new Department {
                                        Name = "Advertisement"
                                    }, new Department{
                                        Name = "Production"
                                    } }
                },

                new Employee {
                    ID          = 106,
                    FirstName   = "Ioana",
                    LastName    = "Mihaescu",
                    Salary      = 160000,
                    Departments = { new Department {
                                        Name = "Production"
                                    }, new Department{
                                        Name = "Sales"
                                    } }
                }
            };


            var result = LINQFunctions.Intersect(oldEmployees, newEmployees, new IntersectComparer());

            Assert.Throws <ArgumentNullException>(() => result.Count());
        }
Example #18
0
        public void TestSelect()
        {
            var employees = Employee.GetEmployees();

            Func <Employee, bool> myFunc = (x) => x.FirstName.StartsWith('P');
            var selectedEmployees        = LINQFunctions.Select(employees, p => myFunc(p));

            int counter = 0;

            foreach (var current in selectedEmployees)
            {
                if (current)
                {
                    counter++;
                }
            }

            Assert.Equal(2, counter);
        }
Example #19
0
        public void TestUnion()
        {
            string[] oldEmployees = { "Andreea", "Maria", "Ioana" };
            string[] newEmployees = { "Andreea", "George", "Andrei", "Ionut" };

            var unionComparer = new EqualityComparer <string>();
            var result        = LINQFunctions.Union(oldEmployees, newEmployees, unionComparer);

            int counter = 0;

            foreach (var current in result)
            {
                if (current.Equals("Andreea"))
                {
                    counter++;
                }
            }

            Assert.Equal(1, counter);
            Assert.Equal(6, result.Count());
        }
Example #20
0
        public void TestThenByWithOneCriterion()
        {
            var students = new List <Student>()
            {
                new Student(5, "John", "Computer Science"),
                new Student(4, "Steve", "Biology"),
                new Student(1, "Bill", "Math"),
                new Student(4, "Ram", "Math"),
                new Student(3, "Ron", "Math"),
                new Student(4, "Ram", "Biology"),
            };

            Func <Student, int>    idFunc   = (x) => x.ID;
            Func <Student, string> nameFunc = (x) => x.Name;

            var result = LINQFunctions.OrderBy(students, x => idFunc(x), new KeyComparer <int>()).
                         ThenBy(y => nameFunc(y), new KeyComparer <string>()).ToList();

            Assert.Equal(1, result[0].ID);
            Assert.Equal(5, result[5].ID);
            Assert.Equal("Steve", result[4].Name);
        }
Example #21
0
        public void TestGroupBy()
        {
            var students = Student.GetSudents();

            Func <Student, string> elementSelector = x => x.Name;
            Func <Student, string> keySelector     = x => x.Class;
            Func <string, IEnumerable <string>, KeyValuePair <string, IEnumerable <string> > > resultSelector = (Class, NameList) =>
            {
                {
                    return(new KeyValuePair <string, IEnumerable <string> >(Class, NameList));
                }
            };

            var result = LINQFunctions.GroupBy(students,
                                               x => keySelector(x),
                                               y => elementSelector(y),
                                               (ClassName, NamesList) => resultSelector(ClassName, NamesList),
                                               new EqualityComparer <string>()
                                               );


            Assert.Equal(2, result.Count());
        }
Example #22
0
        public void TestIntersect()
        {
            List <Employee> oldEmployees = new List <Employee>
            {
                new Employee {
                    ID          = 104,
                    FirstName   = "Ionut",
                    LastName    = "Popescu",
                    Salary      = 90000,
                    Departments = { new Department {
                                        Name = "Marketing"
                                    }, new Department{
                                        Name = "Sales"
                                    } }
                },

                new Employee {
                    ID          = 105,
                    FirstName   = "Andreea",
                    LastName    = "Popescu",
                    Salary      = 100000,
                    Departments = { new Department {
                                        Name = "Advertisement"
                                    }, new Department{
                                        Name = "Production"
                                    } }
                },

                new Employee {
                    ID          = 106,
                    FirstName   = "Mihai",
                    LastName    = "Andreescu",
                    Salary      = 160000,
                    Departments = { new Department {
                                        Name = "Production"
                                    }, new Department{
                                        Name = "Sales"
                                    } }
                }
            };

            List <Employee> newEmployees = new List <Employee>
            {
                new Employee {
                    ID          = 104,
                    FirstName   = "George",
                    LastName    = "Petrescu",
                    Salary      = 90000,
                    Departments = { new Department {
                                        Name = "Marketing"
                                    }, new Department{
                                        Name = "Sales"
                                    } }
                },

                new Employee {
                    ID          = 105,
                    FirstName   = "Ionut",
                    LastName    = "Ghita",
                    Salary      = 100000,
                    Departments = { new Department {
                                        Name = "Advertisement"
                                    }, new Department{
                                        Name = "Production"
                                    } }
                },

                new Employee {
                    ID          = 106,
                    FirstName   = "Ioana",
                    LastName    = "Mihaescu",
                    Salary      = 160000,
                    Departments = { new Department {
                                        Name = "Production"
                                    }, new Department{
                                        Name = "Sales"
                                    } }
                }
            };

            var intersectComparer = new IntersectComparer();
            var result            = LINQFunctions.Intersect(oldEmployees, newEmployees, intersectComparer);

            Assert.Equal(1, result.Count());
        }