public void TestMultipleElementList() { var values = new[] { Generator.GetInt32(), Generator.GetInt32(), Generator.GetInt32() }; // Construction using ImmutableSortedTreeList.Create var list = ImmutableSortedTreeList.Create(values); Assert.Equal(values.OrderBy(x => x), list); list = ImmutableSortedTreeList.Create <int>(comparer: null, values); Assert.Same(Comparer <int> .Default, list.Comparer); Assert.Equal(values.OrderBy(x => x), list); list = ImmutableSortedTreeList.Create(ReverseComparer <int> .Default, values); Assert.Same(ReverseComparer <int> .Default, list.Comparer); Assert.Equal(values.OrderByDescending(x => x), list); // Construction using ImmutableSortedTreeList.ToImmutableSortedTreeList list = values.ToImmutableSortedTreeList(); Assert.Same(Comparer <int> .Default, list.Comparer); Assert.Equal(values.OrderBy(x => x), list); list = values.ToImmutableSortedTreeList(comparer: null); Assert.Same(Comparer <int> .Default, list.Comparer); Assert.Equal(values.OrderBy(x => x), list); list = values.ToImmutableSortedTreeList(ReverseComparer <int> .Default); Assert.Same(ReverseComparer <int> .Default, list.Comparer); Assert.Equal(values.OrderByDescending(x => x), list); }
public void TestIndexOf() { ImmutableSortedTreeList <int> .Builder list = ImmutableSortedTreeList.CreateBuilder <int>(); var reference = new List <int>(); for (int i = 0; i < 4 * 8 * 8; i++) { int item = Generator.GetInt32(list.Count + 1); list.Add(item); reference.Add(item); } reference.Sort(); Assert.Throws <ArgumentOutOfRangeException>(() => list.IndexOf(list[0], -1)); Assert.Throws <ArgumentOutOfRangeException>(() => list.IndexOf(list[0], 0, -1)); Assert.Throws <ArgumentException>(() => list.IndexOf(list[0], 0, list.Count + 1)); Assert.Equal(-1, list.IndexOf(-1)); for (int i = 0; i < list.Count; i++) { Assert.Equal(reference.IndexOf(i), list.IndexOf(i)); int firstIndex = list.IndexOf(i); Assert.Equal(reference.IndexOf(i, firstIndex + 1), list.IndexOf(i, firstIndex + 1)); } ImmutableSortedTreeList <int> .Builder empty = ImmutableSortedTreeList.CreateBuilder <int>(); Assert.Equal(-1, empty.IndexOf(0)); }
public void TestEnumerator() { ImmutableSortedTreeList <int> .Builder list = ImmutableSortedTreeList.CreateBuilder <int>(); ImmutableSortedTreeList <int> .Enumerator enumerator = list.GetEnumerator(); Assert.Equal(0, enumerator.Current); Assert.False(enumerator.MoveNext()); Assert.Equal(0, enumerator.Current); Assert.False(enumerator.MoveNext()); Assert.Equal(0, enumerator.Current); // Adding an item to the list invalidates it, but Current is still unchecked CollectionAssert.EnumeratorInvalidated(list, () => list.Add(1)); Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext()); Assert.Equal(0, enumerator.Current); enumerator = list.GetEnumerator(); Assert.Equal(0, enumerator.Current); Assert.True(enumerator.MoveNext()); Assert.Equal(1, enumerator.Current); // Reset has no effect due to boxing the value type ((IEnumerator <int>)enumerator).Reset(); Assert.Equal(1, enumerator.Current); Assert.False(enumerator.MoveNext()); Assert.Equal(1, enumerator.Current); }
public void TestRemoveAt() { ImmutableSortedTreeList <int> .Builder list = ImmutableSortedTreeList.CreateBuilder <int>(); var reference = new List <int>(); for (int i = 0; i < 4 * 8 * 8; i++) { int item = Generator.GetInt32(); list.Add(item); reference.Add(item); } reference.Sort(); while (list.Count > 0) { int index = Generator.GetInt32(list.Count); Assert.Equal(reference[index], list[index]); reference.RemoveAt(index); list.RemoveAt(index); list.Validate(ValidationRules.None); Assert.Equal(reference, list); } Assert.Empty(list); Assert.Empty(reference); }
public void NegTest3() { char[] iArray = { '#', ' ', '&', 'c', '1', '_', 'A' }; var listObject = ImmutableSortedTreeList.Create(iArray); Assert.Throws <ArgumentException>(() => listObject.GetRange(4, 4)); }
public void PosTest5() { int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; var listObject = ImmutableSortedTreeList.Create(iArray); Assert.Same(listObject, listObject.GetRange(0, listObject.Count)); }
public void TestImmutableSortedTreeListCreateRangeValidation() { Assert.Throws <ArgumentNullException>("items", () => ImmutableSortedTreeList.CreateRange <int>(null !)); Assert.Throws <ArgumentNullException>("items", () => ImmutableSortedTreeList.CreateRange(Comparer <int> .Default, null !)); Assert.Throws <ArgumentNullException>("items", () => default(IEnumerable <int>) !.ToImmutableSortedTreeList()); Assert.Throws <ArgumentNullException>("items", () => default(IEnumerable <int>) !.ToImmutableSortedTreeList(Comparer <int> .Default)); }
public void TestImmutableSortedTreeListCreateRange() { var values = new[] { Generator.GetInt32(), Generator.GetInt32(), Generator.GetInt32() }; var list = ImmutableSortedTreeList.CreateRange(values); Assert.Equal(values.OrderBy(x => x), list); }
public void TestBinarySearchFullList() { ImmutableSortedTreeList <int> .Builder list = ImmutableSortedTreeList.CreateBuilder <int>(); var reference = new List <int>(); for (int i = 0; i < 4 * 8 * 8; i++) { list.Add(i * 2); reference.Add(i * 2); } // Test below start value Assert.Equal(reference.BinarySearch(reference[0] - 1), list.BinarySearch(reference[0] - 1)); for (int i = 0; i < reference.Count; i++) { // Test current value Assert.Equal(reference.BinarySearch(reference[i]), list.BinarySearch(reference[i])); // Test above current value Assert.Equal(reference.BinarySearch(reference[i] + 1), list.BinarySearch(reference[i] + 1)); } ImmutableSortedTreeList <int> .Builder empty = ImmutableSortedTreeList.CreateBuilder <int>(); Assert.Equal(~0, empty.BinarySearch(0)); }
public void NegTest2() { int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; var listObject = ImmutableSortedTreeList.Create(iArray); Assert.Throws <ArgumentOutOfRangeException>(() => listObject.GetRange(6, -4)); }
public void NegTest1() { int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; var listObject = ImmutableSortedTreeList.Create(iArray); Action <int>?action = null; Assert.Throws <ArgumentNullException>(() => listObject.ForEach(action !)); }
public void TestEmptyImmutableSortedTreeList() { var list = ImmutableSortedTreeList.Create <int>(); Assert.Same(ImmutableSortedTreeList <int> .Empty, list); Assert.Empty(list); Assert.True(list.IsEmpty); }
public void TestLastIndexOfInvalidOperations() { var single = ImmutableSortedTreeList.CreateRange(Enumerable.Range(1, 1)).ToBuilder(); Assert.Throws <ArgumentException>(() => single.LastIndexOf(0, 1, 0)); Assert.Throws <ArgumentOutOfRangeException>(() => single.LastIndexOf(0, 0, -1)); Assert.Throws <ArgumentOutOfRangeException>(() => single.LastIndexOf(0, 0, 2)); }
public void PosTest4() { int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; var listObject = ImmutableSortedTreeList.Create(iArray); ImmutableSortedTreeList <int> listResult = listObject.GetRange(5, 0); Assert.NotNull(listResult); Assert.Empty(listResult); }
public void TestRemoveValue() { var list = ImmutableSortedTreeList.CreateRange(comparer: null, Enumerable.Range(0, 10)).ToBuilder(); Assert.False(list.Remove(-1)); Assert.Equal(10, list.Count); Assert.True(list.Remove(3)); Assert.Equal(9, list.Count); }
public void TestIListTInterface() { IList <int> list = ImmutableSortedTreeList.CreateRange <int>(Enumerable.Range(0, 10)).ToBuilder(); Assert.False(list.IsReadOnly); Assert.Equal(0, list[0]); Assert.Throws <NotSupportedException>(() => list.Insert(0, 0)); }
public void TestTrimExcess() { ImmutableSortedTreeList <int> .Builder list = ImmutableSortedTreeList.CreateBuilder <int>(); List <int> reference = new List <int>(); for (int i = 0; i < 4 * 8 * 8; i++) { int value = Generator.GetInt32(list.Count + 1); list.Add(i); reference.Add(i); } reference.Sort(); list.Validate(ValidationRules.None); // In the first call to TrimExcess, items will move list.TrimExcess(); list.Validate(ValidationRules.RequirePacked); Assert.Equal(reference, list); // In the second call, the list is already packed so nothing will move list.TrimExcess(); list.Validate(ValidationRules.RequirePacked); Assert.Equal(reference, list); ImmutableSortedTreeList <int> .Builder empty = ImmutableSortedTreeList.CreateBuilder <int>(); empty.Validate(ValidationRules.RequirePacked); empty.TrimExcess(); empty.Validate(ValidationRules.RequirePacked); var single = ImmutableSortedTreeList.CreateRange(Enumerable.Range(0, 1)).ToBuilder(); single.Validate(ValidationRules.RequirePacked); single.TrimExcess(); single.Validate(ValidationRules.RequirePacked); // Construct a poorly-packed list with several levels ImmutableSortedTreeList <int> .Builder binary = ImmutableSortedTreeList.CreateBuilder <int>(); for (int i = 5000; i >= 0; i--) { binary.Add(i); } binary.TrimExcess(); binary.Validate(ValidationRules.RequirePacked); // Construct a poorly-packed list with several levels ImmutableSortedTreeList <int> .Builder ternary = ImmutableSortedTreeList.CreateRange <int>(comparer: null, Enumerable.Range(0, 5000)).ToBuilder(); for (int i = 5000; i >= 0; i--) { ternary.Add(i); } ternary.TrimExcess(); ternary.Validate(ValidationRules.RequirePacked); }
public void TestTrueForAll() { ImmutableSortedTreeList <int> .Builder list = ImmutableSortedTreeList.CreateBuilder <int>(); Assert.True(list.TrueForAll(i => false)); Assert.Throws <ArgumentNullException>(() => list.TrueForAll(null !)); list.Add(1); Assert.True(list.TrueForAll(i => i > 0)); Assert.False(list.TrueForAll(i => i <= 0)); }
public void PosTest1() { int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; var listObject = ImmutableSortedTreeList.Create(iArray); var myClass = new MyClass(); var action = new Action <int>(myClass.SumCalc); listObject.ForEach(action); Assert.Equal(40, myClass.Sum); }
public void PosTest2() { string[] strArray = { "Hello", "wor", "l", "d" }; var listObject = ImmutableSortedTreeList.Create(new ComparisonComparer <string>((x, y) => 0), strArray); var myClass = new MyClass(); var action = new Action <string>(myClass.JoinStr); listObject.ForEach(action); Assert.Equal("Helloworld", myClass.Result); }
public void TestCopyToValidation() { var list = ImmutableSortedTreeList.CreateRange <int>(Enumerable.Range(0, 10)).ToBuilder(); Assert.Throws <ArgumentNullException>("array", () => list.CopyTo(0, null !, 0, list.Count)); Assert.Throws <ArgumentOutOfRangeException>("index", () => list.CopyTo(-1, new int[list.Count], 0, list.Count)); Assert.Throws <ArgumentOutOfRangeException>("arrayIndex", () => list.CopyTo(0, new int[list.Count], -1, list.Count)); Assert.Throws <ArgumentOutOfRangeException>("count", () => list.CopyTo(0, new int[list.Count], 0, -1)); Assert.Throws <ArgumentOutOfRangeException>(null, () => list.CopyTo(1, new int[list.Count], 0, list.Count)); Assert.Throws <ArgumentOutOfRangeException>(string.Empty, () => list.CopyTo(0, new int[list.Count], 1, list.Count)); }
public void TestRemoveAll() { var list = ImmutableSortedTreeList.CreateRange(comparer: null, Enumerable.Range(0, 10)).ToBuilder(); Assert.Throws <ArgumentNullException>(() => list.RemoveAll(null !)); Assert.Equal(5, list.RemoveAll(i => (i % 2) == 0)); Assert.Equal(new[] { 1, 3, 5, 7, 9 }, list); Assert.Equal(0, list.RemoveAll(i => i < 0)); Assert.Equal(5, list.Count); }
public void TestICollectionInterface() { TestICollectionInterfaceImpl(ImmutableSortedTreeList.Create <int>(600, 601).ToBuilder(), supportsNullValues: false); TestICollectionInterfaceImpl(ImmutableSortedTreeList.Create <int?>(600, 601).ToBuilder(), supportsNullValues: true); TestICollectionInterfaceImpl(ImmutableSortedTreeList.Create <object>(600, 601).ToBuilder(), supportsNullValues: true); // Run the same set of tests on ImmutableTreeList<T> to ensure consistent behavior TestICollectionInterfaceImpl(ImmutableTreeList.Create <int>(600, 601).ToBuilder(), supportsNullValues: false); TestICollectionInterfaceImpl(ImmutableTreeList.Create <int?>(600, 601).ToBuilder(), supportsNullValues: true); TestICollectionInterfaceImpl(ImmutableTreeList.Create <object>(600, 601).ToBuilder(), supportsNullValues: true); }
public void TestSortedTreeListBuilderConstructor() { ImmutableSortedTreeList <int> .Builder list = ImmutableSortedTreeList.CreateBuilder <int>(); Assert.Empty(list); Assert.Equal(Comparer <int> .Default, list.Comparer); ImmutableSortedTreeList <string?> .Builder stringList = ImmutableSortedTreeList.CreateBuilder(StringComparer.Ordinal); Assert.Same(StringComparer.Ordinal, stringList.Comparer); stringList = ImmutableSortedTreeList.CreateBuilder(StringComparer.OrdinalIgnoreCase); Assert.Same(StringComparer.OrdinalIgnoreCase, stringList.Comparer); }
public void TestAddStaysPacked() { ImmutableSortedTreeList <int> .Builder list = ImmutableSortedTreeList.CreateBuilder <int>(); for (int i = 0; i < 4 * 8 * 8; i++) { // This test assumes items are being added are already ordered by hash code Assert.Equal(i, i.GetHashCode()); list.Add(i); list.Validate(ValidationRules.RequirePacked); } }
public void TestFind() { var list = ImmutableSortedTreeList.CreateRange(comparer: null, Enumerable.Range(1, 10)).ToBuilder(); Assert.Throws <ArgumentNullException>(() => list.Find(null !)); Assert.Equal(0, list.Find(value => value < 0)); foreach (var i in list) { Assert.Equal(i, list.Find(value => value == i)); } Assert.Equal(2, list.Find(value => value > 1)); }
public void TestExists() { var list = ImmutableSortedTreeList.CreateRange(comparer: null, Enumerable.Range(0, 10)).ToBuilder(); Assert.Throws <ArgumentNullException>(() => list.Exists(null !)); Assert.False(list.Exists(value => value < 0)); foreach (var i in list) { Assert.True(list.Exists(value => value == i)); } Assert.False(list.Exists(value => value > 10)); }
public void PosTest2() { string[] strArray = { "apple", "banana", "chocolate", "dog", "food" }; var listObject = ImmutableSortedTreeList.Create(strArray); int startIdx = Generator.GetInt32(0, 4); // The starting index of the section to make a shallow copy int endIdx = Generator.GetInt32(startIdx, 5); // The end index of the section to make a shallow copy int count = endIdx - startIdx + 1; ImmutableSortedTreeList <string> listResult = listObject.GetRange(startIdx, count); for (int i = 0; i < count; i++) { Assert.Equal(strArray[i + startIdx], listResult[i]); } }
public void TestFindAll() { var list = ImmutableSortedTreeList.CreateRange(comparer: null, Enumerable.Range(0, 10)).ToBuilder(); Assert.Throws <ArgumentNullException>(() => list.FindAll(null !)); ImmutableSortedTreeList <int> found = list.FindAll(i => (i % 2) == 0); Assert.Equal(10, list.Count); Assert.Equal(5, found.Count); Assert.Equal(new[] { 0, 2, 4, 6, 8 }, found); Assert.Empty(list.FindAll(i => i < 0)); }
public void PosTest1() { int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 }; var listObject = ImmutableSortedTreeList.Create(new ComparisonComparer <int>((x, y) => 0), iArray); int startIdx = Generator.GetInt32(0, 9); // The starting index of the section to make a shallow copy int endIdx = Generator.GetInt32(startIdx, 10); // The end index of the section to make a shallow copy int count = endIdx - startIdx + 1; ImmutableSortedTreeList <int> listResult = listObject.GetRange(startIdx, count); for (int i = 0; i < count; i++) { Assert.Equal(iArray[i + startIdx], listResult[i]); } }