public void AtomicIntegerArray_Store_Should_Success(int initialValue, int storeValue, MemoryOrder order) { var atomicIntegerArray = new AtomicIntegerArray(new int[] { initialValue }, MemoryOrder.Relaxed); atomicIntegerArray.Store(0, storeValue, order); Assert.Equal(storeValue, atomicIntegerArray[0]); }
private void AssertAtomicIntegerArray(AtomicIntegerArray array) { for (int i = 0; i < array.Length(); ++i) { NUnit.Framework.Assert.AreEqual(1, array.Get(i)); } }
public void AtomicIntegerArray_Should_Copy_Source(MemoryOrder memoryOrder) { var item1 = 1; var item2 = 2; var source = new [] { item1, 0, item2, 0 }; var ar = new AtomicIntegerArray(source, memoryOrder); Assert.True(source.SequenceEqual(ar)); Assert.Equal(0, source[1]); Assert.Equal(0, ar[1]); source[1] = -1; Assert.False(source.SequenceEqual(ar)); Assert.Equal(0, source[3]); Assert.Equal(0, ar[3]); Assert.Equal(0, source[3]); Assert.Equal(0, ar[3]); source[3] = -1; Assert.Equal(-1, source[3]); Assert.Equal(0, ar[3]); Assert.False(source.SequenceEqual(ar)); }
public void AtomicIntegerArray_Load_Should_Success() { var atomicIntegerArray = new AtomicIntegerArray(new []{100}); Assert.Equal(100, atomicIntegerArray.Load(0, MemoryOrder.Relaxed)); Assert.Equal(100, atomicIntegerArray.Load(0, MemoryOrder.Acquire)); Assert.Equal(100, atomicIntegerArray.Load(0, MemoryOrder.AcqRel)); Assert.Equal(100, atomicIntegerArray.Load(0, MemoryOrder.SeqCst)); }
public void AtomicIntegerArray_Load_Should_Success() { var atomicIntegerArray = new AtomicIntegerArray(new [] { 100 }); Assert.Equal(100, atomicIntegerArray.Load(0, MemoryOrder.Relaxed)); Assert.Equal(100, atomicIntegerArray.Load(0, MemoryOrder.Acquire)); Assert.Equal(100, atomicIntegerArray.Load(0, MemoryOrder.AcqRel)); Assert.Equal(100, atomicIntegerArray.Load(0, MemoryOrder.SeqCst)); }
public void AtomicIntegerArray_Load_Should_Fail() { var atomicIntegerArray = new AtomicIntegerArray(new [] { 100 }); Assert.Throws <InvalidOperationException>(() => atomicIntegerArray.Load(0, MemoryOrder.Release)); #pragma warning disable 618 Assert.Throws <NotSupportedException>(() => atomicIntegerArray.Load(0, MemoryOrder.Consume)); #pragma warning restore 618 }
public void AtomicIntegerArray_Store_Should_Fail() { var atomicIntegerArray = new AtomicIntegerArray(new [] { 100 }); Assert.Throws <InvalidOperationException>(() => atomicIntegerArray.Store(0, int.MinValue, MemoryOrder.Acquire)); #pragma warning disable 618 Assert.Throws <NotSupportedException>(() => atomicIntegerArray.Store(0, int.MinValue, MemoryOrder.Consume)); #pragma warning restore 618 }
public void AddDeltaAndReturnPreviousValue() { AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE); for(int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) { ai[i] = 1; Assert.AreEqual(1, ai.AddDeltaAndReturnPreviousValue(i, 2)); Assert.AreEqual(3, ai[i]); Assert.AreEqual(3, ai.AddDeltaAndReturnPreviousValue(i, -4)); Assert.AreEqual(-1, ai[i]); } }
public void CompareAndSetInMultipleThreads() { AtomicIntegerArray a = new AtomicIntegerArray(1); a[0] = 1; Thread t = ThreadManager.StartAndAssertRegistered( "T1", () => { while (!a.CompareAndSet(0, 2, 3)) Thread.Sleep(0); }); Assert.IsTrue(a.CompareAndSet(0, 1, 2)); ThreadManager.JoinAndVerify(Delays.Long); Assert.IsFalse(t.IsAlive); Assert.AreEqual(a[0], 3); }
public TestCallbackHandler1(TestNMClientAsync _enclosing, int expectedSuccess, int expectedFailure) { this._enclosing = _enclosing; this.expectedSuccess = expectedSuccess; this.expectedFailure = expectedFailure; this.actualStartSuccessArray = new AtomicIntegerArray(expectedSuccess); this.actualStartFailureArray = new AtomicIntegerArray(expectedFailure); this.actualQuerySuccessArray = new AtomicIntegerArray(expectedSuccess); this.actualQueryFailureArray = new AtomicIntegerArray(expectedFailure); this.actualStopSuccessArray = new AtomicIntegerArray(expectedSuccess); this.actualStopFailureArray = new AtomicIntegerArray(expectedFailure); }
public void AtomicIntegerArray_Items_With_Length_Ctor_Should_Be_Null(int length, MemoryOrder memoryOrder) { var ar = new AtomicIntegerArray(length, memoryOrder); foreach (var o in ar) { Assert.Equal(0, o); } for (int i = 0; i < ar.Count; i++) { Assert.Equal(0, ar[i]); } }
public void AtomicIntegerArray_Indexer_MemoryOrder_Should_Success(MemoryOrder order) { var atomicIntegerArray = new AtomicIntegerArray(new int[3], order); for (int i = 0; i < atomicIntegerArray.Count; i++) { Assert.Equal(0, atomicIntegerArray[i]); atomicIntegerArray[i] = i; } for (int i = 0; i < atomicIntegerArray.Count; i++) { Assert.Equal(i, atomicIntegerArray[i]); } }
public void AtomicIntegerArray_Load_Acquire_Should_Success(MemoryOrder order) { var atomicIntegerArray = new AtomicIntegerArray(new int[3], order); for (int i = 0; i < atomicIntegerArray.Count; i++) { Assert.Equal(0, atomicIntegerArray.Load(i, MemoryOrder.Acquire)); atomicIntegerArray.Store(i, i, MemoryOrder.Release); } for (int i = 0; i < atomicIntegerArray.Count; i++) { Assert.Equal(i, atomicIntegerArray.Load(i, MemoryOrder.Acquire)); } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldKeepForkedOrderIntactWhenChangingProcessorCount() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldKeepForkedOrderIntactWhenChangingProcessorCount() { int length = 100; AtomicIntegerArray reference = new AtomicIntegerArray(length); // GIVEN StageControl control = mock(typeof(StageControl)); int availableProcessors = Runtime.Runtime.availableProcessors(); ForkedProcessorStep <int[]> step = new ForkedProcessorStepAnonymousInnerClass(this, control, Config(availableProcessors), reference); DeadEndStep downstream = new DeadEndStep(control); step.Downstream = downstream; // WHEN step.Start(0); downstream.Start(0); ThreadLocalRandom random = ThreadLocalRandom.current(); for (int ticket = 0; ticket < 200; ticket++) { // The processor count is changed here in this block simply because otherwise // it's very hard to know how many processors we expect to see have processed // a particular batch. if (random.nextFloat() < 0.1) { int p = step.Processors(random.Next(-2, 4)); } int[] batch = new int[length]; batch[0] = ticket; for (int j = 1; j < batch.Length; j++) { batch[j] = j - 1; } step.Receive(ticket, batch); } step.EndOfUpstream(); step.AwaitCompleted(); step.Close(); }
public void AtomicIntegerArray_Decrement_Should_Success(MemoryOrder memoryOrder) { var ar = new AtomicIntegerArray(10, memoryOrder); foreach (var o in ar) { Assert.Equal(0, o); } for (int i = 0; i < ar.Count; i++) { Assert.Equal(0, ar[i]); } for (int i = 0; i < ar.Count; i++) { ar[i] = i; ar.DecrementAt(i); } Assert.Equal(Enumerable.Range(-1, 10), ar); }
public void AtomicIntegerArray_Store_MemoryOrder_Should_Success(MemoryOrder order) { var atomicIntegerArray = new AtomicIntegerArray(new int[3], order); for (int i = 0; i < atomicIntegerArray.Count; i++) { atomicIntegerArray.Store(i, i, order); } for (int i = 0; i < atomicIntegerArray.Count; i++) { Assert.Equal(i, atomicIntegerArray.Load(i, order)); } for (int i = 0; i < atomicIntegerArray.Count; i++) { atomicIntegerArray.Store(i, i, order); } for (int i = 0; i < atomicIntegerArray.Count; i++) { Assert.Equal(i, atomicIntegerArray.Load(i, order)); } }
public void GetReturnsLastValueSetAtIndex() { AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE); for(int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) { ai[i] = 1; Assert.AreEqual(1, ai[i]); ai[i] = 2; Assert.AreEqual(2, ai[i]); ai[i] = -3; Assert.AreEqual(-3, ai[i]); } }
public void Exchange() { AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE); for(int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) { ai[i] = 1; Assert.AreEqual(1, ai.Exchange(i, 0)); Assert.AreEqual(0, ai.Exchange(i, -10)); Assert.AreEqual(-10, ai.Exchange(i, 1)); } }
public void CompareExistingValueAndSetNewValue() { AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE); for(int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) { ai[i] = 1; Assert.IsTrue(ai.CompareAndSet(i, 1, 2)); Assert.IsTrue(ai.CompareAndSet(i, 2, -4)); Assert.AreEqual(-4, ai[i]); Assert.IsFalse(ai.CompareAndSet(i, -5, 7)); Assert.IsFalse(7 == ai[i]); Assert.IsTrue(ai.CompareAndSet(i, -4, 7)); Assert.AreEqual(7, ai[i]); } }
public void AtomicIntegerArray_Store_Should_Success(int initialValue, int storeValue, MemoryOrder order) { var atomicIntegerArray = new AtomicIntegerArray(new int[] {initialValue}, MemoryOrder.Relaxed); atomicIntegerArray.Store(0, storeValue, order); Assert.Equal(storeValue, atomicIntegerArray[0]); }
public void WeakCompareAndSet() { AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE); for(int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) { ai[i] = 1; while(!ai.WeakCompareAndSet(i, 1, 2)) {} while(!ai.WeakCompareAndSet(i, 2, -4)) {} Assert.AreEqual(-4, ai[i]); while(!ai.WeakCompareAndSet(i, -4, 7)) {} Assert.AreEqual(7, ai[i]); Assert.IsFalse(ai.WeakCompareAndSet(i, -4, 7)); } }
public void SerializeAndDeserialize() { AtomicIntegerArray l = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE); for(int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) l[i] = -i; MemoryStream bout = new MemoryStream(10000); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(bout, l); MemoryStream bin = new MemoryStream(bout.ToArray()); BinaryFormatter formatter2 = new BinaryFormatter(); AtomicIntegerArray r = (AtomicIntegerArray)formatter2.Deserialize(bin); for(int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) { Assert.AreEqual(l[i], r[i]); } }
public void AtomicIntegerArray_IsLockFree_Should_Success(int initialValue, MemoryOrder order, bool isLockFree) { var atomicIntegerArray = new AtomicIntegerArray(new[] { initialValue }, order); Assert.Equal(atomicIntegerArray.IsLockFree, isLockFree); }
public ForkedProcessorStepAnonymousInnerClass(ForkedProcessorStepTest outerInstance, [email protected] control, Configuration config, AtomicIntegerArray reference) : base(control, "Processor", config) { this.outerInstance = outerInstance; this._reference = reference; }
public void IndexerChokesOnOutOfRangeIndex() { AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE); int a = 0; TestHelper.AssertException<IndexOutOfRangeException>( delegate { a = ai[DEFAULT_COLLECTION_SIZE]; }); TestHelper.AssertException<IndexOutOfRangeException>( delegate { a = ai[-1]; }); TestHelper.AssertException<IndexOutOfRangeException>( delegate { ai.Exchange(DEFAULT_COLLECTION_SIZE, 0); }); TestHelper.AssertException<IndexOutOfRangeException>( delegate { ai.Exchange(-1, 0); }); Assert.AreEqual(0, a); }
public void ReturnValueAndIncrement() { AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE); for(int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) { ai[i] = 1; Assert.AreEqual(1, ai.ReturnValueAndIncrement(i)); Assert.AreEqual(2, ai[i]); ai[i] = -2; Assert.AreEqual(-2, ai.ReturnValueAndIncrement(i)); Assert.AreEqual(-1, ai.ReturnValueAndIncrement(i)); Assert.AreEqual(0, ai.ReturnValueAndIncrement(i)); Assert.AreEqual(1, ai[i]); } }
public void AtomicIntegerArray_Store_Should_Fail() { var atomicIntegerArray = new AtomicIntegerArray(new []{100}); Assert.Throws<InvalidOperationException>(() => atomicIntegerArray.Store(0, int.MinValue, MemoryOrder.Acquire)); #pragma warning disable 618 Assert.Throws<NotSupportedException>(() => atomicIntegerArray.Store(0, int.MinValue, MemoryOrder.Consume)); #pragma warning restore 618 }
public void ToStringTest() { int[] a = new int[] { 17, 3, -42, 99, -7 }; AtomicIntegerArray ai = new AtomicIntegerArray(a); Assert.AreEqual(ToString(a), ai.ToString()); int[] b = new int[0]; Assert.AreEqual("[]", new AtomicIntegerArray(b).ToString()); }
public void ConstructAtomicIntegerArryWithGivenSize() { AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE); for(int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) Assert.AreEqual(0, ai[i]); }
internal Counter(AtomicIntegerArray a) { ai = a; }
public void ConstructFromExistingArray() { int[] a = new int[] { 17, 3, -42, 99, -7 }; AtomicIntegerArray ai = new AtomicIntegerArray(a); Assert.AreEqual(a.Length, ai.Count); for(int i = 0; i < a.Length; ++i) Assert.AreEqual(a[i], ai[i]); }
public void AtomicIntegerArray_Load_Should_Fail() { var atomicIntegerArray = new AtomicIntegerArray(new []{100}); Assert.Throws<InvalidOperationException>(() => atomicIntegerArray.Load(0, MemoryOrder.Release)); #pragma warning disable 618 Assert.Throws<NotSupportedException>(() => atomicIntegerArray.Load(0, MemoryOrder.Consume)); #pragma warning restore 618 }
public void CountingInMultipleThreads() { AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE); for(int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) ai[i] = COUNTDOWN; Counter c1 = new Counter(ai); Counter c2 = new Counter(ai); ThreadManager.StartAndAssertRegistered("T", c1.Run, c2.Run); ThreadManager.JoinAndVerify(); Assert.AreEqual(c1.counts + c2.counts, DEFAULT_COLLECTION_SIZE * COUNTDOWN); }
/// <summary> /// Initializes a new instance of the <see cref="PercentileBucketData"/> class. /// </summary> /// <param name="dataLength">The maximum number of values to store.</param> public PercentileBucketData(int dataLength) { this.length = dataLength; this.list = new AtomicIntegerArray(dataLength); }
public void AtomicIntegerArray_Should_Copy_Source(MemoryOrder memoryOrder) { var item1 = 1; var item2 = 2; var source = new [] { item1, 0, item2, 0}; var ar = new AtomicIntegerArray(source, memoryOrder); Assert.True(source.SequenceEqual(ar)); Assert.Equal(0, source[1]); Assert.Equal(0, ar[1]); source[1] = -1; Assert.False(source.SequenceEqual(ar)); Assert.Equal(0, source[3]); Assert.Equal(0, ar[3]); Assert.Equal(0, source[3]); Assert.Equal(0, ar[3]); source[3] = -1; Assert.Equal(-1, source[3]); Assert.Equal(0, ar[3]); Assert.False(source.SequenceEqual(ar)); }