public static void TestExceptions() { var dictionary = new NonBlocking.ConcurrentDictionary <string, int>(); Assert.Throws <ArgumentNullException>( () => dictionary.TryAdd(null, 0)); // "TestExceptions: FAILED. TryAdd didn't throw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.ContainsKey(null)); // "TestExceptions: FAILED. Contains didn't throw ANE when null key is passed"); int item; Assert.Throws <ArgumentNullException>( () => dictionary.TryRemove(null, out item)); // "TestExceptions: FAILED. TryRemove didn't throw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.TryGetValue(null, out item)); // "TestExceptions: FAILED. TryGetValue didn't throw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => { var x = dictionary[null]; }); // "TestExceptions: FAILED. this[] didn't throw ANE when null key is passed"); Assert.Throws <KeyNotFoundException>( () => { var x = dictionary["1"]; }); // "TestExceptions: FAILED. this[] TryGetValue didn't throw KeyNotFoundException!"); Assert.Throws <ArgumentNullException>( () => dictionary[null] = 1); // "TestExceptions: FAILED. this[] didn't throw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.GetOrAdd(null, (k) => 0)); // "TestExceptions: FAILED. GetOrAdd didn't throw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.GetOrAdd("1", null)); // "TestExceptions: FAILED. GetOrAdd didn't throw ANE when null valueFactory is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.GetOrAdd(null, 0)); // "TestExceptions: FAILED. GetOrAdd didn't throw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.AddOrUpdate(null, (k) => 0, (k, v) => 0)); // "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null key is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.AddOrUpdate("1", null, (k, v) => 0)); // "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null updateFactory is passed"); Assert.Throws <ArgumentNullException>( () => dictionary.AddOrUpdate(null, (k) => 0, null)); // "TestExceptions: FAILED. AddOrUpdate didn't throw ANE when null addFactory is passed"); dictionary.TryAdd("1", 1); Assert.Throws <ArgumentException>( () => ((IDictionary <string, int>)dictionary).Add("1", 2)); // "TestExceptions: FAILED. IDictionary didn't throw AE when duplicate key is passed"); }
private static void GetBenchRndNB() { var dict = new NonBlocking.ConcurrentDictionary <int, string>(); Parallel.For(0, 100000, (i) => dict[i] = "qq"); Parallel.For(0, 100000, (i) => { var dummy = dict[i]; }); var benchmarkName = "======== Random Get NonBlocking int->string 1M Ops/sec:"; Action <int, int> act = (i, threadBias) => { string dummy; int randomIndex = GetRandomIndex(i, threadBias, 100000); dict.TryGetValue(randomIndex, out dummy); }; RunBench(benchmarkName, act); }
private static void TestRead1(int cLevel, int threads, int readsPerThread) { IDictionary <int, int> dict = new NonBlocking.ConcurrentDictionary <int, int>(cLevel, 1); for (int i = 0; i < readsPerThread; i += 2) { dict[i] = i; } int count = threads; using (ManualResetEvent mre = new ManualResetEvent(false)) { for (int i = 0; i < threads; i++) { int ii = i; Task.Run( () => { for (int j = 0; j < readsPerThread; j++) { int val = 0; if (dict.TryGetValue(j, out val)) { Assert.Equal(0, j % 2); Assert.Equal(j, val); } else { Assert.Equal(1, j % 2); } } if (Interlocked.Decrement(ref count) == 0) { mre.Set(); } }); } mre.WaitOne(); } }