forked from scala-native/scala-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AtomicReferenceTest.scala
76 lines (53 loc) · 1.85 KB
/
AtomicReferenceTest.scala
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
package java.util
package concurrent
package atomic
import java.util.function.UnaryOperator
import org.junit.Ignore
import org.junit.Test
import org.junit.Assert._
@Ignore
class AtomicReferenceTest {
// This test suite is INCOMPLETE (obviously!).
//
// The get() method is used by getAndUpdate() and updateAndGet().
// The test is only a shallow probe before use.
//
// getAndUpdate() and updateAndGet() test only that the expected
// values are returned in the success case. It was not evident
// how to test concurrent and contended access patterns within
// the scope of unit-tests.
@Test def get(): Unit = {
val expected = -1
val ar = new AtomicReference(expected)
val result = ar.get()
assertTrue(s"result: ${result} != expected: ${expected}",
result == expected)
}
@Test def getAndUpdateUpdateFunction(): Unit = {
val expectedValue = 100
val expectedNewValue = expectedValue / 2
val tax = new UnaryOperator[Int] {
override def apply(t: Int): Int = t / 2
}
val ar = new AtomicReference[Int](expectedValue)
val value = ar.getAndUpdate(tax)
assertTrue(
s"result before function: ${value} != expected: ${expectedValue}",
value == expectedValue)
val newValue = ar.get()
assertTrue(s"newValue after function: ${newValue} != " +
s"expected: ${expectedNewValue}",
newValue == expectedNewValue)
}
@Test def updateAndGetUpdateFunction(): Unit = {
val initialValue = 100
val expected = initialValue * 3
val reward = new UnaryOperator[Int] {
override def apply(t: Int): Int = t * 3
}
val ar = new AtomicReference[Int](initialValue)
val result = ar.updateAndGet(reward)
assertTrue(s"result after function: ${result} != expected: ${expected}",
result == expected)
}
}