📄 orderedsettests.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;
#endregion
namespace Wintellect.PowerCollections.Tests
{
[TestFixture]
public class OrderedSetTests
{
class ComparableClass1 : IComparable<ComparableClass1>
{
public int Value = 0;
int IComparable<ComparableClass1>.CompareTo(ComparableClass1 other)
{
if (Value > other.Value)
return 1;
else if (Value < other.Value)
return -1;
else
return 0;
}
}
class ComparableClass2 : IComparable
{
public int Value = 0;
int IComparable.CompareTo(object other)
{
if (other is ComparableClass2) {
ComparableClass2 o = (ComparableClass2)other;
if (Value > o.Value)
return 1;
else if (Value < o.Value)
return -1;
else
return 0;
}
else
throw new ArgumentException("Argument of wrong type.", "other");
}
}
// Not comparable, because the type parameter on ComparableClass is incorrect.
class UncomparableClass1 : IComparable<ComparableClass1>
{
public int Value = 0;
int IComparable<ComparableClass1>.CompareTo(ComparableClass1 other)
{
if (Value > other.Value)
return 1;
else if (Value < other.Value)
return -1;
else
return 0;
}
}
class UncomparableClass2
{
public int Value = 0;
}
[Test]
public void RandomAddDelete() {
const int SIZE = 5000;
bool[] present = new bool[SIZE];
Random rand = new Random();
OrderedSet<int> set1 = new OrderedSet<int>();
bool b;
// Add and delete values at random.
for (int i = 0; i < SIZE * 10; ++i) {
int v = rand.Next(SIZE);
if (present[v]) {
Assert.IsTrue(set1.Contains(v));
b = set1.Remove(v);
Assert.IsTrue(b);
present[v] = false;
}
else {
Assert.IsFalse(set1.Contains(v));
b = set1.Add(v);
Assert.IsFalse(b);
present[v] = true;
}
}
// Make sure the set has all the correct values in order.
int last = -1;
int index = 0;
foreach (int v in set1) {
Assert.IsTrue(v > last);
Assert.AreEqual(v, set1[index]);
Assert.AreEqual(index, set1.IndexOf(v));
for (int i = last + 1; i < v; ++i)
Assert.IsFalse(present[i]);
Assert.IsTrue(present[v]);
last = v;
++index;
}
int count = 0;
foreach (bool x in present)
if (x)
++count;
Assert.AreEqual(count, set1.Count);
int[] vals = new int[count];
int j = 0;
for (int i = 0; i < present.Length; ++i)
if (present[i])
vals[j++] = i;
InterfaceTests.TestReadOnlyListGeneric<int>(set1.AsList(), vals, null);
int[] array = set1.ToArray();
Assert.IsTrue(Algorithms.EqualCollections(array, vals));
}
[Test]
public void ICollectionInterface()
{
string[] s_array = {"Foo", "Eric", "Clapton", "hello", "goodbye", "C#"};
OrderedSet<string> set1 = new OrderedSet<string>();
foreach (string s in s_array)
set1.Add(s);
Array.Sort(s_array);
InterfaceTests.TestCollection<string>((ICollection) set1, s_array, true);
}
[Test]
public void GenericICollectionInterface()
{
string[] s_array = { "Foo", "Eric", "Clapton", "hello", "goodbye", "C#", "Java" };
OrderedSet<string> set1 = new OrderedSet<string>();
foreach (string s in s_array)
set1.Add(s);
Array.Sort(s_array);
InterfaceTests.TestReadWriteCollectionGeneric<string>((ICollection<string>)set1, s_array, true, null);
}
[Test]
public void Add()
{
OrderedSet<string> set1 = new OrderedSet<string>(StringComparer.InvariantCultureIgnoreCase);
bool b;
b = set1.Add("hello"); Assert.IsFalse(b);
b = set1.Add("foo"); Assert.IsFalse(b);
b = set1.Add(""); Assert.IsFalse(b);
b = set1.Add("HELLO"); Assert.IsTrue(b);
b = set1.Add("foo"); Assert.IsTrue(b);
b = set1.Add(null); Assert.IsFalse(b);
b = set1.Add("Hello"); Assert.IsTrue(b);
b = set1.Add("Eric"); Assert.IsFalse(b);
b = set1.Add(null); Assert.IsTrue(b);
InterfaceTests.TestReadWriteCollectionGeneric(set1, new string[] { null, "", "Eric", "foo", "Hello" }, true, null);
}
[Test]
public void GetItemByIndex()
{
OrderedSet<string> set1 = new OrderedSet<string>(StringComparer.InvariantCultureIgnoreCase);
bool b;
b = set1.Add("hello"); Assert.IsFalse(b);
b = set1.Add("foo"); Assert.IsFalse(b);
b = set1.Add(""); Assert.IsFalse(b);
b = set1.Add("HELLO"); Assert.IsTrue(b);
b = set1.Add("foo"); Assert.IsTrue(b);
b = set1.Add(null); Assert.IsFalse(b);
b = set1.Add("Hello"); Assert.IsTrue(b);
b = set1.Add("Eric"); Assert.IsFalse(b);
b = set1.Add(null); Assert.IsTrue(b);
Assert.AreEqual(set1[0], null);
Assert.AreEqual(set1[1], "");
Assert.AreEqual(set1[2], "Eric");
Assert.AreEqual(set1[3], "foo");
Assert.AreEqual(set1[4], "Hello");
try {
string s = set1[-1];
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
string s = set1[5];
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
string s = set1[Int32.MaxValue];
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
try {
string s = set1[Int32.MinValue];
Assert.Fail("Should throw");
}
catch (Exception e) {
Assert.IsTrue(e is ArgumentOutOfRangeException);
}
}
[Test]
public void IndexOf()
{
OrderedSet<string> set1 = new OrderedSet<string>(StringComparer.InvariantCultureIgnoreCase);
bool b;
b = set1.Add("hello"); Assert.IsFalse(b);
b = set1.Add("foo"); Assert.IsFalse(b);
b = set1.Add(""); Assert.IsFalse(b);
b = set1.Add("HELLO"); Assert.IsTrue(b);
b = set1.Add("foo"); Assert.IsTrue(b);
b = set1.Add(null); Assert.IsFalse(b);
b = set1.Add("Hello"); Assert.IsTrue(b);
b = set1.Add("Eric"); Assert.IsFalse(b);
b = set1.Add(null); Assert.IsTrue(b);
Assert.AreEqual(0, set1.IndexOf(null));
Assert.AreEqual(1, set1.IndexOf(""));
Assert.AreEqual(2, set1.IndexOf("ERIC"));
Assert.AreEqual(3, set1.IndexOf("foo"));
Assert.AreEqual(4, set1.IndexOf("HELlo"));
Assert.AreEqual(-1, set1.IndexOf("goodbye"));
Assert.AreEqual(-1, set1.IndexOf("zipf"));
}
[Test]
public void AsList()
{
OrderedSet<string> set1 = new OrderedSet<string>(StringComparer.InvariantCultureIgnoreCase);
bool b;
b = set1.Add("hello"); Assert.IsFalse(b);
b = set1.Add("foo"); Assert.IsFalse(b);
b = set1.Add(""); Assert.IsFalse(b);
b = set1.Add("HELLO"); Assert.IsTrue(b);
b = set1.Add("foo"); Assert.IsTrue(b);
b = set1.Add(null); Assert.IsFalse(b);
b = set1.Add("Hello"); Assert.IsTrue(b);
b = set1.Add("Eric"); Assert.IsFalse(b);
b = set1.Add(null); Assert.IsTrue(b);
InterfaceTests.TestReadOnlyListGeneric(set1.AsList(), new string[] { null, "", "Eric", "foo", "Hello" }, null);
OrderedSet<string> set2 = new OrderedSet<string>(StringComparer.InvariantCultureIgnoreCase);
InterfaceTests.TestReadOnlyListGeneric(set2.AsList(), new string[] { }, null);
}
[Test]
public void CountAndClear()
{
OrderedSet<string> set1 = new OrderedSet<string>(StringComparer.InvariantCultureIgnoreCase);
Assert.AreEqual(0, set1.Count);
set1.Add("hello"); Assert.AreEqual(1, set1.Count);
set1.Add("foo"); Assert.AreEqual(2, set1.Count);
set1.Add(""); Assert.AreEqual(3, set1.Count);
set1.Add("HELLO"); Assert.AreEqual(3, set1.Count);
set1.Add("foo"); Assert.AreEqual(3, set1.Count);
set1.Add(null); Assert.AreEqual(4, set1.Count);
set1.Add("Hello"); Assert.AreEqual(4, set1.Count);
set1.Add("Eric"); Assert.AreEqual(5, set1.Count);
set1.Add(null); Assert.AreEqual(5, set1.Count);
set1.Clear();
Assert.AreEqual(0, set1.Count);
bool found = false;
foreach (string s in set1)
found = true;
Assert.IsFalse(found);
}
[Test]
public void Remove()
{
OrderedSet<string> set1 = new OrderedSet<string>(StringComparer.InvariantCultureIgnoreCase);
bool b;
b = set1.Remove("Eric"); Assert.IsFalse(b);
b = set1.Add("hello"); Assert.IsFalse(b);
b = set1.Add("foo"); Assert.IsFalse(b);
b = set1.Add(""); Assert.IsFalse(b);
b = set1.Remove("HELLO"); Assert.IsTrue(b);
b = set1.Remove("hello"); Assert.IsFalse(b);
b = set1.Remove(null); Assert.IsFalse(b);
b = set1.Add("Hello"); Assert.IsFalse(b);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -