1
+ using System . Threading ;
2
+ using NUnit . Framework ;
3
+
4
+ namespace Atomic . Tests
5
+ {
6
+ [ TestFixture ]
7
+ public class VolatileLongArrayTests
8
+ {
9
+ private Volatile . LongArray _volatile ;
10
+ private static readonly long [ ] InitialValues = new [ ] { 0L , 1L , 2L , 3L } ;
11
+ private const long InitialValue = 0L ;
12
+ private const long NewValue = 1L ;
13
+
14
+ [ SetUp ]
15
+ public void SetUp ( )
16
+ {
17
+ _volatile = new Volatile . LongArray ( InitialValues ) ;
18
+ }
19
+
20
+ [ Test ]
21
+ public void ConstructorWithLengthSetExpectedLength ( )
22
+ {
23
+ const int length = 2 ;
24
+ _volatile = new Volatile . LongArray ( length ) ;
25
+
26
+ Assert . AreEqual ( length , _volatile . Length ) ;
27
+ }
28
+
29
+ [ Test ]
30
+ public void ConstructorWithArraySetExpectedLength ( )
31
+ {
32
+ Assert . AreEqual ( InitialValues . Length , _volatile . Length ) ;
33
+ }
34
+
35
+ [ Test ]
36
+ public void ReadFullFenceReturnsInitialValue ( )
37
+ {
38
+ Assert . AreEqual ( InitialValue , _volatile . ReadFullFence ( 0 ) ) ;
39
+ }
40
+
41
+ [ Test ]
42
+ public void ReadCompilerOnlyFenceReturnsInitialValue ( )
43
+ {
44
+ Assert . AreEqual ( InitialValue , _volatile . ReadCompilerOnlyFence ( 0 ) ) ;
45
+ }
46
+
47
+ [ Test ]
48
+ public void ReadUnfencedReturnsInitialValue ( )
49
+ {
50
+ Assert . AreEqual ( InitialValue , _volatile . ReadUnfenced ( 0 ) ) ;
51
+ }
52
+
53
+ [ Test ]
54
+ public void WriteReleaseFenceChangesInitialValue ( )
55
+ {
56
+ _volatile . WriteReleaseFence ( 0 , NewValue ) ;
57
+ Assert . AreEqual ( NewValue , _volatile . ReadUnfenced ( 0 ) ) ;
58
+ }
59
+
60
+ [ Test ]
61
+ public void WriteFullFenceChangesInitialValue ( )
62
+ {
63
+ _volatile . WriteFullFence ( 0 , NewValue ) ;
64
+ Assert . AreEqual ( NewValue , _volatile . ReadUnfenced ( 0 ) ) ;
65
+ }
66
+
67
+ [ Test ]
68
+ public void WriteCompilerOnlyFenceChangesInitialValue ( )
69
+ {
70
+ _volatile . WriteCompilerOnlyFence ( 0 , NewValue ) ;
71
+ Assert . AreEqual ( NewValue , _volatile . ReadUnfenced ( 0 ) ) ;
72
+ }
73
+
74
+ [ Test ]
75
+ public void WriteUnfencedInitialValue ( )
76
+ {
77
+ _volatile . WriteUnfenced ( 0 , NewValue ) ;
78
+ Assert . AreEqual ( NewValue , _volatile . ReadUnfenced ( 0 ) ) ;
79
+ }
80
+
81
+ [ Test ]
82
+ public void AtomicCompareExchangeReturnsTrueIfComparandEqualsCurrentValue ( )
83
+ {
84
+ Assert . IsTrue ( _volatile . AtomicCompareExchange ( 0 , NewValue , InitialValue ) ) ;
85
+ }
86
+
87
+ [ Test ]
88
+ public void AtomicCompareExchangeMutatesValueIfComparandEqualsCurrentValue ( )
89
+ {
90
+ _volatile . AtomicCompareExchange ( 0 , NewValue , InitialValue ) ;
91
+ Assert . AreEqual ( NewValue , _volatile . ReadUnfenced ( 0 ) ) ;
92
+ }
93
+
94
+ [ Test ]
95
+ public void AtomicCompareExchangeReturnsFalseIfComparandDifferentFromCurrentValue ( )
96
+ {
97
+ Assert . IsFalse ( _volatile . AtomicCompareExchange ( 0 , NewValue , InitialValue + 1 ) ) ;
98
+ }
99
+
100
+ [ Test ]
101
+ public void AtomicExchangeReturnsInitialValue ( )
102
+ {
103
+ Assert . AreEqual ( InitialValue , _volatile . AtomicExchange ( 0 , NewValue ) ) ;
104
+ }
105
+
106
+ [ Test ]
107
+ public void AtomicExchangeMutatesValue ( )
108
+ {
109
+ _volatile . AtomicExchange ( 0 , NewValue ) ;
110
+ Assert . AreEqual ( NewValue , _volatile . ReadUnfenced ( 0 ) ) ;
111
+ }
112
+
113
+ [ Test ]
114
+ public void AtomicAddAndGetReturnsNewValue ( )
115
+ {
116
+ const int delta = 5 ;
117
+ Assert . AreEqual ( InitialValue + delta , _volatile . AtomicAddAndGet ( 0 , delta ) ) ;
118
+ }
119
+
120
+ [ Test ]
121
+ public void AtomicAddAndGetMutatesValue ( )
122
+ {
123
+ const int delta = 5 ;
124
+ _volatile . AtomicAddAndGet ( 0 , delta ) ;
125
+ Assert . AreEqual ( InitialValue + delta , _volatile . ReadUnfenced ( 0 ) ) ;
126
+ }
127
+
128
+ [ Test ]
129
+ public void AtomicIncrementAndGetReturnsNewValue ( )
130
+ {
131
+ Assert . AreEqual ( InitialValue + 1 , _volatile . AtomicIncrementAndGet ( 0 ) ) ;
132
+ }
133
+
134
+ [ Test ]
135
+ public void AtomicIncrementAndGetMutatesValue ( )
136
+ {
137
+ _volatile . AtomicIncrementAndGet ( 0 ) ;
138
+ Assert . AreEqual ( InitialValue + 1 , _volatile . ReadUnfenced ( 0 ) ) ;
139
+ }
140
+
141
+ [ Test ]
142
+ public void AtomicDecrementAndGetReturnsNewValue ( )
143
+ {
144
+ Assert . AreEqual ( InitialValue - 1 , _volatile . AtomicDecrementAndGet ( 0 ) ) ;
145
+ }
146
+
147
+ [ Test ]
148
+ public void AtomicDecrementAndGetMutatesValue ( )
149
+ {
150
+ _volatile . AtomicDecrementAndGet ( 0 ) ;
151
+ Assert . AreEqual ( InitialValue - 1 , _volatile . ReadUnfenced ( 0 ) ) ;
152
+ }
153
+ }
154
+ }
0 commit comments