コード例 #1
0
 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);
 }
コード例 #2
0
 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);
 }
コード例 #3
0
 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);
 }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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));
        }
コード例 #7
0
 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);
 }
コード例 #8
0
 public void Constructor()
 {
     AtomicLong ai = new AtomicLong(1);
     Assert.AreEqual(1, ai.Value);
 }
コード例 #9
0
 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);
 }
コード例 #10
0
 public void ImplicitConverter()
 {
     AtomicLong ai = new AtomicLong(1);
     long result = ai;
     Assert.AreEqual(1L, result);
     ai.Value = -3;
     result = ai;
     Assert.AreEqual(-3L, result);
 }
コード例 #11
0
        public void DefaultConstructor()
        {
            AtomicLong ai = new AtomicLong();

            Assert.AreEqual(0, ai.Value);
        }
コード例 #12
0
 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));
 }
コード例 #13
0
 public void Value()
 {
     AtomicLong ai = new AtomicLong(42);
     Assert.AreEqual(ai.Value, 42L);
 }
コード例 #14
0
        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);
        }
コード例 #15
0
 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);
 }
コード例 #16
0
 public void ReturnValueAndDecrement()
 {
     AtomicLong ai = new AtomicLong(1);
     Assert.AreEqual(1, ai.ReturnValueAndDecrement());
     Assert.AreEqual(0, ai.ReturnValueAndDecrement());
     Assert.AreEqual(- 1, ai.ReturnValueAndDecrement());
 }
コード例 #17
0
 public void LongValueToString()
 {
     AtomicLong ai = new AtomicLong();
     for(long i = -12; i < 6; ++i) {
         ai.Value = i;
         Assert.AreEqual(ai.ToString(), Convert.ToString(i));
     }
 }
コード例 #18
0
        public void Value()
        {
            AtomicLong ai = new AtomicLong(42);

            Assert.AreEqual(ai.Value, 42L);
        }
コード例 #19
0
        public void Constructor()
        {
            AtomicLong ai = new AtomicLong(1);

            Assert.AreEqual(1, ai.Value);
        }
コード例 #20
0
        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));
        }
コード例 #21
0
 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));
 }
コード例 #22
0
 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);
 }
コード例 #23
0
        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));
        }
コード例 #24
0
 public void DefaultConstructor()
 {
     AtomicLong ai = new AtomicLong();
     Assert.AreEqual(0, ai.Value);
 }