Example #1
0
        public void TestTrimExcess()
        {
            ImmutableSortedTreeList <int> .Builder list = ImmutableSortedTreeList.CreateBuilder <int>();
            List <int> reference = new List <int>();

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

            reference.Sort();

            list.Validate(ValidationRules.None);

            // In the first call to TrimExcess, items will move
            list.TrimExcess();
            list.Validate(ValidationRules.RequirePacked);
            Assert.Equal(reference, list);

            // In the second call, the list is already packed so nothing will move
            list.TrimExcess();
            list.Validate(ValidationRules.RequirePacked);
            Assert.Equal(reference, list);

            ImmutableSortedTreeList <int> .Builder empty = ImmutableSortedTreeList.CreateBuilder <int>();
            empty.Validate(ValidationRules.RequirePacked);
            empty.TrimExcess();
            empty.Validate(ValidationRules.RequirePacked);

            var single = ImmutableSortedTreeList.CreateRange(Enumerable.Range(0, 1)).ToBuilder();

            single.Validate(ValidationRules.RequirePacked);
            single.TrimExcess();
            single.Validate(ValidationRules.RequirePacked);

            // Construct a poorly-packed list with several levels
            ImmutableSortedTreeList <int> .Builder binary = ImmutableSortedTreeList.CreateBuilder <int>();
            for (int i = 5000; i >= 0; i--)
            {
                binary.Add(i);
            }

            binary.TrimExcess();
            binary.Validate(ValidationRules.RequirePacked);

            // Construct a poorly-packed list with several levels
            ImmutableSortedTreeList <int> .Builder ternary = ImmutableSortedTreeList.CreateRange <int>(comparer: null, Enumerable.Range(0, 5000)).ToBuilder();
            for (int i = 5000; i >= 0; i--)
            {
                ternary.Add(i);
            }

            ternary.TrimExcess();
            ternary.Validate(ValidationRules.RequirePacked);
        }
Example #2
0
        public void TestRemoveAt()
        {
            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.Add(item);
                reference.Add(item);
            }

            reference.Sort();

            while (list.Count > 0)
            {
                int index = Generator.GetInt32(list.Count);
                Assert.Equal(reference[index], list[index]);
                reference.RemoveAt(index);
                list.RemoveAt(index);
                list.Validate(ValidationRules.None);

                Assert.Equal(reference, list);
            }

            Assert.Empty(list);
            Assert.Empty(reference);
        }
Example #3
0
        public void TestAddStaysPacked()
        {
            ImmutableSortedTreeList <int> .Builder list = ImmutableSortedTreeList.CreateBuilder <int>();
            for (int i = 0; i < 4 * 8 * 8; i++)
            {
                // This test assumes items are being added are already ordered by hash code
                Assert.Equal(i, i.GetHashCode());

                list.Add(i);
                list.Validate(ValidationRules.RequirePacked);
            }
        }