Skip to content

Commit

Permalink
volatile long array added
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Deheurles committed Dec 24, 2011
1 parent 3efa936 commit 3d0767b
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Atomic.Tests/Atomic.Tests.csproj
Expand Up @@ -44,6 +44,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Support\ClassStub.cs" />
<Compile Include="VolatileLongArrayTests.cs" />
<Compile Include="VolatileIntegerArrayTests.cs" />
<Compile Include="VolatileBooleanTests.cs" />
<Compile Include="VolatileLongTests.cs" />
Expand Down
4 changes: 4 additions & 0 deletions Atomic.Tests/Support/ClassStub.cs
@@ -0,0 +1,4 @@
namespace Atomic.Tests.Support
{
public class ClassStub{}
}
154 changes: 154 additions & 0 deletions Atomic.Tests/VolatileLongArrayTests.cs
@@ -0,0 +1,154 @@
using System.Threading;
using NUnit.Framework;

namespace Atomic.Tests
{
[TestFixture]
public class VolatileLongArrayTests
{
private Volatile.LongArray _volatile;
private static readonly long[] InitialValues = new[] {0L, 1L, 2L, 3L};
private const long InitialValue = 0L;
private const long NewValue = 1L;

[SetUp]
public void SetUp()
{
_volatile = new Volatile.LongArray(InitialValues);
}

[Test]
public void ConstructorWithLengthSetExpectedLength()
{
const int length = 2;
_volatile = new Volatile.LongArray(length);

Assert.AreEqual(length, _volatile.Length);
}

[Test]
public void ConstructorWithArraySetExpectedLength()
{
Assert.AreEqual(InitialValues.Length, _volatile.Length);
}

[Test]
public void ReadFullFenceReturnsInitialValue()
{
Assert.AreEqual(InitialValue, _volatile.ReadFullFence(0));
}

[Test]
public void ReadCompilerOnlyFenceReturnsInitialValue()
{
Assert.AreEqual(InitialValue, _volatile.ReadCompilerOnlyFence(0));
}

[Test]
public void ReadUnfencedReturnsInitialValue()
{
Assert.AreEqual(InitialValue, _volatile.ReadUnfenced(0));
}

[Test]
public void WriteReleaseFenceChangesInitialValue()
{
_volatile.WriteReleaseFence(0, NewValue);
Assert.AreEqual(NewValue, _volatile.ReadUnfenced(0));
}

[Test]
public void WriteFullFenceChangesInitialValue()
{
_volatile.WriteFullFence(0, NewValue);
Assert.AreEqual(NewValue, _volatile.ReadUnfenced(0));
}

[Test]
public void WriteCompilerOnlyFenceChangesInitialValue()
{
_volatile.WriteCompilerOnlyFence(0, NewValue);
Assert.AreEqual(NewValue, _volatile.ReadUnfenced(0));
}

[Test]
public void WriteUnfencedInitialValue()
{
_volatile.WriteUnfenced(0, NewValue);
Assert.AreEqual(NewValue, _volatile.ReadUnfenced(0));
}

[Test]
public void AtomicCompareExchangeReturnsTrueIfComparandEqualsCurrentValue()
{
Assert.IsTrue(_volatile.AtomicCompareExchange(0, NewValue, InitialValue));
}

[Test]
public void AtomicCompareExchangeMutatesValueIfComparandEqualsCurrentValue()
{
_volatile.AtomicCompareExchange(0, NewValue, InitialValue);
Assert.AreEqual(NewValue, _volatile.ReadUnfenced(0));
}

[Test]
public void AtomicCompareExchangeReturnsFalseIfComparandDifferentFromCurrentValue()
{
Assert.IsFalse(_volatile.AtomicCompareExchange(0, NewValue, InitialValue + 1));
}

[Test]
public void AtomicExchangeReturnsInitialValue()
{
Assert.AreEqual(InitialValue, _volatile.AtomicExchange(0, NewValue));
}

[Test]
public void AtomicExchangeMutatesValue()
{
_volatile.AtomicExchange(0, NewValue);
Assert.AreEqual(NewValue, _volatile.ReadUnfenced(0));
}

[Test]
public void AtomicAddAndGetReturnsNewValue()
{
const int delta = 5;
Assert.AreEqual(InitialValue + delta, _volatile.AtomicAddAndGet(0, delta));
}

[Test]
public void AtomicAddAndGetMutatesValue()
{
const int delta = 5;
_volatile.AtomicAddAndGet(0, delta);
Assert.AreEqual(InitialValue + delta, _volatile.ReadUnfenced(0));
}

[Test]
public void AtomicIncrementAndGetReturnsNewValue()
{
Assert.AreEqual(InitialValue + 1, _volatile.AtomicIncrementAndGet(0));
}

[Test]
public void AtomicIncrementAndGetMutatesValue()
{
_volatile.AtomicIncrementAndGet(0);
Assert.AreEqual(InitialValue + 1, _volatile.ReadUnfenced(0));
}

[Test]
public void AtomicDecrementAndGetReturnsNewValue()
{
Assert.AreEqual(InitialValue - 1, _volatile.AtomicDecrementAndGet(0));
}

[Test]
public void AtomicDecrementAndGetMutatesValue()
{
_volatile.AtomicDecrementAndGet(0);
Assert.AreEqual(InitialValue - 1, _volatile.ReadUnfenced(0));
}
}
}
11 changes: 5 additions & 6 deletions Atomic.Tests/VolatileReferenceTests.cs
@@ -1,19 +1,20 @@
using System.Threading;
using Atomic.Tests.Support;
using NUnit.Framework;

namespace Atomic.Tests
{
[TestFixture]
public class VolatileReferenceTests
{
private Volatile.Reference<RefStub> _volatile;
private readonly RefStub _initialValue = new RefStub();
private readonly RefStub _newValue = new RefStub();
private Volatile.Reference<ClassStub> _volatile;
private readonly ClassStub _initialValue = new ClassStub();
private readonly ClassStub _newValue = new ClassStub();

[SetUp]
public void SetUp()
{
_volatile = new Volatile.Reference<RefStub>(_initialValue);
_volatile = new Volatile.Reference<ClassStub>(_initialValue);
}

[Test]
Expand Down Expand Up @@ -106,6 +107,4 @@ public void ToStringReturnsInitialValueAsString()
Assert.AreEqual(_initialValue.ToString(), _volatile.ToString());
}
}

public class RefStub{}
}

0 comments on commit 3d0767b

Please sign in to comment.