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(); }
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(); } } } }
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()); }