1 /*
<lambda>null2  * Copyright 2016-2020 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 internal_test2.Updater
8 import kotlinx.atomicfu.*
9 import kotlin.test.Test
10 import kotlin.test.assertEquals
11 
12 class CounterWithDefaultTrace {
13     private val a = atomic(0)
14 
15     private val defaultTrace = Trace()
16     private val a1 = atomic(5, defaultTrace)
17 
18     fun inc(): Int {
19         val x = a.incrementAndGet()
20         return x
21     }
22 
23     fun multTen(): Boolean {
24         val oldValue = a1.value
25         defaultTrace { "current value = $oldValue" }
26         return a1.compareAndSet(oldValue, oldValue * 10)
27     }
28 
29     internal fun getA() = a.value
30     internal fun getA1() = a1.value
31 }
32 
33 class CounterWithCustomSizeTrace {
34     private val t = Trace(30)
35     private val a = atomic(0, t)
36 
decnull37     fun dec(): Int {
38         t { "current value = ${a.value}" }
39         return a.getAndDecrement()
40     }
getnull41     internal fun get() = a.value
42 }
43 
44 class CounterWithCustomSizeAndFuncTrace {
45     private val t = Trace(30, TraceFormat { id, text -> "$id: $text"})
46     private val a = atomic(0)
47 
48     fun dec(): Int {
49         t { "current value = ${a.value}" }
50         return a.getAndDecrement()
51     }
52     internal fun get() = a.value
53 }
54 
55 class CounterWithInternalTrace {
56     private val u = Updater()
57     private val a = atomic(0, u.internalTrace)
58 
updatenull59     fun update() {
60         val oldValue = a.value
61         u.internalTrace { "old value = $oldValue" }
62         a.compareAndSet(oldValue, oldValue + 5)
63     }
getnull64     internal fun get() = a.value
65 }
66 
67 class InternalTraceTest {
68     @Test
69     fun testInternalTrace() {
70         val cit = CounterWithInternalTrace()
71         repeat(5) { cit.update() }
72         assertEquals(cit.get(), 25)
73     }
74 }
75 
76 class MyCounter {
77     @Test
testEasynull78     fun testEasy() {
79         val c = CounterWithDefaultTrace()
80         assertEquals(0, c.getA())
81         c.inc()
82         assertEquals(1, c.getA())
83         c.multTen()
84         assertEquals(c.getA1(), 50)
85     }
86 
87     @Test
testCustomSizeTracenull88     fun testCustomSizeTrace() {
89         val n = 1000
90         val c = CounterWithCustomSizeTrace()
91         repeat(n) {
92             c.dec()
93         }
94         assertEquals(-n, c.get())
95     }
96 
97     @Test
testCustomSizeAndFuncTracenull98     fun testCustomSizeAndFuncTrace() {
99         val n = 1000
100         val c = CounterWithCustomSizeAndFuncTrace()
101         repeat(n) {
102             c.dec()
103         }
104         assertEquals(-n, c.get())
105     }
106 }
107 
108 class TraceAppendOverridesTest {
inull109     private val aTrace = Trace(format = TraceFormat { i, text -> "[$i: $text]" })
110     private val a = atomic(0, aTrace)
111 
112     private enum class Status { START, END }
113 
incnull114     fun inc(n: Int, i: Int) {
115         aTrace.append(i, n, Status.START)
116         val res = a.getAndAdd(n)
117         aTrace.append(i, n, res, Status.END)
118     }
119 
120     @Test
testTraceAppendOverridesnull121     fun testTraceAppendOverrides() {
122         val n = 1000
123         repeat(n) {
124             inc(1, it)
125         }
126         assertEquals(n, a.value)
127     }
128 }