📄 biglisttests.cs
字号:
InterfaceTests.TestEnumerableElements<int>(list1, new int[] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 187, 188, 189,
190, 191, 192, 193, 194, 195, 196, 197, 198 });
}
[Test]
public void RemoveRangeExceptions()
{
BigList<int> list1 = CreateList(0, 100);
try {
list1.RemoveRange(3, 98);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.RemoveRange(-1, 1);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("index", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.RemoveRange(0, int.MaxValue);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.RemoveRange(1, int.MinValue);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.RemoveRange(45, int.MinValue);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.RemoveRange(0, 101);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.RemoveRange(100, 1);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("index", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.RemoveRange(int.MinValue, 1);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("index", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.RemoveRange(int.MaxValue, 1);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("index", ((ArgumentOutOfRangeException)e).ParamName);
}
}
[Test]
public void AddToSelf()
{
BigList<int> list1 = new BigList<int>();
for (int i = 0; i < 20; ++i)
list1.Add(i);
list1.AddRange(list1);
#if DEBUG
list1.Validate();
#endif //DEBUG
Assert.AreEqual(40, list1.Count);
for (int i = 0; i < 40; ++i)
Assert.AreEqual(i % 20, list1[i]);
list1.Clear();
for (int i = 0; i < 20; ++i)
list1.Add(i);
list1.AddRangeToFront(list1);
#if DEBUG
list1.Validate();
#endif //DEBUG
Assert.AreEqual(40, list1.Count);
for (int i = 0; i < 40; ++i)
Assert.AreEqual(i % 20, list1[i]);
list1.Clear();
for (int i = 0; i < 20; ++i)
list1.Add(i);
list1.InsertRange(7, list1);
#if DEBUG
list1.Validate();
#endif //DEBUG
Assert.AreEqual(40, list1.Count);
for (int i = 0; i < 40; ++i) {
if (i < 7)
Assert.AreEqual(i, list1[i]);
else if (i >= 7 && i < 27)
Assert.AreEqual(i - 7, list1[i]);
else if (i >= 27)
Assert.AreEqual(i - 20, list1[i]);
}
}
void CheckListAndBigList(List<int> l1, BigList<int> l2, Random rand)
{
Assert.AreEqual(l1.Count, l2.Count);
for (int i = 0; i < l1.Count; ++i) {
Assert.AreEqual(l1[i], l2[i]);
}
int j = 0;
foreach (int x in l2) {
Assert.AreEqual(l1[j++], x);
}
int start = rand.Next(l1.Count);
int count = rand.Next(l1.Count - start);
j = start;
foreach (int x in l2.Range(start, count)) {
Assert.AreEqual(l1[j++], x);
}
Assert.AreEqual(j, start+count);
}
[Test]
public void GenericIListInterface()
{
BigList<int> list = new BigList<int>();
int[] array = new int[0];
InterfaceTests.TestReadWriteListGeneric<int>((IList<int>)list, array);
list = CreateList(0, 5);
array = new int[5];
for (int i = 0; i < array.Length; ++i)
array[i] = i;
InterfaceTests.TestReadWriteListGeneric<int>((IList<int>)list, array);
list = CreateList(0, 300);
array = new int[300];
for (int i = 0; i < array.Length; ++i)
array[i] = i;
InterfaceTests.TestReadWriteListGeneric<int>((IList<int>)list, array);
}
[Test]
public void IListInterface()
{
BigList<int> list = new BigList<int>();
int[] array = new int[0];
InterfaceTests.TestReadWriteList<int>((IList)list, array);
list = CreateList(0, 5);
array = new int[5];
for (int i = 0; i < array.Length; ++i)
array[i] = i;
InterfaceTests.TestReadWriteList<int>((IList)list, array);
list = CreateList(0, 300);
array = new int[300];
for (int i = 0; i < array.Length; ++i)
array[i] = i;
InterfaceTests.TestReadWriteList<int>((IList)list, array);
}
[Test]
public void Clone()
{
BigList<int> list1, list2, list3, list4;
list1 = new BigList<int>();
list2 = list1.Clone();
list3 = (BigList<int>)(((ICloneable)list1).Clone());
list4 = new BigList<int>(list1);
InterfaceTests.TestListGeneric<int>(list2, new int[0], null);
InterfaceTests.TestListGeneric<int>(list3, new int[0], null);
InterfaceTests.TestListGeneric<int>(list4, new int[0], null);
list1.Add(5);
InterfaceTests.TestListGeneric<int>(list2, new int[0], null);
InterfaceTests.TestListGeneric<int>(list3, new int[0], null);
InterfaceTests.TestListGeneric<int>(list4, new int[0], null);
int[] array = {0, 1, 2, 3, 4};
list1 = CreateList(0, 5);
list2 = list1.Clone();
list3 = (BigList<int>)(((ICloneable)list1).Clone());
list4 = new BigList<int>(list1);
InterfaceTests.TestListGeneric<int>(list2, array, null);
InterfaceTests.TestListGeneric<int>(list3, array, null);
InterfaceTests.TestListGeneric<int>(list4, array, null);
list2[3] = -1;
InterfaceTests.TestListGeneric<int>(list1, array, null);
InterfaceTests.TestListGeneric<int>(list3, array, null);
InterfaceTests.TestListGeneric<int>(list4, array, null);
array = new int[100];
for (int i = 0; i < 100; ++i)
array[i] = i;
list1 = CreateList(0, 100);
list2 = list1.Clone();
list3 = (BigList<int>)(((ICloneable)list1).Clone());
list4 = new BigList<int>(list1);
InterfaceTests.TestListGeneric<int>(list2, array, null);
InterfaceTests.TestListGeneric<int>(list3, array, null);
InterfaceTests.TestListGeneric<int>(list4, array, null);
list4.Clear();
InterfaceTests.TestListGeneric<int>(list1, array, null);
InterfaceTests.TestListGeneric<int>(list2, array, null);
InterfaceTests.TestListGeneric<int>(list3, array, null);
}
// Simple class for testing cloning.
class MyInt : ICloneable
{
public int value;
public MyInt(int value)
{
this.value = value;
}
public object Clone()
{
return new MyInt(value);
}
public override bool Equals(object obj)
{
return (obj is MyInt && ((MyInt)obj).value == value);
}
public override int GetHashCode()
{
return value.GetHashCode();
}
public override string ToString()
{
return value.ToString();
}
}
void CompareClones<T>(BigList<T> s1, BigList<T> s2)
{
IEnumerator<T> e1 = s1.GetEnumerator();
IEnumerator<T> e2 = s2.GetEnumerator();
Assert.IsTrue(s1.Count == s2.Count);
// Check that the lists are equal, but not reference equals (e.g., have been cloned).
while (e1.MoveNext()) {
e2.MoveNext();
if (e1.Current == null) {
Assert.IsNull(e2.Current);
}
else {
Assert.IsTrue(e1.Current.Equals(e2.Current));
Assert.IsFalse(object.ReferenceEquals(e1.Current, e2.Current));
}
}
}
[Test]
public void CloneContents()
{
BigList<MyInt> list1 = new BigList<MyInt>();
list1.Add(new MyInt(143));
list1.Add(new MyInt(2));
list1.Add(new MyInt(9));
list1.Add(null);
list1.Add(new MyInt(2));
list1.Add(new MyInt(111));
BigList<MyInt> list2 = list1.CloneContents();
CompareClones(list1, list2);
BigList<int> list3 = new BigList<int>(new int[] { 144, 5, 23, 1, 0, 8 });
BigList<int> list4 = list3.CloneContents();
CompareClones(list3, list4);
BigList<UtilTests.CloneableStruct> list5 = new BigList<UtilTests.CloneableStruct>();
list5.Add(new UtilTests.CloneableStruct(143));
list5.Add(new UtilTests.CloneableStruct(5));
list5.Add(new UtilTests.CloneableStruct(23));
list5.Add(new UtilTests.CloneableStruct(1));
list5.Add(new UtilTests.CloneableStruct(8));
BigList<UtilTests.CloneableStruct> list6 = list5.CloneContents();
Assert.AreEqual(list5.Count, list6.Count);
// Check that the lists are equal, but not identical (e.g., have been cloned via ICloneable).
IEnumerator<UtilTests.CloneableStruct> e1 = list5.GetEnumerator();
IEnumerator<UtilTests.CloneableStruct> e2 = list6.GetEnumerator();
// Check that the lists are equal, but not reference equals (e.g., have been cloned).
while (e1.MoveNext()) {
e2.MoveNext();
Assert.IsTrue(e1.Current.Equals(e2.Current));
Assert.IsFalse(e1.Current.Identical(e2.Current));
}
}
class NotCloneable { }
[Test, ExpectedException(typeof(InvalidOperationException))]
public void CantCloneContents()
{
BigList<NotCloneable> list1 = new BigList<NotCloneable>();
list1.Add(new NotCloneable());
list1.Add(new NotCloneable());
BigList<NotCloneable> list2 = list1.CloneContents();
}
[Test]
public void MultiCopies()
{
BigList<int> list1;
// Check empty special case.
list1 = new BigList<int>(new int[] { 1, 2, 3 }, 0);
InterfaceTests.TestListGeneric<int>(list1, new int[0], null);
list1 = new BigList<int>(new int[] { }, 5);
InterfaceTests.TestListGeneric<int>(list1, new int[0], null);
list1 = new BigList<int>(new BigList<int>(new int[] { 1, 2, 3 }), 0);
InterfaceTests.TestListGeneric<int>(list1, new int[0], null);
list1 = new BigList<int>(new BigList<int>(), 5);
InterfaceTests.TestListGeneric<int>(list1, new int[0], null);
// Small cases.
list1 = new BigList<int>(new int[] { 1, 2, 3, 4 }, 7);
InterfaceTests.TestListGeneric<int>(list1, new int[] { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 }, null);
list1[7] = 12;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -