1
+ import kotlinx.coroutines.*
2
+ import org.example.Tree
3
+ import org.junit.jupiter.api.Assertions
4
+ import org.junit.jupiter.api.BeforeEach
5
+ import org.junit.jupiter.params.ParameterizedTest
6
+ import org.junit.jupiter.params.provider.Arguments
7
+ import org.junit.jupiter.params.provider.MethodSource
8
+ import kotlin.random.Random
9
+
10
+ abstract class TreeTest {
11
+ protected lateinit var tree: Tree <Int , String >
12
+ private val rnd = Random (0 )
13
+
14
+ @BeforeEach
15
+ abstract fun setUp ()
16
+
17
+ @OptIn(ExperimentalCoroutinesApi ::class , DelicateCoroutinesApi ::class )
18
+ @ParameterizedTest
19
+ @MethodSource(" threadNumsProvider" )
20
+ fun addingValuesTest (threadsNum : Int ) {
21
+ val valuesToAddLists = List (threadsNum) { List (5000 ) { rnd.nextInt(5000 ) } }
22
+ runBlocking {
23
+ valuesToAddLists.forEachIndexed { id, list ->
24
+ launch(newSingleThreadContext(" Thread$id " )) {
25
+ list.forEach { tree.insert(it, " hz" ) }
26
+ }
27
+ }
28
+ }
29
+ runBlocking {
30
+ valuesToAddLists.forEachIndexed { id, list ->
31
+ launch(newSingleThreadContext(" Thread$id " )) {
32
+ list.forEach { Assertions .assertNotNull(tree.search(it)) }
33
+ }
34
+ }
35
+ }
36
+ }
37
+
38
+ @OptIn(ExperimentalCoroutinesApi ::class , DelicateCoroutinesApi ::class )
39
+ @ParameterizedTest
40
+ @MethodSource(" threadNumsProvider" )
41
+ fun deletingValuesTest (threadsNum : Int ) {
42
+ val valuesToRemoveLists = List (threadsNum) { List (5000 ) { rnd.nextInt(5000 ) } }
43
+ valuesToRemoveLists.forEach {
44
+ println (it)
45
+ }
46
+ val jobs = mutableListOf<Job >()
47
+ runBlocking {
48
+ valuesToRemoveLists.forEachIndexed { id, list ->
49
+ launch(newSingleThreadContext(" Thread$id " )) {
50
+ list.forEach { tree.insert(it, " hz" ) }
51
+ }.let {
52
+ jobs.add(it)
53
+ }
54
+ }
55
+
56
+ jobs.forEach {
57
+ it.join()
58
+ }
59
+ valuesToRemoveLists.forEachIndexed { id, list ->
60
+ launch(newSingleThreadContext(" Thread$id " )) {
61
+ list.forEach { tree.delete(it) }
62
+ }
63
+ }
64
+ }
65
+ runBlocking {
66
+ valuesToRemoveLists.forEachIndexed { id, list ->
67
+ launch(newSingleThreadContext(" Thread$id " )) {
68
+ list.forEach { Assertions .assertNull(tree.search(it)) }
69
+ }
70
+ }
71
+ }
72
+ }
73
+
74
+ companion object {
75
+ @JvmStatic
76
+ fun threadNumsProvider (): List <Arguments > =
77
+ listOf (Arguments .of(1 ), Arguments .of(2 ), Arguments .of(4 ))
78
+ }
79
+ }
0 commit comments