ln.types/test/WeakKeyDictionaryTests.cs

67 lines
1.2 KiB
C#

using System;
using NUnit.Framework;
using ln.types.collections;
using System.Threading;
namespace ln.types.test
{
[TestFixture]
public class WeakKeyDictionaryTests
{
class KeyClass
{
static int next = 1;
public readonly int Value = next++;
public override int GetHashCode() => Value;
public override bool Equals(object obj)
{
return ((obj is KeyClass) && ((obj as KeyClass).Value == Value));
}
public override string ToString()
{
return string.Format("[KeyClass Value={0}]",Value);
}
}
KeyClass[] keys = new KeyClass[1024];
WeakKeyDictionary<KeyClass, int> testDict = new WeakKeyDictionary<KeyClass, int>();
public void Fill()
{
for (int n = 0; n < keys.Length; n++)
{
keys[n] = new KeyClass();
testDict.Add(keys[n], n);
}
}
public void Strip()
{
keys[0] = null;
keys[keys.Length - 1] = null;
}
[TestCase()]
public void TestWeakKeyDictionary()
{
Fill();
Thread.Sleep(250);
Assert.AreEqual(keys.Length, testDict.Count);
Assert.AreEqual(keys.Length, testDict.Keys.Count);
Strip();
Thread.Sleep(250);
GC.Collect();
Thread.Sleep(250);
Assert.AreEqual(keys.Length-2, testDict.Keys.Count);
}
}
}