Example #1
0
        public static void TestIDictionary()
        {
            IDictionary dictionary = new LurchTable <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 returned true for incorrect key type");
            Assert.False(dictionary.Contains("100"), "TestIDictionary:  FAILED.  Contain returned true for incorrect key");
            Assert.True(dictionary.Contains("1"), "TestIDictionary:  FAILED.  Contain returned 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"]);

            //non-existing key
            Assert.Null(dictionary["NotAKey"]);
        }
 public void IDictionary_NonGeneric_Contains_KeyOfWrongType()
 {
     if (!IsReadOnly)
     {
         IDictionary dictionary = new LurchTable <string, int>();
         Assert.False(dictionary.Contains(1));
     }
 }
Example #3
0
        public void Dictionary_Generic_ContainsValue_Present(int count)
        {
            LurchTable <TKey, TValue> dictionary = (LurchTable <TKey, TValue>)GenericIDictionaryFactory(count);
            int seed = 4315;

            SCG.KeyValuePair <TKey, TValue> notPresent = CreateT(seed++);
            while (dictionary.Contains(notPresent))
            {
                notPresent = CreateT(seed++);
            }
            dictionary.Add(notPresent.Key, notPresent.Value);
            Assert.True(dictionary.ContainsValue(notPresent.Value));
        }
Example #4
0
        public static void TestIDictionary_Negative()
        {
            IDictionary dictionary = new LurchTable <string, int>();

            Assert.Null(Record.Exception(
                            () => dictionary.Add(null, 1)));
            // "TestIDictionary:  FAILED.  Add threw ANE when null key is passed");

            // Invalid key type.
            AssertExtensions.Throws <ArgumentException>("key", () => dictionary.Add(1, 1));

            // Invalid value type.
            AssertExtensions.Throws <ArgumentException>("value", () => dictionary.Add("1", "1"));

            Assert.Null(Record.Exception(
                            () => dictionary.Contains(null)));
            // "TestIDictionary:  FAILED.  Contain threw ANE when null key is passed");

            //Test Remove
            Assert.Null(Record.Exception(
                            () => dictionary.Remove(null)));
            // "TestIDictionary:  FAILED.  Remove threw ANE when null key is passed");

            //Test this[]
            Assert.Null(Record.Exception(
                            () => { object val = dictionary[null]; }));
            // "TestIDictionary:  FAILED.  this[] getter threw ANE when null key is passed");
            Assert.Null(Record.Exception(
                            () => dictionary[null] = 0));
            // "TestIDictionary:  FAILED.  this[] setter threw ANE when null key is passed");

            // Invalid key type.
            AssertExtensions.Throws <ArgumentException>("key", () => dictionary[1] = 0);

            // Invalid value type.
            AssertExtensions.Throws <ArgumentException>("value", () => dictionary["1"] = "0");
        }