public void AddDeltaAndReturnPreviousValue() { AtomicLong ai = new AtomicLong(1); Assert.AreEqual(1, ai.AddDeltaAndReturnPreviousValue(2)); Assert.AreEqual(3, ai.Value); Assert.AreEqual(3, ai.AddDeltaAndReturnPreviousValue(-4)); Assert.AreEqual(-1, ai.Value); }
public void CompareExpectedValueAndSetNewValueInMultipleThreads() { AtomicLong ai = new AtomicLong(1); Thread t = ThreadManager.StartAndAssertRegistered( "T1", () => { while (!ai.CompareAndSet(2, 3)) Thread.Sleep(0); }); Assert.IsTrue(ai.CompareAndSet(1, 2)); ThreadManager.JoinAndVerify(Delays.Long); Assert.IsFalse(t.IsAlive); Assert.AreEqual(ai.Value, 3); }
public void CompareExpectedValueAndSetNewValue() { AtomicLong ai = new AtomicLong(1); Assert.IsTrue(ai.CompareAndSet(1, 2)); Assert.IsTrue(ai.CompareAndSet(2, -4)); Assert.AreEqual(-4, ai.Value); Assert.IsFalse(ai.CompareAndSet(-5, 7)); Assert.IsFalse((7 == ai.Value)); Assert.IsTrue(ai.CompareAndSet(-4, 7)); Assert.AreEqual(7, ai.Value); }
public void CompareExpectedValueAndSetNewValueInMultipleThreads() { AtomicLong ai = new AtomicLong(1); Thread t = ThreadManager.StartAndAssertRegistered( "T1", () => { while (!ai.CompareAndSet(2, 3)) { Thread.Sleep(0); } }); Assert.IsTrue(ai.CompareAndSet(1, 2)); ThreadManager.JoinAndVerify(Delays.Long); Assert.IsFalse(t.IsAlive); Assert.AreEqual(ai.Value, 3); }
public void SerializationAndDeserialization() { AtomicLong l = new AtomicLong(); l.Value = -22; MemoryStream bout = new MemoryStream(10000); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(bout, l); MemoryStream bin = new MemoryStream(bout.ToArray()); BinaryFormatter formatter2 = new BinaryFormatter(); AtomicLong r = (AtomicLong)formatter2.Deserialize(bin); Assert.AreEqual(l.Value, r.Value); }
public void WeakCompareExpectedValueAndSetNewValue() { AtomicLong ai = new AtomicLong(1); while (!ai.WeakCompareAndSet(1, 2)) { } while (!ai.WeakCompareAndSet(2, -4)) { } Assert.AreEqual(-4, ai.Value); while (!ai.WeakCompareAndSet(-4, 7)) { } Assert.AreEqual(7, ai.Value); Assert.IsFalse(ai.WeakCompareAndSet(-4, 7)); }
public void DecrementValueAndReturn() { AtomicLong ai = new AtomicLong(1); Assert.AreEqual(0, ai.DecrementValueAndReturn()); Assert.AreEqual(-1, ai.DecrementValueAndReturn()); Assert.AreEqual(-2, ai.DecrementValueAndReturn()); Assert.AreEqual(-2, ai.Value); }
public void Constructor() { AtomicLong ai = new AtomicLong(1); Assert.AreEqual(1, ai.Value); }
public void GetLastLazySetValue() { AtomicLong ai = new AtomicLong(1); Assert.AreEqual(1, ai.Value); ai.LazySet(2); Assert.AreEqual(2, ai.Value); ai.LazySet(-3); Assert.AreEqual(-3, ai.Value); }
public void ImplicitConverter() { AtomicLong ai = new AtomicLong(1); long result = ai; Assert.AreEqual(1L, result); ai.Value = -3; result = ai; Assert.AreEqual(-3L, result); }
public void DefaultConstructor() { AtomicLong ai = new AtomicLong(); Assert.AreEqual(0, ai.Value); }
public void WeakCompareExpectedValueAndSetNewValue() { AtomicLong ai = new AtomicLong(1); while(!ai.WeakCompareAndSet(1, 2)) {} while(!ai.WeakCompareAndSet(2, -4)) {} Assert.AreEqual(-4, ai.Value); while(!ai.WeakCompareAndSet(-4, 7)) {} Assert.AreEqual(7, ai.Value); Assert.IsFalse(ai.WeakCompareAndSet(-4, 7)); }
public void Value() { AtomicLong ai = new AtomicLong(42); Assert.AreEqual(ai.Value, 42L); }
public void ReturnValueAndIncrement() { AtomicLong ai = new AtomicLong(1); Assert.AreEqual(1, ai.ReturnValueAndIncrement()); Assert.AreEqual(2, ai.Value); ai.Value = - 2; Assert.AreEqual(- 2, ai.ReturnValueAndIncrement()); Assert.AreEqual(- 1, ai.ReturnValueAndIncrement()); Assert.AreEqual(0, ai.ReturnValueAndIncrement()); Assert.AreEqual(1, ai.Value); }
public void ReturnValueAndDecrement() { AtomicLong ai = new AtomicLong(1); Assert.AreEqual(1, ai.ReturnValueAndDecrement()); Assert.AreEqual(0, ai.ReturnValueAndDecrement()); Assert.AreEqual(- 1, ai.ReturnValueAndDecrement()); }
public void LongValueToString() { AtomicLong ai = new AtomicLong(); for(long i = -12; i < 6; ++i) { ai.Value = i; Assert.AreEqual(ai.ToString(), Convert.ToString(i)); } }
public void ForInt64LocalSum(long from, long to) { var total = new AtomicLong(0); var result = Parallel.For( from, to, () => (long)0, (i, s, l) => l + i, l => total.AddDeltaAndReturnNewValue(l)); Assert.That(result.IsCompleted, Is.True); Assert.That(total.Value, Is.EqualTo((from + to - 1) * (to - from) / 2)); }
public void Exchange() { AtomicLong ai = new AtomicLong(1); Assert.AreEqual(1, ai.Exchange(0)); Assert.AreEqual(0, ai.Exchange(-10)); Assert.AreEqual(-10, ai.Exchange(1)); }
public void GetLastSetValue() { AtomicLong ai = new AtomicLong(1); Assert.AreEqual(1, ai.Value); ai.Value = 2; Assert.AreEqual(2, ai.Value); ai.Value = -3; Assert.AreEqual(-3, ai.Value); }
public void ForInt64LocalSumWithParallelism(long from, long to, int parallelism) { var options = new ParallelOptions { MaxDegreeOfParallelism = parallelism }; var total = new AtomicLong(0); var result = Parallel.For( from, to, options, () => (long)0, (i, s, l) => l + i, l => total.AddDeltaAndReturnNewValue(l)); Assert.That(result.IsCompleted, Is.True); Assert.That(total.Value, Is.EqualTo((from + to - 1) * (to - from) / 2)); }