forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSequencedHashMapFixture.cs
447 lines (367 loc) · 10.9 KB
/
SequencedHashMapFixture.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using NHibernate.Util;
using NUnit.Framework;
namespace NHibernate.Test.UtilityTest
{
/// <summary>
/// Summary description for SequencedHashMapFixture.
/// </summary>
[TestFixture]
public class SequencedHashMapFixture
{
private SequencedHashMap _shm;
private SequencedHashMap _emptyShm;
private IList _expectedKeys;
private IList _expectedValues;
[SetUp]
public void SetUp()
{
_shm = new SequencedHashMap();
_emptyShm = new SequencedHashMap();
_expectedKeys = new ArrayList();
_expectedKeys.Add("test1");
_expectedKeys.Add("test2");
_expectedKeys.Add("test3");
_expectedValues = new ArrayList();
_expectedValues.Add(1);
_expectedValues.Add("2");
_expectedValues.Add(true);
for (int i = 0; i < _expectedKeys.Count; i++)
{
_shm[_expectedKeys[i]] = _expectedValues[i];
}
}
[Test]
public void Add()
{
object newKey = "test4";
object newValue = "test4's value";
_expectedKeys.Add(newKey);
_expectedValues.Add(newValue);
_shm.Add(newKey, newValue);
int i = 0;
foreach (DictionaryEntry de in _shm)
{
Assert.AreEqual(_expectedKeys[i], de.Key);
Assert.AreEqual(_expectedValues[i], de.Value);
i++;
}
Assert.AreEqual(4, i, "Did not loop through 4 items");
Assert.AreEqual(4, _shm.Count);
}
[Test]
public void Clear()
{
_shm.Clear();
Assert.AreEqual(0, _shm.Count);
foreach (DictionaryEntry de in _shm)
{
Assert.Fail("Should not be any entries but found Key = " + de.Key.ToString() + " and Value = " + de.Value.ToString());
}
}
[Test]
public void Contains()
{
Assert.IsTrue(_shm.Contains("test1"));
Assert.IsFalse(_shm.Contains("test10"));
}
[Test]
public void GetEnumerator()
{
int i = 0;
foreach (DictionaryEntry de in _shm)
{
Assert.AreEqual(_expectedKeys[i], de.Key);
Assert.AreEqual(_expectedValues[i], de.Value);
i++;
}
Assert.AreEqual(3, i);
}
[Test]
public void GetEnumeratorEmpty()
{
bool noEntries = true;
for (int i = 0; i < _emptyShm.Count; i++)
{
noEntries = false;
}
Assert.IsTrue(noEntries, "should not have any entries in the enumerator");
}
[Test]
public void GetEnumeratorModifyExceptionFromAdd()
{
Assert.Throws<InvalidOperationException>(() =>
{
foreach (DictionaryEntry de in _shm)
{
_shm["newkey"] = de.Value;
}
});
}
[Test]
public void GetEnumeratorModifyExceptionFromRemove()
{
Assert.Throws<InvalidOperationException>(() =>
{
foreach (DictionaryEntry de in _shm)
{
_shm.Remove(de.Key);
}
});
}
[Test]
public void GetEnumeratorModifyExceptionFromUpdate()
{
Assert.Throws<InvalidOperationException>(() =>
{
foreach (DictionaryEntry de in _shm)
{
_shm[de.Key] = new object();
}
});
}
[Test]
public void Remove()
{
// remove an item that exists
_shm.Remove("test1");
Assert.AreEqual(2, _shm.Count);
// try to remove an item that does not exist
_shm.Remove("test10");
Assert.AreEqual(2, _shm.Count);
}
[Test]
public void Item()
{
Assert.AreEqual(1, _shm["test1"]);
Assert.AreEqual("2", _shm["test2"]);
Assert.AreEqual(true, _shm["test3"]);
}
[Test]
public void Count()
{
Assert.AreEqual(3, _shm.Count);
_shm.Add("new key", "new value");
Assert.AreEqual(4, _shm.Count);
}
[Test]
public void ContainsKey()
{
Assert.IsTrue(_shm.ContainsKey("test1"));
Assert.IsFalse(_shm.ContainsKey("test10"));
}
[Test]
public void ContainsValue()
{
Assert.IsTrue(_shm.ContainsValue("2"));
Assert.IsTrue(_shm.ContainsValue(true));
Assert.IsFalse(_shm.ContainsValue("not in there"));
}
[Test]
public void CopyTo()
{
DictionaryEntry[] destArray = new DictionaryEntry[3];
_shm.CopyTo(destArray, 0);
Assert.AreEqual(3, destArray.Length);
for (int i = 0; i < destArray.Length; i++)
{
Assert.AreEqual(_expectedKeys[i], destArray[i].Key);
Assert.AreEqual(_expectedValues[i], destArray[i].Value);
}
}
[Test]
public void Keys()
{
int i = 0;
foreach (object obj in _shm.Keys)
{
i++;
Assert.IsTrue(_expectedKeys.Contains(obj));
}
Assert.AreEqual(3, i);
SequencedHashMap empty = new SequencedHashMap();
foreach (object obj in empty.Keys)
{
Assert.Fail("should not be a key: " + obj);
}
}
[Test]
public void Values()
{
int i = 0;
foreach (object obj in _shm.Values)
{
i++;
Assert.IsTrue(_expectedValues.Contains(obj));
}
Assert.AreEqual(3, i);
SequencedHashMap empty = new SequencedHashMap();
foreach (object obj in empty.Values)
{
Assert.Fail("should not be a value:" + obj);
}
}
[Test]
public void ValuesEmpty()
{
bool noValues = true;
for (int i = 0; i < _emptyShm.Values.Count; i++)
{
noValues = false;
}
Assert.IsTrue(noValues, "should have no values.");
}
[Test]
public void FirstKey()
{
Assert.IsNotNull(_shm.FirstKey);
Assert.AreEqual("test1", _shm.FirstKey);
Assert.IsNull(_emptyShm.FirstKey);
}
[Test]
public void FirstValue()
{
Assert.IsNotNull(_shm.FirstValue);
Assert.AreEqual(1, _shm.FirstValue);
Assert.IsNull(_emptyShm.FirstValue);
}
[Test]
public void LastKey()
{
Assert.IsNotNull(_shm.LastKey);
Assert.AreEqual("test3", _shm.LastKey);
Assert.IsNull(_emptyShm.LastKey);
}
[Test]
public void LastValue()
{
Assert.IsNotNull(_shm.LastValue);
Assert.AreEqual(true, _shm.LastValue);
Assert.IsNull(_emptyShm.LastValue);
}
[Test, Explicit]
public void Performance()
{
// set the hashtable and SequencedHashMap to be the
IDictionary hashtable;
IDictionary sequenced;
IDictionary list;
int numOfRuns = 4;
int numOfEntries = Int16.MaxValue;
long hashStart;
long[] hashPopulateTicks = new long[numOfRuns];
long[] hashItemTicks = new long[numOfRuns];
long seqStart;
long[] seqPopulateTicks = new long[numOfRuns];
long[] seqItemTicks = new long[numOfRuns];
long listStart;
long[] listPopulateTicks = new long[numOfRuns];
long[] listItemTicks = new long[numOfRuns];
for (int runIndex = 0; runIndex < numOfRuns; runIndex++)
{
object key;
object value;
hashtable = new Hashtable();
sequenced = new SequencedHashMap();
list = new ListDictionary();
hashStart = DateTime.Now.Ticks;
for (int i = 0; i < numOfEntries; i++)
{
hashtable.Add("test" + i, new object());
}
hashPopulateTicks[runIndex] = DateTime.Now.Ticks - hashStart;
hashStart = DateTime.Now.Ticks;
for (int i = 0; i < numOfEntries; i++)
{
key = "test" + i;
value = hashtable[key];
}
hashItemTicks[runIndex] = DateTime.Now.Ticks - hashStart;
hashtable.Clear();
seqStart = DateTime.Now.Ticks;
for (int i = 0; i < numOfEntries; i++)
{
sequenced.Add("test" + i, new object());
}
seqPopulateTicks[runIndex] = DateTime.Now.Ticks - seqStart;
seqStart = DateTime.Now.Ticks;
for (int i = 0; i < numOfEntries; i++)
{
key = "test" + i;
value = sequenced[key];
}
seqItemTicks[runIndex] = DateTime.Now.Ticks - seqStart;
sequenced.Clear();
listStart = DateTime.Now.Ticks;
for (int i = 0; i < numOfEntries; i++)
{
list.Add("test" + i, new object());
}
listPopulateTicks[runIndex] = DateTime.Now.Ticks - listStart;
listStart = DateTime.Now.Ticks;
for (int i = 0; i < numOfEntries; i++)
{
key = "test" + i;
value = list[key];
}
listItemTicks[runIndex] = DateTime.Now.Ticks - listStart;
list.Clear();
}
for (int runIndex = 0; runIndex < numOfRuns; runIndex++)
{
decimal seqPopulateOverhead = ((decimal) seqPopulateTicks[runIndex] / (decimal) hashPopulateTicks[runIndex]);
decimal seqItemOverhead = ((decimal) seqItemTicks[runIndex] / (decimal) hashItemTicks[runIndex]);
string errMessage = "SequenceHashMap vs Hashtable:";
errMessage += "\n POPULATE:";
errMessage += "\n\t seqPopulateTicks[" + runIndex + "] took " + seqPopulateTicks[runIndex] + " ticks.";
errMessage += "\n\t hashPopulateTicks[" + runIndex + "] took " + hashPopulateTicks[runIndex] + " ticks.";
errMessage += "\n\t for an overhead of " + seqPopulateOverhead.ToString();
errMessage += "\n ITEM:";
errMessage += "\n\t seqItemTicks[" + runIndex + "] took " + seqItemTicks[runIndex] + " ticks.";
errMessage += "\n\t hashItemTicks[" + runIndex + "] took " + hashItemTicks[runIndex] + " ticks.";
errMessage += "\n\t for an overhead of " + seqItemOverhead.ToString();
Console.Out.WriteLine(errMessage);
decimal listPopulateOverhead = ((decimal) listPopulateTicks[runIndex] / (decimal) seqPopulateTicks[runIndex]);
decimal listItemOverhead = ((decimal) listItemTicks[runIndex] / (decimal) seqItemTicks[runIndex]);
errMessage = "ListDictionary vs SequenceHashMap:";
errMessage += "\n POPULATE:";
errMessage += "\n\t listPopulateTicks[" + runIndex + "] took " + listPopulateTicks[runIndex] + " ticks.";
errMessage += "\n\t seqPopulateTicks[" + runIndex + "] took " + seqPopulateTicks[runIndex] + " ticks.";
errMessage += "\n\t for an overhead of " + listPopulateOverhead.ToString();
errMessage += "\n ITEM:";
errMessage += "\n\t listItemTicks[" + runIndex + "] took " + listItemTicks[runIndex] + " ticks.";
errMessage += "\n\t seqItemTicks[" + runIndex + "] took " + seqItemTicks[runIndex] + " ticks.";
errMessage += "\n\t for an overhead of " + listItemOverhead.ToString();
Console.Out.WriteLine(errMessage);
}
}
[Test]
public void Serialize()
{
MemoryStream stream = new MemoryStream();
var f = new BinaryFormatter
{
#if !NETFX
SurrogateSelector = new SerializationHelper.SurrogateSelector()
#endif
};
f.Serialize(stream, _shm);
stream.Position = 0;
SequencedHashMap shm = (SequencedHashMap) f.Deserialize(stream);
stream.Close();
Assert.AreEqual(3, shm.Count);
int index = 0;
foreach (DictionaryEntry de in shm)
{
Assert.AreEqual(_expectedKeys[index], de.Key);
Assert.AreEqual(_expectedValues[index], de.Value);
index++;
}
Assert.AreEqual(3, index);
}
}
}