public void TryPopRight_IsAtomic() { //Arrange const int initialCount = 5000000; const double stopAt = initialCount * 0.9; int popCount = 0; var deque = new ConcurrentDeque <int>(); for (int i = 0; i < initialCount; i++) { deque.PushRight(i); } Action popRight = () => { while (popCount <= stopAt) { int i; Assert.True(deque.TryPopRight(out i)); Interlocked.Increment(ref popCount); } }; //Act popRight.RunInParallel(ThreadCount); //Assert var expectedCount = initialCount - popCount; long expectedSum = Enumerable.Range(0, expectedCount).LongSum(); VerifyState(deque, expectedCount, expectedSum); }
public void WithEnumerable_MaintainsPointersIntegrity(int[] collection) { var deque = new ConcurrentDeque <int>(collection); Assert.Equal(collection, deque.TraverseLeftRight().Select(n => n._value)); Assert.Equal(collection.Reverse(), deque.TraverseRightLeft().Select(n => n._value)); }
public void WithIEnumerableCopiesCollection(Int32[] collection) { Int32[] array = { 1, 2, 3, 4 }; var deque = new ConcurrentDeque <Int32>(array); Assert.AreEqual(array, deque); }
public void WithIEnumerable_CopiesCollection(int[] collection) { int[] array = { 1, 2, 3, 4 }; var deque = new ConcurrentDeque <int>(array); Assert.Equal(array, deque); }
public void EnumeratorDoesNotIncludeConcurrentModifications() { //Arrange var arr = new[] { 1, 2, 3 }; var deque = new ConcurrentDeque <Int32>(arr); Int32 item; //Act var iterator = deque.GetEnumerator(); iterator.MoveNext(); deque.TryPopLeft(out item); deque.TryPopLeft(out item); deque.PushLeft(6); deque.TryPopRight(out item); deque.PushRight(6); //Assert Assert.AreEqual(1, iterator.Current); Assert.True(iterator.MoveNext()); Assert.AreEqual(2, iterator.Current); Assert.True(iterator.MoveNext()); Assert.AreEqual(3, iterator.Current); Assert.False(iterator.MoveNext()); }
public void PushLeft_IsAtomic() { //Arrange long pushCount = 0; long sum = 0; bool cancelled = false; var deque = new ConcurrentDeque <int>(); //keep adding items to the deque Action pushLeft = () => { Random rnd = new Random(); while (!cancelled) { int val = rnd.Next(1, 11); deque.PushLeft(val); Interlocked.Increment(ref pushCount); Interlocked.Add(ref sum, val); } }; //Act pushLeft.RunInParallel(() => cancelled = true, ThreadCount, RunningTime); //Assert VerifyState(deque, pushCount, sum); }
private static void ExecuteOp(ConcurrentDeque <int> deque, int op) { int item; switch (op) { case PopLeft: deque.TryPopLeft(out item); break; case PopRight: deque.TryPopRight(out item); break; case PushLeft: deque.PushLeft(10); break; case PushRight: deque.PushRight(10); break; default: throw new InvalidOperationException(); } }
public void PushRight_AppendsNode_ToEmptyDeque() { var deque = new ConcurrentDeque <int>(); deque.PushRight(1); Assert.Equal(new[] { 1 }, deque); }
public void PushRight_AppendsNode_ToNonEmptyList() { var deque = new ConcurrentDeque <int>(new[] { 1, 2, 3 }); deque.PushRight(4); Assert.Equal(new[] { 1, 2, 3, 4 }, deque); }
public void PushLeftAppendsNodeToNonEmptyList() { var deque = new ConcurrentDeque <Int32>(new[] { 1, 2, 3 }); deque.PushLeft(0); Assert.AreEqual(new[] { 0, 1, 2, 3 }, deque); }
public void PushLeftAppendsNodeToEmptyDeque() { var deque = new ConcurrentDeque <Int32>(); deque.PushLeft(1); Assert.AreEqual(new[] { 1 }, deque); }
public void TryPeekLeft_Fails_IfDequeIsEmpty() { var deque = new ConcurrentDeque <int>(); int item; Assert.False(deque.TryPeekLeft(out item)); }
public void PushLeft_AppendsNode_ToNonEmptyList() { var deque = new ConcurrentDeque <int>(new[] { 1, 2, 3 }); deque.PushLeft(0); Assert.Equal(new[] { 0, 1, 2, 3 }, deque); }
public void TryPeekLeftFailsIfDequeIsEmpty() { var deque = new ConcurrentDeque <Int32>(); Int32 item; Assert.False(deque.TryPeekLeft(out item)); }
public void ToArrayReturnsSnapshot() { Int32[] array = { 0, 1, 2 }; var deque = new ConcurrentDeque <Int32>(array); var snapshot = deque.ToArray(); Assert.AreEqual(array, snapshot); }
/// <summary> /// Constructs a new debugger view object for the provided collection object. /// </summary> /// <param name="deque">A collection to browse in the debugger.</param> public ConcurrentDequeDebugView(ConcurrentDeque <T> deque) { if (deque == null) { throw new ArgumentNullException("deque"); } _deque = deque; }
public void TryPeekLeftInspectsTheLeftmostItem() { var deque = new ConcurrentDeque <Int32>(new[] { 1, 2, 3 }); Int32 item; Assert.True(deque.TryPeekLeft(out item)); Assert.AreEqual(1, item); Assert.Contains(1, deque); }
public void IsStable_ByDefault() { //Act var deque = new ConcurrentDeque <int>(); //Assert var anchor = deque._anchor; Assert.Equal(ConcurrentDeque <int> .DequeStatus.Stable, anchor._status); }
public void TryPeekRight_Inspects_TheRightmostItem() { var deque = new ConcurrentDeque <int>(new[] { 1, 2, 3 }); int item; Assert.True(deque.TryPeekRight(out item)); Assert.Equal(3, item); Assert.Contains(3, deque); }
public void TryPopRight_FailsOnEmptyDeque() { //Arrange var deque = new ConcurrentDeque <int>(); //Act & Assert int item; Assert.False(deque.TryPopRight(out item)); Assert.Equal(item, default(int)); }
public void PointersAreNull_ByDefault() { //Act var deque = new ConcurrentDeque <int>(); //Assert var anchor = deque._anchor; Assert.Null(anchor._left); Assert.Null(anchor._right); }
public void TryPopLeftFailsOnEmptyDeque() { //Arrange var deque = new ConcurrentDeque <Int32>(); //Act & Assert Int32 item; Assert.False(deque.TryPopLeft(out item)); Assert.AreEqual(item, default(Int32)); }
private void VerifyState(ConcurrentDeque <int> deque, long expectedCount, long expectedSum) { //Assert Assert.Equal(expectedCount, deque.Count); Assert.Equal(expectedSum, deque.LongSum()); //test internal state //traverse the deque in both directions Assert.Equal(expectedCount, deque.TraverseLeftRight().LongCount()); Assert.Equal(expectedCount, deque.TraverseRightLeft().LongCount()); }
public void ClearRemovesAllItems() { //Arrange var deque = new ConcurrentDeque <Int32>(new[] { 1, 2, 3, 4 }); //Act deque.Clear(); //Assert Assert.True(deque.IsEmpty); }
public void TryAdd_AppendsValue_ToTheRight() { //Act IProducerConsumerCollection <int> deque = new ConcurrentDeque <int>(); deque.TryAdd(2); deque.TryAdd(3); //Assert Assert.Equal(new[] { 2, 3 }, deque); }
public void CollectionCopyToCopiesItems() { Int32[] array = { 0, 1, 2 }; var deque = new ConcurrentDeque <Int32>(array); var copy = new Int32[3]; ((ICollection)deque).CopyTo(copy, 0); Assert.AreEqual(array, copy); }
public void TryPopRight_ReturnsTheRightmostItem() { //Arrange var deque = new ConcurrentDeque <int>(new[] { 1, 3, 5 }); //Act & Assert int item; Assert.True(deque.TryPopRight(out item)); Assert.Equal(item, 5); Assert.Equal(new[] { 1, 3 }, deque); }
public void ToArray_ReturnsSnapshot() { //Arrange int[] array = { 0, 1, 2 }; var deque = new ConcurrentDeque <int>(array); //Act var snapshot = deque.ToArray(); //Assert Assert.Equal(array, snapshot); }
public void TryPopLeftReturnsTheLeftmostItem() { //Arrange var deque = new ConcurrentDeque <Int32>(new[] { 1, 3, 5 }); //Act & Assert Int32 item; Assert.True(deque.TryPopLeft(out item)); Assert.AreEqual(item, 1); Assert.AreEqual(new[] { 3, 5 }, deque); }
public void TryPopLeftReturnsTheLastRemainingItem() { //Arrange var deque = new ConcurrentDeque <Int32>(); deque.PushRight(1); //Act & Assert Int32 item; Assert.True(deque.TryPopLeft(out item)); Assert.AreEqual(item, 1); Assert.AreEqual(0, deque.Count); }
internal WorkerThread(int index, ThreadTaskManager manager, Action threadStart) { this.manager = manager; thread = new Thread(ThreadExecutionLoop); thread.IsBackground = true; taskData = new ConcurrentDeque<TaskEntry>(); this.threadStart = threadStart; UpdateIndex(index); thread.Start(); }
internal WorkerThread(int index, ThreadTaskManager manager, Action<object> threadStart, object initializationInformation) { this.manager = manager; thread = new Thread(ThreadExecutionLoop); thread.IsBackground = true; taskData = new ConcurrentDeque<TaskEntry>(); this.threadStart = threadStart; this.initializationInformation = initializationInformation; UpdateIndex(index); //#if WINDOWS // ResourcePool.addThreadID(thread.ManagedThreadId); //#endif thread.Start(); }