📄 bagtests.cs
字号:
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()
{
Bag<double> bag1 = new Bag<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()
{
Bag<double> bag1 = new Bag<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()
{
Bag<double> bag1 = new Bag<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()
{
Bag<double> bag1 = new Bag<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 }, false);
bag1 = new Bag<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 }, false);
bag1 = new Bag<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()
{
Bag<double> bag1 = new Bag<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 };
InterfaceTests.TestEnumerableElementsAnyOrder(bag1.FindAll(delegate(double d) { return Math.Abs(d) > 5; }), expected);
}
[Test]
public void IsDisjointFrom()
{
Bag<int> bag1 = new Bag<int>(new int[] { 3, 6, 7, 1, 1, 11, 9, 3, 8 });
Bag<int> bag2 = new Bag<int>();
Bag<int> bag3 = new Bag<int>();
Bag<int> bag4 = new Bag<int>(new int[] { 8, 9, 1, 8, 3, 7, 6, 11, 7 });
Bag<int> bag5 = new Bag<int>(new int[] { 17, 3, 12, 10, 22 });
Bag<int> bag6 = new Bag<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()
{
Bag<int> bagOdds = new Bag<int>(new int[] { 1, 1, 1, 3, 3, 3, 5, 7, 7, 9, 11, 11, 13, 15, 17, 17, 19 });
Bag<int> bagDigits = new Bag<int>(new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 });
Bag<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 }, false);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag2.IntersectionWith(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag2, new int[] { 1, 3, 3, 3, 5, 7, 7, 9 }, false);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag3 = bag1.Intersection(bag2);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 3, 3, 3, 5, 7, 7, 9 }, false);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag3 = bag2.Intersection(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 3, 3, 3, 5, 7, 7, 9 }, false);
// 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 }, false);
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 }, false);
}
[Test]
public void Union()
{
Bag<int> bagOdds = new Bag<int>(new int[] { 1, 1, 1, 3, 3, 3, 5, 7, 7, 9, 11, 11, 13, 15, 17, 17, 19 });
Bag<int> bagDigits = new Bag<int>(new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 });
Bag<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 }, false);
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 }, false);
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 }, false);
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 }, false);
// 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 }, false);
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 }, false);
}
[Test]
public void Sum()
{
Bag<int> bagOdds = new Bag<int>(new int[] { 1, 1, 1, 3, 3, 3, 5, 7, 7, 9, 11, 11, 13, 15, 17, 17, 19 });
Bag<int> bagDigits = new Bag<int>(new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 });
Bag<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 }, false);
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 }, false);
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 }, false);
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 }, false);
// 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 }, false);
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 }, false);
}
[Test]
public void SymmetricDifference()
{
Bag<int> bagOdds = new Bag<int>(new int[] { 1, 1, 1, 3, 3, 3, 5, 7, 7, 9, 11, 11, 13, 15, 17, 17, 19 });
Bag<int> bagDigits = new Bag<int>(new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 });
Bag<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 }, false);
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 }, false);
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 }, false);
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 }, false);
// 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);
}
[Test]
public void Difference()
{
Bag<int> bagOdds = new Bag<int>(new int[] { 1, 1, 1, 3, 3, 3, 5, 7, 7, 9, 11, 11, 13, 15, 17, 17, 19 });
Bag<int> bagDigits = new Bag<int>(new int[] { 1, 2, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9 });
Bag<int> bag1, bag2, bag3;
// Algorithms work different depending on sizes, so try both ways.
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag1.DifferenceWith(bag2);
InterfaceTests.TestReadWriteCollectionGeneric(bag1, new int[] { 1, 1, 11, 11, 13, 15, 17, 17, 19 }, false);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag2.DifferenceWith(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag2, new int[] { 2, 2, 4, 5, 6, 7, 7, 7, 7, 8 }, false);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag3 = bag1.Difference(bag2);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 1, 1, 11, 11, 13, 15, 17, 17, 19 }, false);
bag1 = bagOdds.Clone(); bag2 = bagDigits.Clone();
bag3 = bag2.Difference(bag1);
InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { 2, 2, 4, 5, 6, 7, 7, 7, 7, 8 }, false);
// Make sure intersection with itself works.
bag1 = bagDigits.Clone();
bag1.DifferenceWith(bag1);
Assert.AreEqual(0, bag1.Count);
bag1 = bagDigits.Clone();
bag3 = bag1.Difference(bag1);
Assert.AreEqual(0, bag3.Count);
}
[Test]
public void Subset()
{
Bag<int> set1 = new Bag<int>(new int[] { 1, 1, 3, 6, 6, 6, 6, 7, 8, 9, 9 });
Bag<int> set2 = new Bag<int>();
Bag<int> set3 = new Bag<int>(new int[] { 1, 6, 6, 9, 9 });
Bag<int> set4 = new Bag<int>(new int[] { 1, 6, 6, 9, 9 });
Bag<int> set5 = new Bag<int>(new int[] { 1, 1, 3, 6, 6, 6, 7, 7, 8, 9, 9 });
Assert.IsTrue(set1.IsSupersetOf(set2));
Assert.IsTrue(set2.IsSubsetOf(set1));
Assert.IsTrue(set1.IsProperSupersetOf(set2));
Assert.IsTrue(set2.IsProperSubsetOf(set1));
Assert.IsTrue(set1.IsSupersetOf(set3));
Assert.IsTrue(set3.IsSubsetOf(set1));
Assert.IsTrue(set1.IsProperSupersetOf(set3));
Assert.IsTrue(set3.IsProperSubsetOf(set1));
Assert.IsFalse(set3.IsSupersetOf(set1));
Assert.IsFalse(set1.IsSubsetOf(set3));
Assert.IsFalse(set3.IsProperSupersetOf(set1));
Assert.IsFalse(set1.IsProperSubsetOf(set3));
Assert.IsFalse(set1.IsSupersetOf(set5));
Assert.IsFalse(set5.IsSupersetOf(set1));
Assert.IsFalse(set1.IsSubsetOf(set5));
Assert.IsFalse(set5.IsSubsetOf(set1));
Assert.IsFalse(set1.IsProperSupersetOf(set5));
Assert.IsFalse(set5.IsProperSupersetOf(set1));
Assert.IsFalse(set1.IsProperSubsetOf(set5));
Assert.IsFalse(set5.IsProperSubsetOf(set1));
Assert.IsTrue(set3.IsSupersetOf(set4));
Assert.IsTrue(set3.IsSubsetOf(set4));
Assert.IsFalse(set3.IsProperSupersetOf(set4));
Assert.IsFalse(set3.IsProperSubsetOf(set4));
Assert.IsTrue(set1.IsSupersetOf(set1));
Assert.IsTrue(set1.IsSubsetOf(set1));
Assert.IsFalse(set1.IsProperSupersetOf(set1));
Assert.IsFalse(set1.IsProperSubsetOf(set1));
}
[Test]
public void IsEqualTo()
{
Bag<int> set1 = new Bag<int>(new int[] { 1, 1, 3, 6, 6, 6, 6, 7, 8, 9, 9 });
Bag<int> set2 = new Bag<int>();
Bag<int> set3 = new Bag<int>(new int[] { 1, 6, 6, 9, 9 });
Bag<int> set4 = new Bag<int>(new int[] { 1, 6, 6, 9, 9 });
Bag<int> set5 = new Bag<int>(new int[] { 1, 1, 3, 6, 6, 6, 7, 7, 8, 9, 9 });
Bag<int> set6 = new Bag<int>();
Assert.IsFalse(set1.IsEqualTo(set5));
Assert.IsFalse(set5.IsEqualTo(set1));
Assert.IsTrue(set3.IsEqualTo(set4));
Assert.IsTrue(set4.IsEqualTo(set3));
Assert.IsTrue(set1.IsEqualTo(set1));
Assert.IsTrue(set2.IsEqualTo(set6));
Assert.IsFalse(set1.IsEqualTo(set2));
Assert.IsFalse(set2.IsEqualTo(set1));
}
[Test]
public void Clone()
{
Bag<int> bag1 = new Bag<int>(new int[] { 1, 7, 9, 11, 7, 13, 15, -17, 19, -21, 1 });
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -