Skip to content

Commit

Permalink
feat(plc4py): Add more tests for the write buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
hutcheb committed May 12, 2023
1 parent e57df53 commit 9c8431c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
7 changes: 3 additions & 4 deletions sandbox/plc4py/plc4py/spi/generation/WriteBuffer.py
Expand Up @@ -153,12 +153,11 @@ def write_unsigned_short(self, value: c_uint16, bit_length: int = 16, logical_na
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":
Expand Down
56 changes: 52 additions & 4 deletions sandbox/plc4py/tests/unit/plc4py/spi/test_write_buffer.py
Expand Up @@ -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"))

0 comments on commit 9c8431c

Please sign in to comment.