You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2020/06/29 14:23:47 UTC
[plc4x] branch develop updated: - Started implementing the C
read_buffer and implementing unit tests for the implemented functions.
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 600d45f - Started implementing the C read_buffer and implementing unit tests for the implemented functions.
600d45f is described below
commit 600d45f313e0ed5fd1ffba0fc99b158fc7c65be6
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Mon Jun 29 16:23:39 2020 +0200
- Started implementing the C read_buffer and implementing unit tests for the implemented functions.
---
sandbox/plc4c/api/include/plc4c/types.h | 3 +
sandbox/plc4c/spi/include/plc4c/spi/read_buffer.h | 19 +-
sandbox/plc4c/spi/src/read_buffer.c | 78 ++++++-
sandbox/plc4c/spi/test/read_buffer_test.c | 253 ++++++++++++++++++++++
sandbox/plc4c/spi/test/spi_test.c | 35 +++
sandbox/plc4c/spi/test/system_test.c | 14 +-
6 files changed, 376 insertions(+), 26 deletions(-)
diff --git a/sandbox/plc4c/api/include/plc4c/types.h b/sandbox/plc4c/api/include/plc4c/types.h
index 9a33247..7a108ab 100644
--- a/sandbox/plc4c/api/include/plc4c/types.h
+++ b/sandbox/plc4c/api/include/plc4c/types.h
@@ -35,6 +35,9 @@ typedef enum plc4c_return_code {
INVALID_CONNECTION_STRING,
NON_MATCHING_LISTS,
INVALID_LIST_SIZE,
+ NULL_VALUE,
+ OUT_OF_RANGE,
+ INVALID_ARGUMENT,
NOT_REACHABLE,
PERMISSION_DENIED,
diff --git a/sandbox/plc4c/spi/include/plc4c/spi/read_buffer.h b/sandbox/plc4c/spi/include/plc4c/spi/read_buffer.h
index 591b130..85c4133 100644
--- a/sandbox/plc4c/spi/include/plc4c/spi/read_buffer.h
+++ b/sandbox/plc4c/spi/include/plc4c/spi/read_buffer.h
@@ -24,19 +24,30 @@
#include <plc4c/types.h>
struct plc4c_spi_read_buffer {
-
+ // Pointer to the data itself
+ uint8_t* data;
+ // Total size of the data array.
+ uint16_t length;
+ // Current full byte position
+ uint16_t curPosByte;
+ // Current bit-position inside the current byte
+ unsigned int curPosBit : 4;
};
typedef struct plc4c_spi_read_buffer plc4c_spi_read_buffer;
+plc4c_return_code plc4c_spi_read_buffer_create(uint8_t* data, uint16_t length, plc4c_spi_read_buffer** buffer);
+
+void plc4c_spi_read_buffer_destroy(plc4c_spi_read_buffer* buffer);
+
uint32_t plc4c_spi_read_get_pos(plc4c_spi_read_buffer* buf);
uint32_t plc4c_spi_read_get_total_bytes(plc4c_spi_read_buffer* buf);
-bool plc4c_spi_read_has_more(plc4c_spi_read_buffer* buf, uint8_t num_bits);
+bool plc4c_spi_read_has_more(plc4c_spi_read_buffer* buf, uint16_t num_bits);
-uint8_t* plc4c_spi_read_get_bytes(plc4c_spi_read_buffer* buf, uint32_t start_pos_in_bytes, uint32_t end_pos_in_bytes);
+plc4c_return_code plc4c_spi_read_get_bytes(plc4c_spi_read_buffer* buf, uint16_t start_pos_in_bytes, uint16_t end_pos_in_bytes, uint8_t** dest);
-uint8_t plc4c_spi_read_peek_byte(plc4c_spi_read_buffer* buf, uint32_t offset_in_bytes);
+uint8_t plc4c_spi_read_peek_byte(plc4c_spi_read_buffer* buf, uint16_t offset_in_bytes);
bool plc4c_spi_read_bit(plc4c_spi_read_buffer* buf);
diff --git a/sandbox/plc4c/spi/src/read_buffer.c b/sandbox/plc4c/spi/src/read_buffer.c
index f9aa6d2..09732b6 100644
--- a/sandbox/plc4c/spi/src/read_buffer.c
+++ b/sandbox/plc4c/spi/src/read_buffer.c
@@ -18,29 +18,89 @@
*/
#include <plc4c/spi/read_buffer.h>
+#include <string.h>
+
+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));
+ if(*buffer == NULL) {
+ return NO_MEMORY;
+ }
+
+ (*buffer)->data = data;
+ (*buffer)->length = length;
+ (*buffer)->curPosByte = 0;
+ (*buffer)->curPosBit = 0;
+
+ return OK;
+}
+
+void plc4c_spi_read_buffer_destroy(plc4c_spi_read_buffer* buffer) {
+ free(buffer);
+}
uint32_t plc4c_spi_read_get_pos(plc4c_spi_read_buffer* buf) {
- return 0;
+ return buf->curPosByte;
}
uint32_t plc4c_spi_read_get_total_bytes(plc4c_spi_read_buffer* buf) {
- return 0;
+ return buf->length;
}
-bool plc4c_spi_read_has_more(plc4c_spi_read_buffer* buf, uint8_t num_bits) {
- return false;
+bool plc4c_spi_read_has_more(plc4c_spi_read_buffer* buf, uint16_t num_bits) {
+ return (((buf->length - buf->curPosByte) * 8) - buf->curPosBit) >= num_bits;
}
-uint8_t* plc4c_spi_read_get_bytes(plc4c_spi_read_buffer* buf, uint32_t start_pos_in_bytes, uint32_t end_pos_in_bytes) {
- return NULL;
+plc4c_return_code plc4c_spi_read_get_bytes(plc4c_spi_read_buffer* buf, uint16_t start_pos_in_bytes, uint16_t end_pos_in_bytes, uint8_t** dest) {
+ if(buf == NULL) {
+ return NULL_VALUE;
+ }
+ if(dest == NULL) {
+ return NULL_VALUE;
+ }
+ // Check if the arguments for start and stop position are correct.
+ if(end_pos_in_bytes < start_pos_in_bytes) {
+ return INVALID_ARGUMENT;
+ }
+ if(end_pos_in_bytes > buf->length) {
+ return OUT_OF_RANGE;
+ }
+ uint16_t num_bytes = end_pos_in_bytes - start_pos_in_bytes;
+
+ *dest = malloc(sizeof(uint8_t) * num_bytes);
+ if(*dest == NULL) {
+ return NO_MEMORY;
+ }
+
+ // Copy the requested bytes to the output.
+ memcpy(*dest, buf->data, num_bytes);
+ return OK;
}
-uint8_t plc4c_spi_read_peek_byte(plc4c_spi_read_buffer* buf, uint32_t offset_in_bytes) {
- return 0;
+uint8_t plc4c_spi_read_peek_byte(plc4c_spi_read_buffer* buf, uint16_t offset_in_bytes) {
+ if(buf == NULL) {
+ return 0;
+ }
+ if(buf->curPosByte + offset_in_bytes > buf->length) {
+ return 0;
+ }
+ return (*buf->data) + (buf->curPosByte + offset_in_bytes);
}
bool plc4c_spi_read_bit(plc4c_spi_read_buffer* buf) {
- return false;
+ uint8_t cur_byte = (*buf->data) + buf->curPosByte;
+ // We have to invert the position as bit 0 will be the first
+ // (most significant bit).
+ unsigned int bit_pos = ((unsigned int) 7) - buf->curPosBit;
+ // Get the bit's value.
+ bool value = ((cur_byte >> bit_pos) & 1) != 0;
+ // If this was the last bit in this byte, move on to the next one.
+ if(buf->curPosBit == 7) {
+ buf->curPosByte++;
+ buf->curPosBit = 0;
+ } else {
+ buf->curPosBit++;
+ }
+ return value;
}
// Unsigned Integers ...
diff --git a/sandbox/plc4c/spi/test/read_buffer_test.c b/sandbox/plc4c/spi/test/read_buffer_test.c
new file mode 100644
index 0000000..a991f08
--- /dev/null
+++ b/sandbox/plc4c/spi/test/read_buffer_test.c
@@ -0,0 +1,253 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <stdlib.h>
+#include <unity.h>
+#include "plc4c/spi/read_buffer.h"
+
+void test_plc4c_spi_read_buffer_create_args(
+ uint8_t *data, uint16_t length, plc4c_return_code expected_return_code) {
+
+ printf("Running read_buffer create test with %d, expecting return code %d", length, expected_return_code);
+
+ // Create a new read_buffer instance
+ plc4c_spi_read_buffer* read_buffer;
+
+ plc4c_return_code return_code = plc4c_spi_read_buffer_create(data, length, &read_buffer);
+
+ TEST_ASSERT_EQUAL_INT(expected_return_code, return_code);
+ if (expected_return_code != OK) {
+ TEST_ASSERT_NULL(read_buffer);
+ } else {
+ TEST_ASSERT_NOT_NULL(read_buffer);
+ free(read_buffer);
+ }
+ printf(" -> OK\n");
+}
+
+void test_plc4c_spi_read_buffer_create(void) {
+ // Prepare input data
+ uint8_t data[] = {1,2,3,4,5,6,7,8};
+
+ // Run test
+ test_plc4c_spi_read_buffer_create_args(&data[0], 8, OK);
+}
+
+
+
+
+
+void test_plc4c_spi_read_buffer_get_total_bytes_args(
+ plc4c_spi_read_buffer* read_buffer, uint16_t expected_length) {
+
+ printf("Running read_buffer get_total_bytes test. Expecting %d length", expected_length);
+
+ uint32_t length = plc4c_spi_read_get_total_bytes(read_buffer);
+
+ TEST_ASSERT_EQUAL_INT(expected_length, length);
+ printf(" -> OK\n");
+}
+
+void test_plc4c_spi_read_buffer_get_total_bytes(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
+ test_plc4c_spi_read_buffer_get_total_bytes_args(read_buffer, 8);
+}
+
+
+
+
+void test_plc4c_spi_read_buffer_has_more_args(
+ plc4c_spi_read_buffer* read_buffer, uint16_t num_bytes, bool expected_result) {
+
+ printf("Running read_buffer has_more test. Checking if %d bytes are available", num_bytes);
+
+ bool result = plc4c_spi_read_has_more(read_buffer, num_bytes);
+
+ TEST_ASSERT_EQUAL_INT(expected_result, result);
+ printf(" -> OK\n");
+}
+
+void test_plc4c_spi_read_buffer_has_more(void) {
+ // Prepare input data
+ uint8_t data[] = {1,2,3,4};
+ plc4c_spi_read_buffer* read_buffer;
+ plc4c_spi_read_buffer_create(data, 4, &read_buffer);
+
+ // Run test
+ test_plc4c_spi_read_buffer_has_more_args(read_buffer, 0, true);
+ test_plc4c_spi_read_buffer_has_more_args(read_buffer, 1, true);
+ test_plc4c_spi_read_buffer_has_more_args(read_buffer, 4, true);
+ test_plc4c_spi_read_buffer_has_more_args(read_buffer, 14, true);
+ test_plc4c_spi_read_buffer_has_more_args(read_buffer, 31, true);
+ test_plc4c_spi_read_buffer_has_more_args(read_buffer, 32, true);
+
+ // 4 bytes are 32 bits, so these should fail.
+ test_plc4c_spi_read_buffer_has_more_args(read_buffer, 33, false);
+ test_plc4c_spi_read_buffer_has_more_args(read_buffer, 50, false);
+}
+
+
+
+
+void test_plc4c_spi_read_buffer_get_bytes_args(
+ plc4c_spi_read_buffer* read_buffer, uint16_t start_byte, uint16_t end_byte, plc4c_return_code expected_return_code, uint8_t* expected_bytes, uint8_t expected_bytes_length) {
+
+ printf("Running read_buffer get_bytes test. Checking if reading from %d to %d bytes gives the correct response", start_byte, end_byte);
+
+ uint8_t* read_bytes = NULL;
+ plc4c_return_code result = plc4c_spi_read_get_bytes(read_buffer, start_byte, end_byte, &read_bytes);
+
+ TEST_ASSERT_EQUAL_INT(expected_return_code, result);
+ if(expected_return_code != OK) {
+ TEST_ASSERT_NULL(read_bytes);
+ } else {
+ TEST_ASSERT_NOT_NULL(read_bytes);
+ for(int i = 0; i < expected_bytes_length; i++) {
+ uint8_t cur_byte = *read_buffer->data + start_byte + i;
+ uint8_t expected_byte = *expected_bytes + i;
+ TEST_ASSERT_EQUAL_INT(expected_byte, cur_byte);
+ }
+ }
+
+ printf(" -> OK\n");
+}
+
+void test_plc4c_spi_read_buffer_get_bytes(void) {
+ // Prepare input data
+ uint8_t data[] = {1,2,3,4,5,6,7};
+ plc4c_spi_read_buffer* read_buffer;
+ plc4c_spi_read_buffer_create(data, 8, &read_buffer);
+
+ // Run test
+ uint8_t result_data1[] = {1};
+ test_plc4c_spi_read_buffer_get_bytes_args(read_buffer, 0, 0, OK, result_data1, 1);
+ uint8_t result_data2[] = {2};
+ test_plc4c_spi_read_buffer_get_bytes_args(read_buffer, 1, 1, OK, result_data2, 1);
+ uint8_t result_data3[] = {2,3};
+ test_plc4c_spi_read_buffer_get_bytes_args(read_buffer, 1, 2, OK, result_data3, 2);
+ uint8_t result_data4[] = {4,5,6};
+ test_plc4c_spi_read_buffer_get_bytes_args(read_buffer, 3, 5, OK, result_data4, 3);
+ uint8_t result_data5[] = {4,5,6,7};
+ test_plc4c_spi_read_buffer_get_bytes_args(read_buffer, 3, 6, OK, result_data5, 4);
+
+ // These should fail for various reasons ...
+ uint8_t result_data6[] = {};
+ test_plc4c_spi_read_buffer_get_bytes_args(read_buffer, 0, 10, OUT_OF_RANGE, result_data6, 0);
+ test_plc4c_spi_read_buffer_get_bytes_args(read_buffer, 6, 3, INVALID_ARGUMENT, result_data6, 0);
+ test_plc4c_spi_read_buffer_get_bytes_args(NULL, 0, 0, NULL_VALUE, result_data6, 0);
+}
+
+
+
+
+void test_plc4c_spi_read_buffer_peek_byte_args(
+ plc4c_spi_read_buffer* read_buffer, uint16_t peek_byte, uint8_t expected_value) {
+
+ printf("Running read_buffer peek_byte test. Checking if peeking byte number %d gives the correct response", peek_byte);
+
+ uint8_t peeked_byte = plc4c_spi_read_peek_byte(read_buffer, peek_byte);
+
+ TEST_ASSERT_EQUAL_INT(expected_value, peeked_byte);
+
+ printf(" -> OK\n");
+}
+
+void test_plc4c_spi_read_buffer_peek_byte(void) {
+ // Prepare input data
+ uint8_t data[] = {1,2,3,4,5,6,7};
+ plc4c_spi_read_buffer* read_buffer;
+ plc4c_spi_read_buffer_create(data, 8, &read_buffer);
+
+ // Run test
+ test_plc4c_spi_read_buffer_peek_byte_args(read_buffer, 0, 1);
+ test_plc4c_spi_read_buffer_peek_byte_args(read_buffer, 4, 5);
+ test_plc4c_spi_read_buffer_peek_byte_args(read_buffer, 4, 5);
+ test_plc4c_spi_read_buffer_peek_byte_args(read_buffer, 6, 7);
+ // Bump the cur buffer position.
+ read_buffer->curPosByte = 2;
+ test_plc4c_spi_read_buffer_peek_byte_args(read_buffer, 2, 5);
+
+ // These should fail
+ test_plc4c_spi_read_buffer_peek_byte_args(read_buffer, 8, 0);
+}
+
+
+
+
+void test_plc4c_spi_read_buffer_read_bit_args(
+ plc4c_spi_read_buffer* read_buffer, bool expected_value) {
+
+ printf("Running read_buffer peek_byte test. Checking if reading a bit gives the correct response");
+
+ bool value = plc4c_spi_read_bit(read_buffer);
+
+ TEST_ASSERT_EQUAL_INT(expected_value, value);
+
+ printf(" -> OK\n");
+}
+
+void test_plc4c_spi_read_buffer_read_bit(void) {
+ // Prepare input data
+ uint8_t data[] = {1,2,3,4,5,6,7};
+ plc4c_spi_read_buffer* read_buffer;
+ plc4c_spi_read_buffer_create(data, 8, &read_buffer);
+
+ // Run test
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, true);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, true);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, false);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, true);
+ test_plc4c_spi_read_buffer_read_bit_args(read_buffer, true);
+}
+
+
+
+
+
+void test_plc4c_spi_read_buffer(void) {
+ test_plc4c_spi_read_buffer_create();
+ test_plc4c_spi_read_buffer_get_total_bytes();
+ test_plc4c_spi_read_buffer_has_more();
+ test_plc4c_spi_read_buffer_get_bytes();
+ test_plc4c_spi_read_buffer_peek_byte();
+ test_plc4c_spi_read_buffer_read_bit();
+}
\ No newline at end of file
diff --git a/sandbox/plc4c/spi/test/spi_test.c b/sandbox/plc4c/spi/test/spi_test.c
new file mode 100644
index 0000000..1e80b6f
--- /dev/null
+++ b/sandbox/plc4c/spi/test/spi_test.c
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <unity.h>
+
+void test_plc4c_spi_read_buffer(void);
+void test_system_plc4c_system_create_connection(void);
+
+void setUp(void) {}
+
+void tearDown(void) {}
+
+int main(void) {
+ UNITY_BEGIN();
+
+ RUN_TEST(test_plc4c_spi_read_buffer);
+ RUN_TEST(test_system_plc4c_system_create_connection);
+
+ return UNITY_END();
+}
\ No newline at end of file
diff --git a/sandbox/plc4c/spi/test/system_test.c b/sandbox/plc4c/spi/test/system_test.c
index 0ee760a..b4bf2e3 100644
--- a/sandbox/plc4c/spi/test/system_test.c
+++ b/sandbox/plc4c/spi/test/system_test.c
@@ -22,10 +22,6 @@
#include "plc4c/spi/system_private.h"
-void setUp(void) {}
-
-void tearDown(void) {}
-
void test_system_plc4c_system_create_connection_args(
char *connection_string, plc4c_return_code expected_return_code,
char *expected_connection_string, char *expected_protocol_code,
@@ -51,7 +47,7 @@ void test_system_plc4c_system_create_connection_args(
TEST_ASSERT_EQUAL_STRING(expected_parameters, connection->parameters);
free(connection);
}
- printf("OK\n");
+ printf(" -> OK\n");
}
void test_system_plc4c_system_create_connection(void) {
@@ -87,11 +83,3 @@ void test_system_plc4c_system_create_connection(void) {
test_system_plc4c_system_create_connection_args(
"a://a?b?c", INVALID_CONNECTION_STRING, NULL, NULL, NULL, NULL, NULL);
}
-
-int main(void) {
- UNITY_BEGIN();
-
- RUN_TEST(test_system_plc4c_system_create_connection);
-
- return UNITY_END();
-}
\ No newline at end of file