-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConcurrentDictionaryTests.cs
247 lines (210 loc) · 6.76 KB
/
ConcurrentDictionaryTests.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
using NUnit.Framework;
using System;
using System.Collections.Concurrent;
namespace ConcurrentDictionaryTests
{
public class CacheKeys
{
public string Key { get; set; }
public DateTime Expiration { get; set; }
public CacheKeys(string key, DateTime expiration)
{
Key = key;
Expiration = expiration;
}
}
[TestFixture]
public class ConcurrentDictionaryUnitTest
{
private ConcurrentDictionary<string, CacheKeys> _cacheDictionary;
[SetUp]
public void Setup()
{
// Initialize the dictionary before each test
_cacheDictionary = new ConcurrentDictionary<string, CacheKeys>();
}
[Test]
public void AddOrUpdate_Item_ShouldBeAdded()
{
// Arrange
var cacheKey = new CacheKeys("Key1", DateTime.UtcNow.AddMinutes(5));
// Act
var result = _cacheDictionary.AddOrUpdate("Key1", cacheKey, (key, oldValue) => cacheKey);
// Assert
Assert.AreEqual(cacheKey, result);
Assert.IsTrue(_cacheDictionary.ContainsKey("Key1"));
Assert.AreEqual("Key1", _cacheDictionary["Key1"].Key);
}
[Test]
public void TryGetValue_ItemExists_ShouldReturnTrue()
{
// Arrange
var cacheKey = new CacheKeys("Key1", DateTime.UtcNow.AddMinutes(5));
_cacheDictionary.TryAdd("Key1", cacheKey);
// Act
var success = _cacheDictionary.TryGetValue("Key1", out var retrievedCacheKey);
// Assert
Assert.IsTrue(success);
Assert.AreEqual(cacheKey, retrievedCacheKey);
}
[Test]
public void TryRemove_ItemExists_ShouldBeRemoved()
{
// Arrange
var cacheKey = new CacheKeys("Key1", DateTime.UtcNow.AddMinutes(5));
_cacheDictionary.TryAdd("Key1", cacheKey);
// Act
var success = _cacheDictionary.TryRemove("Key1", out var removedCacheKey);
// Assert
Assert.IsTrue(success);
Assert.AreEqual(cacheKey, removedCacheKey);
Assert.IsFalse(_cacheDictionary.ContainsKey("Key1"));
}
[Test]
public void Count_ShouldReturnCorrectCount()
{
// Arrange
var cacheKey1 = new CacheKeys("Key1", DateTime.UtcNow.AddMinutes(5));
var cacheKey2 = new CacheKeys("Key2", DateTime.UtcNow.AddMinutes(10));
_cacheDictionary.TryAdd("Key1", cacheKey1);
_cacheDictionary.TryAdd("Key2", cacheKey2);
// Act
var count = _cacheDictionary.Count;
// Assert
Assert.AreEqual(2, count);
}
}
}
using NUnit.Framework;
using System.Collections.Concurrent;
using System.Collections.Generic;
[TestFixture]
public class ConcurrentDictionaryExtensionsTests
{
[Test]
public void CompareTo_EmptyDictionaries_ReturnsEmptyResult()
{
// Arrange
var firstDict = new ConcurrentDictionary<string, int>();
var secondDict = new ConcurrentDictionary<string, int>();
// Act
var result = firstDict.CompareTo(secondDict);
// Assert
Assert.IsNotNull(result);
Assert.IsEmpty(result.Added);
Assert.IsEmpty(result.Updated);
Assert.IsEmpty(result.Deleted);
}
[Test]
public void CompareTo_AddedItems_DetectsNewItems()
{
// Arrange
var firstDict = new ConcurrentDictionary<string, int>();
var secondDict = new ConcurrentDictionary<string, int>
{
["A"] = 1,
["B"] = 2
};
// Act
var result = firstDict.CompareTo(secondDict);
// Assert
Assert.AreEqual(2, result.Added.Count);
Assert.IsEmpty(result.Updated);
Assert.IsEmpty(result.Deleted);
Assert.AreEqual(1, result.Added["A"]);
Assert.AreEqual(2, result.Added["B"]);
}
[Test]
public void CompareTo_DeletedItems_DetectsRemovedItems()
{
// Arrange
var firstDict = new ConcurrentDictionary<string, int>
{
["A"] = 1,
["B"] = 2
};
var secondDict = new ConcurrentDictionary<string, int>();
// Act
var result = firstDict.CompareTo(secondDict);
// Assert
Assert.IsEmpty(result.Added);
Assert.IsEmpty(result.Updated);
Assert.AreEqual(2, result.Deleted.Count);
Assert.AreEqual(1, result.Deleted["A"]);
Assert.AreEqual(2, result.Deleted["B"]);
}
[Test]
public void CompareTo_UpdatedItems_DetectsChanges()
{
// Arrange
var firstDict = new ConcurrentDictionary<string, int>
{
["A"] = 1,
["B"] = 2
};
var secondDict = new ConcurrentDictionary<string, int>
{
["A"] = 1,
["B"] = 3
};
// Act
var result = firstDict.CompareTo(secondDict);
// Assert
Assert.IsEmpty(result.Added);
Assert.AreEqual(1, result.Updated.Count);
Assert.IsEmpty(result.Deleted);
Assert.AreEqual(2, result.Updated["B"].OldValue);
Assert.AreEqual(3, result.Updated["B"].NewValue);
}
[Test]
public void CompareTo_MixedChanges_DetectsAllDifferences()
{
// Arrange
var firstDict = new ConcurrentDictionary<string, int>
{
["A"] = 1, // Will be deleted
["B"] = 2, // Will be updated
["C"] = 3 // Will remain same
};
var secondDict = new ConcurrentDictionary<string, int>
{
["B"] = 5, // Updated
["C"] = 3, // Same
["D"] = 4 // Added
};
// Act
var result = firstDict.CompareTo(secondDict);
// Assert
// Added
Assert.AreEqual(1, result.Added.Count);
Assert.AreEqual(4, result.Added["D"]);
// Updated
Assert.AreEqual(1, result.Updated.Count);
Assert.AreEqual(2, result.Updated["B"].OldValue);
Assert.AreEqual(5, result.Updated["B"].NewValue);
// Deleted
Assert.AreEqual(1, result.Deleted.Count);
Assert.AreEqual(1, result.Deleted["A"]);
}
[Test]
public void CompareTo_SameDictionaries_ReturnsEmptyResult()
{
// Arrange
var firstDict = new ConcurrentDictionary<string, int>
{
["A"] = 1,
["B"] = 2
};
var secondDict = new ConcurrentDictionary<string, int>
{
["A"] = 1,
["B"] = 2
};
// Act
var result = firstDict.CompareTo(secondDict);
// Assert
Assert.IsEmpty(result.Added);
Assert.IsEmpty(result.Updated);
Assert.IsEmpty(result.Deleted);
}
}