forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionHelperFixture.cs
308 lines (264 loc) · 9.48 KB
/
CollectionHelperFixture.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.Util;
using NUnit.Framework;
namespace NHibernate.Test.UtilityTest
{
[TestFixture]
public class CollectionHelperFixture
{
#region Bag
[Test]
public void BagComparedToSelfShouldBeEqual()
{
var c1 = new List<string> { "1", "2", "3", "4" };
Assert.That(CollectionHelper.BagEquals(c1, c1), Is.True);
}
[Test]
public void BagComparedToNullShouldBeInequal()
{
var c1 = new List<string> { "1", "2", "3", "4" };
Assert.That(CollectionHelper.BagEquals(c1, null), Is.False);
Assert.That(CollectionHelper.BagEquals(null, c1), Is.False);
}
[Test]
public void BagNullComparedToNullShouldBeEqual()
{
Assert.That(CollectionHelper.BagEquals<string>(null, null), Is.True);
}
[Test]
public void BagsWithSameContentShouldBeEqual()
{
var c1 = new List<string> { "1", "2", "3", "4", "2" };
var c2 = new List<string> { "1", "2", "3", "4", "2" };
Assert.That(CollectionHelper.BagEquals(c1, c2), Is.True);
}
[Test]
public void BagsWithSameCountButDistinctValuesShouldBeInequal()
{
var c1 = new List<string> { "1", "2", "3", "4" };
var c2 = new List<string> { "1", "2", "3", "3" };
Assert.That(CollectionHelper.BagEquals(c1, c2), Is.False);
}
[Test]
public void BagsWithSameElementsButDistinctOrderShouldBeEqual()
{
var c1 = new List<string> { "1", "2", "3", "4", "2" };
var c2 = new List<string> { "2", "1", "2", "4", "3" };
Assert.That(CollectionHelper.BagEquals(c1, c2), Is.True);
}
[Test]
public void BagsWithoutSameCountShouldBeInequal()
{
var c1 = new List<string> { "1", "2", "2", "1" };
var c2 = new List<string> { "1", "2" };
Assert.That(CollectionHelper.BagEquals(c1, c2), Is.False);
Assert.That(CollectionHelper.BagEquals(c2, c1), Is.False);
}
#endregion
#region Dictionary/Map
[Test]
public void MapComparedToSelfShouldBeEqual()
{
var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } };
Assert.That(CollectionHelper.DictionaryEquals<string, string>(d1, d1), Is.True);
}
[Test]
public void MapComparedToNullShouldBeInequal()
{
var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } };
Assert.That(CollectionHelper.DictionaryEquals<string, string>(d1, null), Is.False);
Assert.That(CollectionHelper.DictionaryEquals<string, string>(null, d1), Is.False);
}
[Test]
public void MapNullComparedToNullShouldBeEqual()
{
Assert.That(CollectionHelper.DictionaryEquals<string, string>(null, null), Is.True);
}
[Test]
public void MapsWithSameContentShouldBeEqual()
{
var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } };
var d2 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } };
Assert.That(CollectionHelper.DictionaryEquals<string, string>(d1, d2), Is.True);
}
// Fails till NH-3981 is fixed.
[Test]
public void MapsWithSameCountButDistinctKeysShouldBeInequal()
{
var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } };
var d2 = new Dictionary<string, string> { { "1", "2" }, { "4", "3" } };
Assert.That(CollectionHelper.DictionaryEquals<string, string>(d1, d2), Is.False);
}
[Test]
public void MapsWithSameCountButDistinctValuesShouldBeInequal()
{
var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } };
var d2 = new Dictionary<string, string> { { "1", "2" }, { "3", "3" } };
Assert.That(CollectionHelper.DictionaryEquals<string, string>(d1, d2), Is.False);
}
[Test]
public void MapsWithoutSameCountShouldBeInequal()
{
var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } };
var d2 = new Dictionary<string, string> { { "1", "2" } };
Assert.That(CollectionHelper.DictionaryEquals<string, string>(d1, d2), Is.False);
Assert.That(CollectionHelper.DictionaryEquals<string, string>(d2, d1), Is.False);
}
#endregion
#region Set
[Test]
public void SetComparedToSelfShouldBeEqual()
{
var c1 = new HashSet<string> { "1", "2", "3", "4" };
Assert.That(CollectionHelper.SetEquals(c1, c1), Is.True);
}
[Test]
public void SetComparedToNullShouldBeInequal()
{
var c1 = new HashSet<string> { "1", "2", "3", "4" };
Assert.That(CollectionHelper.SetEquals(c1, null), Is.False);
Assert.That(CollectionHelper.SetEquals(null, c1), Is.False);
}
[Test]
public void SetNullComparedToNullShouldBeEqual()
{
Assert.That(CollectionHelper.SetEquals<string>(null, null), Is.True);
}
[Test]
public void SetsWithSameContentShouldBeEqual()
{
var c1 = new HashSet<string> { "1", "2", "3", "4" };
var c2 = new HashSet<string> { "1", "2", "3", "4" };
Assert.That(CollectionHelper.SetEquals(c1, c2), Is.True);
}
[Test]
public void SetsWithSameCountButDistinctValuesShouldBeInequal()
{
var c1 = new HashSet<string> { "1", "2", "3", "4" };
var c2 = new HashSet<string> { "1", "2", "3", "5" };
Assert.That(CollectionHelper.SetEquals(c1, c2), Is.False);
}
[Test]
public void SetsWithSameElementsButDistinctOrderShouldBeEqual()
{
var c1 = new HashSet<string> { "1", "2", "3", "4" };
var c2 = new HashSet<string> { "1", "2", "4", "3" };
Assert.That(CollectionHelper.SetEquals(c1, c2), Is.True);
}
[Test]
public void SetsWithoutSameCountShouldBeInequal()
{
var c1 = new HashSet<string> { "1", "2", "3", "4" };
var c2 = new HashSet<string> { "1", "2" };
Assert.That(CollectionHelper.SetEquals(c1, c2), Is.False);
Assert.That(CollectionHelper.SetEquals(c2, c1), Is.False);
}
[Test]
public void MapsWithSameContentShouldHaveSameHashCode()
{
var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } };
var d2 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } };
Assert.That(
CollectionHelper.GetHashCode(d1, KvpStringComparer.Instance),
Is.EqualTo(CollectionHelper.GetHashCode(d2, KvpStringComparer.Instance)));
}
// Failure of following tests is not an error from GetHashCode semantic viewpoint, but it causes it
// to be potentially inefficients for usages in dictionnaries or hashset.
[Test]
public void MapsWithSameCountButDistinctKeysShouldNotHaveSameHashCode()
{
var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } };
var d2 = new Dictionary<string, string> { { "1", "2" }, { "4", "3" } };
Assert.That(
CollectionHelper.GetHashCode(d1, KvpStringComparer.Instance),
Is.Not.EqualTo(CollectionHelper.GetHashCode(d2, KvpStringComparer.Instance)));
}
[Test]
public void MapsWithSameCountButDistinctValuesShouldNotHaveSameHashCode()
{
var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } };
var d2 = new Dictionary<string, string> { { "1", "2" }, { "3", "3" } };
Assert.That(
CollectionHelper.GetHashCode(d1, KvpStringComparer.Instance),
Is.Not.EqualTo(CollectionHelper.GetHashCode(d2, KvpStringComparer.Instance)));
}
[Test]
public void MapsWithoutSameCountShouldNotHaveSameHashCode()
{
var d1 = new Dictionary<string, string> { { "1", "2" }, { "3", "4" } };
var d2 = new Dictionary<string, string> { { "1", "2" } };
Assert.That(
CollectionHelper.GetHashCode(d1, KvpStringComparer.Instance),
Is.Not.EqualTo(CollectionHelper.GetHashCode(d2, KvpStringComparer.Instance)));
}
internal class KvpStringComparer : IEqualityComparer<KeyValuePair<string, string>>
{
public static readonly KvpStringComparer Instance = new KvpStringComparer();
public bool Equals(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
{
return StringComparer.Ordinal.Equals(x.Key, y.Key) &&
StringComparer.Ordinal.Equals(x.Value, y.Value);
}
public int GetHashCode(KeyValuePair<string, string> obj)
{
unchecked
{
return 397 * StringComparer.Ordinal.GetHashCode(obj.Key) ^
(obj.Value != null ? StringComparer.Ordinal.GetHashCode(obj.Value) : 0);
}
}
}
#endregion
#region List/Sequence
[Test]
public void SequenceComparedToSelfShouldBeEqual()
{
var c1 = new List<string> { "1", "2", "3", "4" };
Assert.That(CollectionHelper.SequenceEquals(c1, c1), Is.True);
}
[Test]
public void SequenceComparedToNullShouldBeInequal()
{
var c1 = new List<string> { "1", "2", "3", "4" };
Assert.That(CollectionHelper.SequenceEquals(c1, null), Is.False);
Assert.That(CollectionHelper.SequenceEquals(null, c1), Is.False);
}
[Test]
public void SequenceNullComparedToNullShouldBeEqual()
{
Assert.That(CollectionHelper.SequenceEquals<string>(null, null), Is.True);
}
[Test]
public void SequencesWithSameContentShouldBeEqual()
{
var c1 = new List<string> { "1", "2", "3", "4" };
var c2 = new List<string> { "1", "2", "3", "4" };
Assert.That(CollectionHelper.SequenceEquals(c1, c2), Is.True);
}
[Test]
public void SequencesWithSameCountButDistinctValuesShouldBeInequal()
{
var c1 = new List<string> { "1", "2", "3", "4" };
var c2 = new List<string> { "1", "2", "3", "3" };
Assert.That(CollectionHelper.SequenceEquals(c1, c2), Is.False);
}
[Test]
public void SequencesWithSameElementsButDistinctOrderShouldBeInequal()
{
var c1 = new List<string> { "1", "2", "3", "4" };
var c2 = new List<string> { "1", "2", "4", "3" };
Assert.That(CollectionHelper.SequenceEquals(c1, c2), Is.False);
}
[Test]
public void SequencesWithoutSameCountShouldBeInequal()
{
var c1 = new List<string> { "1", "2", "3", "4" };
var c2 = new List<string> { "1", "2" };
Assert.That(CollectionHelper.SequenceEquals(c1, c2), Is.False);
Assert.That(CollectionHelper.SequenceEquals(c2, c1), Is.False);
}
#endregion
}
}