-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathWeakHashtableFixture.cs
117 lines (94 loc) · 2.45 KB
/
WeakHashtableFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using NHibernate.Util;
using NUnit.Framework;
namespace NHibernate.Test.UtilityTest
{
[TestFixture]
public class WeakHashtableFixture
{
protected WeakHashtable Create()
{
return new WeakHashtable();
}
[Test]
public void Basic()
{
// Keep references to the key and the value
object key = new object();
object value = new object();
WeakHashtable table = Create();
table[key] = value;
Assert.AreSame(value, table[key]);
}
[Test]
public void WeakReferenceGetsFreedButHashCodeRemainsConstant()
{
object obj = new object();
WeakRefWrapper wr = new WeakRefWrapper(obj);
int hashCode = wr.GetHashCode();
obj = null;
GC.Collect();
Assert.IsFalse(wr.IsAlive);
Assert.IsNull(wr.Target);
Assert.AreEqual(hashCode, wr.GetHashCode());
}
[Test]
public void Scavenging()
{
WeakHashtable table = Create();
table[new object()] = new object();
table[new object()] = new object();
GC.Collect();
table.Scavenge();
Assert.AreEqual(0, table.Count);
}
[Test]
public void IterationAfterGC()
{
WeakHashtable table = Create();
table[new object()] = new object();
table[new object()] = new object();
GC.Collect();
Assert.AreEqual(2, table.Count, "should not have been scavenged yet");
Assert.IsFalse(table.GetEnumerator().MoveNext(), "should not have live elements");
}
[Test]
public void Iteration()
{
object key = new object();
object value = new object();
WeakHashtable table = Create();
table[key] = value;
foreach (DictionaryEntry de in table)
{
Assert.AreSame(key, de.Key);
Assert.AreSame(value, de.Value);
}
}
[Test]
public void RetrieveNonExistentItem()
{
WeakHashtable table = Create();
object obj = table[new object()];
Assert.IsNull(obj);
}
[Test]
public void WeakRefWrapperEquals()
{
object obj = new object();
Assert.AreEqual(new WeakRefWrapper(obj), new WeakRefWrapper(obj));
Assert.IsFalse(new WeakRefWrapper(obj).Equals(null));
Assert.IsFalse(new WeakRefWrapper(obj).Equals(10));
}
[Test]
public void IsSerializable()
{
WeakHashtable weakHashtable = new WeakHashtable();
weakHashtable.Add("key", new object());
NHAssert.IsSerializable(weakHashtable);
}
}
}