Exemple #1
0
        public static void TestIDictionary()
        {
            IDictionary dictionary = new NonBlocking.ConcurrentDictionary <string, int>();

            Assert.False(dictionary.IsReadOnly);

            // Empty dictionary should not enumerate
            Assert.Empty(dictionary);

            const int SIZE = 10;

            for (int i = 0; i < SIZE; i++)
            {
                dictionary.Add(i.ToString(), i);
            }

            Assert.Equal(SIZE, dictionary.Count);

            //test contains
            Assert.False(dictionary.Contains(1), "TestIDictionary:  FAILED.  Contain retuned true for incorrect key type");
            Assert.False(dictionary.Contains("100"), "TestIDictionary:  FAILED.  Contain retuned true for incorrect key");
            Assert.True(dictionary.Contains("1"), "TestIDictionary:  FAILED.  Contain retuned false for correct key");

            //test GetEnumerator
            int count = 0;

            foreach (var obj in dictionary)
            {
                DictionaryEntry entry         = (DictionaryEntry)obj;
                string          key           = (string)entry.Key;
                int             value         = (int)entry.Value;
                int             expectedValue = int.Parse(key);
                Assert.True(value == expectedValue,
                            String.Format("TestIDictionary:  FAILED.  Unexpected value returned from GetEnumerator, expected {0}, actual {1}", value, expectedValue));
                count++;
            }

            Assert.Equal(SIZE, count);
            Assert.Equal(SIZE, dictionary.Keys.Count);
            Assert.Equal(SIZE, dictionary.Values.Count);

            //Test Remove
            dictionary.Remove("9");
            Assert.Equal(SIZE - 1, dictionary.Count);

            //Test this[]
            for (int i = 0; i < dictionary.Count; i++)
            {
                Assert.Equal(i, (int)dictionary[i.ToString()]);
            }

            dictionary["1"] = 100; // try a valid setter
            Assert.Equal(100, (int)dictionary["1"]);

            //nonsexist key
            Assert.Null(dictionary["NotAKey"]);
        }
Exemple #2
0
        public static void TestIDictionary_Negative()
        {
            IDictionary dictionary = new NonBlocking.ConcurrentDictionary <string, int>();

            Assert.Throws <ArgumentNullException>(
                () => dictionary.Add(null, 1));
            // "TestIDictionary:  FAILED.  Add didn't throw ANE when null key is passed");

            Assert.Throws <ArgumentException>(
                () => dictionary.Add(1, 1));
            // "TestIDictionary:  FAILED.  Add didn't throw AE when incorrect key type is passed");

            Assert.Throws <ArgumentException>(
                () => dictionary.Add("1", "1"));
            // "TestIDictionary:  FAILED.  Add didn't throw AE when incorrect value type is passed");

            Assert.Throws <ArgumentNullException>(
                () => dictionary.Contains(null));
            // "TestIDictionary:  FAILED.  Contain didn't throw ANE when null key is passed");

            //Test Remove
            Assert.Throws <ArgumentNullException>(
                () => dictionary.Remove(null));
            // "TestIDictionary:  FAILED.  Remove didn't throw ANE when null key is passed");

            //Test this[]
            Assert.Throws <ArgumentNullException>(
                () => { object val = dictionary[null]; });
            // "TestIDictionary:  FAILED.  this[] getter didn't throw ANE when null key is passed");
            Assert.Throws <ArgumentNullException>(
                () => dictionary[null] = 0);
            // "TestIDictionary:  FAILED.  this[] setter didn't throw ANE when null key is passed");

            Assert.Throws <ArgumentException>(
                () => dictionary[1] = 0);
            // "TestIDictionary:  FAILED.  this[] setter didn't throw AE when invalid key type is passed");

            Assert.Throws <ArgumentException>(
                () => dictionary["1"] = "0");
            // "TestIDictionary:  FAILED.  this[] setter didn't throw AE when invalid value type is passed");
        }