コード例 #1
0
        public void When_creating_easy_dictionary_with_key_selector_comparer_and_collection()
        {
            var collection = Enumerable.Range(1, 5).Select(n => new Person(n.ToString(), n)).ToList();
            var comparer   = StringComparer.OrdinalIgnoreCase;
            Func <Person, string>           selector = p => p.Id;
            EasyDictionary <string, Person> dic      = new EasyDictionary <string, Person>(
                selector, collection, comparer);

            dic.ShouldNotBeNull();
            dic.KeySelector.ShouldBe(selector);
            dic.Count.ShouldBe(5);
            dic.Keys.ShouldNotBeEmpty();
            dic.Values.ShouldNotBeEmpty();
            dic.IsReadOnly.ShouldBeFalse();
            dic.Comparer.ShouldNotBe(EqualityComparer <string> .Default);
            dic.Comparer.ShouldBe(comparer);

            dic.Keys.ShouldBe(new[] { "1", "2", "3", "4", "5" });
            dic.Values.ShouldBe(collection);

            dic.ContainsKey("1").ShouldBeTrue();
            dic.Contains(collection[0]).ShouldBeTrue();

            dic["1"].Age.ShouldBe(1);

            dic.ContainsKey("0").ShouldBeFalse();
        }
コード例 #2
0
        public void When_creating_easy_dictionary_with_key_selector_comparer_and_dictionary()
        {
            var comparer   = StringComparer.OrdinalIgnoreCase;
            var somePerson = new Person("A", 1);
            var someDic    = new Dictionary <string, Person> {
                ["B"] = somePerson
            };

            Func <Person, string>           selector = p => p.Id;
            EasyDictionary <string, Person> dic      = new EasyDictionary <string, Person>(
                selector, someDic, comparer);

            dic.ShouldNotBeNull();
            dic.KeySelector.ShouldBe(selector);
            dic.Count.ShouldBe(1);
            dic.Keys.ShouldNotBeEmpty();
            dic.Values.ShouldNotBeEmpty();
            dic.IsReadOnly.ShouldBeFalse();
            dic.Comparer.ShouldNotBe(EqualityComparer <string> .Default);
            dic.Comparer.ShouldBe(comparer);

            dic.Keys.ShouldBe(new[] { "A" });
            dic.Values.ShouldBe(new[] { somePerson });

            dic.ContainsKey("A").ShouldBeTrue();
            dic.Contains(somePerson).ShouldBeTrue();

            dic.ContainsKey("B").ShouldBeFalse();

            dic["A"].Age.ShouldBe(1);
        }
コード例 #3
0
        public void When_creating_easy_dictionary_with_key_selector_and_capacity()
        {
            Func <Person, string>           selector = p => p.Id;
            EasyDictionary <string, Person> dic      = new EasyDictionary <string, Person>(selector, 10);

            dic.ShouldNotBeNull();
            dic.KeySelector.ShouldBe(selector);
            dic.Count.ShouldBe(0);
            dic.Keys.ShouldBeEmpty();
            dic.Values.ShouldBeEmpty();
            dic.IsReadOnly.ShouldBeFalse();
            dic.Comparer.ShouldBe(EqualityComparer <string> .Default);
        }
コード例 #4
0
        public void When_using_as_readonly_dictionary()
        {
            var p1 = new Person("A", 1);
            var p2 = new Person("B", 2);

            var comparer = StringComparer.OrdinalIgnoreCase;
            Func <Person, string> selector           = p => p.Id;
            IReadOnlyDictionary <string, Person> dic = new EasyDictionary <string, Person>(
                selector, 10, comparer)
            {
                p1,
                p2
            };

            dic.ShouldNotBeNull();
            dic.Count.ShouldBe(2);

            dic.Keys.ShouldBe(new [] { "A", "B" });
            dic.Values.ShouldBe(new [] { p1, p2 });

            dic.ContainsKey("A").ShouldBeTrue();
            dic.ContainsKey("a").ShouldBeTrue();

            dic.ContainsKey("B").ShouldBeTrue();
            dic.ContainsKey("b").ShouldBeTrue();

            dic.ContainsKey("C").ShouldBeFalse();
            dic.ContainsKey("c").ShouldBeFalse();

            Person found;

            dic.TryGetValue("A", out found).ShouldBeTrue();
            found.ShouldBe(p1);

            dic.TryGetValue("a", out found).ShouldBeTrue();
            found.ShouldBe(p1);

            dic.TryGetValue("B", out found).ShouldBeTrue();
            found.ShouldBe(p2);

            dic.TryGetValue("b", out found).ShouldBeTrue();
            found.ShouldBe(p2);
        }
コード例 #5
0
        public void When_clearing_dictionary()
        {
            EasyDictionary <string, Person> dic = new EasyDictionary <string, Person>(p => p.Id)
            {
                new Person("A", 1),
                new Person("B", 2)
            };

            dic.ShouldNotBeNull();
            dic.Count.ShouldBe(2);

            dic.ContainsKey("A").ShouldBeTrue();
            dic.ContainsKey("B").ShouldBeTrue();

            dic.Clear();

            dic.Count.ShouldBe(0);

            dic.ContainsKey("A").ShouldBeFalse();
            dic.ContainsKey("B").ShouldBeFalse();
        }
コード例 #6
0
        public void When_using_as_collection()
        {
            var p1 = new Person("A", 1);
            var p2 = new Person("B", 2);

            var comparer = StringComparer.OrdinalIgnoreCase;
            Func <Person, string> selector = p => p.Id;
            ICollection <Person>  dic      = new EasyDictionary <string, Person>(
                selector, 10, comparer)
            {
                p1,
                p2
            };

            dic.ShouldNotBeNull();
            dic.Count.ShouldBe(2);

            dic.Contains(p1).ShouldBeTrue();
            dic.Contains(p2).ShouldBeTrue();

            var p3 = new Person("C", 3);

            dic.Add(p3);

            dic.Contains(p3).ShouldBeTrue();

            dic.Remove(p1).ShouldBeTrue();

            dic.Count.ShouldBe(2);

            dic.Contains(p1).ShouldBeFalse();

            dic.Clear();

            dic.Count.ShouldBe(0);
        }
コード例 #7
0
        public void When_creating_easy_dictionary_with_key_selector_and_sequence()
        {
            var seq = Enumerable.Range(1, 5).Select(n => new Person(n.ToString(), n));
            Func <Person, string>           selector = p => p.Id;
            EasyDictionary <string, Person> dic      = new EasyDictionary <string, Person>(selector, seq);

            dic.ShouldNotBeNull();
            dic.KeySelector.ShouldBe(selector);
            dic.Count.ShouldBe(5);
            dic.Keys.ShouldNotBeEmpty();
            dic.Values.ShouldNotBeEmpty();
            dic.IsReadOnly.ShouldBeFalse();
            dic.Comparer.ShouldBe(EqualityComparer <string> .Default);

            dic.Keys.ShouldBe(new[] { "1", "2", "3", "4", "5" });
            dic.Values.ShouldBe(seq);

            dic.ContainsKey("1").ShouldBeTrue();
            dic.Contains(seq.First()).ShouldBeTrue();

            dic["1"].Age.ShouldBe(1);

            dic.ContainsKey("0").ShouldBeFalse();
        }