Skip to content

Commit 8d38e18

Browse files
committed
测试
1 parent 0ced0f3 commit 8d38e18

File tree

3 files changed

+143
-1
lines changed

3 files changed

+143
-1
lines changed

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@
190190
</build>
191191

192192
<dependencies>
193+
<dependency>
194+
<groupId>com.alibaba.fastjson2</groupId>
195+
<artifactId>fastjson2-kotlin</artifactId>
196+
<version>2.0.21</version>
197+
</dependency>
193198
<dependency>
194199
<groupId>com.github.masx200</groupId>
195200
<artifactId>leetcode-treenode-java</artifactId>
@@ -220,6 +225,12 @@
220225
<version>1.4.1</version>
221226

222227
</dependency>
228+
<dependency>
229+
<groupId>org.jetbrains.kotlin</groupId>
230+
<artifactId>kotlin-reflect</artifactId>
231+
<version>1.7.21</version>
232+
<scope>compile</scope>
233+
</dependency>
223234

224235

225236
</dependencies>

test/com/github/masx200/leetcode_test/operations_lcci/OperationsTest.kt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package com.github.masx200.leetcode_test.operations_lcci
22

3+
import com.alibaba.fastjson2.JSON
4+
import com.alibaba.fastjson2.JSONArray
5+
import com.github.masx200.leetcode_test.utils.runScript
6+
import kotlinx.serialization.decodeFromString
7+
import kotlinx.serialization.json.Json
38
import org.junit.jupiter.api.Test
9+
import kotlin.test.assertContentEquals
410
import kotlin.test.assertEquals
511

612
class OperationsTest {
@@ -15,6 +21,66 @@ class OperationsTest {
1521

1622
@Test
1723
fun multiply() {
24+
25+
val actual: JSONArray = JSON.parseArray(
26+
"""
27+
[
28+
null,
29+
2147483647,
30+
-2147483648,
31+
2147483647,
32+
-2147483600,
33+
-2147483647,
34+
-2147483648
35+
]
36+
"""
37+
)
38+
val expected: List<Any?> = runScript(
39+
Json.decodeFromString(
40+
"""
41+
[
42+
"Operations",
43+
"minus",
44+
"minus",
45+
"multiply",
46+
"multiply",
47+
"divide",
48+
"divide"
49+
]
50+
"""
51+
),
52+
JSON.parseArray(
53+
"""
54+
[
55+
[],
56+
[0, -2147483647],
57+
[-1, 2147483647],
58+
[-1, -2147483647],
59+
[-100, 21474836],
60+
[2147483647, -1],
61+
[-2147483648, 1]
62+
]
63+
"""
64+
) as ArrayList<ArrayList<Any>>,
65+
Operations::class,
66+
)
67+
println("expected")
68+
expected.forEach {
69+
if (it != null) {
70+
println(it.javaClass)
71+
}
72+
}
73+
println("actual")
74+
actual.forEach {
75+
if (it != null) {
76+
println(it.javaClass)
77+
}
78+
}
79+
assertContentEquals(
80+
81+
expected,
82+
actual,
83+
);
1884
}
1985

2086

utils/runScript.kt

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,68 @@
11
package com.github.masx200.leetcode_test.utils
22

3-
fun runScript(){}
3+
import kotlin.reflect.KClass
4+
import kotlin.reflect.KFunction
5+
import kotlin.reflect.full.declaredFunctions
6+
import kotlin.reflect.jvm.jvmName
7+
8+
fun runScript(commands: List<String>, inputs: ArrayList<ArrayList<Any>>, classes: KClass<*>): List<Any?> {
9+
// println(commands)
10+
// println(inputs)
11+
// println(classes)
12+
val kFunction = classes.constructors.first()
13+
if (kFunction.parameters.size != inputs[0].size) throw Error("constructor parameters mismatch")
14+
// println(kFunction.parameters)
15+
val instance = kFunction.call(*Array(inputs[0].size) { inputs[0][it] })
16+
val res = List<Any?>(commands.size) { null }.toMutableList()
17+
val methodMap = hashMapOf<String, KFunction<*>>()
18+
for (fu in classes.declaredFunctions) {
19+
20+
methodMap[fu.name] = fu
21+
}
22+
// println(instance)
23+
for ((i, arg) in inputs.withIndex()) {
24+
if (i != 0) {
25+
val name = commands[i]
26+
val fu = methodMap[name] ?: throw Error("method not found")
27+
// (fu.parameters).forEach(::println)
28+
// println(arg[0].javaClass)
29+
30+
for (j in arg.indices) {
31+
if (arg[j].javaClass != (fu.parameters[j + 1].type.classifier) as KClass<*>) {
32+
val jvmName = (fu.parameters[j + 1].type.classifier as KClass<*>).jvmName
33+
val old = arg[j]
34+
if (old is Number) {
35+
arg[j] = when (jvmName) {
36+
"int" -> old.toInt()
37+
"double" -> old.toDouble()
38+
"float" -> old.toFloat()
39+
"long" -> old.toLong()
40+
"char" -> old.toChar()
41+
"short" -> old.toShort()
42+
"byte" -> old.toByte()
43+
else -> {
44+
throw Error("error number type:" + jvmName)
45+
}
46+
}
47+
}
48+
// println(arg[j].javaClass.name)
49+
50+
// println(jvmName)
51+
52+
// println(arg[j] is Number)
53+
}
54+
}
55+
56+
val re = fu.call(instance, *Array(arg.size) { arg[it] })
57+
58+
if (re == Unit) {
59+
res[i] = null
60+
} else {
61+
res[i] = re
62+
}
63+
}
64+
65+
}
66+
67+
return res
68+
}

0 commit comments

Comments
 (0)