Esempio n. 1
0
        public void BinaryTreeMustSortTheSameAsSortedDictionary()
        {
            // Arrange
            var asm = Assembly.GetExecutingAssembly();
            var importer = new Importer(asm.GetManifestResourceStream("ClientImport.Tests.records.txt"), ',');
            var dictionary = new SortedDictionary<string, IPerson>();
            var tree = new BinarySearchTree<string, IPerson>();

            //Act
            importer.Data
                           .ToList()
                           .ForEach(record =>
                           {
                               var person = new Person
                               {
                                   FirstName = record.FirstName,
                                   Surname = record.Surname,
                                   Age = Convert.ToInt16(record.Age)
                               };
                               var key = PersonRepository.BuildKey(person, SortKey.SurnameFirstNameAge);
                               if (tree.Find(key) == null)
                                   tree.Add(key, person);
                           }
                              );

            tree
                .ToList()
                .Shuffle() //Shuffle result from binary tree, to test sorting
                .ForEach(r => dictionary.Add(PersonRepository.BuildKey(r.KeyValue.Value, SortKey.SurnameFirstNameAge), r.KeyValue.Value));

            var expected = dictionary.Select(r => r.Value).ToList();
            var actual = tree.Select(n => n.KeyValue.Value).ToList();
            // Assert
            CollectionAssert.AreEqual(expected, actual);
        }
Esempio n. 2
0
        public void CanAddAPerson()
        {
            // Arrange
            var repository = new PersonRepository();
            var person1 = new Person { Age = 1, FirstName = "Alpha", Surname = "Beta" };

            //Act
            repository.Add(person1);

            // Assert
            Assert.AreEqual(repository.ToList().First(), person1);
        }
Esempio n. 3
0
        public void CanDeletePerson()
        {
            // Arrange
            var repository = new PersonRepository();
            var person1 = new Person { Age = 1, FirstName = "Zebra", Surname = "Zebra" };
            var person2 = new Person { Age = 1, FirstName = "Alpha", Surname = "Beta" };
            var person3 = new Person { Age = 1, FirstName = "MandM", Surname = "MandM" };

            //Act
            repository.Add(person1);
            repository.Add(person2);
            repository.Add(person3);
            repository.Delete(person2);

            // Assert
            Assert.AreEqual(repository.Count(), 2);
            Assert.IsTrue(repository.Contains(person3));
            Assert.IsTrue(repository.Contains(person1));
            Assert.IsFalse(repository.Contains(person2));
        }
Esempio n. 4
0
        public void DataIsSortedUsingDefaultSortKey()
        {
            // Arrange
            var repository = new PersonRepository();
            var person1 = new Person {Age = 1, FirstName = "Alpha", Surname = "Beta"};
            var person2 = new Person { Age = 2, FirstName = "Aleph", Surname = "Bate" };
            var person3 = new Person { Age = 0, FirstName = "Aleph", Surname = "Bates" };

            //Act
            repository.Add(person1);
            repository.Add(person3);
            repository.Add(person2);

            // Assert
            Assert.AreEqual(repository.First(), person2);
            Assert.AreEqual(repository.Last(), person1);
        }