visionforge/visionforge-gdml/src/commonTest/kotlin/TestCubes.kt

66 lines
2.2 KiB
Kotlin
Raw Normal View History

2021-03-07 16:19:43 +03:00
package space.kscience.visionforge.gdml
2021-03-01 19:10:15 +03:00
2021-03-07 16:19:43 +03:00
import space.kscience.dataforge.context.Context
import space.kscience.dataforge.names.toName
2021-03-01 19:10:15 +03:00
import space.kscience.gdml.Gdml
import space.kscience.gdml.GdmlBox
import space.kscience.gdml.decodeFromString
import space.kscience.gdml.encodeToString
2021-03-07 16:19:43 +03:00
import space.kscience.visionforge.Vision
import space.kscience.visionforge.gdml.GdmlShowcase.cubes
import space.kscience.visionforge.get
import space.kscience.visionforge.solid.*
import space.kscience.visionforge.visionManager
2021-03-01 19:10:15 +03:00
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
internal val testContext = Context("TEST") {
plugin(Solids)
}
class TestCubes {
@Test
fun testCubesDirect() {
val vision = cubes.toVision()
val smallBoxPrototype = vision.getPrototype("solids.smallBox".toName()) as? Box
assertNotNull(smallBoxPrototype)
assertEquals(30.0, smallBoxPrototype.xSize.toDouble())
val smallBoxVision = vision["composite[1,1,1].smallBox"]?.prototype as? Box
assertNotNull(smallBoxVision)
assertEquals(30.0, smallBoxVision.xSize.toDouble())
}
@Test
fun testGdmlExport() {
val xml = cubes.encodeToString()
//println(xml)
val gdml = Gdml.decodeFromString(xml)
val smallBox = gdml.getSolid<GdmlBox>("smallBox")
assertNotNull(smallBox)
assertEquals(30.0, smallBox.x.toDouble())
}
@Test
fun testCubesReSerialize() {
val vision = cubes.toVision()
val serialized = Solids.encodeToString(vision)
val deserialized = testContext.visionManager.decodeFromString(serialized) as SolidGroup
val smallBox = deserialized.getPrototype("solids.smallBox".toName()) as? Box
assertNotNull(smallBox)
assertEquals(30.0, smallBox.xSize.toDouble())
//println(testContext.visionManager.encodeToString(deserialized))
fun Vision.checkPrototypes() {
if (this is SolidReference) {
assertNotNull(this.prototype)
}
if (this is SolidGroup) {
children.forEach {
it.value.checkPrototypes()
}
}
}
deserialized.checkPrototypes()
}
}