Beispiel #1
0
        public void CouldAddRemoveIncreasing()
        {
            var sd = new SortedDeque <int>();

            for (int i = 0; i < 4; i++)
            {
                sd.Add(i);
            }

            for (int i = 4; i < 100; i++)
            {
                if (i % 2 == 0)
                {
                    sd.RemoveFirst();
                }
                sd.Add(i);
                //Assert.AreEqual(i - 3, sd.First);
                Assert.AreEqual(i, sd.Last);
            }
        }
Beispiel #2
0
        public void CouldAddKVsWithSimilarKey()
        {
            var sd = new SortedDeque <KV <int, int> >(new ZipNComparer <int>(Comparer <int> .Default));

            for (int i = 0; i < 3; i++)
            {
                sd.Add(new KV <int, int>(1, i));
            }
            var expected = 1 * 3;

            int[] values = new int[3];
            foreach (var item in sd)
            {
                values[item.Value] = item.Key;
            }
            var actual = values.Sum();

            Console.WriteLine(actual);
            Assert.AreEqual(expected, actual);
        }
Beispiel #3
0
        public void RemoveTest()
        {
            for (int r = 0; r < 10000; r++)
            {
                var rng = new System.Random();

                var sd  = new SortedDeque <int>();
                var set = new HashSet <int>();
                for (int i = 0; i < 100; i++)
                {
                    for (var next = rng.Next(1000); !set.Contains(next);)
                    {
                        set.Add(next);
                        sd.TryAdd(next);
                    }
                }
                while (sd.Count > 0)
                {
                    var first = sd.RemoveFirst();
                    set.Remove(first);
                    Assert.AreEqual(set.Count, sd.Count);
                    Assert.AreEqual(set.Sum(), sd.Sum());
                    Assert.IsTrue(DequeIsSorted(sd));
                    var c    = 0;
                    var prev = 0;
                    foreach (var e in sd)
                    {
                        if (c == 0)
                        {
                            c++;
                            prev = e;
                        }
                        else
                        {
                            Assert.IsTrue(e > prev);
                            prev = e;
                        }
                    }
                }
            }
        }
Beispiel #4
0
        private bool DequeIsSorted <T>(SortedDeque <T> deque)
        {
            bool isFirst = true;
            T    prev    = default(T);

            foreach (var e in deque)
            {
                if (isFirst)
                {
                    isFirst = false;
                    prev    = e;
                }
                else
                {
                    if (deque._comparer.Compare(e, prev) < 0)
                    {
                        return(false);
                    }
                    prev = e;
                }
            }
            return(true);
        }
Beispiel #5
0
        public void AddRemoveTestWithRemoveElement()
        {
            var rng = new System.Random();

            for (int r = 0; r < 1000; r++)
            {
                var sd  = new SortedDeque <int>();
                var set = new HashSet <int>();
                for (int i = 0; i < 50; i++)
                {
                    for (var next = rng.Next(1000); !set.Contains(next);)
                    {
                        set.Add(next);
                        sd.TryAdd(next);
                    }
                }

                for (int i = 0; i < 1000; i++)
                {
                    for (var next = rng.Next(1000); !set.Contains(next);)
                    {
                        set.Add(next);
                        sd.TryAdd(next);
                    }

                    Assert.AreEqual(set.Count, sd.Count);
                    Assert.AreEqual(set.Sum(), sd.Sum());

                    var first = sd.First;
                    sd.Remove(first);
                    set.Remove(first);
                    Assert.AreEqual(set.Count, sd.Count);
                    Assert.AreEqual(set.Sum(), sd.Sum());
                    Assert.IsTrue(DequeIsSorted(sd));
                }
            }
        }
Beispiel #6
0
        public void TestAddingSmallestValueWhenFirstBucketIsFull()
        {
            int SIZE_OF_BUCKET = 3;
            SortedDeque<int> sortedDeque = new SortedDeque<int>(SIZE_OF_BUCKET);

            List<int> valuesToAdd = new List<int> { 2, 1, 9 };

            for (int i = 0; i < valuesToAdd.Count; ++i)
                sortedDeque.Insert(valuesToAdd[i]);

            valuesToAdd = valuesToAdd.OrderBy(x => x).ToList();

            for (int i = 0; i < valuesToAdd.Count; ++i)
            {
                Assert.AreEqual(valuesToAdd.ElementAt(i), sortedDeque.ElementAt(i));
            }

            Assert.IsFalse(sortedDeque.Empty());
            Assert.AreEqual(valuesToAdd.Count, sortedDeque.SizeTotal);
            Assert.AreEqual(valuesToAdd.Count, sortedDeque.SizeUnique);

            Assert.AreEqual(SIZE_OF_BUCKET, sortedDeque.Capacity);
            Assert.AreEqual(1, sortedDeque.NumberOfBuckets);

            /////////////////////////////////////////////////////////
            const int valueSmallerThanPreviousValuesToAdd = 0;
            sortedDeque.Insert(valueSmallerThanPreviousValuesToAdd);
            Assert.AreEqual(valueSmallerThanPreviousValuesToAdd, sortedDeque.ElementAt(0));

            Assert.IsFalse(sortedDeque.Empty());
            Assert.AreEqual(valuesToAdd.Count + 1, sortedDeque.SizeTotal);
            Assert.AreEqual(valuesToAdd.Count + 1, sortedDeque.SizeUnique);

            Assert.AreEqual(2 * SIZE_OF_BUCKET, sortedDeque.Capacity);
            Assert.AreEqual(2, sortedDeque.NumberOfBuckets);
        }
Beispiel #7
0
        public void TestAddingDuplicatedElements()
        {
            SortedDeque<int> sortedDeque = new SortedDeque<int>();

            List<int> valuesToAdd = new List<int> { 2, 1, 9, 1, 5, 2, 9 };

            for (int i = 0; i < valuesToAdd.Count; ++i)
                sortedDeque.Insert(valuesToAdd[i]);

            valuesToAdd = valuesToAdd.OrderBy(x => x).ToList();

            List<int> uniqueValues = new List<int>(valuesToAdd).Distinct().ToList();

            for (int i = 0; i < uniqueValues.Count; ++i)
            {
                Assert.AreEqual(uniqueValues.ElementAt(i), sortedDeque.At(i));
            }

            Assert.IsFalse(sortedDeque.Empty());
            Assert.AreEqual(valuesToAdd.Count, sortedDeque.SizeTotal);
            Assert.AreEqual(uniqueValues.Count, sortedDeque.SizeUnique);

            Assert.AreEqual(1, sortedDeque.NumberOfBuckets);
        }
Beispiel #8
0
        public void CouldRemoveInTheMiddleSplit()
        {
            var rng = new System.Random();

            var sd  = new SortedDeque <int>();
            var set = new HashSet <int>();

            for (int i = 0; i < 100; i++)
            {
                if (i % 2 == 0)
                {
                    set.Add(i);
                    sd.TryAdd(i);
                }
                else
                {
                    set.Add(-i);
                    sd.TryAdd(-i);
                }
            }
            {
                var midElement = sd._buffer[0]; //[.___    ____]
                sd.Remove(midElement);
                set.Remove(midElement);
                Assert.AreEqual(set.Count, sd.Count);
                Assert.AreEqual(set.Sum(), sd.Sum());
                Assert.IsTrue(DequeIsSorted(sd));
                var c    = 0;
                var prev = 0;
                foreach (var e in sd)
                {
                    if (c == 0)
                    {
                        c++;
                        prev = e;
                    }
                    else
                    {
                        Assert.IsTrue(e > prev);
                        prev = e;
                    }
                }
            }
            {
                var midElement = sd._buffer[sd._buffer.Length - 1]; //[___    ____.]
                sd.Remove(midElement);
                set.Remove(midElement);
                Assert.AreEqual(set.Count, sd.Count);
                Assert.AreEqual(set.Sum(), sd.Sum());
                Assert.IsTrue(DequeIsSorted(sd));
                var c    = 0;
                var prev = 0;
                foreach (var e in sd)
                {
                    if (c == 0)
                    {
                        c++;
                        prev = e;
                    }
                    else
                    {
                        Assert.IsTrue(e > prev);
                        prev = e;
                    }
                }
            }
        }
Beispiel #9
0
        public void TestSimpleAddingIntoSingleBucket()
        {
            List<int> valuesToAdd = new List<int> { 2, 0, 9, 1 };

            int SIZE_OF_BUCKET = 10;
            SortedDeque<int> sortedDeque = new SortedDeque<int>(SIZE_OF_BUCKET);
            for (int i = 0; i < valuesToAdd.Count; ++i)
                sortedDeque.Insert(valuesToAdd[i]);

            valuesToAdd = valuesToAdd.OrderBy(x => x).ToList();

            for (int i = 0; i < valuesToAdd.Count; ++i)
            {
                Assert.AreEqual(valuesToAdd.ElementAt(i), sortedDeque.ElementAt(i));
            }

            Assert.IsFalse(sortedDeque.Empty());
            Assert.AreEqual(valuesToAdd.Count, sortedDeque.SizeTotal);
            Assert.AreEqual(valuesToAdd.Count, sortedDeque.SizeUnique);

            Assert.AreEqual(SIZE_OF_BUCKET, sortedDeque.Capacity);
            Assert.AreEqual(1, sortedDeque.NumberOfBuckets);
        }
Beispiel #10
0
        public void TestReserveWithSmallerNewCapacity()
        {
            int SIZE_OF_BUCKET = 3;
            SortedDeque<int> sortedDeque = new SortedDeque<int>(SIZE_OF_BUCKET);

            int capacityWhichIWant = SIZE_OF_BUCKET * 10;
            sortedDeque.Capacity = capacityWhichIWant;

            int capacityWhichIWant_new = SIZE_OF_BUCKET * 5;
            sortedDeque.Capacity = capacityWhichIWant;

            Assert.AreEqual(capacityWhichIWant / SIZE_OF_BUCKET, sortedDeque.NumberOfBuckets);
            Assert.AreEqual(capacityWhichIWant, sortedDeque.Capacity);

            Assert.IsTrue(sortedDeque.Empty());
            Assert.AreEqual(0, sortedDeque.SizeTotal);
            Assert.AreEqual(0, sortedDeque.SizeUnique);
        }
Beispiel #11
0
        public void TestReserve()
        {
            const int SIZE_OF_BUCKET = 3;
            SortedDeque<int> sortedDeque = new SortedDeque<int>(SIZE_OF_BUCKET);

            const int capacityWhichIWant = SIZE_OF_BUCKET * 10 + 1;
            sortedDeque.Capacity = capacityWhichIWant;

            int expectedNumberOfBuckets = (int)Math.Ceiling((double)(capacityWhichIWant) / (double)(SIZE_OF_BUCKET));
            Assert.AreEqual(expectedNumberOfBuckets, sortedDeque.NumberOfBuckets);

            int expectedCapacity = expectedNumberOfBuckets * SIZE_OF_BUCKET;
            Assert.AreEqual(expectedCapacity, sortedDeque.Capacity);

            Assert.IsTrue(sortedDeque.Empty());
            Assert.AreEqual(0, sortedDeque.SizeTotal);
            Assert.AreEqual(0, sortedDeque.SizeUnique);
        }
Beispiel #12
0
 public void TestConstructionOfEmptyContainer()
 {
     SortedDeque<int> sortedDeque = new SortedDeque<int>();
     Assert.IsTrue(sortedDeque.Empty());
     Assert.AreEqual(0, sortedDeque.SizeTotal);
     Assert.AreEqual(0, sortedDeque.SizeUnique);
     Assert.AreEqual(0, sortedDeque.Capacity);
 }
Beispiel #13
0
        public void TestAddingValueWhichShouldBeAddedBetweenTwoValuesInFullBucketAfterWhichIsAnotherFullBucket()
        {
            int SIZE_OF_BUCKET = 3;
            SortedDeque<double> sortedDeque = new SortedDeque<double>(SIZE_OF_BUCKET);

            List<double> valuesToAddToFirstBucket = new List<double> { 1, 2, 3 };
            for (int i = 0; i < valuesToAddToFirstBucket.Count; ++i)
                sortedDeque.Insert(valuesToAddToFirstBucket[i]);

            List<double> valuesToAddToSecondBucket = new List<double> { 5, 6, 7 };
            for (int i = 0; i < valuesToAddToSecondBucket.Count; ++i)
                sortedDeque.Insert(valuesToAddToSecondBucket[i]);

            double valueWhichShouldBeAddedBetweenTwoFullBuckets = 2.78;
            sortedDeque.Insert(valueWhichShouldBeAddedBetweenTwoFullBuckets);

            /////////////////////////////////////////////////////////

            List<double> allValues = new List<double>();
            allValues.AddRange(valuesToAddToFirstBucket);
            allValues.AddRange(valuesToAddToSecondBucket);
            allValues.Add(valueWhichShouldBeAddedBetweenTwoFullBuckets);
            allValues = allValues.OrderBy(x => x).ToList();

            for (int i = 0; i < allValues.Count; ++i)
            {
                Assert.AreEqual(allValues.ElementAt(i), sortedDeque.ElementAt(i));
            }

            /////////////////////////////////////////////////////////

            int expectedTotalNumberOfElements = valuesToAddToFirstBucket.Count + 1 + valuesToAddToSecondBucket.Count;
            Assert.AreEqual(expectedTotalNumberOfElements, sortedDeque.SizeTotal);
            Assert.AreEqual(expectedTotalNumberOfElements, sortedDeque.SizeUnique);

            Assert.AreEqual(3 * SIZE_OF_BUCKET, sortedDeque.Capacity);
            Assert.AreEqual(3, sortedDeque.NumberOfBuckets);
        }
Beispiel #14
0
        public void TestAddingValueWhichShouldBeAddedBetweenTwoFullBuckets()
        {
            int SIZE_OF_BUCKET = 3;
            SortedDeque<int> sortedDeque = new SortedDeque<int>(SIZE_OF_BUCKET);

            List<int> valuesToAddToFirstBucket = new List<int>{ 1, 2, 3 };
            for (int i = 0; i < valuesToAddToFirstBucket.Count; ++i)
                sortedDeque.Insert(valuesToAddToFirstBucket[i]);

            List<int> valuesToAddToSecondBucket = new List<int> { 5, 6, 7 };
            for (int i = 0; i < valuesToAddToSecondBucket.Count; ++i)
                sortedDeque.Insert(valuesToAddToSecondBucket[i]);

            int valueWhichShouldBeAddedBetweenTwoFullBuckets = 4;
            sortedDeque.Insert(valueWhichShouldBeAddedBetweenTwoFullBuckets);

            /////////////////////////////////////////////////////////

            int expectedTotalNumberOfElements = valuesToAddToFirstBucket.Count + 1 + valuesToAddToSecondBucket.Count;
            Assert.AreEqual(expectedTotalNumberOfElements, sortedDeque.SizeTotal);
            Assert.AreEqual(expectedTotalNumberOfElements, sortedDeque.SizeUnique);

            Assert.AreEqual(3 * SIZE_OF_BUCKET, sortedDeque.Capacity);
            Assert.AreEqual(3, sortedDeque.NumberOfBuckets);
        }