-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathIndexTests.cs
More file actions
286 lines (248 loc) · 9.11 KB
/
IndexTests.cs
File metadata and controls
286 lines (248 loc) · 9.11 KB
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
using SharpCoreDB.DataStructures;
using System.Diagnostics;
namespace SharpCoreDB.Tests;
/// <summary>
/// Unit and integration tests for index functionality.
/// Covers hash indexes, performance benchmarks, and edge cases.
/// </summary>
public class IndexTests
{
[Fact]
public void HashIndex_Lookup_Performance_Benchmark()
{
// Arrange
var index = new HashIndex("benchmark", "key");
var rowCount = 100000;
var uniqueKeys = 1000;
// Act - Build index with large dataset
var sw = Stopwatch.StartNew();
for (int i = 0; i < rowCount; i++)
{
var row = new Dictionary<string, object>
{
{ "key", $"key_{i % uniqueKeys}" },
{ "value", i },
{ "data", $"data_{i}" }
};
index.Add(row, i);
}
sw.Stop();
var buildTime = sw.ElapsedMilliseconds;
// Act - Perform lookups
sw.Restart();
var lookupCount = 1000;
for (int i = 0; i < lookupCount; i++)
{
var positions = index.LookupPositions($"key_{i % uniqueKeys}");
Assert.NotEmpty(positions);
}
sw.Stop();
var lookupTime = sw.ElapsedMilliseconds;
// Assert - Performance should be reasonable
Assert.True(buildTime < 5000, $"Index build took {buildTime}ms, should be < 5000ms");
Assert.True(lookupTime < 1000, $"Index lookups took {lookupTime}ms, should be < 1000ms");
var stats = index.GetStatistics();
Assert.Equal(uniqueKeys, stats.UniqueKeys);
Assert.Equal(rowCount, stats.TotalRows);
}
[Fact]
public void HashIndex_ConcurrentAccess_ThreadSafe()
{
// Arrange
var index = new HashIndex("concurrent", "id");
var tasks = new List<Task>();
// Act - Add rows from multiple threads
for (int t = 0; t < 10; t++)
{
int threadId = t; // Capture loop variable for closure
tasks.Add(Task.Run(() =>
{
for (int i = 0; i < 100; i++)
{
var row = new Dictionary<string, object>
{
{ "id", i },
{ "thread", threadId },
{ "data", $"data_{threadId}_{i}" }
};
index.Add(row, i + threadId * 100);
}
}));
}
Task.WaitAll(tasks.ToArray());
// Assert - All rows should be indexed
var totalRows = 0;
for (int i = 0; i < 100; i++)
{
var positions = index.LookupPositions(i);
Assert.Equal(10, positions.Count); // 10 threads added same id
totalRows += positions.Count;
}
Assert.Equal(1000, totalRows);
}
[Fact]
public void HashIndex_MemoryUsage_Efficient()
{
// Arrange
var index = new HashIndex("memory", "key");
var initialMemory = GC.GetTotalMemory(true);
// Act - Add many rows
for (int i = 0; i < 50000; i++)
{
var row = new Dictionary<string, object>
{
{ "key", $"key_{i % 1000}" },
{ "value", i }
};
index.Add(row, i);
}
var finalMemory = GC.GetTotalMemory(true);
var memoryUsed = finalMemory - initialMemory;
// Assert - Memory usage should be reasonable (< 50MB for 50k rows)
Assert.True(memoryUsed < 50 * 1024 * 1024, $"Memory used: {memoryUsed / 1024 / 1024}MB, should be < 50MB");
}
[Fact]
public void HashIndex_IndexLookup_Vs_TableScan_Performance()
{
// Arrange - Create test data
var index = new HashIndex("perf_test", "category");
var rows = new List<Dictionary<string, object>>();
for (int i = 0; i < 10000; i++)
{
rows.Add(new Dictionary<string, object>
{
{ "id", i },
{ "category", $"cat_{i % 10}" },
{ "data", $"data_{i}" }
});
}
index.Rebuild(rows);
// Act - Index lookup
var sw = Stopwatch.StartNew();
var indexPositions = index.LookupPositions("cat_5");
sw.Stop();
var indexTime = sw.ElapsedTicks;
// Act - Simulate table scan
sw.Restart();
var scanResults = rows.Where(r => r["category"].ToString() == "cat_5").ToList();
sw.Stop();
var scanTime = sw.ElapsedTicks;
// Assert - Index should be significantly faster
Assert.Equal(scanResults.Count, indexPositions.Count);
Assert.True(indexTime < scanTime / 10, $"Index lookup should be at least 10x faster. Index: {indexTime} ticks, Scan: {scanTime} ticks");
}
[Fact]
public void HashIndex_Rebuild_LargeDataset_Efficient()
{
// Arrange
var index = new HashIndex("rebuild_test", "key");
var rows = new List<Dictionary<string, object>>();
for (int i = 0; i < 100000; i++)
{
rows.Add(new Dictionary<string, object>
{
{ "key", $"key_{i % 5000}" },
{ "value", i }
});
}
// Act
var sw = Stopwatch.StartNew();
index.Rebuild(rows);
sw.Stop();
// Assert - Rebuild should be fast
Assert.True(sw.ElapsedMilliseconds < 2000, $"Rebuild took {sw.ElapsedMilliseconds}ms, should be < 2000ms");
var stats = index.GetStatistics();
Assert.Equal(5000, stats.UniqueKeys);
Assert.Equal(100000, stats.TotalRows);
}
[Fact]
public void HashIndex_UpdateOperations_MaintainConsistency()
{
// Arrange
var index = new HashIndex("update_test", "status");
var rows = new List<Dictionary<string, object>>
{
new() { { "id", 1 }, { "status", "active" }, { "name", "Item1" } },
new() { { "id", 2 }, { "status", "active" }, { "name", "Item2" } },
new() { { "id", 3 }, { "status", "inactive" }, { "name", "Item3" } }
};
foreach (var row in rows)
{
index.Add(row, 0); // Dummy position
}
// Act - Update status
var updatedRow = new Dictionary<string, object> { { "id", 1 }, { "status", "inactive" }, { "name", "Item1" } };
index.Remove(rows[0], 0);
index.Add(updatedRow, 0);
// Assert - Index should reflect changes
Assert.Single(index.LookupPositions("active"));
Assert.Equal(2, index.LookupPositions("inactive").Count);
}
[Fact]
public void HashIndex_EdgeCases_NullAndEmptyKeys()
{
// Arrange
var index = new HashIndex("edge_cases", "key");
// Act & Assert - Null keys should be ignored
var nullRow = new Dictionary<string, object> { { "key", (object)null! }, { "value", 1 } };
index.Add(nullRow, 0);
Assert.Equal(0, index.Count);
// Empty string keys should work
var emptyRow = new Dictionary<string, object> { { "key", "" }, { "value", 2 } };
index.Add(emptyRow, 0);
Assert.Single(index.LookupPositions(""));
Assert.Equal(1, index.Count);
// Missing key column should be ignored
var missingKeyRow = new Dictionary<string, object> { { "value", 3 } };
index.Add(missingKeyRow, 0);
Assert.Equal(1, index.Count); // Still only the empty string key
}
[Fact]
public void HashIndex_Statistics_Accurate()
{
// Arrange
var index = new HashIndex("stats_test", "group");
// Act - Add rows with varying distribution
var distributions = new[] { 1, 1, 1, 2, 2, 5, 5, 5, 5, 5 }; // Expect 3 unique keys: 1,2,5
foreach (var key in distributions)
{
var row = new Dictionary<string, object> { { "group", key }, { "data", $"item_{key}" } };
index.Add(row, 0); // Dummy position
}
var stats = index.GetStatistics();
// Assert
Assert.Equal(3, stats.UniqueKeys);
Assert.Equal(10, stats.TotalRows);
Assert.Equal(10.0 / 3.0, stats.AvgRowsPerKey);
}
[Fact]
public void HashIndex_ClearAndRebuild_Consistent()
{
// Arrange
var index = new HashIndex("clear_test", "id");
var initialRows = new List<Dictionary<string, object>>();
for (int i = 0; i < 100; i++)
{
initialRows.Add(new Dictionary<string, object> { { "id", i }, { "value", $"val_{i}" } });
index.Add(initialRows[i], i);
}
// Act - Clear and rebuild with different data
index.Clear();
var newRows = new List<Dictionary<string, object>>();
for (int i = 0; i < 50; i++)
{
newRows.Add(new Dictionary<string, object> { { "id", i + 100 }, { "value", $"new_{i}" } });
}
index.Rebuild(newRows);
// Assert - Should only contain new data
Assert.Equal(50, index.Count);
for (int i = 0; i < 50; i++)
{
Assert.Single(index.LookupPositions(i + 100));
}
for (int i = 0; i < 100; i++)
{
Assert.Empty(index.LookupPositions(i));
}
}
}