|
|
@ -1,6 +1,7 @@ |
|
|
|
#include <stdio.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdint.h> |
|
|
|
#include <string.h> |
|
|
|
|
|
|
|
#include "unity.h" |
|
|
|
#include "circ_buff.h" |
|
|
@ -8,6 +9,25 @@ |
|
|
|
#define BUFFER_SIZE 8 |
|
|
|
#define BUFFER_ELEMENT_SIZE 278 |
|
|
|
|
|
|
|
int compar(const void* entry1, const void* entry2) { |
|
|
|
char **pstr1 = (char**)entry1, **pstr2 = (char**)entry2; |
|
|
|
char *str1 = calloc(strlen(*pstr1)+1, sizeof(char)), |
|
|
|
*str2 = calloc(strlen(*pstr2)+1, sizeof(char)); |
|
|
|
strcpy(str1, *pstr1); |
|
|
|
strcpy(str2, *pstr2); |
|
|
|
|
|
|
|
const char s[2] = "_"; |
|
|
|
strtok(str1, s); |
|
|
|
char* rest = strtok(NULL, ""); |
|
|
|
int num1 = atoi(rest); |
|
|
|
|
|
|
|
strtok(str2, s); |
|
|
|
rest = strtok(NULL, ""); |
|
|
|
int num2 = atoi(rest); |
|
|
|
|
|
|
|
return num1 - num2; |
|
|
|
} |
|
|
|
|
|
|
|
void free_buffer(char** buffer) { |
|
|
|
for(uint8_t i = 0; i < BUFFER_SIZE; ++i) { |
|
|
|
free(buffer[i]); |
|
|
@ -20,7 +40,7 @@ void test_circ_buf_init(void) { |
|
|
|
buffer[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
} |
|
|
|
|
|
|
|
TEST_ASSERT_NOT_NULL(circ_buf_init(buffer, BUFFER_SIZE)); |
|
|
|
TEST_ASSERT_NOT_NULL(circ_buf_init(buffer, BUFFER_SIZE, BUFFER_ELEMENT_SIZE)); |
|
|
|
|
|
|
|
free_buffer(buffer); |
|
|
|
free(buffer); |
|
|
@ -32,7 +52,7 @@ void test_circ_buf_put(void) { |
|
|
|
buffer[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
} |
|
|
|
|
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE); |
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE, BUFFER_ELEMENT_SIZE); |
|
|
|
char* temp = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
|
|
|
|
for(uint8_t i = 0; i < (BUFFER_SIZE - 2); ++i) { |
|
|
@ -70,13 +90,63 @@ void test_circ_buf_put(void) { |
|
|
|
free(buffer); |
|
|
|
} |
|
|
|
|
|
|
|
void test_circ_buf_mul_add(void) { |
|
|
|
char** buffer = (char **) malloc(BUFFER_SIZE * sizeof(char *)); |
|
|
|
for (uint8_t buff_el = 0; buff_el < BUFFER_SIZE; ++buff_el) { |
|
|
|
buffer[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
} |
|
|
|
|
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE, BUFFER_ELEMENT_SIZE); |
|
|
|
char* temp = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
char** to_add = (char **) malloc(5 * sizeof(char*)); |
|
|
|
|
|
|
|
for(uint8_t i = 0; i < (BUFFER_SIZE - 2); ++i) { |
|
|
|
snprintf(temp, BUFFER_ELEMENT_SIZE, "string_%d", i); |
|
|
|
|
|
|
|
to_add[i] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
|
|
|
|
if (i < 5) { |
|
|
|
snprintf(to_add[i], BUFFER_ELEMENT_SIZE, "string_%d", i + 10); |
|
|
|
} |
|
|
|
|
|
|
|
circ_buf_put(circ_buf, temp); |
|
|
|
} |
|
|
|
|
|
|
|
free(temp); |
|
|
|
|
|
|
|
circ_buf_mul_add(circ_buf, to_add, 5, compar); |
|
|
|
|
|
|
|
char* actual = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
|
|
|
|
circ_buf_get(circ_buf, actual); |
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_3", actual, BUFFER_ELEMENT_SIZE); |
|
|
|
circ_buf_get(circ_buf, actual); |
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_4", actual, BUFFER_ELEMENT_SIZE); |
|
|
|
circ_buf_get(circ_buf, actual); |
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_5", actual, BUFFER_ELEMENT_SIZE); |
|
|
|
circ_buf_get(circ_buf, actual); |
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_10", actual, BUFFER_ELEMENT_SIZE); |
|
|
|
circ_buf_get(circ_buf, actual); |
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_11", actual, BUFFER_ELEMENT_SIZE); |
|
|
|
circ_buf_get(circ_buf, actual); |
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_12", actual, BUFFER_ELEMENT_SIZE); |
|
|
|
circ_buf_get(circ_buf, actual); |
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_13", actual, BUFFER_ELEMENT_SIZE); |
|
|
|
circ_buf_get(circ_buf, actual); |
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_14", actual, BUFFER_ELEMENT_SIZE); |
|
|
|
|
|
|
|
free(actual); |
|
|
|
circ_buf_free(circ_buf); |
|
|
|
free(buffer); |
|
|
|
} |
|
|
|
|
|
|
|
void test_circ_buf_get(void) { |
|
|
|
char** buffer = (char **) malloc(BUFFER_SIZE * sizeof(char *)); |
|
|
|
for (uint8_t buff_el = 0; buff_el < BUFFER_SIZE; ++buff_el) { |
|
|
|
buffer[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
} |
|
|
|
|
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE); |
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE, BUFFER_ELEMENT_SIZE); |
|
|
|
char* temp = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
|
|
|
|
for(uint8_t i = 0; i < (BUFFER_SIZE - 2); ++i) { |
|
|
@ -105,7 +175,41 @@ void test_circ_buf_get(void) { |
|
|
|
free(buffer); |
|
|
|
} |
|
|
|
|
|
|
|
void test_circ_buf_read(void) { |
|
|
|
char** buffer = (char **) malloc(BUFFER_SIZE * sizeof(char *)); |
|
|
|
for (uint8_t buff_el = 0; buff_el < BUFFER_SIZE; ++buff_el) { |
|
|
|
buffer[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
} |
|
|
|
|
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE, BUFFER_ELEMENT_SIZE); |
|
|
|
char* temp = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
|
|
|
|
for(uint8_t i = 0; i < (BUFFER_SIZE - 2); ++i) { |
|
|
|
snprintf(temp, BUFFER_ELEMENT_SIZE, "string_%d", i); |
|
|
|
|
|
|
|
circ_buf_put(circ_buf, temp); |
|
|
|
} |
|
|
|
|
|
|
|
free(temp); |
|
|
|
|
|
|
|
char* actual = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
char* test_against = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
|
|
|
|
for(uint8_t i = 0; i < (BUFFER_SIZE - 2); ++i) { |
|
|
|
circ_buf_read(circ_buf, i, actual); |
|
|
|
snprintf(test_against, BUFFER_ELEMENT_SIZE, "string_%d", i); |
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN(test_against, actual, BUFFER_ELEMENT_SIZE); |
|
|
|
TEST_ASSERT_EQUAL_INT8(BUFFER_SIZE - 2, circ_buf_size(circ_buf)); |
|
|
|
} |
|
|
|
|
|
|
|
free(actual); |
|
|
|
free(test_against); |
|
|
|
|
|
|
|
circ_buf_free(circ_buf); |
|
|
|
free_buffer(buffer); |
|
|
|
free(buffer); |
|
|
|
} |
|
|
|
|
|
|
|
void test_circ_buf_empty(void) { |
|
|
|
char** buffer = (char **) malloc(BUFFER_SIZE * sizeof(char *)); |
|
|
@ -113,7 +217,7 @@ void test_circ_buf_empty(void) { |
|
|
|
buffer[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
} |
|
|
|
|
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE); |
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE, BUFFER_ELEMENT_SIZE); |
|
|
|
|
|
|
|
TEST_ASSERT_TRUE(circ_buf_empty(circ_buf)); |
|
|
|
|
|
|
@ -145,7 +249,7 @@ void test_circ_buf_full(void) { |
|
|
|
buffer[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
} |
|
|
|
|
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE); |
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE, BUFFER_ELEMENT_SIZE); |
|
|
|
|
|
|
|
for(uint8_t i = 0; i < (BUFFER_SIZE - 1); ++i) { |
|
|
|
circ_buf_put(circ_buf, "Lorem ipsum"); |
|
|
@ -167,7 +271,7 @@ void test_circ_buf_capacity(void) { |
|
|
|
buffer[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
} |
|
|
|
|
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE); |
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE, BUFFER_ELEMENT_SIZE); |
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT8(BUFFER_SIZE, circ_buf_capacity(circ_buf)); |
|
|
|
|
|
|
@ -187,14 +291,13 @@ void test_circ_buf_capacity(void) { |
|
|
|
free(buffer); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void test_circ_buf_size(void) { |
|
|
|
char** buffer = (char **) malloc(BUFFER_SIZE * sizeof(char *)); |
|
|
|
for (uint8_t buff_el = 0; buff_el < BUFFER_SIZE; ++buff_el) { |
|
|
|
buffer[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
} |
|
|
|
|
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE); |
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE, BUFFER_ELEMENT_SIZE); |
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT8(0, circ_buf_size(circ_buf)); |
|
|
|
|
|
|
@ -223,7 +326,7 @@ void test_circ_buf_reset(void) { |
|
|
|
buffer[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
} |
|
|
|
|
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE); |
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE, BUFFER_ELEMENT_SIZE); |
|
|
|
|
|
|
|
for(uint8_t i = 0; i < BUFFER_SIZE - 3; ++i) { |
|
|
|
circ_buf_put(circ_buf, "Lorem ipsum"); |
|
|
@ -240,3 +343,113 @@ void test_circ_buf_reset(void) { |
|
|
|
free_buffer(buffer); |
|
|
|
free(buffer); |
|
|
|
} |
|
|
|
|
|
|
|
void test_circ_buf_element_size(void) { |
|
|
|
char** buffer = (char **) malloc(BUFFER_SIZE * sizeof(char *)); |
|
|
|
for (uint8_t buff_el = 0; buff_el < BUFFER_SIZE; ++buff_el) { |
|
|
|
buffer[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
} |
|
|
|
|
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE, BUFFER_ELEMENT_SIZE); |
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT8(BUFFER_ELEMENT_SIZE, circ_buf_element_size(circ_buf)); |
|
|
|
|
|
|
|
circ_buf_put(circ_buf, "Lorem ipsum"); |
|
|
|
circ_buf_put(circ_buf, "Lorem ipsum"); |
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT8(BUFFER_ELEMENT_SIZE, circ_buf_element_size(circ_buf)); |
|
|
|
|
|
|
|
char* temp = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
circ_buf_get(circ_buf, temp); |
|
|
|
free(temp); |
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT8(BUFFER_ELEMENT_SIZE, circ_buf_element_size(circ_buf)); |
|
|
|
|
|
|
|
circ_buf_free(circ_buf); |
|
|
|
free_buffer(buffer); |
|
|
|
free(buffer); |
|
|
|
} |
|
|
|
|
|
|
|
void test_advance_pointer(void) { |
|
|
|
char** buffer = (char **) malloc(BUFFER_SIZE * sizeof(char *)); |
|
|
|
for (uint8_t buff_el = 0; buff_el < BUFFER_SIZE; ++buff_el) { |
|
|
|
buffer[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
} |
|
|
|
|
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE, BUFFER_ELEMENT_SIZE); |
|
|
|
|
|
|
|
advance_pointer(circ_buf); |
|
|
|
TEST_ASSERT_EQUAL_INT8(1, circ_buf_size(circ_buf)); |
|
|
|
advance_pointer(circ_buf); |
|
|
|
TEST_ASSERT_EQUAL_INT8(2, circ_buf_size(circ_buf)); |
|
|
|
|
|
|
|
circ_buf_free(circ_buf); |
|
|
|
free_buffer(buffer); |
|
|
|
free(buffer); |
|
|
|
} |
|
|
|
|
|
|
|
void test_retreat_pointer(void) { |
|
|
|
char** buffer = (char **) malloc(BUFFER_SIZE * sizeof(char *)); |
|
|
|
for (uint8_t buff_el = 0; buff_el < BUFFER_SIZE; ++buff_el) { |
|
|
|
buffer[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
} |
|
|
|
|
|
|
|
cbuf_handle_t circ_buf = circ_buf_init(buffer, BUFFER_SIZE, BUFFER_ELEMENT_SIZE); |
|
|
|
|
|
|
|
for(uint8_t i = 0; i < BUFFER_SIZE - 3; ++i) { |
|
|
|
circ_buf_put(circ_buf, "Lorem ipsum"); |
|
|
|
} |
|
|
|
|
|
|
|
retreat_pointer(circ_buf); |
|
|
|
TEST_ASSERT_EQUAL_INT8(BUFFER_SIZE - 4, circ_buf_size(circ_buf)); |
|
|
|
retreat_pointer(circ_buf); |
|
|
|
TEST_ASSERT_EQUAL_INT8(BUFFER_SIZE - 5, circ_buf_size(circ_buf)); |
|
|
|
|
|
|
|
circ_buf_free(circ_buf); |
|
|
|
free_buffer(buffer); |
|
|
|
free(buffer); |
|
|
|
} |
|
|
|
|
|
|
|
void test_diff_bufs(void) { |
|
|
|
char** buffer1 = (char **) malloc(BUFFER_SIZE * sizeof(char *)); |
|
|
|
char** buffer2 = (char **) malloc(BUFFER_SIZE * sizeof(char *)); |
|
|
|
for (uint8_t buff_el = 0; buff_el < BUFFER_SIZE; ++buff_el) { |
|
|
|
buffer1[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
buffer2[buff_el] = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
} |
|
|
|
|
|
|
|
cbuf_handle_t circ_buf1 = circ_buf_init(buffer1, BUFFER_SIZE, BUFFER_ELEMENT_SIZE); |
|
|
|
cbuf_handle_t circ_buf2 = circ_buf_init(buffer2, BUFFER_SIZE, BUFFER_ELEMENT_SIZE); |
|
|
|
|
|
|
|
char* temp = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char)); |
|
|
|
for(uint8_t i = 0; i < (BUFFER_SIZE - 2); ++i) { |
|
|
|
snprintf(temp, BUFFER_ELEMENT_SIZE, "string_%d", i); |
|
|
|
circ_buf_put(circ_buf1, temp); |
|
|
|
|
|
|
|
snprintf(temp, BUFFER_ELEMENT_SIZE, "string_%d", i + 2); |
|
|
|
circ_buf_put(circ_buf2, temp); |
|
|
|
} |
|
|
|
|
|
|
|
free(temp); |
|
|
|
|
|
|
|
char** add1 = NULL; |
|
|
|
char** add2 = NULL; |
|
|
|
diff_bufs(circ_buf1, circ_buf2, &add1, &add2); |
|
|
|
|
|
|
|
TEST_ASSERT_NOT_NULL(add1[0]); |
|
|
|
TEST_ASSERT_NOT_NULL(add2[0]); |
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_STRING("string_6", add1[0]); |
|
|
|
TEST_ASSERT_EQUAL_STRING("string_7", add1[1]); |
|
|
|
TEST_ASSERT_EQUAL_STRING(EOB, add1[2]); |
|
|
|
TEST_ASSERT_EQUAL_STRING("string_0", add2[0]); |
|
|
|
TEST_ASSERT_EQUAL_STRING("string_1", add2[1]); |
|
|
|
TEST_ASSERT_EQUAL_STRING(EOB, add2[2]); |
|
|
|
|
|
|
|
circ_buf_free(circ_buf1); |
|
|
|
free_buffer(buffer1); |
|
|
|
free(buffer1); |
|
|
|
circ_buf_free(circ_buf2); |
|
|
|
free_buffer(buffer2); |
|
|
|
free(buffer2); |
|
|
|
} |
|
|
|