Skip to content

Commit 6111996

Browse files
DaniilStepanovlehvolk
authored andcommitted
Comparison with zero finally fixed
1 parent 77e9188 commit 6111996

File tree

5 files changed

+64
-36
lines changed

5 files changed

+64
-36
lines changed

jacodb-core/src/main/kotlin/org/jacodb/impl/cfg/MethodNodeBuilder.kt

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -264,42 +264,38 @@ class MethodNodeBuilder(
264264
cond.lhv.typeName.isPrimitive -> Triple(JcRawInt(0), Opcodes.IFNE, Opcodes.IF_ICMPNE)
265265
else -> Triple(JcRawNull(), Opcodes.IFNONNULL, Opcodes.IF_ACMPNE)
266266
}
267-
268-
is JcRawGeExpr -> Triple(JcRawInt(0), Opcodes.IFGE, Opcodes.IF_ICMPGE).also { shouldReverse = true }
269-
is JcRawGtExpr -> Triple(JcRawInt(0), Opcodes.IFGT, Opcodes.IF_ICMPGT).also { shouldReverse = true }
270-
is JcRawLeExpr -> Triple(JcRawInt(0), Opcodes.IFLE, Opcodes.IF_ICMPLE).also { shouldReverse = true }
271-
is JcRawLtExpr -> Triple(JcRawInt(0), Opcodes.IFLT, Opcodes.IF_ICMPLT).also { shouldReverse = true }
267+
is JcRawGeExpr -> Triple(JcRawInt(0), Opcodes.IFGE, Opcodes.IF_ICMPGE)
268+
is JcRawGtExpr -> Triple(JcRawInt(0), Opcodes.IFGT, Opcodes.IF_ICMPGT)
269+
is JcRawLeExpr -> Triple(JcRawInt(0), Opcodes.IFLE, Opcodes.IF_ICMPLE)
270+
is JcRawLtExpr -> Triple(JcRawInt(0), Opcodes.IFLT, Opcodes.IF_ICMPLT)
272271
else -> error("Unknown condition expr: $cond")
273272
}
274-
val elseBranchTarget: LabelNode
275273
currentInsnList.add(
276274
when {
277275
cond.lhv == zeroValue -> {
278276
cond.rhv.accept(this)
279-
if (shouldReverse) {
280-
elseBranchTarget = trueTarget
281-
JumpInsnNode(zeroCmpOpcode, falseTarget)
282-
} else {
283-
elseBranchTarget = falseTarget
284-
JumpInsnNode(zeroCmpOpcode, trueTarget)
285-
}
277+
val invertedZeroCmpOpcode =
278+
when (zeroCmpOpcode) {
279+
Opcodes.IFGE -> Opcodes.IFLE
280+
Opcodes.IFGT -> Opcodes.IFLT
281+
Opcodes.IFLE -> Opcodes.IFGE
282+
Opcodes.IFLT -> Opcodes.IFGT
283+
else -> zeroCmpOpcode
284+
}
285+
JumpInsnNode(invertedZeroCmpOpcode, trueTarget)
286286
}
287-
288287
cond.rhv == zeroValue -> {
289288
cond.lhv.accept(this)
290-
elseBranchTarget = falseTarget
291289
JumpInsnNode(zeroCmpOpcode, trueTarget)
292290
}
293-
294291
else -> {
295292
cond.lhv.accept(this)
296293
cond.rhv.accept(this)
297-
elseBranchTarget = falseTarget
298294
JumpInsnNode(defaultOpcode, trueTarget)
299295
}
300296
}
301297
)
302-
currentInsnList.add(JumpInsnNode(Opcodes.GOTO, elseBranchTarget))
298+
currentInsnList.add(JumpInsnNode(Opcodes.GOTO, falseTarget))
303299
updateStackInfo(-stackSize)
304300
}
305301

jacodb-core/src/test/kotlin/org/jacodb/testing/cfg/InstructionsTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class InstructionsTest : BaseInstructionsTest() {
296296
}
297297

298298
@Test
299-
@Disabled("Unexpected behavior on java 8 and 11")
299+
@DisabledOnJre(JRE.JAVA_8, JRE.JAVA_11)
300300
fun `instance method ref bug`() {
301301
val clazz = cp.findClass<Close>()
302302
val javaClazz = testAndLoadClass(clazz)

jacodb-core/src/test/kotlin/org/jacodb/testing/cfg/KotlinInstructionsTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class KotlinInstructionsTest: BaseInstructionsTest() {
4141
@Test
4242
fun `kotlin range test`() = runKotlinTest(Ranges::class.java.name)
4343

44-
// @Test
45-
// fun `kotlin range test 2`() = runKotlinTest(Ranges2::class.java.name)
46-
//
44+
@Test
45+
fun `kotlin range test 2`() = runKotlinTest(Ranges2::class.java.name)
46+
4747
// @Test
4848
// fun `kotlin overloading test`() = runKotlinTest(Overloading::class.java.name)
4949

jacodb-core/src/test/kotlin/org/jacodb/testing/cfg/LoopsTest.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import org.jacodb.testing.WithGlobalDB
2626
import org.junit.jupiter.api.Assertions
2727
import org.junit.jupiter.api.Assertions.assertEquals
2828
import org.junit.jupiter.api.Test
29+
import org.junit.jupiter.api.condition.DisabledOnJre
30+
import org.junit.jupiter.api.condition.JRE
2931

3032
class LoopsTest : BaseTest() {
3133

@@ -86,13 +88,17 @@ class LoopsTest : BaseTest() {
8688
}
8789
}
8890

91+
//Disabled on JAVA_8 because of different bytecode and different lineNumbers for loops
8992
@Test
93+
@DisabledOnJre(JRE.JAVA_8)
9094
fun `combined loops`() {
9195
val clazz = cp.findClass<JavaTasks>()
9296
with(clazz.findMethod("sortTimes").loops) {
9397
assertEquals(3, size)
9498
with(first()) {
95-
Assertions.assertTrue(head.lineNumber == 53 || head.lineNumber == 51)
99+
assertEquals(53, head.lineNumber)
100+
assertEquals(listOf(53, 61, 73), exits.map { it.lineNumber }.toSet().sorted())
101+
assertSources(53, 75)
96102
}
97103

98104
with(get(1)) {

jacodb-core/src/testFixtures/kotlin/org/jacodb/testing/cfg/Ranges.kt

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,52 @@ class Ranges {
7272

7373
class Ranges2 {
7474

75-
fun isDigit(a: Int) : String {
76-
val aa = ArrayList<Int> ()
77-
aa.add(239)
78-
79-
return when(a) {
80-
in aa -> "array list"
75+
fun assertDigit(i: Int): String {
76+
val res = when (i) {
8177
in 0..9 -> "digit"
82-
!in 0..100 -> "not small"
78+
!in 0..100 -> "not small1 $i"
79+
else -> "something $i"
80+
}
81+
if (res == "digit") return "OK"
82+
return "fail $res"
83+
}
84+
85+
fun assertDigit2(i: Int): String {
86+
val res = when (i) {
87+
in 9 downTo 0 -> "digit"
88+
!in 0..100 -> "not small2 $i"
8389
else -> "something"
8490
}
91+
if (res == "digit") return "OK"
92+
return "fail $res"
8593
}
8694

87-
fun assertDigit(i: Int, expected: String): String {
88-
val result = isDigit(i)
89-
return if (result == expected) "" else "fail: isDigit($i) = \"$result\""
95+
fun assertDigit3(i: Int): String {
96+
val res = when (i) {
97+
!in 0..9 -> "f1 $i"
98+
else -> "d"
99+
}
100+
if (res == "d") return "OK"
101+
return "fail $res"
90102
}
91103

104+
fun assertDigit4(i: Int): String {
105+
val res = when (i) {
106+
!in 9 downTo 0 -> "f2 $i"
107+
else -> "d"
108+
}
109+
if (res == "d") return "OK"
110+
return "fail $res"
111+
}
112+
113+
92114
fun box(): String {
93-
val result = assertDigit(0, "digit")
94-
if (result == "") return "OK"
95-
return result
115+
(0..9 step 2).map {
116+
assertDigit(it).let { if (it != "OK") return it }
117+
assertDigit2(it).let { if (it != "OK") return it }
118+
assertDigit3(it).let { if (it != "OK") return it }
119+
assertDigit4(it).let { if (it != "OK") return it }
120+
}
121+
return "OK"
96122
}
97123
}

0 commit comments

Comments
 (0)