📄 settests.cs
字号:
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()
{
Set<int> set1 = new Set<int>(new int[] { 6, 7, 1, 11, 9, 3, 8 });
Set<int> set2 = new Set<int>();
Set<int> set3 = new Set<int>();
Set<int> set4 = new Set<int>(new int[] { 9, 11, 1, 3, 6, 7, 8, 14 });
Set<int> set5 = new Set<int>(new int[] { 3, 6, 7, 11, 14, 8, 9 });
Set<int> set6 = new Set<int>(new int[] { 1, 3, 6, 7, 8, 10, 11 });
Set<int> set7 = new Set<int>(new int[] { 9, 1, 8, 3, 7, 6, 11 });
Assert.IsTrue(set1.IsEqualTo(set1));
Assert.IsTrue(set2.IsEqualTo(set2));
Assert.IsTrue(set2.IsEqualTo(set3));
Assert.IsTrue(set3.IsEqualTo(set2));
Assert.IsTrue(set1.IsEqualTo(set7));
Assert.IsTrue(set7.IsEqualTo(set1));
Assert.IsFalse(set1.IsEqualTo(set2));
Assert.IsFalse(set2.IsEqualTo(set1));
Assert.IsFalse(set1.IsEqualTo(set4));
Assert.IsFalse(set4.IsEqualTo(set1));
Assert.IsFalse(set1.IsEqualTo(set5));
Assert.IsFalse(set5.IsEqualTo(set1));
Assert.IsFalse(set1.IsEqualTo(set6));
Assert.IsFalse(set6.IsEqualTo(set1));
Assert.IsFalse(set5.IsEqualTo(set6));
Assert.IsFalse(set6.IsEqualTo(set5));
Assert.IsFalse(set5.IsEqualTo(set7));
Assert.IsFalse(set7.IsEqualTo(set5));
}
[Test]
public void IsDisjointFrom()
{
Set<int> set1 = new Set<int>(new int[] { 6, 7, 1, 11, 9, 3, 8 });
Set<int> set2 = new Set<int>();
Set<int> set3 = new Set<int>();
Set<int> set4 = new Set<int>(new int[] { 9, 1, 8, 3, 7, 6, 11 });
Set<int> set5 = new Set<int>(new int[] { 17, 3, 12, 10 });
Set<int> set6 = new Set<int>(new int[] { 19, 14, 0, 2});
Assert.IsFalse(set1.IsDisjointFrom(set1));
Assert.IsTrue(set2.IsDisjointFrom(set2));
Assert.IsTrue(set1.IsDisjointFrom(set2));
Assert.IsTrue(set2.IsDisjointFrom(set1));
Assert.IsTrue(set2.IsDisjointFrom(set3));
Assert.IsTrue(set3.IsDisjointFrom(set2));
Assert.IsFalse(set1.IsDisjointFrom(set4));
Assert.IsFalse(set4.IsDisjointFrom(set1));
Assert.IsFalse(set1.IsDisjointFrom(set5));
Assert.IsFalse(set5.IsDisjointFrom(set1));
Assert.IsTrue(set1.IsDisjointFrom(set6));
Assert.IsTrue(set6.IsDisjointFrom(set1));
Assert.IsTrue(set5.IsDisjointFrom(set6));
Assert.IsTrue(set6.IsDisjointFrom(set5));
}
[Test]
public void Intersection()
{
Set<int> setOdds = new Set<int>(new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 });
Set<int> setDigits = new Set<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
Set<int> set1, set2, set3;
// Algorithms work different depending on sizes, so try both ways.
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set1.IntersectionWith(set2);
InterfaceTests.TestReadWriteCollectionGeneric(set1, new int[] { 1, 3, 5, 7, 9 }, false);
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set2.IntersectionWith(set1);
InterfaceTests.TestReadWriteCollectionGeneric(set2, new int[] { 1, 3, 5, 7, 9 }, false);
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set3 = set1.Intersection(set2);
InterfaceTests.TestReadWriteCollectionGeneric(set3, new int[] { 1, 3, 5, 7, 9 }, false);
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set3 = set2.Intersection(set1);
InterfaceTests.TestReadWriteCollectionGeneric(set3, new int[] { 1, 3, 5, 7, 9 }, false);
// Make sure intersection with itself works.
set1 = setDigits.Clone();
set1.IntersectionWith(set1);
InterfaceTests.TestReadWriteCollectionGeneric(set1, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, false);
set1 = setDigits.Clone();
set3 = set1.Intersection(set1);
InterfaceTests.TestReadWriteCollectionGeneric(set3, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, false);
}
[Test]
public void Union()
{
Set<int> setOdds = new Set<int>(new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 });
Set<int> setDigits = new Set<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
Set<int> set1, set2, set3;
// Algorithms work different depending on sizes, so try both ways.
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set1.UnionWith(set2);
InterfaceTests.TestReadWriteCollectionGeneric(set1, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 17, 19, 21, 23, 25 }, false);
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set2.UnionWith(set1);
InterfaceTests.TestReadWriteCollectionGeneric(set2, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 17, 19, 21, 23, 25 }, false);
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set3 = set1.Union(set2);
InterfaceTests.TestReadWriteCollectionGeneric(set3, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 17, 19, 21, 23, 25 }, false);
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set3 = set2.Union(set1);
InterfaceTests.TestReadWriteCollectionGeneric(set3, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 17, 19, 21, 23, 25 }, false);
// Make sure intersection with itself works.
set1 = setDigits.Clone();
set1.UnionWith(set1);
InterfaceTests.TestReadWriteCollectionGeneric(set1, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, false);
set1 = setDigits.Clone();
set3 = set1.Union(set1);
InterfaceTests.TestReadWriteCollectionGeneric(set3, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, false);
}
[Test]
public void SymmetricDifference()
{
Set<int> setOdds = new Set<int>(new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 });
Set<int> setDigits = new Set<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
Set<int> set1, set2, set3;
// Algorithms work different depending on sizes, so try both ways.
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set1.SymmetricDifferenceWith(set2);
InterfaceTests.TestReadWriteCollectionGeneric(set1, new int[] { 2, 4, 6, 8, 11, 13, 15, 17, 19, 21, 23, 25 }, false);
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set2.SymmetricDifferenceWith(set1);
InterfaceTests.TestReadWriteCollectionGeneric(set2, new int[] { 2, 4, 6, 8, 11, 13, 15, 17, 19, 21, 23, 25 }, false);
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set3 = set1.SymmetricDifference(set2);
InterfaceTests.TestReadWriteCollectionGeneric(set3, new int[] { 2, 4, 6, 8, 11, 13, 15, 17, 19, 21, 23, 25 }, false);
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set3 = set2.SymmetricDifference(set1);
InterfaceTests.TestReadWriteCollectionGeneric(set3, new int[] { 2, 4, 6, 8, 11, 13, 15, 17, 19, 21, 23, 25 }, false);
// Make sure intersection with itself works.
set1 = setDigits.Clone();
set1.SymmetricDifferenceWith(set1);
Assert.AreEqual(0, set1.Count);
set1 = setDigits.Clone();
set3 = set1.SymmetricDifference(set1);
Assert.AreEqual(0, set3.Count);
}
[Test]
public void Difference()
{
Set<int> setOdds = new Set<int>(new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 });
Set<int> setDigits = new Set<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
Set<int> set1, set2, set3;
// Algorithms work different depending on sizes, so try both ways.
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set1.DifferenceWith(set2);
InterfaceTests.TestReadWriteCollectionGeneric(set1, new int[] { 11, 13, 15, 17, 19, 21, 23, 25 }, false);
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set2.DifferenceWith(set1);
InterfaceTests.TestReadWriteCollectionGeneric(set2, new int[] { 2, 4, 6, 8 }, false);
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set3 = set1.Difference(set2);
InterfaceTests.TestReadWriteCollectionGeneric(set3, new int[] { 11, 13, 15, 17, 19, 21, 23, 25 }, false);
set1 = setOdds.Clone(); set2 = setDigits.Clone();
set3 = set2.Difference(set1);
InterfaceTests.TestReadWriteCollectionGeneric(set3, new int[] { 2, 4, 6, 8 }, false);
// Make sure intersection with itself works.
set1 = setDigits.Clone();
set1.DifferenceWith(set1);
Assert.AreEqual(0, set1.Count);
set1 = setDigits.Clone();
set3 = set1.Difference(set1);
Assert.AreEqual(0, set3.Count);
}
[Test, ExpectedException(typeof(InvalidOperationException))]
public void InconsistentComparisons1()
{
Set<int> setOdds = new Set<int>(new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 });
Set<int> setDigits = new Set<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, new GOddEvenEqualityComparer());
setOdds.SymmetricDifferenceWith(setDigits);
}
[Test, ExpectedException(typeof(InvalidOperationException))]
public void InconsistentComparisons2()
{
Set<string> set1 = new Set<string>(new string[] { "foo", "Bar" }, StringComparer.CurrentCulture);
Set<string> set2 = new Set<string>(new string[] { "bada", "bing" }, StringComparer.InvariantCulture);
set1.Intersection(set2);
}
[Test]
public void ConsistentComparisons()
{
Set<string> set1 = new Set<string>(new string[] { "foo", "Bar" }, StringComparer.InvariantCulture);
Set<string> set2 = new Set<string>(new string[] { "bada", "bing" }, StringComparer.InvariantCulture);
set1.Difference(set2);
}
[Test]
public void SerializeStrings()
{
Set<string> d = new Set<string>();
d.Add("foo");
d.Add("world");
d.Add("hello");
d.Add("elvis");
d.Add("elvis");
d.Add(null);
d.Add("cool");
d.AddMany(new string[] { "1", "2", "3", "4", "5", "6" });
d.AddMany(new string[] { "7", "8", "9", "10", "11", "12" });
Set<string> result = (Set<string>)InterfaceTests.SerializeRoundTrip(d);
InterfaceTests.TestReadWriteCollectionGeneric<string>((ICollection<string>)result, new string[] { "1", "2", "3", "4", "5", "6", "cool", "elvis", "hello", "foo", "world", null, "7", "8", "9", "10", "11", "12" }, false);
}
[Serializable]
class UniqueStuff
{
public InterfaceTests.Unique[] objects;
public Set<InterfaceTests.Unique> set;
}
[Test]
public void SerializeUnique()
{
UniqueStuff d = new UniqueStuff(), result = new UniqueStuff();
d.objects = new InterfaceTests.Unique[] {
new InterfaceTests.Unique("1"), new InterfaceTests.Unique("2"), new InterfaceTests.Unique("3"), new InterfaceTests.Unique("4"), new InterfaceTests.Unique("5"), new InterfaceTests.Unique("6"),
new InterfaceTests.Unique("cool"), new InterfaceTests.Unique("elvis"), new InterfaceTests.Unique("hello"), new InterfaceTests.Unique("foo"), new InterfaceTests.Unique("world"), new InterfaceTests.Unique("elvis"), new InterfaceTests.Unique(null), null,
new InterfaceTests.Unique("7"), new InterfaceTests.Unique("8"), new InterfaceTests.Unique("9"), new InterfaceTests.Unique("10"), new InterfaceTests.Unique("11"), new InterfaceTests.Unique("12") };
d.set = new Set<InterfaceTests.Unique>();
d.set.Add(d.objects[9]);
d.set.Add(d.objects[10]);
d.set.Add(d.objects[8]);
d.set.Add(d.objects[11]);
d.set.Add(d.objects[7]);
d.set.Add(d.objects[12]);
d.set.Add(d.objects[6]);
d.set.Add(d.objects[13]);
d.set.AddMany(new InterfaceTests.Unique[] { d.objects[0], d.objects[1], d.objects[2], d.objects[3], d.objects[4], d.objects[5] });
d.set.AddMany(new InterfaceTests.Unique[] { d.objects[14], d.objects[15], d.objects[16], d.objects[17], d.objects[18], d.objects[19] });
result = (UniqueStuff)InterfaceTests.SerializeRoundTrip(d);
InterfaceTests.TestReadWriteCollectionGeneric < InterfaceTests.Unique>(result.set, result.objects, false);
for (int i = 0; i < result.objects.Length; ++i) {
if (result.objects[i] != null)
Assert.IsFalse(object.Equals(result.objects[i], d.objects[i]));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -