예제 #1
0
        public void GivenRandomPopulatedTreeWhenGetPrevReturnsCorrectValue()
        {
            var tree = new VanEmdeBoasTree16();
            var rand = new Random();
            var list = new List <ushort>();

            for (int i = 0; i <= ushort.MaxValue; i++)
            {
                var elem = (ushort)rand.Next(1 << 16);
                list.Add(elem);
                tree.Add(elem);
            }
            var arr = list.OrderByDescending(x => x).Distinct().ToArray();

            if (arr[0] != ushort.MaxValue)
            {
                Assert.True(tree.TryGetPrevious(ushort.MaxValue, out ushort res));
                Assert.Equal(arr[0], res);
            }
            for (int i = 1; i < arr.Length - 1; i++)
            {
                if (arr[i - 1] - arr[i] > 1)
                {
                    Assert.True(tree.TryGetPrevious((ushort)((arr[i - 1] - arr[i]) / 2 + arr[i]), out ushort prev));
                    Assert.Equal(arr[i], prev);
                }
                Assert.True(tree.TryGetPrevious(arr[i - 1], out ushort res));
                Assert.Equal(arr[i], res);
            }
            Assert.False(tree.TryGetPrevious(arr[arr.Length - 1], out ushort _));
        }
예제 #2
0
        public void GivenVebTreeWhenAddSameNumberThenReturnFalse()
        {
            var tree = new VanEmdeBoasTree16();

            for (int i = 0; i <= ushort.MaxValue; i++)
            {
                Assert.True(tree.Add((ushort)i));
                Assert.False(tree.Add((ushort)i));
            }

            tree = new VanEmdeBoasTree16();
            for (int i = ushort.MaxValue; i > 0; i--)
            {
                Assert.True(tree.Add((ushort)i));
                Assert.False(tree.Add((ushort)i));
            }
        }
예제 #3
0
        public void GivenTreeWhenPopulateItThenCountReturnsAmountOfAddedElements()
        {
            var tree = new VanEmdeBoasTree16();

            Assert.Equal(0U, tree.Count);
            for (uint i = 0; i <= ushort.MaxValue; i++)
            {
                tree.Add((ushort)i);
                Assert.Equal(i + 1, tree.Count);
            }
        }
예제 #4
0
        public void GivenRandomCollectionWhenAddingElementsToTreeThenAddReturnsSameResultAsAddingToSet()
        {
            var tree = new VanEmdeBoasTree16();
            var rand = new Random();
            var set  = new HashSet <uint>();

            for (int i = 0; i <= ushort.MaxValue; i++)
            {
                var elem = (ushort)rand.Next(1 << 16);
                var add  = set.Add(elem);
                Assert.Equal(add, tree.Add(elem));
            }
        }
예제 #5
0
        public void GivenFullVebTreeWhenRemoveNumberThenFindCantFindIt()
        {
            var tree = new VanEmdeBoasTree16();

            for (int i = 0; i <= ushort.MaxValue; i++)
            {
                tree.Add((ushort)i);
            }
            for (int i = 0; i <= ushort.MaxValue; i++)
            {
                tree.Remove((ushort)i);
                Assert.False(tree.Find((ushort)i));
            }
            for (int i = 0; i <= ushort.MaxValue; i++)
            {
                tree.Add((ushort)i);
            }
            for (int i = 0; i <= ushort.MaxValue; i++)
            {
                var toRemove = (ushort)(ushort.MaxValue - i);
                tree.Remove(toRemove);
                Assert.False(tree.Find(toRemove));
            }
        }
예제 #6
0
        public void GivenVebTreePopulatedWithOddNumbersWhenFindReturnsCorrectValue()
        {
            var tree = new VanEmdeBoasTree16();

            for (int i = 0; i <= ushort.MaxValue; i++)
            {
                if (i % 2 == 1)
                {
                    tree.Add((ushort)i);
                }
            }
            for (int i = 0; i <= ushort.MaxValue; i++)
            {
                Assert.Equal(i % 2 == 1, tree.Find((ushort)i));
            }
        }
예제 #7
0
        public void GivenPopulatedVebTreeWhenFindThenAlwaysReturnTrue()
        {
            var tree = new VanEmdeBoasTree16();
            var rand = new Random();
            var set  = new HashSet <ushort>();

            for (int i = 0; i <= ushort.MaxValue; i++)
            {
                var elem = (ushort)rand.Next(1 << 16);
                set.Add(elem);
                tree.Add(elem);
            }
            foreach (var elem in set)
            {
                Assert.True(tree.Find(elem));
            }
        }
예제 #8
0
        public void GivenRandomFilledTreeWhenRemoveThenMatchesArrayState()
        {
            var tree = new VanEmdeBoasTree16();
            var rand = new Random();
            var arr  = new bool[1 << 16];

            for (int i = 0; i <= ushort.MaxValue; i++)
            {
                var elem = (ushort)rand.Next(1 << 16);
                arr[elem] = true;
                tree.Add(elem);
            }

            for (int i = 0; i <= ushort.MaxValue; i++)
            {
                var elem = (ushort)rand.Next(1 << 16);
                Assert.Equal(arr[elem], tree.Remove(elem));
                arr[elem] = false;
            }
        }
예제 #9
0
        public void GivenRandomFilledTreeWhenCallMaxAndMinThenReturnProperValues()
        {
            var    tree = new VanEmdeBoasTree16();
            var    rand = new Random();
            ushort min  = ushort.MaxValue;
            ushort max  = ushort.MinValue;

            for (int i = 0; i < 1000; i++)
            {
                var value = (ushort)rand.Next(1 << 16);
                if (min > value)
                {
                    min = value;
                }
                if (max < value)
                {
                    max = value;
                }
                tree.Add(value);
            }
            Assert.Equal(min, tree.Min);
            Assert.Equal(max, tree.Max);
        }