public void TestTrimExcess()
        {
            var random = new Random();

            ImmutableSortedTreeSet <int> .Builder set = ImmutableSortedTreeSet.CreateBuilder <int>();
            var reference = new SortedSet <int>();

            for (int i = 0; i < 2 * 4 * 4; i++)
            {
                int value = random.Next(set.Count + 1);
                set.Add(i);
                reference.Add(i);
            }

            set.Validate(ValidationRules.None);

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

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

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

            var single = ImmutableSortedTreeSet.CreateRange <int>(Enumerable.Range(0, 1)).ToBuilder();

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

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

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

            // Construct a poorly-packed list with several levels
            var ternary = ImmutableSortedTreeSet.CreateRange <int>(comparer: null, Enumerable.Range(0, 100)).ToBuilder();

            for (int i = 99; i >= 0; i--)
            {
                ternary.Add(i);
            }

            ternary.TrimExcess();
            ternary.Validate(ValidationRules.RequirePacked);
        }
        public void TestAddStaysPacked()
        {
            ImmutableSortedTreeSet <int> .Builder set = ImmutableSortedTreeSet.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());

                set.Add(i);
                set.Validate(ValidationRules.RequirePacked);
            }
        }
        public void TestContains()
        {
            ImmutableSortedTreeSet <int> .Builder set = ImmutableSortedTreeSet.CreateBuilder <int>();
            for (int i = 0; i < 4 * 8 * 8; i++)
            {
                int value = Generator.GetInt32(set.Count + 1);
                set.Add(i);

                // Use set.Contains(i) since this is a targeted collection API test
#pragma warning disable xUnit2017 // Do not use Contains() to check if a value exists in a collection
                Assert.True(set.Contains(i));
#pragma warning restore xUnit2017 // Do not use Contains() to check if a value exists in a collection
            }

            set.Validate(ValidationRules.None);
        }