You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by hu...@apache.org on 2023/05/12 03:58:12 UTC
[plc4x] 01/02: feat(plc4py): Add more tests for the write buffer
This is an automated email from the ASF dual-hosted git repository.
hutcheb pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit 9c8431c2fd46f4f35271252d1d5eed139c7a0dba
Author: Ben Hutcheson <be...@gmail.com>
AuthorDate: Fri May 12 05:24:48 2023 +0200
feat(plc4py): Add more tests for the write buffer
---
.../plc4py/plc4py/spi/generation/WriteBuffer.py | 7 ++-
.../tests/unit/plc4py/spi/test_write_buffer.py | 56 ++++++++++++++++++++--
2 files changed, 55 insertions(+), 8 deletions(-)
diff --git a/sandbox/plc4py/plc4py/spi/generation/WriteBuffer.py b/sandbox/plc4py/plc4py/spi/generation/WriteBuffer.py
index 78d094b47e..4a7ee42144 100644
--- a/sandbox/plc4py/plc4py/spi/generation/WriteBuffer.py
+++ b/sandbox/plc4py/plc4py/spi/generation/WriteBuffer.py
@@ -153,12 +153,11 @@ class WriteBufferByteBased(WriteBuffer):
raise SerializationException("'ASCII' encoded fields must have a length that is a multiple of 8 bits long")
char_len: int = int(bit_length / 8)
max_value: int = int(10**char_len - 1)
- if value > max_value:
+ if value.value > max_value:
raise SerializationException("Provided value of " + str(value) + " exceeds the max value of " + str(max_value))
- string_value: str = "{}".format(value)
-
+ string_value: str = "{}".format(value.value)
src = bitarray(endian=ByteOrder.get_short_name(self.byte_order))
- src.frombytes(string_value)
+ src.frombytes(bytearray(string_value, value_encoding))
self.bb[self.position:bit_length] = src[:bit_length]
self.position += bit_length
elif value_encoding == "default":
diff --git a/sandbox/plc4py/tests/unit/plc4py/spi/test_write_buffer.py b/sandbox/plc4py/tests/unit/plc4py/spi/test_write_buffer.py
index 68f357913c..c0acae12ee 100644
--- a/sandbox/plc4py/tests/unit/plc4py/spi/test_write_buffer.py
+++ b/sandbox/plc4py/tests/unit/plc4py/spi/test_write_buffer.py
@@ -104,27 +104,75 @@ def test_write_buffer_set_unsigned_byte_big_endian(mocker) -> None:
assert (ba.obj == bitarray("00010010"))
-def test_write_buffer_set_unsigned_byte_little_endian(mocker) -> None:
+def test_write_buffer_set_unsigned_byte_little_endian_niblet(mocker) -> None:
wb: WriteBufferByteBased = WriteBufferByteBased(1, ByteOrder.LITTLE_ENDIAN)
wb.write_unsigned_byte(c_byte(0x12), 4, "Test String 1")
ba: memoryview = wb.get_bytes()
assert (ba.obj == bitarray("01000000"))
-def test_write_buffer_set_unsigned_byte_big_endian(mocker) -> None:
+def test_write_buffer_set_unsigned_byte_big_endian_niblet(mocker) -> None:
wb: WriteBufferByteBased = WriteBufferByteBased(1, ByteOrder.BIG_ENDIAN)
wb.write_unsigned_byte(c_byte(0x12), 4, "Test String 1")
ba: memoryview = wb.get_bytes()
assert (ba.obj == bitarray("00010000"))
-def test_write_buffer_write_unsigned_short_big_endian(mocker) -> None:
+
+def test_write_buffer_write_unsigned_short_little_endian(mocker) -> None:
wb: WriteBufferByteBased = WriteBufferByteBased(2, ByteOrder.LITTLE_ENDIAN)
wb.write_unsigned_short(c_uint16(0x12), 16, "Test String 1")
ba: memoryview = wb.get_bytes()
- assert (ba.obj == bitarray("00010010 00000000", endian="little"))
+ assert (ba.obj == bitarray("01001000 00000000", endian="little"))
+
def test_write_buffer_write_unsigned_short_big_endian(mocker) -> None:
wb: WriteBufferByteBased = WriteBufferByteBased(2, ByteOrder.BIG_ENDIAN)
wb.write_unsigned_short(c_uint16(0x12), 16, "Test String 1")
ba: memoryview = wb.get_bytes()
assert (ba.obj == bitarray("00010010 00000000", endian="big"))
+
+
+def test_write_buffer_write_unsigned_short_little_endian_dual(mocker) -> None:
+ wb: WriteBufferByteBased = WriteBufferByteBased(2, ByteOrder.LITTLE_ENDIAN)
+ wb.write_unsigned_short(c_uint16(0x12), 16)
+ wb.write_unsigned_short(c_uint16(0x34), 16)
+ ba: memoryview = wb.get_bytes()
+ assert (ba.obj == bitarray("01001000 00000000 00101100 00000000", endian="little"))
+
+
+def test_write_buffer_write_unsigned_short_big_endian_dual(mocker) -> None:
+ wb: WriteBufferByteBased = WriteBufferByteBased(2, ByteOrder.BIG_ENDIAN)
+ wb.write_unsigned_short(c_uint16(0x12), 16, "Test String 1")
+ wb.write_unsigned_short(c_uint16(0x34), 16, "Test String 1")
+ ba: memoryview = wb.get_bytes()
+ assert (ba.obj == bitarray("00010010 00000000 00110100 00000000", endian="big"))
+
+
+def test_write_buffer_write_unsigned_short_big_endian_full(mocker) -> None:
+ wb: WriteBufferByteBased = WriteBufferByteBased(2, ByteOrder.BIG_ENDIAN)
+ wb.write_unsigned_short(c_uint16(-1), 16, "Test String 1")
+ ba: memoryview = wb.get_bytes()
+ assert (ba.obj == bitarray("11111111 11111111", endian="bit"))
+
+
+def test_write_buffer_write_unsigned_short_bit_big_endian_full(mocker) -> None:
+ wb: WriteBufferByteBased = WriteBufferByteBased(2, ByteOrder.BIG_ENDIAN)
+ wb.write_bit(c_bool(True), "Test String 1")
+ wb.write_bit(c_bool(False), "Test String 1")
+ wb.write_unsigned_short(c_uint16(-1), 16, "Test String 1")
+ ba: memoryview = wb.get_bytes()
+ assert (ba.obj == bitarray("10 11111111 11111111", endian="big"))
+
+
+def test_write_buffer_write_unsigned_short_ascii_encoding_little_endian(mocker) -> None:
+ wb: WriteBufferByteBased = WriteBufferByteBased(2, ByteOrder.LITTLE_ENDIAN)
+ wb.write_unsigned_short(c_uint16(1), 16, "ASCII Value of 1 - 0x31", encoding="ASCII")
+ ba: memoryview = wb.get_bytes()
+ assert (ba.obj == bitarray("10001100", endian="little"))
+
+
+def test_write_buffer_write_unsigned_short_ascii_encoding_big_endian(mocker) -> None:
+ wb: WriteBufferByteBased = WriteBufferByteBased(2, ByteOrder.BIG_ENDIAN)
+ wb.write_unsigned_short(c_uint16(1), 16, "ASCII Value of 1 - 0x31", encoding="ASCII")
+ ba: memoryview = wb.get_bytes()
+ assert (ba.obj == bitarray("00110001", endian="big"))