コード例 #1
0
        public void TestRemove()
        {
            object objA = new object();
            object objB = new object();
            object objC = new object();

            var dictionary = new SortedNameValueDictionary <object>();

            dictionary.Add("A", objA);
            dictionary.Add("B", objB);
            dictionary.Add("C", objC);

            dictionary.Count.Should().Be(3);

            dictionary.Remove("X").Should().BeFalse();
            dictionary.Remove("B").Should().BeTrue();
            dictionary.Count.Should().Be(2);
            dictionary.Keys.Count.Should().Be(2);
            dictionary.Values.Count.Should().Be(2);
            dictionary.ContainsKey("B").Should().BeFalse();

            dictionary.Remove("A").Should().BeTrue();
            dictionary.Remove("A").Should().BeFalse();
            dictionary.Count.Should().Be(1);
            dictionary.Keys.Count.Should().Be(1);
            dictionary.Values.Count.Should().Be(1);
            dictionary.ContainsKey("A").Should().BeFalse();

            dictionary.Clear();
            dictionary.Count.Should().Be(0);
            dictionary.Keys.Count.Should().Be(0);
            dictionary.Values.Count.Should().Be(0);
            dictionary.ContainsKey("C").Should().BeFalse();
        }
コード例 #2
0
        public void TestAdd()
        {
            var dictionary = new SortedNameValueDictionary <object>();

            object objZ = new object();

            dictionary.Add("Z", objZ);
            dictionary.Count.Should().Be(1);
            dictionary.ToList().Count.Should().Be(1);
            dictionary.Keys.Count.Should().Be(1);
            dictionary.Values.Count.Should().Be(1);
            dictionary["Z"].Should().Be(objZ);

            object objA = new object();

            dictionary.Add("A", objA);
            dictionary.Count.Should().Be(2);
            dictionary.ToList().Count.Should().Be(2);
            dictionary.Keys.Count.Should().Be(2);
            dictionary.Values.Count.Should().Be(2);
            dictionary["A"].Should().Be(objA);

            var keyList = dictionary.Keys.ToList();

            keyList[0].Should().Be("A");
            keyList[1].Should().Be("Z");

            var valueList = dictionary.Values.ToList();

            valueList[0].Should().Be(objA);
            valueList[1].Should().Be(objZ);

            dictionary.ContainsKey("A").Should().BeTrue();
            dictionary.ContainsKey("Z").Should().BeTrue();
            dictionary.ContainsKey("B").Should().BeFalse();

            dictionary.Contains(new KeyValuePair <string, object>("A", objA)).Should().BeTrue();
            dictionary.Contains(new KeyValuePair <string, object>("Z", objZ)).Should().BeTrue();
            dictionary.Contains(new KeyValuePair <string, object>("B", objA)).Should().BeFalse();
            dictionary.Contains(new KeyValuePair <string, object>("A", objZ)).Should().BeFalse();

            object value = null;

            dictionary.TryGetValue("A", out value).Should().BeTrue();
            value.Should().Be(objA);

            dictionary.TryGetValue("B", out value).Should().BeFalse();

            // Value should be default(TValue) per reference source.
            value.Should().Be(default(object));
        }