Esempio n. 1
0
        public void CopyToTest()
        {
            var destination = new KeyValuePair <string, int> [1];

            testObject.CopyTo(destination, 0);
            Assert.AreElementsEqual(destination, testObject);
        }
        public void TestDictionary()
        {
            // Default ctor.
            var data = Enumerable.Range(1, 5).ToDictionary(x => x, x => x.ToString());
            var dict = new ReadOnlyDictionary <int, string>(data);

            Assert.AreEqual(5, dict.Count);
            Assert.IsTrue(dict.IsReadOnly);
            CollectionAssert.AreEqual(data, dict);
            CollectionAssert.AreEqual(data.Keys, dict.Keys);
            CollectionAssert.AreEqual(data.Values, dict.Values);

            Assert.IsTrue(dict.GetEnumerator().MoveNext());
            Assert.IsTrue(((IEnumerable)dict).GetEnumerator().MoveNext());

            Assert.IsTrue(dict.ContainsKey(1));
            Assert.IsTrue(dict.Contains(new KeyValuePair <int, string>(4, "4")));
            Assert.AreEqual("3", dict[3]);

            string val;

            Assert.IsTrue(dict.TryGetValue(2, out val));
            Assert.AreEqual("2", val);

            var arr = new KeyValuePair <int, string> [5];

            dict.CopyTo(arr, 0);
            CollectionAssert.AreEqual(data, arr);

            Assert.Throws <NotSupportedException>(() => dict.Add(1, "2"));
            Assert.Throws <NotSupportedException>(() => dict.Add(new KeyValuePair <int, string>(1, "2")));
            Assert.Throws <NotSupportedException>(() => dict.Clear());
            Assert.Throws <NotSupportedException>(() => dict.Remove(1));
            Assert.Throws <NotSupportedException>(() => dict.Remove(new KeyValuePair <int, string>(1, "2")));
        }
        public void CopyToTest2()
        {
            ICollection <KeyValuePair <string, string> > target = new ReadOnlyDictionary <string, string>(testDictionary);

            KeyValuePair <string, string>[] data = new KeyValuePair <string, string> [target.Count];
            target.CopyTo(data, 0);
            // Need to Assertion statement to handle this type of argument
        }
        public void TestCopyTo_CopiesPairs()
        {
            ICollection <KeyValuePair <int, int> > dictionary = new ReadOnlyDictionary <int, int>(new Dictionary <int, int>()
            {
                { 1, 2 }, { 2, 3 }
            });

            KeyValuePair <int, int>[] array = new KeyValuePair <int, int> [2];
            dictionary.CopyTo(array, 0);
            Assert.IsTrue(array.Contains(new KeyValuePair <int, int>(1, 2)), "The pair (1, 2) was not copied.");
            Assert.IsTrue(array.Contains(new KeyValuePair <int, int>(2, 3)), "The pair (2, 3) was not copied.");
        }
Esempio n. 5
0
        public void TestCopyToCopiesAllValues()
        {
            var dict = new Dictionary <int, string>()
            {
                { 1, "One" },
                { 2, "Two" },
                { 3, "Three" }
            };

            IDictionary <int, string> toTest = new ReadOnlyDictionary <int, string>(dict);

            Assert.IsTrue(toTest.IsReadOnly);

            var copy = new KeyValuePair <int, string> [3];

            toTest.CopyTo(copy, 0);

            Assert.IsTrue(copy.OrderBy(kv => kv.Key).SequenceEqual(dict.OrderBy(kv => kv.Key)));
        }
Esempio n. 6
0
        public void TestCopyToCopiesAllValuesIntoMiddleOfArray()
        {
            var dict = new Dictionary <int, string>()
            {
                { 1, "One" },
                { 2, "Two" },
                { 3, "Three" }
            };

            IDictionary <int, string> toTest = new ReadOnlyDictionary <int, string>(dict);

            Assert.IsTrue(toTest.IsReadOnly);

            var copy = new KeyValuePair <int, string> [7];

            toTest.CopyTo(copy, 2);

            Assert.AreEqual(default(KeyValuePair <int, string>), copy[0]);
            Assert.AreEqual(default(KeyValuePair <int, string>), copy[1]);
            Assert.AreEqual(default(KeyValuePair <int, string>), copy[5]);
            Assert.AreEqual(default(KeyValuePair <int, string>), copy[6]);
            Assert.IsTrue(copy.Skip(2).Take(3).OrderBy(kv => kv.Key).SequenceEqual(dict.OrderBy(kv => kv.Key)));
        }
Esempio n. 7
0
        public void TestReadOperations()
        {
            var dic = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>() {
            {"key1", "val1"},
            {"key2", "val2"},
            {"key3", "val3"},
              });

              Assert.AreEqual(3, dic.Count);

              Assert.AreEqual("val1", dic["key1"]);
              Assert.IsTrue(dic.Contains(new KeyValuePair<string, string>("key2", "val2")));
              Assert.IsTrue(dic.ContainsKey("key3"));

              foreach (var pair in dic) {
              }

              foreach (var key in dic.Keys) {
              }

              foreach (var val in dic.Values) {
              }

              var pairs = new KeyValuePair<string, string>[3];

              dic.CopyTo(pairs, 0);

              string outval = null;

              Assert.IsTrue(dic.TryGetValue("key1", out outval));
              Assert.AreEqual(outval, "val1");
        }
Esempio n. 8
0
        public void ReadOnlyDictionary_Unit_CopyTo_Optimal()
        {
            IDictionary<String, String> dictionary = new Dictionary<String, String>() {
                { "Key1", "Value1" },
                { "Key2", "Value2" },
                { "Key3", "Value3" }
            };
            ReadOnlyDictionary<String, String> target = new ReadOnlyDictionary<String, String>(dictionary);
            KeyValuePair<String, String>[] array = new KeyValuePair<String, String>[dictionary.Count];
            Int32 arrayIndex = 0;

            target.CopyTo(array, arrayIndex);
            Assert.AreEqual(dictionary.Count, array.Length);
            foreach (KeyValuePair<String, String> pair in array) {
                Assert.IsTrue(dictionary.Contains(pair));
            }
        }
Esempio n. 9
0
        public void ReadOnlyDictionary_Unit_CopyTo_ArrayIsTooSmall()
        {
            IDictionary<String, String> dictionary = new Dictionary<String, String>() {
                { "Key1", "Value1" },
                { "Key2", "Value2" },
                { "Key3", "Value3" }
            };
            ReadOnlyDictionary<String, String> target = new ReadOnlyDictionary<String, String>(dictionary);
            KeyValuePair<String, String>[] array = new KeyValuePair<String, String>[dictionary.Count - 2];
            Int32 arrayIndex = 1;

            target.CopyTo(array, arrayIndex);
        }