Exemple #1
0
        public void Constructor_OnDuplicateCourses()
        {
            // Build mockup object with duplicate entry.
            var factory = new CourseMockupFactory();
            var course  = factory.BuildDummy(1);

            var courses = new List <Course>();

            courses.Add(course);
            courses.Add(course);

            var permutator = new CoursePermutator(courses);
        }
Exemple #2
0
        public void GetAllSequences_OnNoCompletionPossible()
        {
            var factory = new CourseMockupFactory();

            // Build mockup list with size = n.
            int n       = 5;
            var courses = factory.BuildDummyList(n);

            // Build circular pre-requisites (invalid pre-requisites).
            factory.SetPrerequisiteAsFirstCourse(ref courses);
            courses[0].Prerequisites.Add(courses[1].Id);

            var permutator = new CoursePermutator(courses);
            var sequences  = permutator.GetAllSequences().ToList();
        }
Exemple #3
0
        public void GetAllSequences_EnsureCorrectOrderLength()
        {
            var factory = new CourseMockupFactory();

            for (int i = 1; i <= 7; i++)
            {
                // Build mockup list with size = i.
                var courses = factory.BuildDummyList(i);
                factory.SetPrerequisiteAsFirstCourse(ref courses);

                var permutator = new CoursePermutator(courses);
                var sequences  = permutator.GetAllSequences();

                foreach (var order in sequences)
                {
                    // Each order needs to have the same length as the total courses length.
                    if (order.Count() != i)
                    {
                        Assert.Fail();
                    }
                }
            }
        }
Exemple #4
0
        public void GetAllSequences_EnsureCorrectOrderSequence()
        {
            var factory = new CourseMockupFactory();

            for (int i = 1; i <= 7; i++)
            {
                // Build mockup list with size = i.
                var courses = factory.BuildDummyList(i);
                factory.SetPrerequisiteAsFirstCourse(ref courses);

                var permutator = new CoursePermutator(courses);
                var sequences  = permutator.GetAllSequences();

                foreach (var course in courses)
                {
                    var prerequisites = course.Prerequisites;

                    foreach (var order in sequences)
                    {
                        bool isCourseFound = false;
                        foreach (var courseId in order.ToList())
                        {
                            if (courseId == course.Id)
                            {
                                isCourseFound = true;
                            }

                            if (isCourseFound && prerequisites.Contains(courseId))
                            {
                                Assert.Fail();
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        ///     Run the test case specified by the TestNumber.
        /// </summary>
        public void RunTest()
        {
            // Initialize new Stopwatch instance to measure the execution time of this test case.
            var sw = new Stopwatch();

            // All output from test case will be appended to following StringBuilder object.
            var outputBuilder = new StringBuilder();

            try
            {
                // Start stopwatch timer.
                sw.Start();

                // Compute results, calling .ToList() to get all results before proceeding.
                var permutator = new CoursePermutator(Courses);
                var sequences  = permutator.GetAllSequences().ToList();

                // End stopwatch timer.
                sw.Stop();

                Console.WriteLine("Successfully run test case. Here's the result.");

                // Output each result.
                foreach (var sequence in sequences)
                {
                    // Store reference to last courseId in this sequence.
                    int lastCourseId = sequence.Last();

                    foreach (var courseId in sequence)
                    {
                        // Don't print comma for last sequence.
                        string format = (courseId == lastCourseId) ? "{0}" : "{0},";

                        string output = string.Format(format, courseId);
                        Console.Write(output);
                    }

                    // Print next line for separation between each sequence.
                    Console.WriteLine();
                }

                // Verify the output.
                var  verifier        = new TestCaseVerifier();
                bool isCorrectOutput = verifier.Verify(sequences, ExpectedOutput);

                // Print the result of the output matched against the ExpectedOutput.
                outputBuilder.AppendLine();
                if (isCorrectOutput)
                {
                    outputBuilder.AppendLine("PASSED - match expected output.");
                }
                else
                {
                    outputBuilder.AppendLine("FAILED - doesn't match expected output.");
                }
            }
            catch (Exception exception)
            {
                // End stopwatch timer (as exception was thrown and was not stopped before).
                sw.Stop();

                // If this block is executed, exception has been thrown.
                // Print the details of the exception.
                string log = string.Format("Exception has been thrown.\n{0}", exception.Message);
                outputBuilder.AppendLine(log);
            }

            // Set the execution time of this test case for output.
            outputBuilder.AppendFormat("\nComputation time: {0}ms.\n", sw.ElapsedMilliseconds);

            // Write the output built so far.
            Console.WriteLine(outputBuilder.ToString());
        }