public void Find_ValueInFineGrainedTree_ReturnValue(IEnumerable <int> elements, int value) { var tree = new FineGrainedSyncBinaryTree <int>(elements); var actual = tree.Find(value); Assert.AreEqual(value, actual); }
public void Find_NonExistentValueInFineTree_ReturnNull(IEnumerable <int> elements, int value) { var tree = new FineGrainedSyncBinaryTree <int>(elements); var actual = tree.Find(value); Assert.IsNull(actual); }
public void Find_ValueInEmptyFineTree_ReturnNull(int value) { var tree = new FineGrainedSyncBinaryTree <int>(); var actual = tree.Find(value); Assert.IsNull(actual); }
public void InsertSequentially_InsertFindConcurrently_ManyValuesToFineTree_UpdateTree(int workers, int maxSize, int maxValue, int maxTimeout) { var actualTree = new FineGrainedSyncBinaryTree <int>(); var elementsToInsertSeq = new ConcurrentQueue <int>(); var elementsToInsertConcur = new ConcurrentQueue <int>(); for (int i = 0; i < maxSize; ++i) { var value = _random.Next(maxValue); elementsToInsertSeq.Enqueue(value); actualTree.Insert(value); value = _random.Next(maxValue); elementsToInsertConcur.Enqueue(value); } var elementsToFind = new ConcurrentQueue <int>(elementsToInsertSeq); Task[] tasks = new Task[workers]; for (int i = 0; i < workers; ++i) { if (_random.Next(2) == 0) { // Create worker to test insertion tasks[i] = Task.Run(() => { while (!elementsToInsertConcur.IsEmpty) { if (elementsToInsertConcur.TryDequeue(out var value)) { actualTree.Insert(value); } Thread.Sleep(_random.Next(maxTimeout)); } }); } else { // Create worker to test search tasks[i] = Task.Run(() => { while (!elementsToFind.IsEmpty) { if (elementsToFind.TryDequeue(out var value)) { Assert.AreEqual(value, actualTree.Find(value)); } Thread.Sleep(_random.Next(maxTimeout)); } }); } } Task.WaitAll(tasks); Assert.IsTrue(Utils.ValidateStructure(actualTree)); }
public void Remove_RootInFineTree_ReturnEmptyTree(int value) { var tree = new FineGrainedSyncBinaryTree <int>(); tree.Insert(value); tree.Remove(value); Assert.IsNull(tree.Root); }
public void Insert_RootValueToEmptyFineTree_UpdateTree(int value) { var tree = new FineGrainedSyncBinaryTree <int>(); tree.Insert(value); Assert.AreEqual(value, tree.Root.Value); Assert.AreEqual(null, tree.Root.Left); Assert.AreEqual(null, tree.Root.Right); }
public void Insert_ExistentValueToFineTree_ReturnSameTree(int value) { var tree = new FineGrainedSyncBinaryTree <int>(); tree.Insert(value); tree.Insert(value); Assert.AreEqual(value, tree.Root.Value); Assert.AreEqual(null, tree.Root.Left); Assert.AreEqual(null, tree.Root.Right); }
public void Remove_NonExistentNodeInFineTree_ReturnEmptyTree(int value) { var tree = new FineGrainedSyncBinaryTree <int>(); tree.Insert(value); Assert.DoesNotThrow(() => tree.Remove(value + 1)); Assert.AreEqual(value, tree.Root.Value); Assert.IsNull(tree.Root.Right); Assert.IsNull(tree.Root.Left); }
public void Remove_LeftLeafInFineTree_UpdateTree(int rootValue, int leftValue) { var tree = new FineGrainedSyncBinaryTree <int>(); tree.Insert(rootValue); tree.Insert(leftValue); tree.Remove(leftValue); Assert.AreEqual(rootValue, tree.Root.Value); Assert.IsNull(tree.Root.Left); Assert.IsNull(tree.Root.Right); }
public void Remove_RootInFineTree_SetNewRootFromRight(int rootValue, int rightValue, int rightOfRightValue) { var tree = new FineGrainedSyncBinaryTree <int>(); tree.Insert(rootValue); tree.Insert(rightValue); tree.Insert(rightOfRightValue); tree.Remove(rootValue); Assert.AreEqual(rightValue, tree.Root.Value); Assert.IsNull(tree.Root.Left); Assert.IsNotNull(tree.Root.Right); Assert.AreEqual(rightOfRightValue, tree.Root.Right.Value); Assert.IsNull(tree.Root.Right.Right); Assert.IsNull(tree.Root.Right.Left); }
public void Insert_InOrderRightLeftToFineTree_UpdateTree(int rootValue, int rightValue, int leftOfRightValue) { var tree = new FineGrainedSyncBinaryTree <int>(); tree.Insert(rootValue); tree.Insert(rightValue); tree.Insert(leftOfRightValue); Assert.AreEqual(rootValue, tree.Root.Value); Assert.IsNull(tree.Root.Left); Assert.IsNotNull(tree.Root.Right); Assert.AreEqual(rightValue, tree.Root.Right.Value); Assert.IsNull(tree.Root.Right.Right); Assert.IsNotNull(tree.Root.Right.Left); Assert.AreEqual(leftOfRightValue, tree.Root.Right.Left.Value); Assert.IsNull(tree.Root.Right.Left.Left); Assert.IsNull(tree.Root.Right.Left.Right); }
public void Remove_RootInFineTree_UpdateRootFromLeftNeighbor(int rootValue, int leftValue, int rightOfLeftValue, int leftOfRightOfLeftValue) { var tree = new FineGrainedSyncBinaryTree <int>(); tree.Insert(rootValue); tree.Insert(leftValue); tree.Insert(rightOfLeftValue); tree.Insert(leftOfRightOfLeftValue); tree.Remove(rootValue); Assert.AreEqual(rightOfLeftValue, tree.Root.Value); Assert.IsNull(tree.Root.Right); Assert.IsNotNull(tree.Root.Left); Assert.AreEqual(leftValue, tree.Root.Left.Value); Assert.IsNull(tree.Root.Left.Left); Assert.IsNotNull(tree.Root.Left.Right); Assert.AreEqual(leftOfRightOfLeftValue, tree.Root.Left.Right.Value); Assert.IsNull(tree.Root.Left.Right.Left); Assert.IsNull(tree.Root.Left.Right.Right); }
public void InsertSequentially_RemoveFindConcurrently_ManyValuesToFineTree_UpdateTree(int workers, int maxSize, int maxValue, int maxTimeout) { var expectedTree = new FineGrainedSyncBinaryTree <int>(); var actualTree = new FineGrainedSyncBinaryTree <int>(); var elementsToInsert = new ConcurrentQueue <int>(); var elementsToRemove = new ConcurrentQueue <int>(); for (int i = 0; i < maxSize; ++i) { var value = _random.Next(maxValue); elementsToInsert.Enqueue(value); actualTree.Insert(value); expectedTree.Insert(value); value = _random.Next(maxValue); elementsToRemove.Enqueue(value); } var elementsToFind = new ConcurrentQueue <int>(elementsToInsert); foreach (int value in elementsToRemove) { expectedTree.Remove(value); } Task[] tasks = new Task[workers]; for (int i = 0; i < workers; ++i) { if (_random.Next(2) == 0) { // Create worker to test removing tasks[i] = Task.Run(() => { while (!elementsToRemove.IsEmpty) { if (elementsToRemove.TryDequeue(out var value)) { actualTree.Remove(value); } Thread.Sleep(_random.Next(maxTimeout)); } }); } else { // Create worker to test search tasks[i] = Task.Run(() => { while (!elementsToFind.IsEmpty) { if (elementsToFind.TryDequeue(out var value)) { actualTree.Find(value); } Thread.Sleep(_random.Next(maxTimeout)); } }); } } Task.WaitAll(tasks); Assert.IsTrue(Utils.ContentEquals(actualTree, expectedTree)); Assert.IsTrue(Utils.ValidateStructure(actualTree)); }