|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: CommonExtendedObjectsTests |
| 4 | + * FILE: ImmutableLookupMapAllTests.cs |
| 5 | + * PURPOSE: Unified test suite for both ImmutableLookupMap and ImmutableLookupMapUnmanaged |
| 6 | + * PROGRAMMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Collections.ObjectModel; |
| 11 | +using System.Diagnostics; |
| 12 | +using System.Linq; |
| 13 | +using ExtendedSystemObjects; |
| 14 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 15 | + |
| 16 | +namespace CommonExtendedObjectsTests |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// Unified tests for both ImmutableLookupMap and ImmutableLookupMapUnmanaged. |
| 20 | + /// </summary> |
| 21 | + [TestClass] |
| 22 | + public class ImmutableLookupMapAllTests |
| 23 | + { |
| 24 | + /// <summary> |
| 25 | + /// The string map data |
| 26 | + /// </summary> |
| 27 | + private Dictionary<int, string> _stringMapData; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// The float map data |
| 31 | + /// </summary> |
| 32 | + private Dictionary<int, float> _floatMapData; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// The string map |
| 36 | + /// </summary> |
| 37 | + private ImmutableLookupMap<int, string> _stringMap; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// The read only map |
| 41 | + /// </summary> |
| 42 | + private ReadOnlyDictionary<int, string> _readOnlyMap; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// The unmanaged map |
| 46 | + /// </summary> |
| 47 | + private ImmutableLookupMapUnmanaged<int, float> _unmanagedMap; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Setups this instance. |
| 51 | + /// </summary> |
| 52 | + [TestInitialize] |
| 53 | + public void Setup() |
| 54 | + { |
| 55 | + _stringMapData = new Dictionary<int, string>(); |
| 56 | + _floatMapData = new Dictionary<int, float>(); |
| 57 | + |
| 58 | + for (var i = 0; i < 10_000; i++) |
| 59 | + { |
| 60 | + _stringMapData[i] = $"Value_{i}"; |
| 61 | + _floatMapData[i] = i * 1.5f; |
| 62 | + } |
| 63 | + |
| 64 | + _stringMap = new ImmutableLookupMap<int, string>(_stringMapData); |
| 65 | + _readOnlyMap = new ReadOnlyDictionary<int, string>(_stringMapData); |
| 66 | + _unmanagedMap = new ImmutableLookupMapUnmanaged<int, float>(_floatMapData); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Cleanups this instance. |
| 71 | + /// </summary> |
| 72 | + [TestCleanup] |
| 73 | + public void Cleanup() |
| 74 | + { |
| 75 | + _unmanagedMap.Dispose(); |
| 76 | + } |
| 77 | + |
| 78 | + // ------------ STRING MAP TESTS ---------------- |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Tests the string map initialization. |
| 82 | + /// </summary> |
| 83 | + [TestMethod] |
| 84 | + public void TestStringMapInitialization() |
| 85 | + { |
| 86 | + var stopwatch = Stopwatch.StartNew(); |
| 87 | + var map = new ImmutableLookupMap<int, string>(_stringMapData); |
| 88 | + stopwatch.Stop(); |
| 89 | + |
| 90 | + Trace.WriteLine($"ImmutableLookupMap (string) Initialization: {stopwatch.ElapsedMilliseconds} ms"); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Tests the read only dictionary initialization. |
| 95 | + /// </summary> |
| 96 | + [TestMethod] |
| 97 | + public void TestReadOnlyDictionaryInitialization() |
| 98 | + { |
| 99 | + var stopwatch = Stopwatch.StartNew(); |
| 100 | + var map = new ReadOnlyDictionary<int, string>(_stringMapData); |
| 101 | + stopwatch.Stop(); |
| 102 | + |
| 103 | + Trace.WriteLine($"ReadOnlyDictionary Initialization: {stopwatch.ElapsedMilliseconds} ms"); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Tests the string map lookup. |
| 108 | + /// </summary> |
| 109 | + [TestMethod] |
| 110 | + public void TestStringMapLookup() |
| 111 | + { |
| 112 | + var stopwatch = Stopwatch.StartNew(); |
| 113 | + |
| 114 | + for (var i = 0; i < 10_000; i++) |
| 115 | + { |
| 116 | + var value = _stringMap.Get(i); |
| 117 | + Assert.AreEqual($"Value_{i}", value); |
| 118 | + } |
| 119 | + |
| 120 | + stopwatch.Stop(); |
| 121 | + Trace.WriteLine($"ImmutableLookupMap (string) Lookup Time: {stopwatch.ElapsedMilliseconds} ms"); |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Tests the read only dictionary lookup. |
| 126 | + /// </summary> |
| 127 | + [TestMethod] |
| 128 | + public void TestReadOnlyDictionaryLookup() |
| 129 | + { |
| 130 | + var stopwatch = Stopwatch.StartNew(); |
| 131 | + |
| 132 | + for (var i = 0; i < 10_000; i++) |
| 133 | + { |
| 134 | + var value = _readOnlyMap[i]; |
| 135 | + Assert.AreEqual($"Value_{i}", value); |
| 136 | + } |
| 137 | + |
| 138 | + stopwatch.Stop(); |
| 139 | + Trace.WriteLine($"ReadOnlyDictionary Lookup Time: {stopwatch.ElapsedMilliseconds} ms"); |
| 140 | + } |
| 141 | + |
| 142 | + // ------------ UNMANAGED MAP TESTS ---------------- |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// Tests the unmanaged map initialization. |
| 146 | + /// </summary> |
| 147 | + [TestMethod] |
| 148 | + public void TestUnmanagedMapInitialization() |
| 149 | + { |
| 150 | + var stopwatch = Stopwatch.StartNew(); |
| 151 | + using var map = new ImmutableLookupMapUnmanaged<int, float>(_floatMapData); |
| 152 | + stopwatch.Stop(); |
| 153 | + |
| 154 | + Trace.WriteLine($"ImmutableLookupMapUnmanaged (float) Initialization: {stopwatch.ElapsedMilliseconds} ms"); |
| 155 | + } |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// Tests the unmanaged map lookup. |
| 159 | + /// </summary> |
| 160 | + [TestMethod] |
| 161 | + public void TestUnmanagedMapLookup() |
| 162 | + { |
| 163 | + var stopwatch = Stopwatch.StartNew(); |
| 164 | + |
| 165 | + for (var i = 0; i < 10_000; i++) |
| 166 | + { |
| 167 | + var value = _unmanagedMap.Get(i); |
| 168 | + Assert.AreEqual(i * 1.5f, value); |
| 169 | + } |
| 170 | + |
| 171 | + stopwatch.Stop(); |
| 172 | + Trace.WriteLine($"ImmutableLookupMapUnmanaged (float) Lookup Time: {stopwatch.ElapsedMilliseconds} ms"); |
| 173 | + } |
| 174 | + |
| 175 | + /// <summary> |
| 176 | + /// Tests the unmanaged map try get value. |
| 177 | + /// </summary> |
| 178 | + [TestMethod] |
| 179 | + public void TestUnmanagedMapTryGetValue() |
| 180 | + { |
| 181 | + Assert.IsTrue(_unmanagedMap.TryGetValue(123, out var value)); |
| 182 | + Assert.AreEqual(123 * 1.5f, value); |
| 183 | + |
| 184 | + Assert.IsFalse(_unmanagedMap.TryGetValue(-1, out _)); |
| 185 | + } |
| 186 | + |
| 187 | + /// <summary> |
| 188 | + /// Tests the unmanaged map enumeration. |
| 189 | + /// </summary> |
| 190 | + [TestMethod] |
| 191 | + public void TestUnmanagedMapEnumeration() |
| 192 | + { |
| 193 | + var count = 0; |
| 194 | + |
| 195 | + foreach (var kvp in _unmanagedMap) |
| 196 | + { |
| 197 | + Assert.AreEqual(kvp.Key * 1.5f, kvp.Value); |
| 198 | + count++; |
| 199 | + } |
| 200 | + |
| 201 | + Assert.AreEqual(10_000, count); |
| 202 | + } |
| 203 | + |
| 204 | + /// <summary> |
| 205 | + /// Tests the initialization and lookup performance comparison. |
| 206 | + /// </summary> |
| 207 | + [TestMethod] |
| 208 | + public void TestInitializationAndLookupPerformanceComparison() |
| 209 | + { |
| 210 | + const int iterations = 100_000; |
| 211 | + var stringData = new Dictionary<int, string>(iterations); |
| 212 | + var floatData = new Dictionary<int, float>(iterations); |
| 213 | + |
| 214 | + for (var i = 0; i < iterations; i++) |
| 215 | + { |
| 216 | + stringData[i] = $"Value_{i}"; |
| 217 | + floatData[i] = i * 1.5f; |
| 218 | + } |
| 219 | + |
| 220 | + // --- Initialization: ImmutableLookupMap<string> |
| 221 | + var swInit1 = Stopwatch.StartNew(); |
| 222 | + var stringMap = new ImmutableLookupMap<int, string>(stringData); |
| 223 | + swInit1.Stop(); |
| 224 | + |
| 225 | + // --- Initialization: ReadOnlyDictionary<string> |
| 226 | + var swInit2 = Stopwatch.StartNew(); |
| 227 | + var readOnlyMap = new ReadOnlyDictionary<int, string>(stringData); |
| 228 | + swInit2.Stop(); |
| 229 | + |
| 230 | + // --- Initialization: ImmutableLookupMapUnmanaged<float> |
| 231 | + var swInit3 = Stopwatch.StartNew(); |
| 232 | + var unmanagedMap = new ImmutableLookupMapUnmanaged<int, float>(floatData); |
| 233 | + swInit3.Stop(); |
| 234 | + |
| 235 | + // --- Lookup: ImmutableLookupMap<string> |
| 236 | + var swLookup1 = Stopwatch.StartNew(); |
| 237 | + for (var i = 0; i < iterations; i++) |
| 238 | + { |
| 239 | + _ = stringMap.Get(i); |
| 240 | + } |
| 241 | + swLookup1.Stop(); |
| 242 | + |
| 243 | + // --- Lookup: ReadOnlyDictionary<string> |
| 244 | + var swLookup2 = Stopwatch.StartNew(); |
| 245 | + for (var i = 0; i < iterations; i++) |
| 246 | + { |
| 247 | + _ = readOnlyMap[i]; |
| 248 | + } |
| 249 | + swLookup2.Stop(); |
| 250 | + |
| 251 | + // --- Lookup: ImmutableLookupMapUnmanaged<float> |
| 252 | + var swLookup3 = Stopwatch.StartNew(); |
| 253 | + for (var i = 0; i < iterations; i++) |
| 254 | + { |
| 255 | + _ = unmanagedMap.Get(i); |
| 256 | + } |
| 257 | + swLookup3.Stop(); |
| 258 | + |
| 259 | + // Dispose unmanaged |
| 260 | + unmanagedMap.Dispose(); |
| 261 | + |
| 262 | + // --- Output Results |
| 263 | + Trace.WriteLine("====== INITIALIZATION TIME ======"); |
| 264 | + Trace.WriteLine($"ImmutableLookupMap<string>: {swInit1.Elapsed.TotalMilliseconds:F3} ms"); |
| 265 | + Trace.WriteLine($"ReadOnlyDictionary<string>: {swInit2.Elapsed.TotalMilliseconds:F3} ms"); |
| 266 | + Trace.WriteLine($"ImmutableLookupMapUnmanaged<float>: {swInit3.Elapsed.TotalMilliseconds:F3} ms"); |
| 267 | + |
| 268 | + Trace.WriteLine("====== LOOKUP TIME ======"); |
| 269 | + Trace.WriteLine($"ImmutableLookupMap<string>: {swLookup1.Elapsed.TotalMilliseconds:F3} ms"); |
| 270 | + Trace.WriteLine($"ReadOnlyDictionary<string>: {swLookup2.Elapsed.TotalMilliseconds:F3} ms"); |
| 271 | + Trace.WriteLine($"ImmutableLookupMapUnmanaged<float>: {swLookup3.Elapsed.TotalMilliseconds:F3} ms"); |
| 272 | + |
| 273 | + // Basic sanity assertions |
| 274 | + Assert.AreEqual(iterations, stringMap.ToList().Count); |
| 275 | + Assert.AreEqual(iterations, readOnlyMap.Count); |
| 276 | + } |
| 277 | + |
| 278 | + } |
| 279 | +} |
0 commit comments