/// <summary> /// Executes unit tests. /// </summary> /// <returns>True if all goes well.</returns> public static bool Test() { bool result = true; RangeSet one = new RangeSet(); one.Add(7); one.Add(5); one.Add(3); one.Add(9); one.Add(7); one.Add(8); one.Add(2); one.Add(4); result &= CheckRangeSet(one, 2, new int[] { 2, 3, 4, 5, 7, 8, 9 }); one.Remove(2); one.Remove(9); one.Remove(4); result &= CheckRangeSet(one, 3, new int[] { 3, 5, 7, 8 }); one.Clear(); one.AddRange(10, 15); result &= CheckRangeSet(one, 1, new int[] { 10, 11, 12, 13, 14, 15 }); one.Add(-1); one.Add(0); one.Add(-2); result &= CheckRangeSet(one, 2, new int[] { -2, -1, 0, 10, 11, 12, 13, 14, 15 }); Debug.WriteLine("RangeSet: test complete (ok=" + result + ")"); return(result); }
/// <summary> /// Internal test function. /// </summary> private static bool CheckRangeSet(RangeSet set, int expectedRanges, int[] expected) { if (set.DebugRangeCount != expectedRanges) { Debug.WriteLine("Expected " + expectedRanges + " ranges, got " + set.DebugRangeCount); return(false); } // Compare actual vs. expected. If we have more actual than expected we'll // throw on the array access. int expIndex = 0; foreach (int val in set) { if (val != expected[expIndex]) { Debug.WriteLine("Expected " + expected[expIndex] + ", got " + val); return(false); } expIndex++; } // See if we have more expected than actual. if (expIndex != expected.Length) { Debug.WriteLine("Expected " + expected.Length + " elements, found " + expIndex); return(false); } // The count is maintained separately, so check it. if (set.Count != expected.Length) { Debug.WriteLine("Expected Count=" + expected.Length + ", got " + set.Count); return(false); } return(true); }
/// <summary> /// Constructor. /// </summary> /// <param name="set">RangeSet to iterate over.</param> public RangeSetIterator(RangeSet set) { mSet = set; Reset(); }