Example #1
0
        public void CheckPos()
        {
            var a = new TestMammals((a1, a2) => a1.Name.CompareTo(a2.Name));

            a.Add(new Mammal("B"));
            a.Add(new Mammal("A"));
            a.Add(new Mammal("D"));
            a.Add(new Mammal("F"));
            a.Add(new Mammal("C"));
            a.Add(new Mammal("E"));
            Assert.That(String.Join("", a.Select(m => m.Name)), Is.EqualTo("ABCDEF"));

            for (int i = 0; i < a.Count; ++i)
            {
                Assert.That(a.CheckPosition(i), Is.EqualTo(i), "Nothing changed.");
            }
            CheckList(a, "ABCDEF");

            a[0].Name = "Z";
            CheckList(a, "ZBCDEF");
            Assert.That(a.CheckPosition(0), Is.EqualTo(5));
            CheckList(a, "BCDEFZ");
            a[5].Name = "Z+";
            CheckList(a, "BCDEFZ+");
            Assert.That(a.CheckPosition(5), Is.EqualTo(5));
            CheckList(a, "BCDEFZ+");
            a[5].Name = "A";
            Assert.That(a.CheckPosition(5), Is.EqualTo(0));
            CheckList(a, "ABCDEF");

            a[1].Name = "A";
            Assert.That(a.CheckPosition(1), Is.LessThan(0));
            CheckList(a, "AACDEF");

            a[1].Name = "B";
            Assert.That(a.CheckPosition(1), Is.EqualTo(1));
            CheckList(a, "ABCDEF");

            a[1].Name = "C";
            Assert.That(a.CheckPosition(1), Is.LessThan(0));
            CheckList(a, "ACCDEF");

            a[1].Name = "Z";
            Assert.That(a.CheckPosition(1), Is.EqualTo(5));
            CheckList(a, "ACDEFZ");

            a[5].Name = "D+";
            Assert.That(a.CheckPosition(5), Is.EqualTo(3));
            CheckList(a, "ACDD+EF");

            a[3].Name = "D";
            Assert.That(a.CheckPosition(3), Is.LessThan(0));
            CheckList(a, "ACDDEF");

            a[3].Name = "B";
            Assert.That(a.CheckPosition(3), Is.EqualTo(1));
            CheckList(a, "ABCDEF");
        }
        private static void CheckList(TestMammals a, string p)
        {
            HashSet <Mammal> dup = new HashSet <Mammal>();
            int i = 0;

            while (i < a.Count)
            {
                a[i].Should().NotBeNull();
                dup.Add(a[i]).Should().BeTrue();
                ++i;
            }
            while (i < a.Tab.Length)
            {
                a.Tab[i].Should().BeNull();
                ++i;
            }
            string.Join("", a.Select(m => m.Name)).Should().Be(p);
        }
Example #3
0
        private static void CheckList(TestMammals a, string p)
        {
            HashSet <Mammal> dup = new HashSet <Mammal>();
            int i = 0;

            while (i < a.Count)
            {
                Assert.That(a[i], Is.Not.Null);
                Assert.That(dup.Add(a[i]), Is.True);
                ++i;
            }
            while (i < a.Tab.Length)
            {
                Assert.That(a.Tab[i], Is.Null);
                ++i;
            }
            Assert.That(String.Join("", a.Select(m => m.Name)), Is.EqualTo(p));
        }
        public void CheckPosition_locally_reorders_the_items()
        {
            var a = new TestMammals((a1, a2) => a1.Name.CompareTo(a2.Name));

            a.Add(new Mammal("B"));
            a.Add(new Mammal("A"));
            a.Add(new Mammal("D"));
            a.Add(new Mammal("F"));
            a.Add(new Mammal("C"));
            a.Add(new Mammal("E"));
            String.Join("", a.Select(m => m.Name)).Should().Be("ABCDEF");

            for (int i = 0; i < a.Count; ++i)
            {
                a.CheckPosition(i).Should().Be(i, "Nothing changed.");
            }
            CheckList(a, "ABCDEF");

            a[0].Name = "Z";
            CheckList(a, "ZBCDEF");
            a.CheckPosition(0).Should().Be(5);
            CheckList(a, "BCDEFZ");
            a[5].Name = "Z+";
            CheckList(a, "BCDEFZ+");
            a.CheckPosition(5).Should().Be(5);
            CheckList(a, "BCDEFZ+");
            a[5].Name = "A";
            a.CheckPosition(5).Should().Be(0);
            CheckList(a, "ABCDEF");

            a[1].Name = "A";
            a.CheckPosition(1).Should().BeLessThan(0);
            CheckList(a, "AACDEF");

            a[1].Name = "B";
            a.CheckPosition(1).Should().Be(1);
            CheckList(a, "ABCDEF");

            a[1].Name = "C";
            a.CheckPosition(1).Should().BeLessThan(0);
            CheckList(a, "ACCDEF");

            a[1].Name = "Z";
            a.CheckPosition(1).Should().Be(5);
            CheckList(a, "ACDEFZ");

            a[5].Name = "D+";
            a.CheckPosition(5).Should().Be(3);
            CheckList(a, "ACDD+EF");

            a[3].Name = "D";
            a.CheckPosition(3).Should().BeLessThan(0);
            CheckList(a, "ACDDEF");

            a[3].Name = "B";
            a.CheckPosition(3).Should().Be(1);
            CheckList(a, "ABCDEF");

            var b = new TestMammals((a1, a2) => a1.Name.CompareTo(a2.Name));

            b.Add(new Mammal("B"));
            b.Add(new Mammal("A"));
            String.Join("", b.Select(m => m.Name)).Should().Be("AB");

            b[0].Name = "Z";
            CheckList(b, "ZB");
            b.CheckPosition(0).Should().Be(1);
            CheckList(b, "BZ");

            var c = new TestMammals((a1, a2) => a1.Name.CompareTo(a2.Name), true);

            c.Add(new Mammal("B"));
            c.Add(new Mammal("A"));
            String.Join("", c.Select(m => m.Name)).Should().Be("AB");

            c[0].Name = "Z";
            CheckList(c, "ZB");
            c.CheckPosition(0).Should().Be(1);
            CheckList(c, "BZ");

            var d = new TestMammals((a1, a2) => a1.Name.CompareTo(a2.Name));

            d.Add(new Mammal("B"));
            d.Add(new Mammal("C"));
            String.Join("", d.Select(m => m.Name)).Should().Be("BC");

            d[1].Name = "A";
            CheckList(d, "BA");
            d.CheckPosition(1).Should().Be(0);
            CheckList(d, "AB");
        }