• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.atomicfu.test
6 
7 import kotlinx.atomicfu.*
8 import kotlin.test.Test
9 
10 private val a = atomic(0)
11 private val b = atomic(2424920024888888848)
12 private val c = atomic(true)
13 private val abcNode = atomic(ANode(BNode(CNode(8))))
14 private val any = atomic<Any?>(null)
15 
16 private val intArr = AtomicIntArray(3)
17 private val longArr = AtomicLongArray(5)
18 private val booleanArr = AtomicBooleanArray(4)
19 private val refArr = AtomicArray<ANode<BNode<CNode>>>(5)
20 private val anyRefArr = AtomicArray<Any>(10)
21 
22 private val stringAtomicNullArr = atomicArrayOfNulls<String>(10)
23 
24 class TopLevelPrimitiveTest {
25     @Test
26     fun testTopLevelInt() {
27         check(a.value == 0)
28         check(a.getAndSet(3) == 0)
29         check(a.compareAndSet(3, 8))
30         a.lazySet(1)
31         check(a.value == 1)
32         check(a.getAndSet(2) == 1)
33         check(a.value == 2)
34         check(a.getAndIncrement() == 2)
35         check(a.value == 3)
36         check(a.getAndDecrement() == 3)
37         check(a.value == 2)
38         check(a.getAndAdd(2) == 2)
39         check(a.value == 4)
40         check(a.addAndGet(3) == 7)
41         check(a.value == 7)
42         check(a.incrementAndGet() == 8)
43         check(a.value == 8)
44         check(a.decrementAndGet() == 7)
45         check(a.value == 7)
46         a.compareAndSet(7, 10)
47     }
48 
49     @Test
50     fun testTopLevelLong() {
51         check(b.value == 2424920024888888848)
52         b.lazySet(8424920024888888848)
53         check(b.value == 8424920024888888848)
54         check(b.getAndSet(8924920024888888848) == 8424920024888888848)
55         check(b.value == 8924920024888888848)
56         check(b.incrementAndGet() == 8924920024888888849)
57         check(b.value == 8924920024888888849)
58         check(b.getAndDecrement() == 8924920024888888849)
59         check(b.value == 8924920024888888848)
60         check(b.getAndAdd(100000000000000000) == 8924920024888888848)
61         check(b.value == 9024920024888888848)
62         check(b.addAndGet(-9223372036854775807) == -198452011965886959)
63         check(b.value == -198452011965886959)
64         check(b.incrementAndGet() == -198452011965886958)
65         check(b.value == -198452011965886958)
66         check(b.decrementAndGet() == -198452011965886959)
67         check(b.value == -198452011965886959)
68     }
69 
70     @Test
71     fun testTopLevelBoolean() {
72         check(c.value)
73         c.lazySet(false)
74         check(!c.value)
75         check(!c.getAndSet(true))
76         check(c.compareAndSet(true, false))
77         check(!c.value)
78     }
79 
80     @Test
81     fun testTopLevelRef() {
82         check(abcNode.value.b.c.d == 8)
83         val newNode = ANode(BNode(CNode(76)))
84         check(abcNode.getAndSet(newNode).b.c.d == 8)
85         check(abcNode.value.b.c.d == 76)
86         val l = IntArray(4){i -> i}
87         any.lazySet(l)
88         check((any.value as IntArray)[2] == 2)
89     }
90 
91     @Test
92     fun testTopLevelArrayOfNulls() {
93         check(stringAtomicNullArr[0].value == null)
94         check(stringAtomicNullArr[0].compareAndSet(null, "aa"))
95         stringAtomicNullArr[1].lazySet("aa")
96         check(stringAtomicNullArr[0].value == stringAtomicNullArr[1].value)
97     }
98 }
99 
100 class TopLevelArrayTest {
101     @Test
testIntArraynull102     fun testIntArray() {
103         check(intArr[0].compareAndSet(0, 3))
104         check(intArr[1].value == 0)
105         intArr[0].lazySet(5)
106         check(intArr[0].value + intArr[1].value + intArr[2].value == 5)
107         check(intArr[0].compareAndSet(5, 10))
108         check(intArr[0].getAndDecrement() == 10)
109         check(intArr[0].value == 9)
110         intArr[2].value = 2
111         check(intArr[2].value == 2)
112         check(intArr[2].compareAndSet(2, 34))
113         check(intArr[2].value == 34)
114     }
115 
116     @Test
testLongArraynull117     fun testLongArray() {
118         longArr[0].value = 2424920024888888848
119         check(longArr[0].value == 2424920024888888848)
120         longArr[0].lazySet(8424920024888888848)
121         check(longArr[0].value == 8424920024888888848)
122         val ac = longArr[0].value
123         longArr[3].value = ac
124         check(longArr[3].getAndSet(8924920024888888848) == 8424920024888888848)
125         check(longArr[3].value == 8924920024888888848)
126         val ac1 = longArr[3].value
127         longArr[4].value = ac1
128         check(longArr[4].incrementAndGet() == 8924920024888888849)
129         check(longArr[4].value == 8924920024888888849)
130         check(longArr[4].getAndDecrement() == 8924920024888888849)
131         check(longArr[4].value == 8924920024888888848)
132         longArr[4].value = 8924920024888888848
133         check(longArr[4].getAndAdd(100000000000000000) == 8924920024888888848)
134         val ac2 = longArr[4].value
135         longArr[1].value = ac2
136         check(longArr[1].value == 9024920024888888848)
137         check(longArr[1].addAndGet(-9223372036854775807) == -198452011965886959)
138         check(longArr[1].value == -198452011965886959)
139         check(longArr[1].incrementAndGet() == -198452011965886958)
140         check(longArr[1].value == -198452011965886958)
141         check(longArr[1].decrementAndGet() == -198452011965886959)
142         check(longArr[1].value == -198452011965886959)
143     }
144 
145     @Test
testBooleanArraynull146     fun testBooleanArray() {
147         check(!booleanArr[1].value)
148         booleanArr[1].compareAndSet(false, true)
149         booleanArr[0].lazySet(true)
150         check(!booleanArr[2].getAndSet(true))
151         check(booleanArr[0].value && booleanArr[1].value && booleanArr[2].value)
152     }
153 
154     @Suppress("UNCHECKED_CAST")
155     @Test
testRefArraynull156     fun testRefArray() {
157         val a2 = ANode(BNode(CNode(2)))
158         val a3 = ANode(BNode(CNode(3)))
159         refArr[0].value = a2
160         check(refArr[0].value!!.b.c.d == 2)
161         check(refArr[0].compareAndSet(a2, a3))
162         check(refArr[0].value!!.b.c.d == 3)
163         val r0 = refArr[0].value
164         refArr[3].value = r0
165         check(refArr[3].value!!.b.c.d == 3)
166         val a = abcNode.value
167         check(refArr[3].compareAndSet(a3, a))
168 
169         val l = listOf(1, 2, 3)
170         anyRefArr[5].lazySet(l)
171         check((anyRefArr[5].value as List<Int>)[1] == 2)
172         check(anyRefArr[5].compareAndSet(l, a2))
173         check((anyRefArr[5].value as ANode<BNode<CNode>>).b.c.d == 2)
174     }
175 
176 }
177 
178 data class ANode<T>(val b: T)
179 data class BNode<T>(val c: T)
180 data class CNode(val d: Int)
181