This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 5f9c762  - Started adding the tests for the signed datatypes - Fixed 
an issue when returning signed negative values that don't use all the bits a 
datatype has to provide (values switched to positive ones in this case)
5f9c762 is described below

commit 5f9c762c6e437068eb5ccd6c9d8184e5b46820de
Author: Christofer Dutz <[email protected]>
AuthorDate: Tue Jul 7 22:28:32 2020 +0200

    - Started adding the tests for the signed datatypes
    - Fixed an issue when returning signed negative values that don't use all 
the bits a datatype has to provide (values switched to positive ones in this 
case)
---
 sandbox/plc4c/spi/src/read_buffer.c       | 24 ++++++++++---
 sandbox/plc4c/spi/test/read_buffer_test.c | 60 +++++++++++++++++++++++++++++--
 2 files changed, 78 insertions(+), 6 deletions(-)

diff --git a/sandbox/plc4c/spi/src/read_buffer.c 
b/sandbox/plc4c/spi/src/read_buffer.c
index d7c4b4b..3be5fd8 100644
--- a/sandbox/plc4c/spi/src/read_buffer.c
+++ b/sandbox/plc4c/spi/src/read_buffer.c
@@ -205,6 +205,20 @@ plc4c_return_code plc4c_spi_read_unsigned_bits_internal(
   }
 }
 
+bool plc4c_spi_read_buffer_is_negative_internal(uint8_t num_bits, int8_t 
value) {
+  int8_t tmp_value = value >> (num_bits - 1);
+  return (tmp_value & 1) != 0;
+}
+
+plc4c_return_code plc4c_spi_fill_sign_internal(uint8_t num_bits, int8_t* 
value) {
+  if(plc4c_spi_read_buffer_is_negative_internal(num_bits, *value)) {
+    // Set all bits above {num_bits} to 1
+    int8_t tmp_value = *value;
+    tmp_value = tmp_value | (255 & bit_matrix[num_bits][0]);
+    *value = tmp_value;
+  }
+}
+
 plc4c_return_code plc4c_spi_read_buffer_create(uint8_t* data, uint16_t length,
                                                plc4c_spi_read_buffer** buffer) 
{
   *buffer = malloc(sizeof(plc4c_spi_read_buffer));
@@ -390,23 +404,25 @@ uint8_t num_bits) { return OK;
 
 plc4c_return_code plc4c_spi_read_signed_byte(plc4c_spi_read_buffer* buf,
                                              uint8_t num_bits, int8_t* value) {
-  return plc4c_spi_read_unsigned_byte(buf, num_bits, (uint8_t*)value);
+  plc4c_return_code res = plc4c_spi_read_unsigned_byte(buf, num_bits, 
(uint8_t*) value);
+  plc4c_spi_fill_sign_internal(num_bits, value);
+  return res;
 }
 
 plc4c_return_code plc4c_spi_read_signed_short(plc4c_spi_read_buffer* buf,
                                               uint8_t num_bits,
                                               int16_t* value) {
-  return plc4c_spi_read_unsigned_byte(buf, num_bits, (uint16_t*)value);
+  return plc4c_spi_read_unsigned_byte(buf, num_bits, (uint16_t*) value);
 }
 
 plc4c_return_code plc4c_spi_read_signed_int(plc4c_spi_read_buffer* buf,
                                             uint8_t num_bits, int32_t* value) {
-  return plc4c_spi_read_unsigned_byte(buf, num_bits, (uint32_t*)value);
+  return plc4c_spi_read_unsigned_byte(buf, num_bits, (uint32_t*) value);
 }
 
 plc4c_return_code plc4c_spi_read_signed_long(plc4c_spi_read_buffer* buf,
                                              uint8_t num_bits, int64_t* value) 
{
-  return plc4c_spi_read_unsigned_byte(buf, num_bits, (uint64_t*)value);
+  return plc4c_spi_read_unsigned_byte(buf, num_bits, (uint64_t*) value);
 }
 
 // TODO: Not sure which type to use in this case ...
diff --git a/sandbox/plc4c/spi/test/read_buffer_test.c 
b/sandbox/plc4c/spi/test/read_buffer_test.c
index 3b39551..4d5ed64 100644
--- a/sandbox/plc4c/spi/test/read_buffer_test.c
+++ b/sandbox/plc4c/spi/test/read_buffer_test.c
@@ -354,7 +354,7 @@ void test_plc4c_spi_read_unsigned_short(void) {
 void test_plc4c_spi_read_unsigned_int_args(char* message,
     plc4c_spi_read_buffer* read_buffer, uint8_t num_bits,
     plc4c_return_code expected_return_code, uint32_t expected_value) {
-  printf("Running read_buffer read_unsigned_int test.");
+  printf("Running read_buffer read_unsigned_int test: %s", message);
 
   uint32_t value = 0;
   plc4c_return_code result =
@@ -409,7 +409,7 @@ void test_plc4c_spi_read_unsigned_int(void) {
 void test_plc4c_spi_read_unsigned_long_args(char* message,
     plc4c_spi_read_buffer* read_buffer, uint8_t num_bits,
     plc4c_return_code expected_return_code, uint64_t expected_value) {
-  printf("Running read_buffer read_unsigned_long test.");
+  printf("Running read_buffer read_unsigned_long test: %s", message);
 
   uint64_t value = 0;
   plc4c_return_code result =
@@ -441,6 +441,61 @@ void test_plc4c_spi_read_unsigned_long(void) {
   test_plc4c_spi_read_unsigned_long_args("Full long starting at bit 3", 
read_buffer, 61, OK, 72623859790382856);
 }
 
+
+void test_plc4c_spi_read_signed_byte_args(char* message,
+                                            plc4c_spi_read_buffer* 
read_buffer, uint8_t num_bits,
+                                            plc4c_return_code 
expected_return_code, int8_t expected_value) {
+  printf("Running read_buffer read_signed_byte test: %s", message);
+
+  int8_t value = -1;
+  plc4c_return_code result =
+      plc4c_spi_read_signed_byte(read_buffer, num_bits, &value);
+
+  TEST_ASSERT_EQUAL_INT(expected_return_code, result);
+  TEST_ASSERT_EQUAL_INT(expected_value, value);
+
+  printf(" -> OK\n");
+}
+
+void test_plc4c_spi_read_signed_byte(void) {
+  // Prepare input data
+  uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8};
+  plc4c_spi_read_buffer* read_buffer;
+  plc4c_spi_read_buffer_create(data, 8, &read_buffer);
+  // Run test
+  // Read all the full bytes
+  test_plc4c_spi_read_signed_byte_args("Simple full signed byte 1", 
read_buffer, 8, OK, 1);
+  test_plc4c_spi_read_signed_byte_args("Simple full signed byte 2", 
read_buffer, 8, OK, 2);
+  test_plc4c_spi_read_signed_byte_args("Simple full signed byte 3", 
read_buffer, 8, OK, 3);
+  test_plc4c_spi_read_signed_byte_args("Simple full signed byte 4", 
read_buffer, 8, OK, 4);
+  test_plc4c_spi_read_signed_byte_args("Simple full signed byte 5", 
read_buffer, 8, OK, 5);
+  test_plc4c_spi_read_signed_byte_args("Simple full signed byte 6", 
read_buffer, 8, OK, 6);
+  test_plc4c_spi_read_signed_byte_args("Simple full signed byte 7", 
read_buffer, 8, OK, 7);
+  // Read a 9th byte (buffer only has 8) (results in error)
+//  test_plc4c_spi_read_signed_byte_args("Exceed read-buffer size", 
read_buffer, 8, OUT_OF_RANGE, -1);
+  plc4c_spi_read_buffer_destroy(read_buffer);
+
+  uint8_t data2[] = {186, 117};
+  plc4c_spi_read_buffer* read_buffer2;
+  plc4c_spi_read_buffer_create(data2, 2, &read_buffer2);
+  // Read part of a byte (fits in one byte)
+  test_plc4c_spi_read_signed_byte_args("Simple 4 bits of signed byte", 
read_buffer2, 4, OK, -5);
+  // Read part of a byte (finishes one byte)
+  test_plc4c_spi_read_signed_byte_args("Simple 4 bits of signed byte, 
finishing rest of first byte", read_buffer2, 4, OK, -6);
+  test_plc4c_spi_read_signed_byte_args("Simple 4 bits of signed byte", 
read_buffer2, 4, OK, 7);
+  test_plc4c_spi_read_signed_byte_args("Simple 4 bits of signed byte", 
read_buffer2, 4, OK, 5);
+  // Read part of a byte (spans two bytes)
+  read_buffer2->curPosByte = 0;
+  read_buffer2->curPosBit = 5;
+  test_plc4c_spi_read_signed_byte_args("Simple 6 bits of signed byte starting 
at bit 5 (flowing over to next byte)", read_buffer2, 6, OK, 19);
+  test_plc4c_spi_read_signed_byte_args("Simple 4 bits of signed byte starting 
at bit 3", read_buffer2, 4, OK, -6);
+  // Read more than a byte (results in error)
+  read_buffer2->curPosByte = 0;
+  read_buffer2->curPosBit = 5;
+  test_plc4c_spi_read_signed_byte_args("Exceed read-buffer size (Part 2)", 
read_buffer2, 10, OUT_OF_RANGE, -1);
+}
+
+
 void test_plc4c_spi_read_buffer(void) {
   test_plc4c_spi_read_buffer_create();
   test_plc4c_spi_read_get_total_bytes();
@@ -452,4 +507,5 @@ void test_plc4c_spi_read_buffer(void) {
   test_plc4c_spi_read_unsigned_short();
   test_plc4c_spi_read_unsigned_int();
   test_plc4c_spi_read_unsigned_long();
+  test_plc4c_spi_read_signed_byte();
 }
\ No newline at end of file

Reply via email to