📄 listbasetests.cs
字号:
//******************************
// Written by Peter Golde
// Copyright (c) 2004-2005, Wintellect
//
// Use and restribution of this code is subject to the license agreement
// contained in the file "License.txt" accompanying this file.
//******************************
#region Using directives
using System;
using System.Collections.Generic;
using System.Collections;
using NUnit.Framework;
using Wintellect.PowerCollections;
#endregion
namespace Wintellect.PowerCollections.Tests
{
// A simple read-write list using an array.
public class ReadWriteArrayList<T> : ListBase<T>
{
T[] array;
public ReadWriteArrayList(T[] items)
{
array = (T[])(items.Clone());
}
public override int Count
{
get { return array.Length; }
}
public override void Clear()
{
array = new T[0];
}
public override T this[int index]
{
get
{
if (index >= 0 && index < array.Length)
return array[index];
else
throw new ArgumentOutOfRangeException("index");
}
set
{
if (index >= 0 && index < array.Length)
array[index] = value;
else
throw new ArgumentOutOfRangeException("index");
}
}
public override void Insert(int index, T item)
{
if (index >= 0 && index <= array.Length) {
T[] newArray = new T[array.Length + 1];
Array.Copy(array, 0, newArray, 0, index);
Array.Copy(array, index, newArray, index + 1, array.Length - index);
newArray[index] = item;
array = newArray;
}
else
throw new ArgumentOutOfRangeException("index");
}
public override void RemoveAt(int index)
{
if (index >= 0 && index < array.Length) {
T[] newArray = new T[array.Length - 1];
Array.Copy(array, 0, newArray, 0, index);
Array.Copy(array, index + 1, newArray, index, array.Length - index - 1);
array = newArray;
}
else
throw new ArgumentOutOfRangeException("index");
}
}
// A simple read-only list using an array.
public class ReadOnlyArrayList<T> : ReadOnlyListBase<T>
{
T[] array;
public ReadOnlyArrayList(T[] items)
{
array = (T[])(items.Clone());
}
public override int Count
{
get { return array.Length; }
}
public override T this[int index]
{
get
{
if (index >= 0 && index < array.Length)
return array[index];
else
throw new ArgumentOutOfRangeException("index");
}
}
}
[TestFixture]
public class ListBaseTests
{
[Test]
public void ReadOnlyListBase()
{
string[] s1 = {"Hello", "Goodbye", "Eric", null, "Clapton", "Hello", "Rules" };
IList<string> list1 = new ReadOnlyArrayList<string>(s1);
InterfaceTests.TestReadOnlyListGeneric<string>(list1, s1, "ReadOnlyArrayList");
string[] s2 = { };
IList<string> list2 = new ReadOnlyArrayList<string>(s2);
InterfaceTests.TestReadOnlyListGeneric<string>(list2, s2, "ReadOnlyArrayList");
string[] s3 = { "foo" };
IList<string> list3 = new ReadOnlyArrayList<string>(s3);
InterfaceTests.TestReadOnlyListGeneric<string>(list3, s3, "ReadOnlyArrayList");
string[] s4 = { null, null };
IList<string> list4 = new ReadOnlyArrayList<string>(s4);
InterfaceTests.TestReadOnlyListGeneric<string>(list4, s4, "ReadOnlyArrayList");
string[] s5 = { "Hello", "Goodbye", "Eric", null, "Clapton", "Hello", "Rules" };
IList list5 = new ReadOnlyArrayList<string>(s5);
InterfaceTests.TestReadOnlyList<string>(list5, s5, "ReadOnlyArrayList");
string[] s6 = { };
IList list6 = new ReadOnlyArrayList<string>(s6);
InterfaceTests.TestReadOnlyList<string>(list6, s6, "ReadOnlyArrayList");
string[] s7 = { "foo" };
IList list7 = new ReadOnlyArrayList<string>(s7);
InterfaceTests.TestReadOnlyList<string>(list7, s7, "ReadOnlyArrayList");
string[] s8 = { null, null };
IList list8 = new ReadOnlyArrayList<string>(s8);
InterfaceTests.TestReadOnlyList<string>(list8, s8, "ReadOnlyArrayList");
}
[Test]
public void ReadWriteListBase()
{
string[] s1 = { "Hello", "Goodbye", "Eric", null, "Clapton", "Hello", "Rules" };
IList<string> list1 = new ReadWriteArrayList<string>(s1);
InterfaceTests.TestReadWriteListGeneric<string>((IList<string>)list1, s1);
string[] s2 = { };
IList<string> list2 = new ReadWriteArrayList<string>(s2);
InterfaceTests.TestReadWriteListGeneric<string>((IList<string>)list2, s2);
string[] s3 = { "foo" };
IList<string> list3 = new ReadWriteArrayList<string>(s3);
InterfaceTests.TestReadWriteListGeneric<string>((IList<string>)list3, s3);
string[] s4 = { null, null };
IList<string> list4 = new ReadWriteArrayList<string>(s4);
InterfaceTests.TestReadWriteListGeneric<string>((IList<string>)list4, s4);
string[] s5 = { "Hello", "Goodbye", "Eric", null, "Clapton", "Hello", "Rules" };
IList<string> list5 = new ReadWriteArrayList<string>(s5);
InterfaceTests.TestReadWriteList<string>((IList)list5, s5);
string[] s6 = { };
IList<string> list6 = new ReadWriteArrayList<string>(s6);
InterfaceTests.TestReadWriteList<string>((IList)list6, s6);
string[] s7 = { "foo" };
IList<string> list7 = new ReadWriteArrayList<string>(s7);
InterfaceTests.TestReadWriteList<string>((IList)list7, s7);
string[] s8 = { null, null };
IList<string> list8 = new ReadWriteArrayList<string>(s8);
InterfaceTests.TestReadWriteList<string>((IList)list8, s8);
}
// Check that InterfaceTests is reasonable by checking against the built in List class.
[Test]
public void CheckList()
{
string[] s1 = { "Hello", "Goodbye", "Eric", null, "Clapton", "Hello", "Rules" };
List<string> list1 = new List<string>(s1);
InterfaceTests.TestReadWriteListGeneric<string>((IList<string>)list1, s1);
List<string> list2 = new List<string>(s1);
InterfaceTests.TestReadWriteList<string>((IList)list2, s1);
ArrayList list3 = new ArrayList(s1);
InterfaceTests.TestReadWriteList<object>((IList)list3, s1);
List<string> list4 = new List<string>();
InterfaceTests.TestReadWriteListGeneric<string>((IList<string>)list4, new string[0]);
List<string> list5 = new List<string>();
InterfaceTests.TestReadWriteList<string>((IList)list5, new string[0]);
ArrayList list6 = new ArrayList();
InterfaceTests.TestReadWriteList<object>((IList)list6, new string[0]);
IList<string> ro1 = new List<string>(s1).AsReadOnly();
InterfaceTests.TestReadOnlyListGeneric<string>((IList<string>)ro1, s1, null);
IList<string> ro2 = new List<string>().AsReadOnly();
InterfaceTests.TestReadOnlyListGeneric<string>((IList<string>)ro2, new string[0], null);
IList ro3 = ArrayList.ReadOnly(new ArrayList(s1));
InterfaceTests.TestReadOnlyList<object>(ro3, s1, null);
IList ro4 = ArrayList.ReadOnly(new ArrayList());
InterfaceTests.TestReadOnlyList<object>(ro4, new string[0], null);
}
[Test]
public void Range()
{
ReadWriteArrayList<int> main = new ReadWriteArrayList<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
IList<int> range = main.Range(2, 4);
InterfaceTests.TestReadWriteListGeneric(range, new int[] { 2, 3, 4, 5 }, null);
main = new ReadWriteArrayList<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
range = main.Range(2, 4);
range[1] = 7;
range.Add(99);
Assert.AreEqual(5, range.Count);
range.RemoveAt(0);
Assert.AreEqual(4, range.Count);
InterfaceTests.TestEnumerableElements(main, new int[] { 0, 1, 7, 4, 5, 99, 6, 7 });
main[3] = 11;
InterfaceTests.TestEnumerableElements(range, new int[] { 7, 11, 5, 99 });
main = new ReadWriteArrayList<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
range = main.Range(5, 3);
Assert.AreEqual(3, range.Count);
main.Remove(6);
main.Remove(5);
Assert.AreEqual(1, range.Count);
Assert.AreEqual(7, range[0]);
main = new ReadWriteArrayList<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
range = main.Range(8, 0);
range.Add(8);
range.Add(9);
InterfaceTests.TestEnumerableElements(main, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
InterfaceTests.TestEnumerableElements(range, new int[] { 8, 9 });
main = new ReadWriteArrayList<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
range = main.Range(0, 4);
range.Clear();
Assert.AreEqual(0, range.Count);
InterfaceTests.TestEnumerableElements(main, new int[] { 4, 5, 6, 7 });
range.Add(100);
range.Add(101);
InterfaceTests.TestEnumerableElements(main, new int[] { 100, 101, 4, 5, 6, 7 });
main = new ReadWriteArrayList<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
range = main.Range(8, 0);
InterfaceTests.TestListGeneric(range, new int[] { }, null);
}
[Test]
public void ReadOnlyRange()
{
ReadOnlyArrayList<int> main = new ReadOnlyArrayList<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
IList<int> range = main.Range(2, 4);
InterfaceTests.TestReadOnlyListGeneric(range, new int[] { 2, 3, 4, 5 }, null);
main = new ReadOnlyArrayList<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
range = main.Range(8, 0);
InterfaceTests.TestReadOnlyListGeneric(range, new int[] { }, null);
}
[Test]
public void RangeExceptions()
{
ReadWriteArrayList<int> list = new ReadWriteArrayList<int>(new int[0]);
IList<int> range;
for (int i = 0; i < 50; ++i)
list.Add(i);
for (int i = 0; i < 50; ++i)
list.Add(i);
try {
range = list.Range(3, 98);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
range = list.Range(-1, 1);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("start", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
range = list.Range(0, int.MaxValue);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
range = list.Range(1, int.MinValue);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
range = list.Range(45, int.MinValue);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
try {
range = list.Range(0, 101);
Assert.Fail("should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
Assert.AreEqual("count", ((ArgumentOutOfRangeException)e).ParamName);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -