Пример #1
0
        public void TestBinarySearchFullList()
        {
            ImmutableSortedTreeList <int> .Builder list = ImmutableSortedTreeList.CreateBuilder <int>();
            var reference = new List <int>();

            for (int i = 0; i < 4 * 8 * 8; i++)
            {
                list.Add(i * 2);
                reference.Add(i * 2);
            }

            // Test below start value
            Assert.Equal(reference.BinarySearch(reference[0] - 1), list.BinarySearch(reference[0] - 1));

            for (int i = 0; i < reference.Count; i++)
            {
                // Test current value
                Assert.Equal(reference.BinarySearch(reference[i]), list.BinarySearch(reference[i]));

                // Test above current value
                Assert.Equal(reference.BinarySearch(reference[i] + 1), list.BinarySearch(reference[i] + 1));
            }

            ImmutableSortedTreeList <int> .Builder empty = ImmutableSortedTreeList.CreateBuilder <int>();
            Assert.Equal(~0, empty.BinarySearch(0));
        }
Пример #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)));
            }
        }