Example #1
0
        public static void TestAddNullValue_IDictionary_ReferenceType_null()
        {
            // using IDictionary interface
            IDictionary dict3 = new LurchTable <string, string>();

            dict3["key"] = null;
            dict3.Add("key2", null);
        }
Example #2
0
        public static void TestAddNullValue_IDictionaryOfString_null()
        {
            // using IDictionary<TKey, TValue> interface
            IDictionary <string, string> dict2 = new LurchTable <string, string>();

            dict2["key"] = null;
            dict2.Add("key2", null);
        }
Example #3
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_Add_KeyOfWrongType()
 {
     if (!IsReadOnly)
     {
         IDictionary dictionary = new LurchTable <string, string>();
         object      missingKey = 23;
         AssertExtensions.Throws <ArgumentException>("key", () => dictionary.Add(missingKey, CreateTValue(12345)));
         Assert.Empty(dictionary);
     }
 }
Example #5
0
        public static void TestAddNullValue_IDictionary_ValueType_null_add()
        {
            Action action = () =>
            {
                IDictionary dict5 = new LurchTable <string, int>();
                dict5.Add("key", null);
            };

            Assert.Throws <ArgumentNullException>(action);
        }
 public void IDictionary_NonGeneric_Add_NullValueWhenDefaultTValueIsNonNull()
 {
     if (!IsReadOnly)
     {
         IDictionary dictionary = new LurchTable <string, int>();
         object      missingKey = GetNewKey(dictionary);
         Assert.Throws <ArgumentNullException>(() => dictionary.Add(missingKey, null));
         Assert.Empty(dictionary);
     }
 }
 public void IDictionary_NonGeneric_Add_ValueOfWrongType()
 {
     if (!IsReadOnly)
     {
         IDictionary dictionary = new LurchTable <string, string>();
         object      missingKey = GetNewKey(dictionary);
         AssertExtensions.Throws <ArgumentException>("value", () => dictionary.Add(missingKey, 324));
         Assert.Empty(dictionary);
     }
 }
        protected override ICollection NonGenericICollectionFactory(int count)
        {
            LurchTable <string, string> list = new LurchTable <string, string>();
            int seed = 13453;

            for (int i = 0; i < count; i++)
            {
                list.Add(CreateT(seed++), CreateT(seed++));
            }
            return(list.Values);
        }
        public void LurchTable_Generic_ValueCollection_GetEnumerator(int count)
        {
            LurchTable <string, string> dictionary = new LurchTable <string, string>();
            int seed = 13453;

            while (dictionary.Count < count)
            {
                dictionary.Add(CreateT(seed++), CreateT(seed++));
            }
            dictionary.Values.GetEnumerator();
        }
        public void CompareTest()
        {
            const int size = 1000000;
            int       reps = 3;
            Stopwatch timer;

            IDictionary <Guid, TestValue> dict = new ConcurrentDictionary <Guid, TestValue>(new Dictionary <Guid, TestValue>(size));
            IDictionary <Guid, TestValue> test = new LurchTable <Guid, TestValue>(size);

            for (int rep = 0; rep < reps; rep++)
            {
                var sample = CreateSample(Guid.NewGuid(), size, 1);

                timer = Stopwatch.StartNew();
                Parallel(1, sample, item => dict.Add(item, new TestValue {
                    Id = item, Count = rep
                }));
                Trace.TraceInformation("Dict Add: {0}", timer.Elapsed);

                timer = Stopwatch.StartNew();
                Parallel(1, sample, item => test.Add(item, new TestValue {
                    Id = item, Count = rep
                }));
                Trace.TraceInformation("Test Add: {0}", timer.Elapsed);

                timer = Stopwatch.StartNew();
                Parallel(1, sample, item => dict[item] = new TestValue {
                    Id = item, Count = rep
                });
                Trace.TraceInformation("Dict Update: {0}", timer.Elapsed);

                timer = Stopwatch.StartNew();
                Parallel(1, sample, item => test[item] = new TestValue {
                    Id = item, Count = rep
                });
                Trace.TraceInformation("Test Update: {0}", timer.Elapsed);

                timer = Stopwatch.StartNew();
                Parallel(1, sample, item => dict.Remove(item));
                Trace.TraceInformation("Dict Rem: {0}", timer.Elapsed);
                Assert.AreEqual(0, dict.Count);

                timer = Stopwatch.StartNew();
                Parallel(1, sample, item => test.Remove(item));
                Trace.TraceInformation("Test Rem: {0}", timer.Elapsed);

                test.Clear();
                dict.Clear();

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
Example #11
0
        public void Dictionary_Generic_RemoveKey_ValidKeyContainedInDictionary(int count)
        {
            LurchTable <TKey, TValue> dictionary = (LurchTable <TKey, TValue>)GenericIDictionaryFactory(count);
            TKey   missingKey = GetNewKey(dictionary);
            TValue outValue;
            TValue inValue = CreateTValue(count);

            dictionary.Add(missingKey, inValue);
            Assert.True(dictionary.Remove(missingKey, out outValue));
            Assert.Equal(count, dictionary.Count);
            Assert.Equal(inValue, outValue);
            Assert.False(dictionary.TryGetValue(missingKey, out outValue));
        }
Example #12
0
        public void Dictionary_Generic_ContainsValue_DefaultValuePresent(int count)
        {
            LurchTable <TKey, TValue> dictionary = (LurchTable <TKey, TValue>)GenericIDictionaryFactory(count);
            int  seed       = 4315;
            TKey notPresent = CreateTKey(seed++);

            while (dictionary.ContainsKey(notPresent))
            {
                notPresent = CreateTKey(seed++);
            }
            dictionary.Add(notPresent, default(TValue));
            Assert.True(dictionary.ContainsValue(default(TValue)));
        }
Example #13
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 #14
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");
        }
Example #15
0
        public static void TestAddValueOfDifferentType()
        {
            Action action = () =>
            {
                IDictionary dict = new LurchTable <string, string>();
                dict["key"] = 1;
            };

            Assert.Throws <ArgumentException>(action);

            action = () =>
            {
                IDictionary dict = new LurchTable <string, string>();
                dict.Add("key", 1);
            };
            Assert.Throws <ArgumentException>(action);
        }
Example #16
0
 public void Add(TKey key, TValue value)
 {
     cache.Add(key, value);
 }