-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ab2663
commit 01f29da
Showing
2 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
subprojects/xcfa/xcfa/src/test/java/hu/bme/mit/theta/xcfa/gson/GsonTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* 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.gson | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.GsonBuilder | ||
import hu.bme.mit.theta.common.dsl.Env | ||
import hu.bme.mit.theta.common.dsl.SymbolTable | ||
import hu.bme.mit.theta.core.decl.VarDecl | ||
import hu.bme.mit.theta.core.stmt.Stmt | ||
import hu.bme.mit.theta.core.type.Expr | ||
import hu.bme.mit.theta.core.type.Type | ||
import hu.bme.mit.theta.core.type.inttype.IntExprs.Int | ||
import hu.bme.mit.theta.core.utils.indexings.BasicVarIndexing | ||
import hu.bme.mit.theta.core.utils.indexings.VarIndexing | ||
import hu.bme.mit.theta.grammar.dsl.expr.ExpressionWrapper | ||
import hu.bme.mit.theta.grammar.dsl.stmt.StatementWrapper | ||
import hu.bme.mit.theta.grammar.dsl.type.TypeWrapper | ||
import hu.bme.mit.theta.grammar.gson.OptionalAdapter | ||
import hu.bme.mit.theta.grammar.gson.PairAdapter | ||
import hu.bme.mit.theta.grammar.gson.StringTypeAdapter | ||
import hu.bme.mit.theta.grammar.gson.VarDeclAdapter | ||
import hu.bme.mit.theta.xcfa.XcfaScope | ||
import hu.bme.mit.theta.xcfa.model.* | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import java.util.* | ||
|
||
class GsonTest { | ||
|
||
private fun getGson(scope: XcfaScope, env: Env, newScope: Boolean): Gson { | ||
val gsonBuilder = GsonBuilder() | ||
lateinit var gson: Gson | ||
gsonBuilder.registerTypeHierarchyAdapter(XcfaLocation::class.java, | ||
StringTypeAdapter(xcfaLocationAdapter)) | ||
gsonBuilder.registerTypeHierarchyAdapter(XCFA::class.java, XcfaAdapter { gson }) | ||
gsonBuilder.registerTypeHierarchyAdapter(VarDecl::class.java, | ||
VarDeclAdapter({ gson }, scope, env, !newScope)) | ||
gsonBuilder.registerTypeHierarchyAdapter(Stmt::class.java, | ||
StringTypeAdapter { StatementWrapper(it, scope).instantiate(env) }) | ||
gsonBuilder.registerTypeHierarchyAdapter(Expr::class.java, | ||
StringTypeAdapter { ExpressionWrapper(scope, it).instantiate(env) }) | ||
gsonBuilder.registerTypeHierarchyAdapter(Type::class.java, | ||
StringTypeAdapter { TypeWrapper(it).instantiate() }) | ||
gsonBuilder.registerTypeHierarchyAdapter(VarIndexing::class.java, | ||
StringTypeAdapter { BasicVarIndexing.fromString(it, scope, env) }) | ||
gsonBuilder.registerTypeHierarchyAdapter(XcfaLabel::class.java, | ||
XcfaLabelAdapter(scope, env, { gson })) | ||
gsonBuilder.registerTypeHierarchyAdapter(MetaData::class.java, MetaDataAdapter()) | ||
gsonBuilder.registerTypeHierarchyAdapter(Pair::class.java, PairAdapter<Any, Any> { gson }) | ||
gsonBuilder.registerTypeHierarchyAdapter(Optional::class.java, OptionalAdapter<Any> { gson }) | ||
gson = gsonBuilder.create() | ||
return gson | ||
} | ||
|
||
@Test | ||
fun testRoundtrip() { | ||
val xcfaSource = xcfa("example") { | ||
global { "x" type Int() init "0" } | ||
procedure("main") { | ||
(init to final) { | ||
"proc1"("x") | ||
} | ||
} | ||
procedure("proc1") { | ||
(init to final) { | ||
assume("true") | ||
} | ||
} | ||
} | ||
|
||
val symbolTable = XcfaScope(SymbolTable()) | ||
val x_symbol = NamedSymbol("x") | ||
symbolTable.add(x_symbol) | ||
val env = Env() | ||
env.define(x_symbol, xcfaSource.vars.find { it.wrappedVar.name == "x" }!!.wrappedVar) | ||
val gson = getGson(symbolTable, env, true) | ||
|
||
val output = gson.fromJson(gson.toJson(xcfaSource), XCFA::class.java) | ||
println(xcfaSource) | ||
println(output) | ||
assertEquals(xcfaSource, output) | ||
} | ||
|
||
} |