示例#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");
        }
示例#2
0
        public void using_binary_search_algorithms_on_SortedArrayList()
        {
            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"));

            int idx;

            // External use of Util.BinarySearch on the exposed Store of the SortedArrayList.
            {
                idx = Util.BinarySearch <Mammal, string>(a.Tab, 0, a.Count, "E", (m, name) => m.Name.CompareTo(name));
                idx.Should().Be(4);

                idx = Util.BinarySearch <Mammal, string>(a.Tab, 0, a.Count, "A", (m, name) => m.Name.CompareTo(name));
                idx.Should().Be(0);

                idx = Util.BinarySearch <Mammal, string>(a.Tab, 0, a.Count, "Z", (m, name) => m.Name.CompareTo(name));
                idx.Should().Be(~6);
            }
            // Use of the extended SortedArrayList.IndexOf().
            {
                idx = a.IndexOf("E", (m, name) => m.Name.CompareTo(name));
                idx.Should().Be(4);

                idx = a.IndexOf("A", (m, name) => m.Name.CompareTo(name));
                idx.Should().Be(0);

                idx = a.IndexOf("Z", (m, name) => m.Name.CompareTo(name));
                idx.Should().Be(~6);
            }
        }
示例#3
0
        public void IndexOfWithComparison()
        {
            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"));

            int idx;

            // External use of Util.BinarySearch on the exposed Store of the SortedArrayList.
            {
                idx = Util.BinarySearch <Mammal, string>(a.Tab, 0, a.Count, "E", (m, name) => m.Name.CompareTo(name));
                Assert.That(idx, Is.EqualTo(4));

                idx = Util.BinarySearch <Mammal, string>(a.Tab, 0, a.Count, "A", (m, name) => m.Name.CompareTo(name));
                Assert.That(idx, Is.EqualTo(0));

                idx = Util.BinarySearch <Mammal, string>(a.Tab, 0, a.Count, "Z", (m, name) => m.Name.CompareTo(name));
                Assert.That(idx, Is.EqualTo(~6));
            }
            // Use of the extended SortedArrayList.IndexOf().
            {
                idx = a.IndexOf("E", (m, name) => m.Name.CompareTo(name));
                Assert.That(idx, Is.EqualTo(4));

                idx = a.IndexOf("A", (m, name) => m.Name.CompareTo(name));
                Assert.That(idx, Is.EqualTo(0));

                idx = a.IndexOf("Z", (m, name) => m.Name.CompareTo(name));
                Assert.That(idx, Is.EqualTo(~6));
            }
        }
示例#4
0
        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");
        }