Skip to content

Commit

Permalink
Added apply() unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leventeBajczi committed Jul 21, 2023
1 parent db55149 commit 4963dbb
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private fun getPredXcfaTransFunc(
}
}

class PredXcfaAnalaysis(xcfa: XCFA, solver: Solver, predAbstractor: PredAbstractor,
class PredXcfaAnalysis(xcfa: XCFA, solver: Solver, predAbstractor: PredAbstractor,
partialOrd: PartialOrd<XcfaState<PredState>>) : XcfaAnalysis<PredState, PredPrec>(
corePartialOrd = partialOrd,
coreInitFunc = getPredXcfaInitFunc(xcfa, predAbstractor),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2023 Budapest University of Technology and Economics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package hu.bme.mit.theta.xcfa.analysis

import hu.bme.mit.theta.analysis.expl.ExplState
import hu.bme.mit.theta.core.type.inttype.IntExprs
import hu.bme.mit.theta.xcfa.model.*
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import java.util.*
import java.util.function.Predicate

class XcfaStateTest {

@Test
fun testApply1() {
val actionOrder: MutableList<XcfaAction> = ArrayList()
val expectations: MutableList<Predicate<XcfaState<ExplState>>> = ArrayList()
lateinit var initState: XcfaState<ExplState>
lateinit var xcfa: XCFA

val edges: MutableList<XcfaEdge> = ArrayList()
xcfa = xcfa("example") {
val proc1 = procedure("proc1") {
val a = "a" type IntExprs.Int() direction ParamDirection.IN
val b = "b" type IntExprs.Int() direction ParamDirection.OUT

edges.add((init to final) {
b assign a.ref
})
}
val main = procedure("main") {
val tmp = "tmp" type IntExprs.Int()
edges.add((init to "L1") {
proc1("1", tmp.ref)
})
edges.add(("L1" to "L2") {
tmp.start(proc1, tmp.ref)
})
edges.add(("L2" to final) {
tmp.join()
})
}

main.start()
}
initState = XcfaState(
xcfa,
mapOf(
Pair(0,
XcfaProcessState(
locs = LinkedList(listOf(edges[1].source)),
varLookup = LinkedList()
)
)
),
ExplState.bottom()
)

actionOrder.add(XcfaAction(0, edges[1]))
expectations.add { it.processes[0]!!.locs.size == 2 && it.processes[0]!!.locs.peek() == edges[0].source }

actionOrder.add(XcfaAction(0, edges[0]))
expectations.add { it.processes[0]!!.locs.size == 2 && it.processes[0]!!.locs.peek() == edges[0].target }

actionOrder.add(XcfaAction(0, XcfaEdge(edges[0].target, edges[0].target, ReturnLabel(NopLabel))))
expectations.add { it.processes[0]!!.locs.size == 1 && it.processes[0]!!.locs.peek() == edges[1].target }

actionOrder.add(XcfaAction(0, edges[2]))
expectations.add {
it.processes.size == 2 &&
it.processes[0]!!.locs.size == 1 && it.processes[0]!!.locs.peek() == edges[2].target &&
it.processes[1]!!.locs.size == 1 && it.processes[1]!!.locs.peek() == edges[0].source
}

actionOrder.add(XcfaAction(1, edges[0]))
expectations.add {
it.processes.size == 2 &&
it.processes[0]!!.locs.size == 1 && it.processes[0]!!.locs.peek() == edges[2].target &&
it.processes[1]!!.locs.size == 1 && it.processes[1]!!.locs.peek() == edges[0].target
}

actionOrder.add(XcfaAction(1, XcfaEdge(edges[0].target, edges[0].target, ReturnLabel(NopLabel))))
expectations.add { it.processes.size == 1 && it.processes[0]!!.locs.size == 1 && it.processes[0]!!.locs.peek() == edges[2].target }

actionOrder.add(XcfaAction(0, edges[3]))
expectations.add { it.processes[0]!!.locs.size == 1 && it.processes[0]!!.locs.peek() == edges[3].target }

var state = initState
for ((index, xcfaAction) in actionOrder.withIndex()) {
println("Test $index: $xcfaAction")
val newState = state.apply(xcfaAction)
assertTrue(expectations[index].test(newState.first))
state = newState.first
println("Test $index OK")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ enum class Domain(
),
PRED_BOOL(
abstractor = { a, b, c, d, e, f, g, h, i ->
getXcfaAbstractor(PredXcfaAnalaysis(a, b, PredAbstractors.booleanAbstractor(b),
getXcfaAbstractor(PredXcfaAnalysis(a, b, PredAbstractors.booleanAbstractor(b),
i as PartialOrd<XcfaState<PredState>>), d, e, f, g, h)
},
itpPrecRefiner = { a ->
Expand All @@ -128,7 +128,7 @@ enum class Domain(
),
PRED_CART(
abstractor = { a, b, c, d, e, f, g, h, i ->
getXcfaAbstractor(PredXcfaAnalaysis(a, b, PredAbstractors.cartesianAbstractor(b),
getXcfaAbstractor(PredXcfaAnalysis(a, b, PredAbstractors.cartesianAbstractor(b),
i as PartialOrd<XcfaState<PredState>>), d, e, f, g, h)
},
itpPrecRefiner = { a ->
Expand All @@ -141,7 +141,7 @@ enum class Domain(
),
PRED_SPLIT(
abstractor = { a, b, c, d, e, f, g, h, i ->
getXcfaAbstractor(PredXcfaAnalaysis(a, b, PredAbstractors.booleanSplitAbstractor(b),
getXcfaAbstractor(PredXcfaAnalysis(a, b, PredAbstractors.booleanSplitAbstractor(b),
i as PartialOrd<XcfaState<PredState>>), d, e, f, g, h)
},
itpPrecRefiner = { a ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,17 @@ class XcfaProcedureBuilderContext(val builder: XcfaProcedureBuilder) {
}
}

infix fun String.to(to: String): (lambda: SequenceLabelContext.() -> SequenceLabel) -> Unit {
infix fun String.to(to: String): (lambda: SequenceLabelContext.() -> SequenceLabel) -> XcfaEdge {
val loc1 = locationLut.getOrElse(this) { XcfaLocation(this) }
locationLut.putIfAbsent(this, loc1)
builder.addLoc(loc1)
val loc2 = locationLut.getOrElse(to) { XcfaLocation(to) }
locationLut.putIfAbsent(to, loc2)
builder.addLoc(loc2)
return { lambda ->
builder.addEdge(XcfaEdge(loc1, loc2, lambda(SequenceLabelContext())))
val edge = XcfaEdge(loc1, loc2, lambda(SequenceLabelContext()))
builder.addEdge(edge)
edge
}
}

Expand Down

0 comments on commit 4963dbb

Please sign in to comment.