Example #1
0
        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");
        }
Example #2
0
        private static void GetOrAddFuncBenchRndNB()
        {
            var dict = new NonBlocking.ConcurrentDictionary <int, string>();
            var cnt  = new Counter32();

            var benchmarkName = "======== Random GetOrAdd Func NonBlocking int->string 1M Ops/sec:";

            Action <int, int> act = (i, threadBias) =>
            {
                // get some random index in [0, 1000000]
                int randomIndex = GetRandomIndex(i, threadBias, 1000000);
                dict.GetOrAdd(randomIndex, (_) => "qq");

                // after making about 1000000 adds, start with a new table
                var c = cnt;
                c.Increment();
                if (Every8K(i) && c.Value > 1000000)
                {
                    if (Interlocked.CompareExchange(ref cnt, new Counter32(), c) == c)
                    {
                        dict = new NonBlocking.ConcurrentDictionary <int, string>();
                    }
                }
            };

            RunBench(benchmarkName, act);
        }
Example #3
0
        private static void TestGetOrAddOrUpdate(int cLevel, int initSize, int threads, int addsPerThread, bool isAdd)
        {
            NonBlocking.ConcurrentDictionary <int, int> dict = new NonBlocking.ConcurrentDictionary <int, int>(cLevel, 1);

            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 < addsPerThread; j++)
                        {
                            if (isAdd)
                            {
                                //call either of the two overloads of GetOrAdd
                                if (j + ii % 2 == 0)
                                {
                                    dict.GetOrAdd(j, -j);
                                }
                                else
                                {
                                    dict.GetOrAdd(j, x => - x);
                                }
                            }
                            else
                            {
                                if (j + ii % 2 == 0)
                                {
                                    dict.AddOrUpdate(j, -j, (k, v) => - j);
                                }
                                else
                                {
                                    dict.AddOrUpdate(j, (k) => - k, (k, v) => - k);
                                }
                            }
                        }
                        if (Interlocked.Decrement(ref count) == 0)
                        {
                            mre.Set();
                        }
                    });
                }
                mre.WaitOne();
            }

            foreach (var pair in dict)
            {
                Assert.Equal(pair.Key, -pair.Value);
            }

            List <int> gotKeys = new List <int>();

            foreach (var pair in dict)
            {
                gotKeys.Add(pair.Key);
            }
            gotKeys.Sort();

            List <int> expectKeys = new List <int>();

            for (int i = 0; i < addsPerThread; i++)
            {
                expectKeys.Add(i);
            }

            Assert.Equal(expectKeys.Count, gotKeys.Count);

            for (int i = 0; i < expectKeys.Count; i++)
            {
                Assert.True(expectKeys[i].Equals(gotKeys[i]),
                            String.Format("* Test '{4}': Level={0}, initSize={1}, threads={2}, addsPerThread={3})" + Environment.NewLine +
                                          "> FAILED.  The set of keys in the dictionary is are not the same as the expected.",
                                          cLevel, initSize, threads, addsPerThread, isAdd ? "GetOrAdd" : "GetOrUpdate"));
            }

            // Finally, let's verify that the count is reported correctly.
            Assert.Equal(addsPerThread, dict.Count);
            Assert.Equal(addsPerThread, dict.ToArray().Length);
        }