1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Threading;
// Tests that CompareExchange(Double, Double, Double)
// actually switches values when location = comparand and
// does not when it does not
public class InterlockedCompareExchange3
{
private const int c_NUM_LOOPS = 100;
public static int Main()
{
InterlockedCompareExchange3 test = new InterlockedCompareExchange3();
TestLibrary.TestFramework.BeginTestCase("InterlockedCompareExchange3");
if (test.RunTests())
{
TestLibrary.TestFramework.EndTestCase();
TestLibrary.TestFramework.LogInformation("PASS");
return 100;
}
else
{
TestLibrary.TestFramework.EndTestCase();
TestLibrary.TestFramework.LogInformation("FAIL");
return 0;
}
}
public bool RunTests()
{
bool retVal = true;
TestLibrary.TestFramework.LogInformation("[Positive]");
retVal = PosTest1() && retVal;
retVal = PosTest2() && retVal;
return retVal;
}
// This test iterates 100 times. Each time it gets two
// Doubles, value and location. It stores location in comparand, so
// they will be equal on comparison. It then uses
// Interlocked.CompareExchange to compare location with comparand.
// Since they are equal, it must exchange: location is replaced by
// value, and the original value in location is returned to oldLocation.
// Then it checks that location now equals value, and that oldLocation
// equals comparand. If either of these are not true, retVal gets set to
// false, and ultimately it returns false. since location is never null,
// it should not throw an exception, so doing so causes the test
// to fail.
public bool PosTest1()
{
bool retVal = true;
Double location;
Double value;
Double comparand;
Double oldLocation;
TestLibrary.TestFramework.BeginScenario("PosTest1: Double Interlocked.CompareExchange(Double&,Double,Double) where comparand is equal");
try
{
for (int i=0; i<c_NUM_LOOPS; i++)
{
value = TestLibrary.Generator.GetDouble();
location = TestLibrary.Generator.GetDouble();
comparand = location;
oldLocation = Interlocked.CompareExchange(ref location, value, comparand);
// At this point, we should be able to make
// the following assertion:
// location = value
// oldLocation = comparand
if (!location.Equals(value))
{
TestLibrary.TestFramework.LogError("001", "Interlocked.CompareExchange() did not do the exchange correctly: Expected(" + value + ") Actual(" + location + ")");
retVal = false;
}
if (!oldLocation.Equals(comparand))
{
TestLibrary.TestFramework.LogError("002", "Interlocked.CompareExchange() did not return the expected value: Expected(" + comparand + ") Actual(" + oldLocation + ")");
retVal = false;
}
}
}
catch (Exception e)
{
// since location is not null, any exception
// causes test failure.
TestLibrary.TestFramework.LogError("003", "Unexpected exception: " + e);
retVal = false;
}
return retVal;
}
// This test iterates 100 times. Each time it gets two
// Doubles, value and location. It then gets a value for comparand, such
// that it is NOT EQUAL to location. It then uses
// Interlocked.CompareExchange to compare location with comparand.
// Since they are not equal, it does not exchange.
// since location is never null,
// it should not throw an exception, so doing so causes the test
// to fail.
public bool PosTest2()
{
bool retVal = true;
Double location;
Double value;
Double comparand;
Double oldLocation;
TestLibrary.TestFramework.BeginScenario("PosTest2: Double Interlocked.CompareExchange(Double&,Double,Double) where comparand are not equal");
try
{
for (int i=0; i<c_NUM_LOOPS; i++)
{
value = TestLibrary.Generator.GetDouble();
location = TestLibrary.Generator.GetDouble();
comparand = value;
while(comparand.Equals(location))
{
comparand = TestLibrary.Generator.GetDouble();
}
oldLocation = Interlocked.CompareExchange(ref location, value, comparand);
// At this point, we should be able to make
// the following assertions:
// location != value
// location != comparand
// location = oldLocation
// oldLocation != comparand
if (location.Equals(value))
{
TestLibrary.TestFramework.LogError("004", "Interlocked.CompareExchange() did not do the exchange correctly: Expected(" + value + ") Actual(" + location + ")");
retVal = false;
}
if (oldLocation.Equals(comparand))
{
TestLibrary.TestFramework.LogError("005", "Interlocked.CompareExchange() did not return the expected value: Expected(" + location + ") Actual(" + oldLocation + ")");
retVal = false;
}
}
}
catch (Exception e)
{
// since location is not null, any exception
// causes test failure.
TestLibrary.TestFramework.LogError("006", "Unexpected exception: " + e);
retVal = false;
}
return retVal;
}
}
|