public void Delete_ingAKeyFromTheBaseOfAColumn_ShouldUpdateTheKeysInTheColumnToTheCorrectValue
            (Skiplist <int> sut, List <int> keys)
        {
            // Fixture setup
            var expectedKey = sut.Head.Bottom().Right.Key;

            var key = keys.Min();

            // Exercise system
            sut.Delete(key);

            // Verify outcome
            var result = sut.Head.EnumerateDown().Where(node => node.Key != expectedKey).ToList();

            var failureString =
                String.Format(
                    "Removing key {0} from skiplist \n\n{1}\n did not update the keys in the column. Instead, nodes {2} remained",
                    key,
                    sut,
                    String.Join(", ", result));

            Assert.False(result.Any(), failureString);

            // Teardown
        }
Beispiel #2
0
        public void SkipList_Erases()
        {
            var list = new Skiplist <int>(4, 0.5);

            list.Add(1);
            list.Add(2);
            list.Add(1);
            list.Add(1);
            list.Add(1);
            list.Add(2);
            list.Add(3);

            // not in list
            Assert.False(list.Erase(4));

            Assert.True(list.Erase(1));
            Assert.True(list.Erase(2));
            Assert.True(list.Erase(1));
            Assert.True(list.Erase(1));
            Assert.True(list.Erase(1));
            Assert.True(list.Erase(2));
            Assert.True(list.Erase(3));

            // list should be empty
            Assert.False(list.Erase(1));
            Assert.False(list.Erase(2));
            Assert.False(list.Erase(3));

            // not in list
            Assert.False(list.Erase(4));
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            list = new Skiplist();

            while (true)
            {
                //Get the input
                string playerinp = Console.ReadLine();
                //Check if the input is null
                if (playerinp == null)
                    break;

                input = playerinp.Split();

                switch (input[0])
                {
                    case "T":
                        AddPlayer();
                        break;
                    case "G":
                        break;
                    case "R":
                        break;
                }
            }
        }
        public void Delete_ingAKeyFromMinimallySizedGapWithAMoreThanMinimalGapToTheRight_ShouldCauseTheGapToBorrowFromTheOneToTheRight
            (Skiplist <int> sut, List <int> keys)
        {
            // Fixture setup
            var expectedResult = 2;

            var key = keys.Min();

            sut.Head.Down.Down.ConnectTo(sut.Head.Down.Right.Down.Left);

            // Exercise system
            sut.Delete(key);

            // Verify outcome
            var result = sut.Head.SizeOfGap();

            var failureString =
                String.Format(
                    "Removing key {0} from skiplist \n\n{1}\n did not cause the left gap to borrow from the right gap",
                    key,
                    sut);

            Assert.True(result == expectedResult, failureString);

            // Teardown
        }
        public void Delete_ingAKeyFromMinimallySizedGapWithNoGapToTheRight_ShouldCauseTheGapToMergeWithTheOneToTheLeft
            (Skiplist <int> sut, List <int> keys)
        {
            // Fixture setup
            var expectedResult = 3;

            var key = keys.Max();

            foreach (var node in sut.Head.Down.EnumerateRight())
            {
                node.Down.ConnectTo(node.Right.Down.Left);
            }

            // Exercise system
            sut.Delete(key);

            // Verify outcome
            var result = sut.Head.Left.SizeOfGap();

            var failureString =
                String.Format(
                    "Removing key {0} from skiplist \n\n{1}\n did not cause the right gap to merge with the left gap",
                    key,
                    sut);

            Assert.True(result == expectedResult, failureString);

            // Teardown
        }
Beispiel #6
0
        public void Many_levels()
        {
            var sList = new Skiplist();
            var size  = 32;

            for (int i = 0; i < size; i += 2)
            {
                sList.Add(i);
            }
            for (int i = 1; i < size; i += 2)
            {
                sList.Add(i);
            }

            for (int i = 0; i < size; i++)
            {
                Assert.True(sList.Search(i));
            }

            // removing every 3rd element
            for (int i = 0; i < size; i += 3)
            {
                Assert.True(sList.Erase(i));
            }
            // make sure the remaining elements are there
            for (int i = 0; i < size; i++)
            {
                if (i % 3 == 0)
                {
                    continue;
                }
                Assert.True(sList.Search(i));
            }
        }
        public void Delete_ingAKeyFromAPairOfMinimallySizedGaps_ShouldCauseHeightOfTheSkiplistToDropByOne
            (Skiplist <int> sut, List <int> keys)
        {
            // Fixture setup
            var expectedResult = 1;

            var key = keys.Min();

            foreach (var node in sut.Head.Down.EnumerateRight())
            {
                node.Down.ConnectTo(node.Right.Down.Left);
            }

            // Exercise system
            sut.Delete(key);

            // Verify outcome
            var result = sut.Head.Height();

            var failureString =
                String.Format(
                    "Removing key {0} from skiplist \n\n{1}\n did not cause the skiplist to drop in height by 1",
                    key,
                    sut);

            Assert.True(result == expectedResult, failureString);

            // Teardown
        }
Beispiel #8
0
        public void TwoLevels_Erase()
        {
            var list = new Skiplist();

            list.Add(1);
            list.Add(2);
            list.Add(5);
            list.Add(7);
            //next add should trigger new level
            list.Add(10);
            list.Add(12);

            //add random order
            list.Add(9);
            list.Add(4);
            list.Add(19);
            list.Add(17);

            Assert.True(list.Erase(5));
            Assert.False(list.Search(5));

            Assert.True(list.Erase(1));
            Assert.False(list.Search(1));

            Assert.True(list.Search(10));
        }
        public void Delete_ingAKeyFromASingleLevelSkiplistOtherThanTheHead_ShouldDeleteExactlyOneNode
            (Skiplist <int> sut, List <int> keys)
        {
            // Fixture setup
            var expectedResult = keys.Count - 1;

            keys.Remove(sut.Head.Down.Key);
            var key = keys.First();

            // Exercise system
            sut.Delete(key);

            // Verify outcome
            var result = sut.Head.Bottom().EnumerateRight().Count();

            var failureString =
                String.Format(
                    "Removing key {0} from skiplist \n\n{1}\n deleted other than exactly one node",
                    key,
                    sut);

            Assert.True(result == expectedResult, failureString);

            // Teardown
        }
        public void Delete_ingAKeyFromTheBaseOfAColumn_ShouldUpdateTheColumnToPointAtTheNodeToTheRight
            (Skiplist <int> sut, List <int> keys)
        {
            // Fixture setup
            var expectedResult = sut.Head.Bottom().Right;

            var key = keys.Min();

            // Exercise system
            sut.Delete(key);

            // Verify outcome
            var result = sut.Head.Bottom();

            var failureString =
                String.Format(
                    "Removing key {0} from skiplist \n\n{1}\n did not update the column to point at node {2}",
                    key,
                    sut,
                    expectedResult);

            Assert.True(result == expectedResult, failureString);

            // Teardown
        }
Beispiel #11
0
        static void Main(string[] args)
        {
            list = new Skiplist();

            while (true)
            {
                //Get the input
                string playerinp = Console.ReadLine();
                //Check if the input is null
                if (playerinp == null)
                {
                    break;
                }

                input = playerinp.Split();

                switch (input[0])
                {
                case "T":
                    AddPlayer();
                    break;

                case "G":
                    break;

                case "R":
                    break;
                }
            }
        }
Beispiel #12
0
        public void IndexSetterUpdatesExisting()
        {
            var list = new Skiplist<int, int>(KeyStrategy.Unique) { { 1, 1 } };

            list[1] = 123;

            Assert.AreEqual(123, list[1]);
        }
Beispiel #13
0
        public void IndexSetterAddsIfMissing()
        {
            var list = new Skiplist<int, int>(KeyStrategy.Unique);

            list[1] = 123;

            Assert.AreEqual(123, list[1]);
        }
Beispiel #14
0
        public void CreateFrom_WhenGivenNoKeys_ShouldReturnASkiplistWithoutAHead
            (Skiplist <int> sut)
        {
            // Fixture setup

            // Exercise system

            // Verify outcome
            Assert.True(sut.Head == null, "SUT has a head!");

            // Teardown
        }
        public void GetEnumeratorT_IfTheSkiplistIsEmpty_ShouldReturnNothing
            (Skiplist <int> sut)
        {
            // Fixture setup

            // Exercise system
            var result = sut.GetEnumerator().MoveNext();

            // Verify outcome
            Assert.False(result);

            // Teardown
        }
        public void Count_OnANonemptySkiplist_ShouldReturnTheNumberOfNodesInTheBottomLevelOfTheSkiplist
            (Skiplist <int> sut, List <int> keys)
        {
            // Fixture setup
            var expectedResult = keys.Count;

            // Exercise system
            var result = sut.Count;

            // Verify outcome
            Assert.Equal(result, expectedResult);

            // Teardown
        }
        public void Count_IfTheSkiplistIsEmpty_ShouldReturn0
            (Skiplist <int> sut)
        {
            // Fixture setup
            var expectedResult = 0;

            // Exercise system
            var result = sut.Count;

            // Verify outcome
            Assert.Equal(result, expectedResult);

            // Teardown
        }
Beispiel #18
0
        public void DistanceRightTo_OnTheHeadAndTailOfACircularListOfINodes_ShouldReturn1LessThanTheNumberOfNodesInTheList
            (Skiplist <int> list, List <int> keys)
        {
            // Fixture setup
            var expectedResult = keys.Count - 1;

            // Exercise system
            var result = list.Head.Down.DistanceRightTo(list.Head.Down.Left);

            // Verify outcome
            Assert.Equal(expectedResult, result);

            // Teardown
        }
Beispiel #19
0
        public void SkipListAddTest()
        {
            var skiplist = new Skiplist <int>();

            skiplist.Add(5);
            skiplist.Add(50);
            var str = skiplist.ToString();

            skiplist.Add(500);
            skiplist.Add(5000);
            skiplist.Add(51);
            skiplist.Add(501);
            Assert.AreEqual("0 5 50 51 500 501 5000 0 ", skiplist.ToString());
        }
Beispiel #20
0
        public void CreateFrom_WhenGivenFewerKeysThanTheMaxGapSize_ShouldReturnADoublyLinkedList
            (Skiplist <int> sut)
        {
            // Fixture setup

            // Exercise system

            // Verify outcome
            var nodes = sut.Head.Down.EnumerateRight().ToList();

            Assert.False(nodes.Any(node => node.Right.Left != node), "There are nodes for which a right-left move does not return the origin!");
            Assert.False(nodes.Any(node => node.Left.Right != node), "There are nodes for which a left-right move does not return the origin!");

            // Teardown
        }
Beispiel #21
0
        public void CreateFrom_WhenGivenFewerKeysThanTheMaxGapSize_ShouldReturnAListOfNodesContainingAllTheKeysInOrder
            (Skiplist <int> sut, List <int> keys)
        {
            // Fixture setup
            var expectedResult = keys.OrderBy(key => key);

            // Exercise system

            // Verify outcome
            var result = sut.Head.Down.EnumerateRight().Select(node => node.Key);

            Assert.Equal(expectedResult, result);

            // Teardown
        }
Beispiel #22
0
        public void SkipList_Adds_Searches()
        {
            var list = new Skiplist <int>(4, 0.5);

            list.Add(1);
            list.Add(2);
            list.Add(1);
            list.Add(1);
            list.Add(2);
            list.Add(3);

            Assert.True(list.Search(1));
            Assert.True(list.Search(2));
            Assert.True(list.Search(3));
        }
Beispiel #23
0
        public void CreateFrom_WhenGivenMoreKeysThanTheMaxGapsize_ShouldReturnASkiplistWithATopLevelWithFewerNodesThanTheMaxGapsize
            (Skiplist <int> sut)
        {
            // Fixture setup
            var expectedResult = SkiplistFactory.MaximumGapSize;

            // Exercise system

            // Verify outcome
            var result = sut.Head.Down.LengthOfList();

            Assert.True(expectedResult >= result, "There are more nodes in the top level than the maximum gap size!");

            // Teardown
        }
Beispiel #24
0
        public void EnumerateRight_OnACircularListOfINodes_ShouldEncounterEveryKeyInTheList
            (Skiplist <int> list, List <int> keys)
        {
            // Fixture setup
            var expectedResults = keys.OrderBy(key => key);

            // Exercise system
            var nodesInList = list.Head.Down.EnumerateRight();

            // Verify outcome
            var results = nodesInList.Select(node => node.Key);

            Assert.Equal(expectedResults, results);

            // Teardown
        }
        public void CopyTo_GivenASufficienlySizedArrayAndAnIndexOf2_ShouldCopyTheKeysInTheSkiplistToTheArrayInOrderFromIndex2
            (Skiplist <int> sut, List <int> keys)
        {
            // Fixture setup
            var expectedResult = new[] { 0, 0, 0 }.Concat(keys.OrderBy(x => x)).ToArray();

            // Exercise system
            var result = new int[expectedResult.Count()];

            sut.CopyTo(result, 3);

            // Verify outcome
            Assert.Equal(expectedResult, result);

            // Teardown
        }
Beispiel #26
0
        public void CreateFrom_WhenGivenMoreKeysThanTheMaxGapsize_ShouldReturnASkiplistWithEveryUpperLevelWithFewerNodesInTheGapThanTheMaxGapsize
            (Skiplist <int> sut)
        {
            // Fixture setup
            var expectedResult = SkiplistFactory.MaximumGapSize;

            // Exercise system
            Debug.WriteLine(sut);

            // Verify outcome
            var upperLevels     = sut.Head.EnumerateDown().TakeWhile(level => level.Down != null);
            var upperLevelNodes = upperLevels.SelectMany(level => level.EnumerateRight());
            var results         = upperLevelNodes.Select(node => node.SizeOfGap());

            Assert.True(results.All(result => expectedResult >= result), "There are more nodes in the top level than the maximum gap size!");

            // Teardown
        }
        public void Delete_ingAKeyFromAnEmptySkiplist_ShouldReturnFalse
            (Skiplist <int> sut, int key)
        {
            // Fixture setup

            // Exercise system
            var result = sut.Delete(key);

            // Verify outcome
            var failureString =
                String.Format(
                    "Removing key {0} from skiplist \n\n{1}\n returned true despite the skiplist being empty",
                    key,
                    sut);

            Assert.False(result, failureString);

            // Teardown
        }
Beispiel #28
0
        public void OneLevel()
        {
            var list = new Skiplist();

            list.Add(1);
            list.Add(5);
            list.Add(10);

            Assert.True(list.Search(1));
            Assert.True(list.Search(5));
            Assert.True(list.Search(10));

            Assert.True(list.Erase(5));
            Assert.False(list.Search(5));

            list.Add(4);
            list.Add(4);
            Assert.True(list.Erase(4));
            Assert.True(list.Search(4));
        }
Beispiel #29
0
        public void Find_GivenAKeyThatIsInTheSkiplist_ShouldReturnTrue
            (Skiplist <int> sut, List <int> keys)
        {
            // Fixture setup
            var key = keys.First();

            // Exercise system
            var result = sut.Find(key);

            // Verify outcome
            var failureString =
                String.Format(
                    "Failed to find key {0} in skiplist \n\n {1}",
                    key,
                    sut);

            Assert.True(result, failureString);

            // Teardown
        }
        public void Insert_ingAKeyIntoASingleLayerSkiplist_ShouldAddThatKeyToTheSkiplist
            (Skiplist <int> sut, int key)
        {
            // Fixture setup

            // Exercise system
            sut.Insert(key);

            // Verify outcome
            var result = sut.Head.Bottom().EnumerateRight().Any(node => node.Key == key);

            var failureString =
                String.Format(
                    "Key {0} was inserted into skiplist \n\n{1}\n\n but was subsequently not found",
                    key,
                    sut);

            Assert.True(result, failureString);

            // Teardown
        }
Beispiel #31
0
        public void Find_GivenAKeyThatIsNotInTheSkiplist_ShouldReturnFalse
            (Skiplist <int> sut, List <int> keys)
        {
            // Fixture setup
            var missingKeys = Enumerable.Range(keys.Min(), keys.Max()).Except(keys).ToList();
            var missingKey  = missingKeys[new Random().Next(missingKeys.Count())];

            // Exercise system
            var result = sut.Find(missingKey);

            // Verify outcome
            var failureString =
                String.Format(
                    "Found key {0} that should be missing from skiplist \n\n {1}",
                    missingKey,
                    sut);

            Assert.False(result, failureString);

            // Teardown
        }
        public void Delete_ingTheKeyOfTheHeadOfASingleNodeSkiplist_ShouldLeaveSkiplistEmpty
            (Skiplist <int> sut)
        {
            // Fixture setup
            var key = sut.Head.Key;

            // Exercise system
            sut.Delete(key);

            // Verify outcome
            var result = sut.Head == null;

            var failureString =
                String.Format(
                    "Removing key {0} from single-node skiplist \n\n{1}\n left the head intact",
                    key,
                    sut);

            Assert.True(result, failureString);

            // Teardown
        }
Beispiel #33
0
        public void Find_GivenACustomOrdererWhichConsidersManyKeysEqual_ShouldStillReturnTrueIfAnElementSatisfyingItsEqualityComparerExists
            (Skiplist <int> sut, List <int> keys)
        {
            // Fixture setup
            sut.InOrder = (keyA, keyB, keyC) => true;

            var key = keys.First();

            // Exercise system
            var result = sut.Find(key);

            // Verify outcome
            var failureString =
                String.Format(
                    "Failed to find key {0} in skiplist \n\n {1}",
                    key,
                    sut);

            Assert.True(result, failureString);

            // Teardown
        }
Beispiel #34
0
 public void ThrowsOnDuplicateKey()
 {
     var list = new Skiplist<int, int>(KeyStrategy.Unique) {{1, 1}};
     Assert.Throws<DuplicateKeyException>(() => list.Add(1, 2));
 }