public static void Run() { Setup(); foreach (var testcase in _testCases) { Write($"Running testcase #{testcase.Id}... ", DarkYellow); bool success = true; bool firstFail = true; IntervalMerger merger = new IntervalMerger(testcase.InitialStateAsIntervals); merger.Add(testcase.IntervalToAddAsInterval); var expected = testcase.ExpectedResultAsIntervals.ToArray(); var actual = merger.Intervals.ToArray(); if (actual.Count() != expected.Count()) { if (firstFail) { firstFail = false; WriteLine($"Testcase #{testcase.Id} failed! ", Red); } WriteLine($"* Number of intervals mismatched, expected: {expected.Count()}, actual: {actual.Count()}"); success = false; } for (int i = 0; i < testcase.ExpectedResultAsIntervals.Length; i++) { if (expected[i].Min != actual[i].Min || expected[i].Max != actual[i].Max) { if (firstFail) { firstFail = false; WriteLine($"Testcase #{testcase.Id} failed! ", Red); } WriteLine($"* Interval {i} mismatched, expected: [{expected[i].Min} - {expected[i].Max}], actual: [{actual[i].Min} - {actual[i].Max}]"); success = false; } } if (success) { WriteLine($"Test case #{testcase.Id} succeeded!", Green); } } }
public void Run(string[] args) { IReader reader; if (args.Length > 0) { reader = new FileReader(args[0]); } else { reader = new ConsoleReader(); } // Test code... //IntervalMergerTests.Run(); //return; IntervalMerger intervalMerger = new IntervalMerger(); while (!reader.EndOfStream) { var input = reader.ReadLine(); if (string.IsNullOrEmpty(input)) { break; } //WriteLine($"Parsing {input}..."); var split = input.Split('-'); var min = long.Parse(split[0]); var max = long.Parse(split[1]); var newInterval = new Interval { Min = min, Max = max }; intervalMerger.Add(newInterval); } long totalSum = 0; foreach (var itvl in intervalMerger.Intervals) { long itvlLen = itvl.Max - itvl.Min + 1; totalSum += itvlLen; } WriteLine(4294967295L + 1 - totalSum); // +1 for the 0 IP... }
public void Run(string[] args) { IReader reader; if (args.Length > 0) { reader = new FileReader(args[0]); } else { reader = new ConsoleReader(); } // Test code... //IntervalMergerTests.Run(); //return; IntervalMerger intervalMerger = new IntervalMerger(); while (!reader.EndOfStream) { var input = reader.ReadLine(); if (string.IsNullOrEmpty(input)) { break; } //WriteLine($"Parsing {input}..."); var split = input.Split('-'); var min = long.Parse(split[0]); var max = long.Parse(split[1]); var newInterval = new Interval { Min = min, Max = max }; intervalMerger.Add(newInterval); } foreach (var itvl in intervalMerger.Intervals) { Write(itvl.Min); Write("-", Green); Write(itvl.Max); WriteLine(); } }