Пример #1
0
        public void AddTest()
        {
            SearchablePhoneBook spb = new SearchablePhoneBook();
            spb.addContact(new Contact("John Johansson"));

            Assert.AreEqual(spb.Contacts.Count, 1);
        }
Пример #2
0
        public void AddManyTest()
        {
            SearchablePhoneBook spb = new SearchablePhoneBook();
            spb.addContact(new Contact("John Johansson"));
            spb.addContact(new Contact("Hans Johansson"));
            spb.addContact(new Contact("Karl Johansson"));
            spb.addContact(new Contact("Peter Johansson"));

            Assert.AreEqual(spb.Contacts.Count, 4);
        }
Пример #3
0
        public void DuplicityTest()
        {
            SearchablePhoneBook spb = new SearchablePhoneBook();
            Contact c = new Contact("John Johansson");
            Contact c1 = new Contact("John Johansson");
            Contact c2 = new Contact("John Johansson");
            spb.addContact(c);
            spb.addContact(c1);
            spb.addContact(c2);

            spb.removeContact(c1);

            Assert.AreEqual(spb.Contacts.Count, 2);
            Assert.IsTrue(spb.Contacts.Contains(c));
            Assert.IsTrue(!spb.Contacts.Contains(c1));
            Assert.IsTrue(spb.Contacts.Contains(c2));
        }
Пример #4
0
        public void RemoveTest()
        {
            SearchablePhoneBook spb = new SearchablePhoneBook();
            Contact c = new Contact("John Johansson");
            Contact c2 = new Contact("Karl Johansson");
            Contact c3 = new Contact("Peter Johansson");
            Contact c4 = new Contact("Hans Johansson");
            spb.addContact(c);
            spb.addContact(c2);
            spb.addContact(c3);
            spb.addContact(c4);

            spb.removeContact(c2);
            spb.removeContact(c4);

            Assert.AreEqual(spb.Contacts.Count, 2);
            Assert.IsTrue(spb.Contacts.Any(x => x.Name.StartsWith("John")));
            Assert.IsTrue(spb.Contacts.Any(x => x.Name.StartsWith("Peter")));
        }
Пример #5
0
        public void SearchAddressTest()
        {
            SearchablePhoneBook spb = new SearchablePhoneBook();
            Contact c = new Contact("John Johansson");
            c.addAddress(new Address("Sverige", "Göteborg", "Kungsgatan", 0));
            c.addAddress(new Address("Sverige", "Göteborg", "Drottningsgatan", 0));
            c.addAddress(new Address("Sverige", "Göteborg", "Parken", 0));

            Contact c1 = new Contact("John Johansson");
            Contact c2 = new Contact("John Johansson");
            Contact c3 = new Contact("Hans Johansson");
            Contact c4 = new Contact("Karl Johansson");
            spb.addContact(c);
            spb.addContact(c1);
            spb.addContact(c2);
            spb.addContact(c3);
            spb.addContact(c4);

            Assert.AreEqual(spb.Search("Ku").Count, 1);
            Assert.AreEqual(spb.Search("Ku").FirstOrDefault(), c);
        }
Пример #6
0
        public void TestFieldListenersTest()
        {
            SearchablePhoneBook spb = new SearchablePhoneBook();
            Contact c = new Contact("John Johansson");

            spb.addContact(c);

            Assert.AreEqual(spb.Search("Jo").Count, 1);

            c.addAddress(new Address("Sverige", "Göteborg", "Kungsgatan", 0));

            Assert.AreEqual(spb.Search("Ku").Count, 1);

            c.addEmail(new Email("*****@*****.**", ContactType.WORK, 0));

            Assert.AreEqual(spb.Search("sten").Count, 1);

            c.removeAddress(c.Addresses.ElementAt(0));

            Assert.AreEqual(spb.Search("Ku").Count, 0);
            Assert.AreEqual(spb.Search("sten").Count, 1);
        }
Пример #7
0
        public void SearchTest()
        {
            SearchablePhoneBook spb = new SearchablePhoneBook();
            Contact c = new Contact("John Johansson");
            Contact c1 = new Contact("John Johansson");
            Contact c2 = new Contact("John Johansson");
            spb.addContact(c);
            spb.addContact(c1);
            spb.addContact(c2);

            Assert.AreEqual(spb.Search("Jo").Count, 3);
        }
Пример #8
0
        public void SearchMultipleFieldsTest()
        {
            SearchablePhoneBook spb = new SearchablePhoneBook();
            Contact c = new Contact("John Johansson");
            c.addAddress(new Address("Sverige", "Göteborg", "Kungsgatan", 0));
            c.addAddress(new Address("Sverige", "Göteborg", "Drottningsgatan", 0));
            c.addAddress(new Address("Sverige", "Göteborg", "Parken", 0));
            c.addEmail(new Email("*****@*****.**", ContactType.WORK, 0));

            Contact c1 = new Contact("John Johansson");
            c1.addAddress(new Address("Sverige", "Göteborg", "Kugsten 15", 0));

            Contact c2 = new Contact("John Johansson");
            c2.addNumber(new Number("Kugge 0720564", ContactType.WORK, 0));

            Contact c3 = new Contact("Hans Johansson");
            c3.addEmail(new Email("*****@*****.**", ContactType.WORK, 0));

            Contact c4 = new Contact("Karl Johansson");
            c4.addEmail(new Email("*****@*****.**",ContactType.HOME,0));

            spb.addContact(c);
            spb.addContact(c1);
            spb.addContact(c2);
            spb.addContact(c3);
            spb.addContact(c4);

            Assert.AreEqual(spb.Search("Ku").Count, 5);
        }
Пример #9
0
        public void SearchManyTest()
        {
            SearchablePhoneBook spb = new SearchablePhoneBook();
            Contact c = new Contact("John Johansson");
            Contact c1 = new Contact("John Johansson");
            Contact c2 = new Contact("John Johansson");
            Contact c3 = new Contact("Hans Johansson");
            Contact c4 = new Contact("Karl Johansson");
            spb.addContact(c);
            spb.addContact(c1);
            spb.addContact(c2);
            spb.addContact(c3);
            spb.addContact(c4);

            Assert.AreEqual(spb.Search("Jo").Count, 3);
            Assert.AreEqual(spb.Search("Ha").Count, 1);
            Assert.AreEqual(spb.Search("Ka").Count, 1);
        }
Пример #10
0
        private void insertValue(string key, Contact c, Dictionary<char, SearchablePhoneBook> dict)
        {
            if (!string.IsNullOrEmpty(key))
            {
                char first = char.ToLower(key[0]);

                SearchablePhoneBook spb;
                if (!dict.ContainsKey(first))
                {
                    spb = new SearchablePhoneBook();
                    dict.Add(first, spb);
                }
                else
                {
                    spb = dict[first];
                }

                spb.addContactInternal(c);
                if (key.Length > 1)
                {
                    string rest = key.Substring(1);
                    insertValue(rest, c, spb.searchDictionary);
                }
            }
        }
Пример #11
0
        private IReadOnlyCollection<Contact> findContacts(string s, SearchablePhoneBook book)
        {
            if (string.IsNullOrEmpty(s))
            {
                return book.Contacts;
            }
            else
            {
                char first = char.ToLower(s[0]);
                if (book.contactList.Count == 0)
                {
                    return new List<Contact>();
                }
                else if (book.searchDictionary.ContainsKey(first))
                {
                    SearchablePhoneBook spb = book.searchDictionary[first];
                    if (s.Length > 1)
                    {
                        return findContacts(s.Substring(1), spb);
                    }
                    else
                    {
                        return spb.Contacts.Distinct().ToList();
                    }
                }
                else
                {
                    return new List<Contact>();
                }

            }
        }