Ejemplo n.º 1
0
        public void TestInsertionLocation()
        {
            var comparer = new ComparisonComparer <StrongBox <int> >((x, y) => x.Value - y.Value);

            ImmutableSortedTreeList <StrongBox <int> > .Builder list = ImmutableSortedTreeList.CreateBuilder(comparer);
            for (int i = 0; i < 1000; i++)
            {
                var value = new StrongBox <int>(Generator.GetInt32(0, 200));
                int index = list.LastIndexOf(value);
                if (index < 0)
                {
                    // No item with this value already exists
                    index = list.FindLastIndex(box => box.Value < value.Value);
                }
                else
                {
                    Assert.Equal(list[index].Value, value.Value);
                }

                if (index < list.Count - 1)
                {
                    Assert.True(list[index + 1].Value > value.Value);
                }

                // The item is inserted after the previous last item with this value (or the last item less than it)
                list.Add(value);
                Assert.Same(list[index + 1], value);
                Assert.Equal(index + 1, list.LastIndexOf(new StrongBox <int>(value.Value)));

                // Check IndexOf as well
                int firstInstance = list.IndexOf(new StrongBox <int>(value.Value));
                Assert.True(firstInstance >= 0 && firstInstance < list.Count);
                Assert.Equal(value.Value, list[firstInstance].Value);
                Assert.True(firstInstance == 0 || list[firstInstance - 1].Value < value.Value);

                // Check BinarySearch consistency
                int binarySearch = list.BinarySearch(new StrongBox <int>(value.Value));
                Assert.True(binarySearch >= firstInstance && binarySearch <= index + 1);
                Assert.Equal(firstInstance, list.BinarySearch(0, firstInstance + 1, new StrongBox <int>(value.Value)));
                Assert.Equal(~firstInstance, list.BinarySearch(0, firstInstance, new StrongBox <int>(value.Value)));
                Assert.Equal(index + 1, list.BinarySearch(index + 1, list.Count - index - 1, new StrongBox <int>(value.Value)));
                Assert.Equal(~(index + 2), list.BinarySearch(index + 2, list.Count - index - 2, new StrongBox <int>(value.Value)));
            }
        }
Ejemplo n.º 2
0
        public void TestLastIndexOf()
        {
            ImmutableSortedTreeList <int> .Builder list = ImmutableSortedTreeList.CreateBuilder <int>();
            var reference = new List <int>();

            Assert.Equal(-1, list.LastIndexOf(0, -1, 0));
            Assert.Equal(-1, reference.LastIndexOf(0, -1, 0));

            Assert.Throws <ArgumentException>(() => list.LastIndexOf(0, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException>(() => list.LastIndexOf(0, -40, -50));
            Assert.Throws <ArgumentOutOfRangeException>(() => list.LastIndexOf(0, 40, 50));

            for (int i = 0; i < 2 * 4 * 4; i++)
            {
                int item = Generator.GetInt32(list.Count + 1);
                list.Add(item);
                reference.Add(item);
            }

            reference.Sort();

            Assert.Throws <ArgumentException>(() => list.LastIndexOf(list[0], list.Count));
            Assert.Throws <ArgumentOutOfRangeException>(() => list.LastIndexOf(list[0], list.Count - 1, -1));
            Assert.Throws <ArgumentOutOfRangeException>(() => list.LastIndexOf(list[0], list.Count - 1, list.Count + 1));

            Assert.Equal(-1, list.LastIndexOf(-1));
            Assert.Equal(-1, list.LastIndexOf(-1, list.Count - 1, list.Count / 2));

            for (int i = 0; i < list.Count; i++)
            {
                Assert.Equal(reference.LastIndexOf(i), list.LastIndexOf(i));

                int lastIndex = list.LastIndexOf(i);
                if (lastIndex < 1)
                {
                    continue;
                }

                Assert.Equal(reference.LastIndexOf(i, lastIndex - 1), list.LastIndexOf(i, lastIndex - 1));
            }
        }