📄 orderedbagtests.cs
字号:
bag1.Clear();
b = bag1.Remove(""); Assert.IsFalse(b);
}
[Test]
public void RemoveAllCopies()
{
OrderedBag<string> bag1 = new OrderedBag<string>(StringComparer.InvariantCultureIgnoreCase);
int i;
i = bag1.RemoveAllCopies("Eric"); Assert.AreEqual(0, i);
bag1.Add("hello");
bag1.Add("foo");
bag1.Add(null);
bag1.Add(null);
bag1.Add("hello");
bag1.Add(null);
i = bag1.RemoveAllCopies("HELLO"); Assert.AreEqual(2, i);
i = bag1.RemoveAllCopies("Hello"); Assert.AreEqual(0, i);
i = bag1.RemoveAllCopies(null); Assert.AreEqual(3, i);
bag1.Add("Hello");
bag1.Add("Eric");
bag1.Add(null);
i = bag1.RemoveAllCopies(null); Assert.AreEqual(1, i);
bag1.Add("ERIC");
i = bag1.RemoveAllCopies("eRic"); Assert.AreEqual(2, i);
}
[Test]
public void GetEqualItems()
{
OrderedBag<string> bag1 = new OrderedBag<string>(
new string[] { "foo", null, "FOO", "Eric", "eric", "bar", null, "foO", "ERIC", "eric", null },
StringComparer.InvariantCultureIgnoreCase);
InterfaceTests.TestEnumerableElements(bag1.GetEqualItems("foo"), new string[] { "foo", "FOO", "foO" });
InterfaceTests.TestEnumerableElements(bag1.GetEqualItems(null), new string[] { null, null, null });
InterfaceTests.TestEnumerableElements(bag1.GetEqualItems("silly"), new string[] { });
InterfaceTests.TestEnumerableElements(bag1.GetEqualItems("ERic"), new string[] { "Eric", "eric", "ERIC", "eric" });
}
[Test]
public void ToArray()
{
string[] s_array = { null, "Foo", "Eric", null, "Clapton", "hello", "Clapton", "goodbye", "C#" };
OrderedBag<string> bag1 = new OrderedBag<string>();
string[] a1 = bag1.ToArray();
Assert.IsNotNull(a1);
Assert.AreEqual(0, a1.Length);
foreach (string s in s_array)
bag1.Add(s);
string[] a2 = bag1.ToArray();
Array.Sort(s_array);
Assert.AreEqual(s_array.Length, a2.Length);
for (int i = 0; i < s_array.Length; ++i)
Assert.AreEqual(s_array[i], a2[i]);
}
[Test]
public void AddMany()
{
OrderedBag<string> bag1 = new OrderedBag<string>(StringComparer.InvariantCultureIgnoreCase);
bag1.Add("foo");
bag1.Add("Eric");
bag1.Add("Clapton");
string[] s_array = { "FOO", "x", "elmer", "fudd", "Clapton", null };
bag1.AddMany(s_array);
InterfaceTests.TestReadWriteCollectionGeneric(bag1, new string[] { null, "Clapton", "Clapton", "elmer", "Eric", "foo", "FOO", "fudd", "x" }, true, null);
bag1.Clear();
bag1.Add("foo");
bag1.Add("Eric");
bag1.AddMany(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag1, new string[] { "Eric", "Eric", "foo", "foo" }, true, null);
}
[Test]
public void RemoveMany()
{
OrderedBag<string> bag1 = new OrderedBag<string>(StringComparer.InvariantCultureIgnoreCase);
bag1.Add("foo");
bag1.Add("Eric");
bag1.Add("Clapton");
bag1.Add(null);
bag1.Add("Foo");
bag1.Add("fudd");
bag1.Add("elmer");
string[] s_array = { "FOO", "jasmine", "eric", null };
int count = bag1.RemoveMany(s_array);
Assert.AreEqual(3, count);
InterfaceTests.TestReadWriteCollectionGeneric(bag1, new string[] { "Clapton", "elmer", "foo", "fudd" }, true, null);
bag1.Clear();
bag1.Add("foo");
bag1.Add("Eric");
bag1.Add("Clapton");
bag1.Add(null);
bag1.Add("Foo");
count = bag1.RemoveMany(bag1);
Assert.AreEqual(5, count);
Assert.AreEqual(0, bag1.Count);
}
[Test]
public void Exists()
{
OrderedBag<double> bag1 = new OrderedBag<double>(new double[] { 4.5, 187.4, 1.2, 7.6, -7.6, -0.04, 1.2, 1.78, 10.11, 187.4 });
Assert.IsTrue(bag1.Exists(delegate(double d) { return d > 100; }));
Assert.IsTrue(bag1.Exists(delegate(double d) { return Math.Abs(d) == 0.04; }));
Assert.IsFalse(bag1.Exists(delegate(double d) { return d < -10.0; }));
bag1.Clear();
Assert.IsFalse(bag1.Exists(delegate(double d) { return Math.Abs(d) == 0.04; }));
}
[Test]
public void TrueForAll()
{
OrderedBag<double> bag1 = new OrderedBag<double>(new double[] { 4.5, 187.4, 1.2, 7.6, -7.6, -0.04, 1.2, 1.78, 10.11, 187.4 });
Assert.IsFalse(bag1.TrueForAll(delegate(double d) { return d > 100; }));
Assert.IsFalse(bag1.TrueForAll(delegate(double d) { return Math.Abs(d) < 10; }));
Assert.IsTrue(bag1.TrueForAll(delegate(double d) { return d > -10; }));
Assert.IsTrue(bag1.TrueForAll(delegate(double d) { return Math.Abs(d) < 200; }));
bag1.Clear();
Assert.IsTrue(bag1.TrueForAll(delegate(double d) { return Math.Abs(d) == 0.04; }));
}
[Test]
public void CountWhere()
{
OrderedBag<double> bag1 = new OrderedBag<double>(new double[] { 4.5, 187.4, 1.2, 7.6, -7.6, -0.04, 1.2, 1.78, 10.11, 187.4 });
Assert.AreEqual(0, bag1.CountWhere(delegate(double d) { return d > 200; }));
Assert.AreEqual(7, bag1.CountWhere(delegate(double d) { return Math.Abs(d) < 10; }));
Assert.AreEqual(10, bag1.CountWhere(delegate(double d) { return d > -10; }));
Assert.AreEqual(5, bag1.CountWhere(delegate(double d) { return Math.Abs(d) > 5; }));
bag1.Clear();
Assert.AreEqual(0, bag1.CountWhere(delegate(double d) { return Math.Abs(d) < 10; }));
}
[Test]
public void RemoveAll()
{
OrderedBag<double> bag1 = new OrderedBag<double>(new double[] { 4.5, 187.4, 1.2, 7.6, -7.6, -0.04, 1.2, 1.78, 10.11, 187.4 });
bag1.RemoveAll(delegate(double d) { return Math.Abs(d) > 5; });
InterfaceTests.TestReadWriteCollectionGeneric(bag1, new double[] { -0.04, 1.2, 1.2, 1.78, 4.5 }, true, null);
bag1 = new OrderedBag<double>(new double[] { 4.5, 187.4, 1.2, 7.6, -7.6, -0.04, 1.2, 1.78, 10.11, 187.4 });
bag1.RemoveAll(delegate(double d) { return d == 0; });
InterfaceTests.TestReadWriteCollectionGeneric(bag1, new double[] { -7.6, -0.04, 1.2, 1.2, 1.78, 4.5, 7.6, 10.11, 187.4, 187.4 }, true, null);
bag1 = new OrderedBag<double>(new double[] { 4.5, 187.4, 1.2, 7.6, -7.6, -0.04, 1.2, 1.78, 10.11, 187.4 });
bag1.RemoveAll(delegate(double d) { return d < 200; });
Assert.AreEqual(0, bag1.Count);
}
[Test]
public void FindAll()
{
OrderedBag<double> bag1 = new OrderedBag<double>(new double[] { 4.5, 187.4, 1.2, 7.6, -7.6, -0.04, 1.2, 1.78, 10.11, 187.4 });
double[] expected = { -7.6, 7.6, 10.11, 187.4, 187.4 };
int i;
i = 0;
foreach (double x in bag1.FindAll(delegate(double d) { return Math.Abs(d) > 5; })) {
Assert.AreEqual(expected[i], x);
++i;
}
Assert.AreEqual(expected.Length, i);
}
[Test]
public void IsDisjointFrom()
{
OrderedBag<int> bag1 = new OrderedBag<int>(new int[] { 3, 6, 7, 1, 1, 11, 9, 3, 8 });
OrderedBag<int> bag2 = new OrderedBag<int>();
OrderedBag<int> bag3 = new OrderedBag<int>();
OrderedBag<int> bag4 = new OrderedBag<int>(new int[] { 8, 9, 1, 8, 3, 7, 6, 11, 7 });
OrderedBag<int> bag5 = new OrderedBag<int>(new int[] { 17, 3, 12, 10, 22 });
OrderedBag<int> bag6 = new OrderedBag<int>(new int[] { 14, 19, 14, 0, 2, 14 });
Assert.IsFalse(bag1.IsDisjointFrom(bag1));
Assert.IsTrue(bag2.IsDisjointFrom(bag2));
Assert.IsTrue(bag1.IsDisjointFrom(bag2));
Assert.IsTrue(bag2.IsDisjointFrom(bag1));
Assert.IsTrue(bag2.IsDisjointFrom(bag3));
Assert.IsTrue(bag3.IsDisjointFrom(bag2));
Assert.IsFalse(bag1.IsDisjointFrom(bag4));
Assert.IsFalse(bag4.IsDisjointFrom(bag1));
Assert.IsFalse(bag1.IsDisjointFrom(bag5));
Assert.IsFalse(bag5.IsDisjointFrom(bag1));
Assert.IsTrue(bag1.IsDisjointFrom(bag6));
Assert.IsTrue(bag6.IsDisjointFrom(bag1));
Assert.IsTrue(bag5.IsDisjointFrom(bag6));
Assert.IsTrue(bag6.IsDisjointFrom(bag5));
}
[Test]
public void Intersection()
{
OrderedBag<int> bagOdds = new OrderedBag<int>(new int[] { 1, 1, 1, 3, 3, 3, 5, 7, 7, 9, 11, 11, 13, 15, 17, 17, 19 });
OrderedBag<int> bagDigits = new OrderedBag<int>(new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 });
OrderedBag<int> bag1, bag2, bag3;
// Algorithms work different depending on sizes, so try both ways.
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag1.IntersectionWith(bag2);
InterfaceTests.TestReadWriteCollectionGeneric(bag1, new int[] { 1, 3, 3, 3, 5, 7, 7, 9 }, true, null);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag2.IntersectionWith(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag2, new int[] { 1, 3, 3, 3, 5, 7, 7, 9 }, true, null);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag3 = bag1.Intersection(bag2);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 3, 3, 3, 5, 7, 7, 9 }, true, null);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag3 = bag2.Intersection(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 3, 3, 3, 5, 7, 7, 9 }, true, null);
// Make sure intersection with itself works.
bag1 = bagDigits.Clone();
bag1.IntersectionWith(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag1, new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 }, true, null);
bag1 = bagDigits.Clone();
bag3 = bag1.Intersection(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 }, true, null);
}
[Test]
public void Union()
{
OrderedBag<int> bagOdds = new OrderedBag<int>(new int[] { 1, 1, 1, 3, 3, 3, 5, 7, 7, 9, 11, 11, 13, 15, 17, 17, 19 });
OrderedBag<int> bagDigits = new OrderedBag<int>(new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 });
OrderedBag<int> bag1, bag2, bag3;
// Algorithms work different depending on sizes, so try both ways.
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag1.UnionWith(bag2);
InterfaceTests.TestReadWriteCollectionGeneric(bag1, new int[] { 1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9, 11, 11, 13, 15, 17, 17, 19 }, true, null);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag2.UnionWith(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag2, new int[] { 1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9, 11, 11, 13, 15, 17, 17, 19 }, true, null);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag3 = bag1.Union(bag2);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9, 11, 11, 13, 15, 17, 17, 19 }, true, null);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag3 = bag2.Union(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9, 11, 11, 13, 15, 17, 17, 19 }, true);
// Make sure intersection with itself works.
bag1 = bagDigits.Clone();
bag1.UnionWith(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag1, new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 }, true);
bag1 = bagDigits.Clone();
bag3 = bag1.Union(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 }, true);
}
[Test]
public void Sum()
{
OrderedBag<int> bagOdds = new OrderedBag<int>(new int[] { 1, 1, 1, 3, 3, 3, 5, 7, 7, 9, 11, 11, 13, 15, 17, 17, 19 });
OrderedBag<int> bagDigits = new OrderedBag<int>(new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 });
OrderedBag<int> bag1, bag2, bag3;
// Algorithms work different depending on sizes, so try both ways.
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag1.SumWith(bag2);
InterfaceTests.TestReadWriteCollectionGeneric(bag1, new int[] { 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 4, 5, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 9, 9, 11, 11, 13, 15, 17, 17, 19 }, true);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag2.SumWith(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag2, new int[] { 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 4, 5, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 9, 9, 11, 11, 13, 15, 17, 17, 19 }, true);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag3 = bag1.Sum(bag2);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 4, 5, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 9, 9, 11, 11, 13, 15, 17, 17, 19 }, true);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag3 = bag2.Sum(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 4, 5, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 9, 9, 11, 11, 13, 15, 17, 17, 19 }, true);
// Make sure intersection with itself works.
bag1 = bagDigits.Clone();
bag1.SumWith(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag1, new int[] { 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 9, 9 }, true);
bag1 = bagDigits.Clone();
bag3 = bag1.Sum(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 9, 9 }, true);
}
[Test]
public void SymmetricDifference()
{
OrderedBag<int> bagOdds = new OrderedBag<int>(new int[] { 1, 1, 1, 3, 3, 3, 5, 7, 7, 9, 11, 11, 13, 15, 17, 17, 19 });
OrderedBag<int> bagDigits = new OrderedBag<int>(new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 });
OrderedBag<int> bag1, bag2, bag3;
// Algorithms work different depending on sizes, so try both ways.
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag1.SymmetricDifferenceWith(bag2);
InterfaceTests.TestReadWriteCollectionGeneric(bag1, new int[] { 1, 1, 2, 2, 4, 5, 6, 7, 7, 7, 7, 8, 11, 11, 13, 15, 17, 17, 19 }, true);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag2.SymmetricDifferenceWith(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag2, new int[] { 1, 1, 2, 2, 4, 5, 6, 7, 7, 7, 7, 8, 11, 11, 13, 15, 17, 17, 19 }, true);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag3 = bag1.SymmetricDifference(bag2);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 1, 2, 2, 4, 5, 6, 7, 7, 7, 7, 8, 11, 11, 13, 15, 17, 17, 19 }, true);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag3 = bag2.SymmetricDifference(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 1, 2, 2, 4, 5, 6, 7, 7, 7, 7, 8, 11, 11, 13, 15, 17, 17, 19 }, true);
// Make sure intersection with itself works.
bag1 = bagDigits.Clone();
bag1.SymmetricDifferenceWith(bag1);
Assert.AreEqual(0, bag1.Count);
bag1 = bagDigits.Clone();
bag3 = bag1.SymmetricDifference(bag1);
Assert.AreEqual(0, bag3.Count);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -