Skip to content

Commit 64b4de7

Browse files
hereisderekDerek Zhu
authored andcommitted
fix
1 parent e3f2a12 commit 64b4de7

File tree

5 files changed

+62
-37
lines changed

5 files changed

+62
-37
lines changed

.idea/workspace.xml

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ TODO: Leetcode_710
189189
|:----------:|:-----:|-------------------------------------|-------------------------------------------------------------------------------------|-------|
190190
| 🟢 || 344. Reverse String | [Leetcode](https://leetcode.com/problems/reverse-string/), [Github]() | |
191191
| 🟢 || 303. Range Sum Query - Immutable | [Leetcode](https://leetcode.com/problems/range-sum-query-immutable/), [Github]() | |
192-
| 🟠 | | 304. Range Sum Query 2D - Immutable | [Leetcode](https://leetcode.com/problems/range-sum-query-2d-immutable/), [Github]() | |
192+
| 🟠 | | 304. Range Sum Query 2D - Immutable | [Leetcode](https://leetcode.com/problems/range-sum-query-2d-immutable/), [Github]() | |
193193

194194

195195

src/main/kotlin/_2022/_06/_20220618.kt

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ private interface Leetcode_304 {
165165
companion object : Testable {
166166
override fun test() {
167167
val tests = listOf<Pair<Array<IntArray>, List<Pair<IntArray, Int>>>>(
168-
/* arrayOf(
168+
arrayOf(
169169
intArrayOf(3, 0, 1, 4, 2),
170170
intArrayOf(5, 6, 3, 2, 1),
171171
intArrayOf(1, 2, 0, 1, 5),
@@ -182,7 +182,7 @@ private interface Leetcode_304 {
182182
intArrayOf(0,0,0,0) to -4,
183183
intArrayOf(0,0,0,1) to -9,
184184
intArrayOf(0,1,0,1) to -5,
185-
), */
185+
),
186186
arrayOf(
187187
intArrayOf(8,-4,5),
188188
intArrayOf(-1,4,4),
@@ -196,20 +196,13 @@ private interface Leetcode_304 {
196196
tests.flatMap {
197197
listOf(
198198
M1(it.first) to it.second,
199-
M1(it.first) to it.second,
199+
// M1(it.first) to it.second,
200200
)
201-
}.forEachPair { m, tests ->
202-
tests.forEachPair { pos, expected ->
203-
m.sumRegion(pos[0],pos[1],pos[2],pos[3]).assertEqualTo(expected)
201+
}.runTimedTests {
202+
second.forEachPair { pos, expected ->
203+
first.sumRegion(pos[0],pos[1],pos[2],pos[3]).assertEqualTo(expected)
204204
}
205205
}
206-
/*
207-
listOf(
208-
M1()::someMethod,
209-
).runTimedTests {
210-
invoke().assertEqualTo(Unit)
211-
}
212-
*/
213206
}
214207
}
215208

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
@file:Suppress("DuplicatedCode", "ClassName")
2+
3+
package _2022._06
4+
5+
import Testable
6+
import utils.assertEqualTo
7+
import utils.runTimedTests
8+
9+
fun main() {
10+
Leetcode.test()
11+
}
12+
13+
private interface Leetcode {
14+
15+
companion object : Testable {
16+
override fun test() {
17+
/*
18+
listOf(
19+
M1()::someMethod,
20+
).runTimedTests {
21+
invoke().assertEqualTo(Unit)
22+
}
23+
*/
24+
}
25+
}
26+
27+
private class M1 : Leetcode {
28+
29+
}
30+
}

src/main/kotlin/utils/_util.kt

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,32 +102,31 @@ inline fun<reified T, reified R> List<T>.runTimedTests(
102102
testName: String? = null,
103103
block: T.()->R
104104
) {
105-
var duration : Duration? = null
106105
try {
107106
firstOrNull()?.also {
108107
measureTime { it.block() }
109108
print("")
110109
}
111-
forEach {
112-
val duration = measureTime{ it.block() }
113-
val prefix = if (testName.isNullOrEmpty()) "" else "${testName}."
114-
if (printTime) println("execution for ${prefix} finished, took ${duration.inWholeNanoseconds} Nanoseconds")
115-
}
116-
} catch (e: Throwable) {
117-
118-
} finally {
119-
110+
} catch (e: Exception) {
111+
// Ignored
120112
}
121113

122-
123-
firstOrNull()?.also {
124-
measureTime { it.block() }
125-
print("")
126-
}
127-
forEach {
128-
val duration = measureTime{ it.block() }
129-
val prefix = if (testName.isNullOrEmpty()) "" else "${testName}."
130-
if (printTime) println("execution for ${prefix} finished, took ${duration.inWholeNanoseconds} Nanoseconds")
114+
forEachIndexed { index, it ->
115+
var duration : Duration? = null
116+
try {
117+
duration = measureTime { it.block() }
118+
} catch (e: Exception) {
119+
throw e
120+
} finally {
121+
val prefix = try {
122+
if (testName.isNullOrEmpty()) {
123+
(Thread.currentThread().stackTrace.getOrNull(1) as StackTraceElement).className.split("$")[0]
124+
} else null
125+
} catch (e: Exception) {
126+
null
127+
} ?: testName
128+
if (printTime) println("execution for ${prefix}@$index finished, took ${duration?.inWholeNanoseconds} Nanoseconds")
129+
}
131130
}
132131
}
133132

0 commit comments

Comments
 (0)