Exemple #1
0
        public void AddRemoveTrim()
        {
            var list = new FreeList <string>();

            var a = list.Add("a");
            var b = list.Add("b");
            var c = list.Add("c");
            var d = list.Add("d");
            var e = list.Add("e");

            list.GetCount().Should().Be(5);

            var items = list.GetValues();

            items.Count().Should().Be(8);

            items.Where(x => x != null).Should().BeEquivalentTo("a", "b", "c", "d", "e");

            list.Remove(c, true);
            list.Remove(e, true);

            list.GetCount().Should().Be(3);

            list.GetValues().Where(x => x != null).Should().BeEquivalentTo("a", "b", "d");

            foreach (var i in Enumerable.Range(0, 30))
            {
                list.Add(i.ToString());                                        // 30
            }
            list.Remove(a, true);
            list.Remove(b, true);
            list.Remove(d, true);

            list.Add("30");

            list.GetValues().Where(x => x != null).Should().BeEquivalentTo(Enumerable.Range(0, 31).Select(x => x.ToString()));
        }
Exemple #2
0
        public void ExpandAndShrink()
        {
            var freeList = new FreeList <string>();

            freeList.GetValues().Length.Should().Be(4);

            var a = freeList.Add("a");
            var b = freeList.Add("b");
            var c = freeList.Add("c");
            var d = freeList.Add("d");

            freeList.GetValues().Length.Should().Be(4);

            var e = freeList.Add("e");
            var f = freeList.Add("f");
            var g = freeList.Add("g");
            var h = freeList.Add("h");

            freeList.GetValues().Length.Should().Be(8);

            foreach (var subscription in new[] { a, b, c, d, e, f, g, h })
            {
                freeList.Remove(subscription, true);
            }
            freeList.GetCount().Should().Be(0);
            freeList.GetValues().Length.Should().Be(8);

            var subscriptions = Enumerable.Range(100, 9).Select(x => freeList.Add(x.ToString())).ToArray();

            freeList.GetCount().Should().Be(9);
            freeList.GetValues().Length.Should().Be(16);

            foreach (var subscription in subscriptions)
            {
                freeList.Remove(subscription, true);
            }
            freeList.GetCount().Should().Be(0);
            freeList.GetValues().Length.Should().Be(4);
        }