Esempio n. 1
0
        public void TestIndexOf()
        {
            ImmutableSortedTreeList <int> .Builder list = ImmutableSortedTreeList.CreateBuilder <int>();
            var reference = new List <int>();

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

            reference.Sort();

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

            Assert.Equal(-1, list.IndexOf(-1));

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

                int firstIndex = list.IndexOf(i);
                Assert.Equal(reference.IndexOf(i, firstIndex + 1), list.IndexOf(i, firstIndex + 1));
            }

            ImmutableSortedTreeList <int> .Builder empty = ImmutableSortedTreeList.CreateBuilder <int>();
            Assert.Equal(-1, empty.IndexOf(0));
        }
Esempio n. 2
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)));
            }
        }