Skip to content

Commit 623de35

Browse files
last format today
1 parent 8b7b161 commit 623de35

13 files changed

Lines changed: 378 additions & 293 deletions

File tree

CommonExtendedObjectsTests/SortedKvStoreTests.cs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313
namespace CommonExtendedObjectsTests
1414
{
1515
/// <summary>
16-
/// Test my Key Value class
16+
/// Test my Key Value class
1717
/// </summary>
1818
[TestClass]
1919
public class SortedKvStoreTests
2020
{
2121
/// <summary>
22-
/// The item count
22+
/// The item count
2323
/// </summary>
2424
private const int ItemCount = 100_000;
2525

2626
/// <summary>
27-
/// Adds the and try get works.
27+
/// Adds the and try get works.
2828
/// </summary>
2929
[TestMethod]
3030
public void AddAndTryGetWorks()
@@ -47,7 +47,7 @@ public void AddAndTryGetWorks()
4747
}
4848

4949
/// <summary>
50-
/// Tries the remove removes existing.
50+
/// Tries the remove removes existing.
5151
/// </summary>
5252
[TestMethod]
5353
public void TryRemoveRemovesExisting()
@@ -62,16 +62,18 @@ public void TryRemoveRemovesExisting()
6262
}
6363

6464
/// <summary>
65-
/// Removes the many works.
65+
/// Removes the many works.
6666
/// </summary>
6767
[TestMethod]
6868
public void RemoveManyWorks()
6969
{
7070
var store = new SortedKvStore();
7171
for (var i = 0; i < 10; i++)
72+
{
7273
store.Add(i, i * 10);
74+
}
7375

74-
store.RemoveMany(new int[] { 2, 4, 6 });
76+
store.RemoveMany(new[] { 2, 4, 6 });
7577

7678
Assert.IsFalse(store.TryGet(2, out _));
7779
Assert.IsFalse(store.TryGet(4, out _));
@@ -81,14 +83,16 @@ public void RemoveManyWorks()
8183
}
8284

8385
/// <summary>
84-
/// Compacts the removes unoccupied.
86+
/// Compacts the removes unoccupied.
8587
/// </summary>
8688
[TestMethod]
8789
public void CompactRemovesUnoccupied()
8890
{
8991
var store = new SortedKvStore();
9092
for (var i = 0; i < 10; i++)
93+
{
9194
store.Add(i, i * 10);
95+
}
9296

9397
store.Remove(1);
9498
store.Remove(3);
@@ -99,7 +103,7 @@ public void CompactRemovesUnoccupied()
99103
}
100104

101105
/// <summary>
102-
/// Indexers the works.
106+
/// Indexers the works.
103107
/// </summary>
104108
[TestMethod]
105109
public void IndexerWorks()
@@ -113,17 +117,19 @@ public void IndexerWorks()
113117
}
114118

115119
/// <summary>
116-
/// Adds the and try get should return expected values.
120+
/// Adds the and try get should return expected values.
117121
/// </summary>
118122
[TestMethod]
119123
public void AddAndTryGetShouldReturnExpectedValues()
120124
{
121125
var store = new SortedKvStore();
122126

123-
for (int i = 0; i < 1000; i++)
127+
for (var i = 0; i < 1000; i++)
128+
{
124129
store.Add(i, i * 10);
130+
}
125131

126-
for (int i = 0; i < 1000; i++)
132+
for (var i = 0; i < 1000; i++)
127133
{
128134
Assert.IsTrue(store.TryGet(i, out var value));
129135
Assert.AreEqual(i * 10, value);
@@ -133,7 +139,7 @@ public void AddAndTryGetShouldReturnExpectedValues()
133139
}
134140

135141
/// <summary>
136-
/// Performances the add and try get.
142+
/// Performances the add and try get.
137143
/// </summary>
138144
[TestMethod]
139145
public void PerformanceAddAndTryGet()
@@ -142,19 +148,23 @@ public void PerformanceAddAndTryGet()
142148

143149
var sw = Stopwatch.StartNew();
144150

145-
for (int i = 0; i < ItemCount; i++)
151+
for (var i = 0; i < ItemCount; i++)
152+
{
146153
store.Add(i, i * 2);
154+
}
147155

148156
sw.Stop();
149157
Trace.WriteLine($"Add {ItemCount} items: {sw.ElapsedMilliseconds} ms");
150158

151159
sw.Restart();
152160

153-
int hits = 0;
154-
for (int i = 0; i < ItemCount; i++)
161+
var hits = 0;
162+
for (var i = 0; i < ItemCount; i++)
155163
{
156164
if (store.TryGet(i, out var value) && value == i * 2)
165+
{
157166
hits++;
167+
}
158168
}
159169

160170
sw.Stop();
@@ -164,19 +174,23 @@ public void PerformanceAddAndTryGet()
164174
}
165175

166176
/// <summary>
167-
/// Performances the remove compact.
177+
/// Performances the remove compact.
168178
/// </summary>
169179
[TestMethod]
170180
public void PerformanceRemoveCompact()
171181
{
172182
var store = new SortedKvStore(ItemCount);
173183

174-
for (int i = 0; i < ItemCount; i++)
184+
for (var i = 0; i < ItemCount; i++)
185+
{
175186
store.Add(i, i);
187+
}
176188

177189
// Remove half of them
178-
for (int i = 0; i < ItemCount; i += 2)
190+
for (var i = 0; i < ItemCount; i += 2)
191+
{
179192
store.Remove(i);
193+
}
180194

181195
var sw = Stopwatch.StartNew();
182196
store.Compact();

CommonExtendedObjectsTests/Utilities.cs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -268,31 +268,33 @@ public void FindSequencesShouldReturnCorrectResultWhenListHasMixedSequences()
268268
}
269269

270270
/// <summary>
271-
/// Binaries the search performance and correctness.
271+
/// Binaries the search performance and correctness.
272272
/// </summary>
273273
[TestMethod]
274274
public void BinarySearchPerformanceAndCorrectness()
275275
{
276276
const int N = 1_000_000;
277-
int[] sortedKeys = new int[N];
278-
for (int i = 0; i < N; i++)
277+
var sortedKeys = new int[N];
278+
for (var i = 0; i < N; i++)
279+
{
279280
sortedKeys[i] = i * 2; // even numbers sorted
281+
}
280282

281283
var keysSpan = sortedKeys.AsSpan();
282284

283285
// Warm-up to avoid JIT bias
284-
for (int i = 0; i < 10_000; i++)
286+
for (var i = 0; i < 10_000; i++)
285287
{
286288
Utility.BinarySearch(keysSpan, i * 2);
287289
}
288290

289291
var sw = Stopwatch.StartNew();
290292

291-
int foundCount = 0;
292-
for (int i = 0; i < N; i++)
293+
var foundCount = 0;
294+
for (var i = 0; i < N; i++)
293295
{
294-
int val = i * 2;
295-
int idx = Utility.BinarySearch(keysSpan, val);
296+
var val = i * 2;
297+
var idx = Utility.BinarySearch(keysSpan, val);
296298

297299
Assert.IsTrue(idx >= 0, $"Key {val} should be found.");
298300
Assert.AreEqual(val, sortedKeys[idx]);
@@ -312,37 +314,41 @@ public void BinarySearchPerformanceAndCorrectness()
312314
public void CompareCustomVsArrayBinarySearch_Performance()
313315
{
314316
const int N = 1_000_000;
315-
int[] sortedKeys = new int[N];
316-
for (int i = 0; i < N; i++)
317+
var sortedKeys = new int[N];
318+
for (var i = 0; i < N; i++)
319+
{
317320
sortedKeys[i] = i * 2; // even numbers sorted
321+
}
318322

319323
var keysSpan = sortedKeys.AsSpan();
320324

321325
// Warm-up both methods
322-
for (int i = 0; i < 10_000; i++)
326+
for (var i = 0; i < 10_000; i++)
323327
{
324328
Utility.BinarySearch(keysSpan, i * 2);
325329
Array.BinarySearch(sortedKeys, i * 2);
326330
}
327331

328332
// Custom BinarySearch benchmark
329333
var swCustom = Stopwatch.StartNew();
330-
for (int i = 0; i < N; i++)
334+
for (var i = 0; i < N; i++)
331335
{
332-
int val = i * 2;
333-
int idx = Utility.BinarySearch(keysSpan, N, val);
336+
var val = i * 2;
337+
var idx = Utility.BinarySearch(keysSpan, N, val);
334338
Assert.IsTrue(idx >= 0);
335339
}
340+
336341
swCustom.Stop();
337342

338343
// Array.BinarySearch benchmark
339344
var swArray = Stopwatch.StartNew();
340-
for (int i = 0; i < N; i++)
345+
for (var i = 0; i < N; i++)
341346
{
342-
int val = i * 2;
343-
int idx = Array.BinarySearch(sortedKeys, val);
347+
var val = i * 2;
348+
var idx = Array.BinarySearch(sortedKeys, val);
344349
Assert.IsTrue(idx >= 0);
345350
}
351+
346352
swArray.Stop();
347353

348354
Trace.WriteLine($"Custom BinarySearch: {swCustom.ElapsedMilliseconds} ms");
@@ -352,6 +358,5 @@ public void CompareCustomVsArrayBinarySearch_Performance()
352358
Assert.IsTrue(swCustom.ElapsedMilliseconds < swArray.ElapsedMilliseconds * 2,
353359
$"Custom BinarySearch is too slow: {swCustom.ElapsedMilliseconds} ms vs {swArray.ElapsedMilliseconds} ms");
354360
}
355-
356361
}
357362
}

ExtendedSystemObjects/CategorizedDictionary.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ public IEnumerable<TK> GetKeys()
126126
_lock.EnterReadLock();
127127
try
128128
{
129-
return _data.Keys.ToList(); ; ; // Create a copy for thread safety
129+
return _data.Keys.ToList();
130+
// Create a copy for thread safety
130131
}
131132
finally
132133
{
@@ -164,7 +165,7 @@ public void Add(TK key, TV value)
164165
}
165166

166167
/// <summary>
167-
/// Removes the specified key.
168+
/// Removes the specified key.
168169
/// </summary>
169170
/// <param name="key">The key.</param>
170171
/// <returns>If item was removed</returns>
@@ -351,7 +352,7 @@ public List<KeyValuePair<TK, TV>> ToKeyValueList()
351352
}
352353

353354
/// <summary>
354-
/// Clears this instance.
355+
/// Clears this instance.
355356
/// </summary>
356357
public void Clear()
357358
{
@@ -472,7 +473,7 @@ public override int GetHashCode()
472473
}
473474

474475
/// <summary>
475-
/// Adds the internal.
476+
/// Adds the internal.
476477
/// </summary>
477478
/// <param name="category">The category.</param>
478479
/// <param name="key">The key.</param>
@@ -487,6 +488,5 @@ private void AddInternal(string category, TK key, TV value)
487488

488489
_data[key] = (category, value);
489490
}
490-
491491
}
492492
}

0 commit comments

Comments
 (0)