ln.types/test/WeakValueDictionaryTests.cs

66 lines
1.7 KiB
C#

// /**
// * File: WeakValueDictionaryTests.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using NUnit.Framework;
using System;
using ln.types.collections;
using System.Threading;
namespace ln.types.test
{
[TestFixture()]
public class WeakValueDictionaryTests
{
class Value
{
public readonly Guid ID;
public Value() : this(Guid.NewGuid()) { }
public Value(Guid id) { ID = id; }
public override int GetHashCode() => ID.GetHashCode();
public override bool Equals(object obj) => (obj is Value) && (obj as Value).ID.Equals(ID);
}
Value[] values = new Value[1000];
WeakValueDictionary<int, Value> dict;
private void FillValues()
{
dict = new WeakValueDictionary<int, Value>();
for (int n=0;n<values.Length;n++)
{
values[n] = new Value();
dict.Add(n, values[n]);
}
}
[Test()]
public void TestWeakValueDictionary()
{
FillValues();
Assert.AreEqual(values.Length, dict.Count);
Assert.AreEqual(values.Length, dict.Keys.Count);
Assert.AreEqual(values.Length, dict.Values.Count);
values[0] = null;
values[values.Length - 1] = null;
Thread.Sleep(250);
GC.Collect();
Thread.Sleep(250);
Assert.AreEqual(values.Length-2, dict.Values.Count);
}
}
}