@@ -2,6 +2,7 @@ package org.modelix.datastructures
2
2
3
3
import org.modelix.datastructures.objects.IObjectGraph
4
4
import org.modelix.datastructures.patricia.PatriciaTrie
5
+ import org.modelix.streams.getBlocking
5
6
import kotlin.random.Random
6
7
import kotlin.test.Test
7
8
import kotlin.test.assertEquals
@@ -24,10 +25,10 @@ class PatriciaTrieTest {
24
25
25
26
fun assertTree () {
26
27
val expectedEntries = addedEntries.associateWith { values[it]!! }
27
- val actualEntries = tree.getAll().toList().getSynchronous( ).toMap()
28
+ val actualEntries = tree.getAll().toList().getBlocking(tree ).toMap()
28
29
assertEquals(expectedEntries, actualEntries)
29
30
30
- val expectedTree = addedEntries.fold(initialTree) { acc, it -> acc.put(it, values[it]!! ).getSynchronous( ) }
31
+ val expectedTree = addedEntries.fold(initialTree) { acc, it -> acc.put(it, values[it]!! ).getBlocking(tree ) }
31
32
assertEquals(expectedTree, tree)
32
33
33
34
assertEquals(expectedTree.asObject().getHash(), tree.asObject().getHash())
@@ -38,13 +39,13 @@ class PatriciaTrieTest {
38
39
val key = removedEntries.random()
39
40
removedEntries.remove(key)
40
41
addedEntries.add(key)
41
- tree = tree.put(key, values[key]!! ).getSynchronous( )
42
+ tree = tree.put(key, values[key]!! ).getBlocking(tree )
42
43
assertTree()
43
44
} else {
44
45
val key = addedEntries.random()
45
46
removedEntries.add(key)
46
47
addedEntries.remove(key)
47
- tree = tree.remove(key).getSynchronous( )
48
+ tree = tree.remove(key).getBlocking(tree )
48
49
assertTree()
49
50
}
50
51
}
@@ -55,55 +56,55 @@ class PatriciaTrieTest {
55
56
@Test
56
57
fun `slice with shorter prefix and single entry` () {
57
58
var tree: PatriciaTrie <String , String > = PatriciaTrie .withStrings(IObjectGraph .FREE_FLOATING )
58
- tree = tree.put(" abcdef" , " 1" ).getSynchronous( )
59
+ tree = tree.put(" abcdef" , " 1" ).getBlocking(tree )
59
60
60
- assertEquals(" 1" , tree.slice(" abc" ).flatMapZeroOrOne { it.get(" abcdef" ) }.getSynchronous( ))
61
+ assertEquals(" 1" , tree.slice(" abc" ).flatMapZeroOrOne { it.get(" abcdef" ) }.getBlocking(tree ))
61
62
}
62
63
63
64
@Test
64
65
fun `slice with shorter prefix and two entries` () {
65
66
var tree: PatriciaTrie <String , String > = PatriciaTrie .withStrings(IObjectGraph .FREE_FLOATING )
66
- tree = tree.put(" abcdef" , " 1" ).getSynchronous( )
67
- tree = tree.put(" abcdeg" , " 2" ).getSynchronous( )
67
+ tree = tree.put(" abcdef" , " 1" ).getBlocking(tree )
68
+ tree = tree.put(" abcdeg" , " 2" ).getBlocking(tree )
68
69
69
- assertEquals(" 1" , tree.slice(" abc" ).flatMapZeroOrOne { it.get(" abcdef" ) }.getSynchronous( ))
70
- assertEquals(" 2" , tree.slice(" abc" ).flatMapZeroOrOne { it.get(" abcdeg" ) }.getSynchronous( ))
70
+ assertEquals(" 1" , tree.slice(" abc" ).flatMapZeroOrOne { it.get(" abcdef" ) }.getBlocking(tree ))
71
+ assertEquals(" 2" , tree.slice(" abc" ).flatMapZeroOrOne { it.get(" abcdeg" ) }.getBlocking(tree ))
71
72
}
72
73
73
74
@Test
74
75
fun `slice with prefix between two entries` () {
75
76
var tree: PatriciaTrie <String , String > = PatriciaTrie .withStrings(IObjectGraph .FREE_FLOATING )
76
- tree = tree.put(" ab" , " 1" ).getSynchronous( )
77
- tree = tree.put(" abcdef" , " 2" ).getSynchronous( )
77
+ tree = tree.put(" ab" , " 1" ).getBlocking(tree )
78
+ tree = tree.put(" abcdef" , " 2" ).getBlocking(tree )
78
79
79
- assertEquals(null , tree.slice(" abcd" ).flatMapZeroOrOne { it.get(" ab" ) }.getSynchronous( ))
80
- assertEquals(" 2" , tree.slice(" abcd" ).flatMapZeroOrOne { it.get(" abcdef" ) }.getSynchronous( ))
80
+ assertEquals(null , tree.slice(" abcd" ).flatMapZeroOrOne { it.get(" ab" ) }.getBlocking(tree ))
81
+ assertEquals(" 2" , tree.slice(" abcd" ).flatMapZeroOrOne { it.get(" abcdef" ) }.getBlocking(tree ))
81
82
}
82
83
83
84
@Test
84
85
fun `slice with one before and two after` () {
85
86
var tree: PatriciaTrie <String , String > = PatriciaTrie .withStrings(IObjectGraph .FREE_FLOATING )
86
- tree = tree.put(" ab" , " 1" ).getSynchronous( )
87
- tree = tree.put(" abcdef" , " 2" ).getSynchronous( )
88
- tree = tree.put(" abcdeg" , " 3" ).getSynchronous( )
87
+ tree = tree.put(" ab" , " 1" ).getBlocking(tree )
88
+ tree = tree.put(" abcdef" , " 2" ).getBlocking(tree )
89
+ tree = tree.put(" abcdeg" , " 3" ).getBlocking(tree )
89
90
90
- assertEquals(null , tree.slice(" abcd" ).flatMapZeroOrOne { it.get(" ab" ) }.getSynchronous( ))
91
- assertEquals(" 2" , tree.slice(" abcd" ).flatMapZeroOrOne { it.get(" abcdef" ) }.getSynchronous( ))
92
- assertEquals(" 3" , tree.slice(" abcd" ).flatMapZeroOrOne { it.get(" abcdeg" ) }.getSynchronous( ))
91
+ assertEquals(null , tree.slice(" abcd" ).flatMapZeroOrOne { it.get(" ab" ) }.getBlocking(tree ))
92
+ assertEquals(" 2" , tree.slice(" abcd" ).flatMapZeroOrOne { it.get(" abcdef" ) }.getBlocking(tree ))
93
+ assertEquals(" 3" , tree.slice(" abcd" ).flatMapZeroOrOne { it.get(" abcdeg" ) }.getBlocking(tree ))
93
94
}
94
95
95
96
@Test
96
97
fun `slice with two before and two after at existing split` () {
97
98
var tree: PatriciaTrie <String , String > = PatriciaTrie .withStrings(IObjectGraph .FREE_FLOATING )
98
- tree = tree.put(" a" , " 0" ).getSynchronous( )
99
- tree = tree.put(" ab" , " 1" ).getSynchronous( )
100
- tree = tree.put(" abcdef" , " 2" ).getSynchronous( )
101
- tree = tree.put(" abcdeg" , " 3" ).getSynchronous( )
102
-
103
- assertEquals(null , tree.slice(" ab" ).flatMapZeroOrOne { it.get(" a" ) }.getSynchronous( ))
104
- assertEquals(" 1" , tree.slice(" ab" ).flatMapZeroOrOne { it.get(" ab" ) }.getSynchronous( ))
105
- assertEquals(" 2" , tree.slice(" ab" ).flatMapZeroOrOne { it.get(" abcdef" ) }.getSynchronous( ))
106
- assertEquals(" 3" , tree.slice(" ab" ).flatMapZeroOrOne { it.get(" abcdeg" ) }.getSynchronous( ))
99
+ tree = tree.put(" a" , " 0" ).getBlocking(tree )
100
+ tree = tree.put(" ab" , " 1" ).getBlocking(tree )
101
+ tree = tree.put(" abcdef" , " 2" ).getBlocking(tree )
102
+ tree = tree.put(" abcdeg" , " 3" ).getBlocking(tree )
103
+
104
+ assertEquals(null , tree.slice(" ab" ).flatMapZeroOrOne { it.get(" a" ) }.getBlocking(tree ))
105
+ assertEquals(" 1" , tree.slice(" ab" ).flatMapZeroOrOne { it.get(" ab" ) }.getBlocking(tree ))
106
+ assertEquals(" 2" , tree.slice(" ab" ).flatMapZeroOrOne { it.get(" abcdef" ) }.getBlocking(tree ))
107
+ assertEquals(" 3" , tree.slice(" ab" ).flatMapZeroOrOne { it.get(" abcdeg" ) }.getBlocking(tree ))
107
108
}
108
109
109
110
@Test
@@ -126,16 +127,16 @@ class PatriciaTrieTest {
126
127
" abcdC" ,
127
128
)
128
129
for (key in initialKeys) {
129
- tree = tree.put(key, " value of $key " ).getSynchronous( )
130
+ tree = tree.put(key, " value of $key " ).getBlocking(tree )
130
131
}
131
132
132
133
assertEquals(
133
134
initialKeys.associateWith { " value of $it " },
134
- tree.getAll().toList().getSynchronous( ).toMap(),
135
+ tree.getAll().toList().getBlocking(tree ).toMap(),
135
136
)
136
137
137
138
for (key in initialKeys) {
138
- assertEquals(" value of $key " , tree.get(key).getSynchronous( ))
139
+ assertEquals(" value of $key " , tree.get(key).getBlocking(tree ))
139
140
}
140
141
141
142
var replacementTree = PatriciaTrie .withStrings(IObjectGraph .FREE_FLOATING )
@@ -150,16 +151,16 @@ class PatriciaTrieTest {
150
151
" abcdB93" ,
151
152
)
152
153
for (key in replacementKeys) {
153
- replacementTree = replacementTree.put(key, " new value of $key " ).getSynchronous( )
154
+ replacementTree = replacementTree.put(key, " new value of $key " ).getBlocking(tree )
154
155
}
155
- tree = tree.replaceSlice(" abcdB" , replacementTree).getSynchronous( )
156
+ tree = tree.replaceSlice(" abcdB" , replacementTree).getBlocking(tree )
156
157
157
158
assertEquals(
158
159
initialKeys.filterNot { it.startsWith(" abcdB" ) }.map { it to " value of $it " }
159
160
.plus(replacementKeys.map { it to " new value of $it " })
160
161
.sortedBy { it.first }
161
162
.joinToString(" \n " ) { " ${it.first} -> ${it.second} " },
162
- tree.getAll().toList().getSynchronous( ).sortedBy { it.first }.joinToString(" \n " ) { " ${it.first} -> ${it.second} " },
163
+ tree.getAll().toList().getBlocking(tree ).sortedBy { it.first }.joinToString(" \n " ) { " ${it.first} -> ${it.second} " },
163
164
)
164
165
}
165
166
}
0 commit comments