public void TestTrimExcess() { Random random = new Random(); TreeSet <int> set = new TreeSet <int>(branchingFactor: 4); SortedSet <int> 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); TreeSet <int> empty = new TreeSet <int>(); empty.Validate(ValidationRules.RequirePacked); empty.TrimExcess(); empty.Validate(ValidationRules.RequirePacked); TreeSet <int> single = new TreeSet <int>(Enumerable.Range(0, 1)); single.Validate(ValidationRules.RequirePacked); single.TrimExcess(); single.Validate(ValidationRules.RequirePacked); // Construct a poorly-packed list with several levels TreeSet <int> binary = new TreeSet <int>(branchingFactor: 2); for (int i = 99; i >= 0; i--) { binary.Add(i); } binary.TrimExcess(); binary.Validate(ValidationRules.RequirePacked); // Construct a poorly-packed list with several levels TreeSet <int> ternary = new TreeSet <int>(branchingFactor: 3, collection: Enumerable.Range(0, 100), comparer: null); for (int i = 99; i >= 0; i--) { ternary.Add(i); } ternary.TrimExcess(); ternary.Validate(ValidationRules.RequirePacked); }