-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_util.kt
213 lines (182 loc) · 6.92 KB
/
_util.kt
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
@file:Suppress("DuplicatedCode")
package utils
import kotlin.reflect.KCallable
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime
import kotlin.jvm.internal.CallableReference
import kotlin.jvm.internal.ClassReference
import kotlin.time.Duration
/**
* For checking if the contents of a list is equal to another list, with optional #[ignoreOrder] flag for ignoring the order
* This is meant for validation of results, not to be used as part of the solution as it is not efficient
*/
inline fun<reified T : Comparable<T>> Iterable<T>.contentEquals(second: Iterable<T>, ignoreOrder: Boolean = false): Boolean {
if (this == second) return true
if (this is Collection && second is Collection) {
if (size != second.size) return false
}
if (!ignoreOrder) {
val i = iterator()
val k = second.iterator()
while (i.hasNext() || k.hasNext()){
if (i.hasNext() != k.hasNext()) return false
if (i.next() != k.next()) return false
}
return true
}
// return this.containsAll(second) && second.containsAll(this) // doesn't handle cases as ["a", "a", "b"] vs ["a", "b", "b"]
// return this.sortedBy { it } == second.sortedBy { it }
return this.sorted() == second.sorted()
}
inline fun<reified T : Comparable<T>> Array<T>.contentEquals(second: Array<T>, ignoreOrder: Boolean = false): Boolean {
if (this === second) return true
if (size != second.size) return false
if (!ignoreOrder) {
for (i in indices) {
if (get(i) != second[i]) return false
}
return true
}
// return this.containsAll(second) && second.containsAll(this) // doesn't handle cases as ["a", "a", "b"] vs ["a", "b", "b"]
return this.sorted() == second.sorted()
}
inline fun IntArray?.contentEquals(second: IntArray?, ignoreOrder: Boolean = false): Boolean {
if (this === second) return true
if (this == null || second == null) return false
if (size != second.size) return false
if (!ignoreOrder) {
for (i in indices) {
if (get(i) != second[i]) return false
}
return true
}
return this.sorted() == second.sorted()
}
inline fun <A, B> Collection<Pair<A, B>>.forEachPair(block: ((first: A, second: B)->Unit)) {
forEach {
block.invoke(it.first, it.second)
}
}
@OptIn(ExperimentalTime::class)
inline fun<reified T : KCallable<*>, reified R> List<T>.runTimedTests(
printTime: Boolean = true,
block: T.()->R
) {
// warming up, as I found that first print is always taking drastic longer time especially inside a measureTime block, no clue why
firstOrNull()?.also {
measureTime { it.block() }
print("")
}
// val testName: String? = Thread.currentThread().stackTrace.getOrNull(1)?.className?.split("$", limit = 0)?.get(0),
forEachIndexed { index, it ->
(it as CallableReference).owner
val testName = try { (it.owner as ClassReference).qualifiedName } catch (e: Exception) { null }
val duration = measureTime{ it.block() }
val prefix = if (testName.isNullOrEmpty()) "" else "${testName}."
if (printTime) println("execution for ${prefix}${it.name} finished, took ${duration.inWholeNanoseconds} Nanoseconds")
}
}
@OptIn(ExperimentalTime::class)
@JvmName("runTimedTestsOn")
inline fun<reified T, reified R> List<T>.runTimedTests(
printTime: Boolean = true,
testName: String? = null,
block: T.()->R
) {
try {
firstOrNull()?.also {
measureTime { it.block() }
print("")
}
} catch (e: Exception) {
// Ignored
}
forEachIndexed { index, it ->
var duration : Duration? = null
try {
duration = measureTime { it.block() }
} catch (e: Exception) {
throw e
} finally {
val prefix = try {
if (testName.isNullOrEmpty()) {
(Thread.currentThread().stackTrace.getOrNull(1) as StackTraceElement).className.split("$")[0]
} else null
} catch (e: Exception) {
null
} ?: testName
if (printTime) println("execution for ${prefix}@$index finished, took ${duration?.inWholeNanoseconds} Nanoseconds")
}
}
}
/// assert
inline infix fun <reified T> T.assertEqualTo(expected: T) = assertEqualTo(expected, checker = null)
inline fun <T> T.assertEqualTo(
expected: T,
noinline checker: ((expected: T, actual: T) -> Boolean)? = null,
crossinline toStr: (T.() -> String) = {
"[" + when (this) {
is Iterable<*> -> joinToString()
is IntArray -> joinToString()
is CharArray -> joinToString()
is FloatArray -> joinToString()
is BooleanArray -> joinToString()
is DoubleArray -> joinToString()
is Array<*> -> joinToString()
else -> toString()
} + "]"
},
noinline lazyMsg: ((expected: T, actual: T) -> String)? = null
) {
val actual = this
val msg = {
lazyMsg?.invoke(expected, actual) ?: "Assertion failed, expected:${expected.toStr()} actual:${actual.toStr()}"
}
assert(
when{
checker != null -> checker.invoke(expected, actual)
expected is IntArray && actual is IntArray -> expected.contentEquals(actual)
expected is CharArray && actual is CharArray -> expected.contentEquals(actual)
expected is FloatArray && actual is FloatArray -> expected.contentEquals(actual)
expected is BooleanArray && actual is BooleanArray -> expected.contentEquals(actual)
expected is DoubleArray && actual is DoubleArray -> expected.contentEquals(actual)
expected is Array<*> && actual is Array<*> -> expected.contentEquals(actual)
else -> expected == actual
},
msg
)
}
/*inline */fun </*reified*/ T> T.assertThat(lazyMsg: ((actual: T)->String)? = null, block: T.() -> Boolean) {
assert(this.block()) {
lazyMsg?.invoke(this) ?: "Assertion failed, actual:$this"
}
}
/* inline fun <reified T> T.assertEqualTo(
expected: T,
noinline lazyMsg: ((expected: T, actual: T) -> String)
) {
assert(this == expected) {
lazyMsg.invoke(expected, this)
}
} */
/*
inline fun <reified T> T.assertEqualTo(
expected: T,
toStr: (T.()->String) = { this.toString() },
noinline lazyMsg: ((expected: T, actual: T) -> Pair<String, String>)? = null
) {
assert(this == expected) {
if (lazyMsg == null) {
"Assertion failed, expected:${expected.toStr()}, actual:${this.toStr()}"
} else {
val strPair = lazyMsg.invoke(expected, this)
"Assertion failed, expected:${strPair.first}, actual:${strPair.second}"
}
}
}
*/
inline fun <reified T> T.assertEqualToAny(vararg expected: T) {
assert(expected.any { it == this }) {
"Assertion failed, actual:$this expected:${expected.joinToString()}"
}
}