Skip to content

Commit

Permalink
Added unit tests for Set operation
Browse files Browse the repository at this point in the history
  • Loading branch information
paulorb committed Dec 9, 2024
1 parent a20c9f8 commit bf053c5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/test/kotlin/SetOperationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,26 @@ class SetOperationTest {
assertEquals(plcMemory.readHoldingRegister(configuration.getConfiguredDevice().configuration.registers.register[1].address.toInt(), 1).first(), 600)
}

@Test
fun `Set must set a FLOAT32 value to a holding register variable`() {
val configuration = ConfigurationParser()
configuration.setReadFromResources(true)
configuration.setFileName("set_operation.xml")
val plcMemory = PlcMemory(configuration)
val setOperation = SetOperation(configuration.getConfiguredDevice().configuration, plcMemory, EnvironmentVariables(listOf<EnvParameter>(),ConfigurationParser() ))
assertTrue(configuration.getConfiguredDevice().simulation.randomElements[2] is Set)
var value1 = plcMemory.readHoldingRegister(configuration.getConfiguredDevice().configuration.registers.register[2].address.toInt(), 2).first()
var value2 = plcMemory.readHoldingRegister(configuration.getConfiguredDevice().configuration.registers.register[2].address.toInt(), 2)[1]
var intValue = (( value2.toInt() shl 16) or (value1.toInt() and 0xFFFF))
var currentFloatValue = java.lang.Float.intBitsToFloat(intValue)
assertEquals(currentFloatValue, 500.0.toFloat())
setOperation.setOperation(configuration.getConfiguredDevice().simulation.randomElements[2] as Set)
value1 = plcMemory.readHoldingRegister(configuration.getConfiguredDevice().configuration.registers.register[2].address.toInt(), 2).first()
value2 = plcMemory.readHoldingRegister(configuration.getConfiguredDevice().configuration.registers.register[2].address.toInt(), 2)[1]
intValue = (( value2.toInt() shl 16) or (value1.toInt() and 0xFFFF))
currentFloatValue = java.lang.Float.intBitsToFloat(intValue)
assertEquals(currentFloatValue, 600.0.toFloat())
}


}
2 changes: 2 additions & 0 deletions src/test/resources/set_operation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
<registers>
<register addressType="COIL" address="0" symbol="RELAYON">0</register>
<register addressType="HOLDING_REGISTER" address="14" symbol="RPM_MOTOR">500</register>
<register addressType="HOLDING_REGISTER" address="20" datatype="FLOAT32" symbol="RPM_MOTOR_2">500.0</register>
</registers>
</configuration>
<simulation plcScanTime="1000">
<set symbol="RELAYON">1</set>
<set symbol="RPM_MOTOR">600</set>
<set symbol="RPM_MOTOR_2">600.0</set>
</simulation>
</device>

0 comments on commit bf053c5

Please sign in to comment.