📄 biglisttests.cs
字号:
if (j == 20)
j = 0;
}
int size = i - 1;
Assert.AreEqual(size, biglist1.Count);
#if DEBUG
biglist1.Validate();
#endif //DEBUG
for (i = 1; i <= size; ++i) {
Assert.AreEqual(i, biglist1[size - i]);
}
i = size;
foreach (int x in biglist1)
Assert.AreEqual(i--, x);
BigList<int> biglist2 = biglist1.Clone();
for (i = 1; i <= SIZE; ++i) {
Assert.AreEqual(i, biglist1[size - i]);
}
i = size;
foreach (int x in biglist2)
Assert.AreEqual(i--, x);
}
[Test]
public void AddBigListEnd()
{
const int SIZE = 8000;
BigList<int> biglist1 = new BigList<int>();
int i = 1, j = 0;
while (i <= SIZE) {
int[] array = new int[j];
for (int x = 0; x < j; ++x)
array[x] = i + x;
BigList<int> biglistOther = new BigList<int>(array);
if (j % 3 == 0)
biglistOther.Clone();
biglist1 = biglist1 + biglistOther;
#if DEBUG
if (i % 30 == 0)
biglist1.Validate();
#endif //DEBUG
if (i % 13 <= 2)
biglist1.Clone();
i += j;
j += 1;
if (j == 20)
j = 0;
}
int size = i - 1;
Assert.AreEqual(size, biglist1.Count);
#if DEBUG
biglist1.Validate();
#endif //DEBUG
for (i = 1; i <= size; ++i) {
Assert.AreEqual(i, biglist1[i - 1]);
}
i = 1;
foreach (int x in biglist1)
Assert.AreEqual(i++, x);
BigList<int> biglist2 = biglist1.Clone();
for (i = 1; i <= size; ++i) {
Assert.AreEqual(i, biglist1[i - 1]);
}
i = 1;
foreach (int x in biglist2)
Assert.AreEqual(i++, x);
}
[Test]
public void AddBigListBeginning()
{
const int SIZE = 8000;
BigList<int> biglist1 = new BigList<int>();
int i = 1, j = 0;
while (i <= SIZE) {
int[] array = new int[j];
for (int x = 0; x < j; ++x)
array[j - x - 1] = i + x;
BigList<int> biglistOther = new BigList<int>(array);
if (j % 3 == 0)
biglistOther.Clone();
biglist1 = biglistOther + biglist1;
#if DEBUG
if (i % 30 == 0)
biglist1.Validate();
#endif //DEBUG
if (i % 13 <= 2)
biglist1.Clone();
i += j;
j += 1;
if (j == 20)
j = 0;
}
int size = i - 1;
Assert.AreEqual(size, biglist1.Count);
#if DEBUG
biglist1.Validate();
#endif //DEBUG
for (i = 1; i <= size; ++i) {
Assert.AreEqual(i, biglist1[size - i]);
}
i = size;
foreach (int x in biglist1)
Assert.AreEqual(i--, x);
BigList<int> biglist2 = biglist1.Clone();
for (i = 1; i <= SIZE; ++i) {
Assert.AreEqual(i, biglist1[size - i]);
}
i = size;
foreach (int x in biglist2)
Assert.AreEqual(i--, x);
}
[Test]
public void Count()
{
BigList<int> list1, list2, list3, list4, list5, list6, list7, list8;
list1 = new BigList<int>();
list2 = new BigList<int>(new int[0]);
list3 = list2 + list1;
Assert.AreEqual(0, list1.Count);
#if DEBUG
list1.Validate();
#endif //DEBUG
Assert.AreEqual(0, list2.Count);
#if DEBUG
list2.Validate();
#endif //DEBUG
Assert.AreEqual(0, list3.Count);
#if DEBUG
list3.Validate();
#endif //DEBUG
list4 = new BigList<int>(new int[2145]);
Assert.AreEqual(2145, list4.Count);
#if DEBUG
list4.Validate();
#endif //DEBUG
list5 = list4.GetRange(1003, 423);
Assert.AreEqual(423, list5.Count);
#if DEBUG
list5.Validate();
#endif //DEBUG
list6 = list4.GetRange(1, 5);
Assert.AreEqual(5, list6.Count);
#if DEBUG
list6.Validate();
#endif //DEBUG
list7 = list5 + list6;
Assert.AreEqual(428, list7.Count);
#if DEBUG
list7.Validate();
#endif //DEBUG
list8 = list7.GetRange(77, 0);
Assert.AreEqual(0, list8.Count);
#if DEBUG
list8.Validate();
#endif //DEBUG
list6.Clear();
Assert.AreEqual(0, list6.Count);
#if DEBUG
list6.Validate();
#endif //DEBUG
}
BigList<int> CreateList(int start, int length)
{
if (length < 24) {
int[] array = new int[length];
for (int i = 0; i < length; ++i)
array[i] = i + start;
return new BigList<int>(array);
}
else {
int split = length / 5 * 2;
return CreateList(start, split) + CreateList(start + split, length - split);
}
}
[Test]
public void GetRange()
{
BigList<int> list1, list2, list3, list4, list5;
list1 = new BigList<int>();
list2 = list1.GetRange(4, 0); // 0 length range permitted anywhere.
Assert.AreEqual(0, list2.Count);
list3 = new BigList<int>(new int[] { 1, 2, 3, 4, 5 });
list4 = list3.GetRange(2, 3);
InterfaceTests.TestEnumerableElements(list4, new int[] { 3, 4, 5 });
list5 = list3.GetRange(0, 3);
InterfaceTests.TestEnumerableElements(list5, new int[] { 1, 2, 3 });
list3[3] = 7;
list4[1] = 2;
list5[2] = 9;
InterfaceTests.TestEnumerableElements(list4, new int[] { 3, 2, 5 });
InterfaceTests.TestEnumerableElements(list5, new int[] { 1, 2, 9 });
list1 = CreateList(0, 132);
list2 = list1.GetRange(27, 53);
for (int i = 0; i < 53; ++i)
Assert.AreEqual(27 + i, list2[i]);
int y = 27;
foreach (int x in list2)
Assert.AreEqual(y++, x);
list3 = list2.GetRange(4, 27);
for (int i = 0; i < 27; ++i)
Assert.AreEqual(31 + i, list3[i]);
y = 31;
foreach (int x in list3)
Assert.AreEqual(y++, x);
}
[Test]
public void GetRangeExceptions()
{
BigList<int> list1 = CreateList(0, 100);
try {
list1.GetRange(3, 98);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.GetRange(-1, 1);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("index", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.GetRange(0, int.MaxValue);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.GetRange(1, int.MinValue);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.GetRange(45, int.MinValue);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.GetRange(0, 101);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.GetRange(100, 1);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("index", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.GetRange(int.MinValue, 1);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("index", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
list1.GetRange(int.MaxValue, 1);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("index", ((ArgumentOutOfRangeException)e).ParamName);
}
}
[Test]
public void ConcatLeaf()
{
BigList<int> list1, list2, list3;
list1 = CreateList(0, 5);
list2 = CreateList(5, 7);
list3 = list1 + list2;
list1[3] = -1;
list2[4] = -1;
for (int i = 0; i < list3.Count; ++i)
Assert.AreEqual(i, list3[i]);
}
[Test]
public void PrependLeaf()
{
BigList<int> list1, list2, list3;
list1 = new BigList<int>();
for (int i = 2; i < 50; ++i)
list1.Add(i);
list1.AddToFront(1);
list1.AddToFront(0);
list3 = list1.Clone();
list2 = CreateList(0, 2);
list1.AddRangeToFront(list2);
list1[17] = -1;
for (int i = 0; i < 50; ++i)
Assert.AreEqual(i, list3[i]);
}
[Test]
public void Indexer()
{
BigList<int> list1, list2, list3;
int i;
list1 = new BigList<int>();
for (i = 0; i < 100; ++i)
list1.Add(i);
for (i = 99; i >= 0; --i)
Assert.AreEqual(i, list1[i]);
list2 = list1.Clone();
for (i = 44; i < 88; ++i)
list1[i] = i * 2;
#if DEBUG
list1.Print();
list2.Print();
#endif //DEBUG
for (i = 99; i >= 0; --i) {
Assert.AreEqual(i, list2[i]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -