📄 collectionbasetests.cs
字号:
InterfaceTests.TestCollection<string>(dict.Keys, new string[] { "Eric", "Clapton", "Rules", "The", "Universe" }, false);
InterfaceTests.TestReadonlyCollectionGeneric<string>(dict.Keys, new string[] { "Eric", "Clapton", "Rules", "The", "Universe" }, false, null);
InterfaceTests.TestCollection<int>(dict.Values, new int[] { 1, 1, 3, 4, 5 }, false);
InterfaceTests.TestReadonlyCollectionGeneric<int>(dict.Values, new int[] { 1, 1, 3, 4, 5 }, false, null);
}
[Test]
public void Exists()
{
ReadWriteTestCollection<double> coll1 = new ReadWriteTestCollection<double>(new double[] { 4.5, 1.2, 7.6, -7.6, -0.04, 1.78, 10.11, 187.4 });
Assert.IsTrue(coll1.Exists(delegate(double d) { return d > 100; }));
Assert.IsTrue(coll1.Exists(delegate(double d) { return Math.Abs(d) == 0.04; }));
Assert.IsFalse(coll1.Exists(delegate(double d) { return d < -10.0; }));
coll1.Clear();
Assert.IsFalse(coll1.Exists(delegate(double d) { return Math.Abs(d) == 0.04; }));
ReadOnlyTestCollection<double> coll2 = new ReadOnlyTestCollection<double>(new double[] { 4.5, 1.2, 7.6, -7.6, -0.04, 1.78, 10.11, 187.4 });
Assert.IsTrue(coll2.Exists(delegate(double d) { return d > 100; }));
Assert.IsTrue(coll2.Exists(delegate(double d) { return Math.Abs(d) == 0.04; }));
Assert.IsFalse(coll2.Exists(delegate(double d) { return d < -10.0; }));
coll2 = new ReadOnlyTestCollection<double>(new double[] { });
Assert.IsFalse(coll2.Exists(delegate(double d) { return Math.Abs(d) == 0.04; }));
}
[Test]
public void TrueForAll()
{
ReadWriteTestCollection<double> coll1 = new ReadWriteTestCollection<double>(new double[] { 4.5, 1.2, 7.6, -7.6, -0.04, 1.78, 10.11, 187.4 });
Assert.IsFalse(coll1.TrueForAll(delegate(double d) { return d > 100; }));
Assert.IsFalse(coll1.TrueForAll(delegate(double d) { return Math.Abs(d) < 10; }));
Assert.IsTrue(coll1.TrueForAll(delegate(double d) { return d > -10; }));
Assert.IsTrue(coll1.TrueForAll(delegate(double d) { return Math.Abs(d) < 200; }));
coll1.Clear();
Assert.IsTrue(coll1.TrueForAll(delegate(double d) { return Math.Abs(d) == 0.04; }));
ReadOnlyTestCollection<double> coll2 = new ReadOnlyTestCollection<double>(new double[] { 4.5, 1.2, 7.6, -7.6, -0.04, 1.78, 10.11, 187.4 });
Assert.IsFalse(coll2.TrueForAll(delegate(double d) { return d > 100; }));
Assert.IsFalse(coll2.TrueForAll(delegate(double d) { return Math.Abs(d) < 10; }));
Assert.IsTrue(coll2.TrueForAll(delegate(double d) { return d > -10; }));
Assert.IsTrue(coll2.TrueForAll(delegate(double d) { return Math.Abs(d) < 200; }));
coll2 = new ReadOnlyTestCollection<double>(new double[] { });
Assert.IsTrue(coll2.TrueForAll(delegate(double d) { return Math.Abs(d) == 0.04; }));
}
[Test]
public void CountWhere()
{
ReadWriteTestCollection<double> coll1 = new ReadWriteTestCollection<double>(new double[] { 4.5, 1.2, 7.6, -7.6, -0.04, 1.78, 10.11, 187.4 });
Assert.AreEqual(0, coll1.CountWhere(delegate(double d) { return d > 200; }));
Assert.AreEqual(6, coll1.CountWhere(delegate(double d) { return Math.Abs(d) < 10; }));
Assert.AreEqual(8, coll1.CountWhere(delegate(double d) { return d > -10; }));
Assert.AreEqual(4, coll1.CountWhere(delegate(double d) { return Math.Abs(d) > 5; }));
coll1.Clear();
Assert.AreEqual(0, coll1.CountWhere(delegate(double d) { return Math.Abs(d) < 10; }));
ReadOnlyTestCollection<double> coll2 = new ReadOnlyTestCollection<double>(new double[] { 4.5, 1.2, 7.6, -7.6, -0.04, 1.78, 10.11, 187.4 });
Assert.AreEqual(0, coll2.CountWhere(delegate(double d) { return d > 200; }));
Assert.AreEqual(6, coll2.CountWhere(delegate(double d) { return Math.Abs(d) < 10; }));
Assert.AreEqual(8, coll2.CountWhere(delegate(double d) { return d > -10; }));
Assert.AreEqual(4, coll2.CountWhere(delegate(double d) { return Math.Abs(d) > 5; }));
coll2 = new ReadOnlyTestCollection<double>(new double[] { });
Assert.AreEqual(0, coll2.CountWhere(delegate(double d) { return Math.Abs(d) < 10; }));
}
[Test]
public void FindAll()
{
ReadWriteTestCollection<double> coll1 = new ReadWriteTestCollection<double>(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
double[] expected = { 7.6, -7.6, 10.11, 187.4 };
int i;
i = 0;
foreach (double x in coll1.FindAll(delegate(double d) { return Math.Abs(d) > 5; })) {
Assert.AreEqual(expected[i], x);
++i;
}
Assert.AreEqual(expected.Length, i);
ReadOnlyTestCollection<double> coll2 = new ReadOnlyTestCollection<double>(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
expected = new double[] { 7.6, -7.6, 10.11, 187.4 };
i = 0;
foreach (double x in coll2.FindAll(delegate(double d) { return Math.Abs(d) > 5; })) {
Assert.AreEqual(expected[i], x);
++i;
}
Assert.AreEqual(expected.Length, i);
}
[Test]
public void RemoveAll()
{
ReadWriteTestCollection<double> coll1 = new ReadWriteTestCollection<double>(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
coll1.RemoveAll(delegate(double d) { return Math.Abs(d) > 5; });
InterfaceTests.TestReadWriteCollectionGeneric(coll1, new double[] { 4.5, 1.2, -0.04, 1.78 }, true, null);
coll1 = new ReadWriteTestCollection<double>(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
coll1.RemoveAll(delegate(double d) { return d == 0; });
InterfaceTests.TestReadWriteCollectionGeneric(coll1, new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 }, true, null);
coll1 = new ReadWriteTestCollection<double>(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
coll1.RemoveAll(delegate(double d) { return d < 200; });
Assert.AreEqual(0, coll1.Count);
}
[Test]
public void ForEach()
{
ReadWriteTestCollection<string> coll1 = new ReadWriteTestCollection<string>(new string[] { "foo", "bar", "hello", "sailor" });
string s = "";
coll1.ForEach(delegate(string x) { s += "!" + x; });
Assert.AreEqual(s, "!foo!bar!hello!sailor");
ReadOnlyTestCollection<string> coll2 = new ReadOnlyTestCollection<string>(new string[] { "foo", "bar", "hello", "sailor" });
s = "";
coll2.ForEach(delegate(string x) { s += "!" + x; });
Assert.AreEqual(s, "!foo!bar!hello!sailor");
coll1 = new ReadWriteTestCollection<string>(new string[] { });
s = "";
coll1.ForEach(delegate(string x) { s += "!" + x; });
Assert.AreEqual(s, "");
coll2 = new ReadOnlyTestCollection<string>(new string[] { });
s = "";
coll2.ForEach(delegate(string x) { s += "!" + x; });
Assert.AreEqual(s, "");
}
[Test]
public void ConvertAll()
{
int[] array = new int[400];
for (int i = 0; i < array.Length; ++i)
array[i] = i;
ReadWriteTestCollection<int> coll1 = new ReadWriteTestCollection<int>(array);
IEnumerable<string> result1;
result1 = coll1.ConvertAll<string>(delegate(int x) { return (x * 2).ToString(); });
string[] expected = new string[400];
for (int i = 0; i < 400; ++i)
expected[i] = (2 * i).ToString();
InterfaceTests.TestEnumerableElements<string>(result1, expected);
coll1 = new ReadWriteTestCollection<int>(new int[0]);
result1 = coll1.ConvertAll<string>(delegate(int x) { return (x * 2).ToString(); });
InterfaceTests.TestEnumerableElements<string>(result1, new string[0]);
ReadOnlyTestCollection<int> coll2 = new ReadOnlyTestCollection<int>(array);
IEnumerable<string> result2;
result2 = coll2.ConvertAll<string>(delegate(int x) { return (x * 2).ToString(); });
InterfaceTests.TestEnumerableElements<string>(result2, expected);
coll2 = new ReadOnlyTestCollection<int>(new int[0]);
result2 = coll2.ConvertAll<string>(delegate(int x) { return (x * 2).ToString(); });
InterfaceTests.TestEnumerableElements<string>(result2, new string[0]);
}
[Test]
public void AsReadOnly()
{
int[] elements = new int[400];
for (int i = 0; i < 400; ++i)
elements[i] = i;
ReadWriteTestCollection<int> coll1 = new ReadWriteTestCollection<int>(elements);
ICollection<int> coll2 = coll1.AsReadOnly();
InterfaceTests.TestReadonlyCollectionGeneric<int>(coll2, elements, true, null);
coll1.Add(27);
coll1.Add(199);
elements = new int[402];
coll2 = coll1.AsReadOnly();
for (int i = 0; i < 400; ++i)
elements[i] = i;
elements[400] = 27;
elements[401] = 199;
InterfaceTests.TestReadonlyCollectionGeneric<int>(coll2, elements, true, null);
coll1 = new ReadWriteTestCollection<int>(new int[0]);
coll2 = coll1.AsReadOnly();
InterfaceTests.TestReadonlyCollectionGeneric<int>(coll2, new int[0], true, null);
coll1.Add(4);
InterfaceTests.TestReadonlyCollectionGeneric<int>(coll2, new int[] { 4 }, true, null);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -