Browse Source

Add diff and multiple add functions to circular buffer

master
Apostolos Fanakis 6 years ago
parent
commit
f6702fdc42
  1. 204
      lib/circ_buff.c
  2. 34
      lib/circ_buff.h
  3. 231
      test/test_circ_buff.c

204
lib/circ_buff.c

@ -4,6 +4,8 @@
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "circ_buff.h"
// Defines the circular buffer structure
@ -12,12 +14,13 @@ struct circ_buf_t {
size_t head;
size_t tail;
size_t max; // of the buffer
size_t el_size;
bool full;
};
// Private Functions
static void advance_pointer(cbuf_handle_t cbuf) {
unit_static void advance_pointer(cbuf_handle_t cbuf) {
assert(cbuf);
if(cbuf->full) {
@ -30,16 +33,150 @@ static void advance_pointer(cbuf_handle_t cbuf) {
cbuf->full = (cbuf->head == cbuf->tail);
}
static void retreat_pointer(cbuf_handle_t cbuf) {
unit_static void retreat_pointer(cbuf_handle_t cbuf) {
assert(cbuf);
cbuf->full = false;
cbuf->tail = (cbuf->tail + 1) % cbuf->max;
}
// Compares the buffers cbuf1 and cbuf2. Elements present on cbuf1 that do not
// exist on cbuf2 are added to the array add2, elements present on cbuf2 that do
// not exist on cbuf1 are added to the array add1.
// Both buffers must be ordered on the same way!
unit_static void diff_bufs(cbuf_handle_t cbuf1, cbuf_handle_t cbuf2,
char*** add1, char*** add2) {
assert(cbuf1 && cbuf2 &&
(circ_buf_element_size(cbuf1) == circ_buf_element_size(cbuf2)) &&
(circ_buf_capacity(cbuf1) == circ_buf_capacity(cbuf2)));
// Initializes the diff arrays
(*add1) = (char **) malloc(circ_buf_capacity(cbuf2) * sizeof(char *));
(*add2) = (char **) malloc(circ_buf_capacity(cbuf1) * sizeof(char *));
char* curr_str1 = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char));
char* curr_str2 = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char));
/*uint8_t diff_array_index = 0;
for (uint8_t i = 0; i < circ_buf_size(cbuf1); ++i) {
// Reads current element of cbuf1
circ_buf_read(cbuf1, i, curr_str1);
bool element_exists = false;
for (uint8_t j = 0; j < circ_buf_size(cbuf2); ++j) {
circ_buf_read(cbuf2, j, curr_str2);
// Checks against cbuf2 elements
if (!strcmp(curr_str2, curr_str1)) {
element_exists = true;
break;
}
}
if (!element_exists) {
(*add2)[diff_array_index] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char));
strcpy((*add2)[diff_array_index], curr_str1);
++diff_array_index;
}
}
(*add1)[diff_array_index] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char));
strcpy((*add1)[diff_array_index], EOB);
diff_array_index = 0;
for (uint8_t i = 0; i < circ_buf_size(cbuf2); ++i) {
// Reads current element of cbuf2
circ_buf_read(cbuf2, i, curr_str2);
bool element_exists = false;
for (uint8_t j = 0; j < circ_buf_size(cbuf1); ++j) {
circ_buf_read(cbuf1, j, curr_str1);
// Checks against cbuf1 elements
if (!strcmp(curr_str1, curr_str2)) {
element_exists = true;
break;
}
}
if (!element_exists) {
(*add1)[diff_array_index] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char));
strcpy((*add1)[diff_array_index], curr_str2);
++diff_array_index;
}
}
(*add2)[diff_array_index] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char));
strcpy((*add2)[diff_array_index], EOB);*/
uint8_t cbuf1_idx = 0, cbuf2_idx = 0, add1_arr_idx = 0, add2_arr_idx = 0;
while ((cbuf1_idx < circ_buf_size(cbuf1)) &&
(cbuf2_idx < circ_buf_size(cbuf2))) {
circ_buf_read(cbuf1, cbuf1_idx, curr_str1);
circ_buf_read(cbuf2, cbuf2_idx, curr_str2);
int strcmp_res = strcmp(curr_str1, curr_str2);
if (!strcmp_res) {
++cbuf1_idx;
++cbuf2_idx;
} else { // TODO: change the inner comparisons (strtok etc)
if (strcmp_res < 0) {
(*add2)[add2_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char));
strcpy((*add2)[add2_arr_idx], curr_str1);
++add2_arr_idx;
++cbuf1_idx;
}
else if (strcmp_res > 0) {
(*add1)[add1_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char));
strcpy((*add1)[add1_arr_idx], curr_str2);
++add1_arr_idx;
++cbuf2_idx;
}
}
}
while (cbuf1_idx < circ_buf_size(cbuf1)) {
(*add2)[add2_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char));
circ_buf_read(cbuf1, cbuf1_idx, curr_str1);
strcpy((*add2)[add2_arr_idx], curr_str1);
++add2_arr_idx;
++cbuf1_idx;
}
while (cbuf2_idx < circ_buf_size(cbuf2)) {
(*add1)[add1_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char));
circ_buf_read(cbuf2, cbuf2_idx, curr_str2);
strcpy((*add1)[add1_arr_idx], curr_str2);
++add1_arr_idx;
++cbuf2_idx;
}
(*add1)[add1_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char));
strcpy((*add1)[add1_arr_idx], EOB);
(*add2)[add2_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char));
strcpy((*add2)[add2_arr_idx], EOB);
free(curr_str1);
free(curr_str2);
/*uint8_t i = 0;
printf("add1:\n");
while (strcmp((*add1)[i], EOB)) {
printf("%s\n", (*add1)[i]);
++i;
}
i = 0;
printf("add2:\n");
while (strcmp((*add2)[i], EOB)) {
printf("%s\n", (*add2)[i]);
++i;
}*/
}
// APIs
cbuf_handle_t circ_buf_init(char** buffer, size_t size) {
cbuf_handle_t circ_buf_init(char** buffer, size_t size, size_t element_size) {
assert(buffer && size);
cbuf_handle_t cbuf = malloc(sizeof(circ_buf_t));
@ -47,6 +184,7 @@ cbuf_handle_t circ_buf_init(char** buffer, size_t size) {
cbuf->buffer = buffer;
cbuf->max = size;
cbuf->el_size = element_size;
circ_buf_reset(cbuf);
assert(circ_buf_empty(cbuf));
@ -90,7 +228,7 @@ size_t circ_buf_capacity(cbuf_handle_t cbuf) {
return cbuf->max;
}
void circ_buf_put(cbuf_handle_t cbuf, char* data) {
void circ_buf_put(cbuf_handle_t cbuf, const char* data) {
assert(cbuf && cbuf->buffer);
strcpy(cbuf->buffer[cbuf->head], data);
@ -98,6 +236,44 @@ void circ_buf_put(cbuf_handle_t cbuf, char* data) {
advance_pointer(cbuf);
}
void circ_buf_mul_add(cbuf_handle_t cbuf, char** data, uint8_t size,
int (*compar)(const void* , const void*)) {
assert(cbuf && data && cbuf->buffer);
qsort(data, size, sizeof(char*), compar);
char* last_element = (char*) malloc(circ_buf_element_size(cbuf) * sizeof(char));
for (uint8_t i = 0; i < size; ++i) {
circ_buf_read(cbuf, 0, last_element);
if (compar(&data[i], &last_element) < 0) {
continue;
}
circ_buf_put(cbuf, data[i]);
}
free(last_element);
int end_buffer_size = circ_buf_size(cbuf);
char** temp_array = (char **) malloc(end_buffer_size * sizeof(char *));
for (uint8_t buff_el = 0; buff_el < end_buffer_size; ++buff_el) {
temp_array[buff_el] = (char *) malloc(circ_buf_element_size(cbuf) * sizeof(char));
circ_buf_get(cbuf, temp_array[buff_el]);
}
qsort(temp_array, end_buffer_size, sizeof(char*), compar);
for (uint8_t i = 0; i < end_buffer_size; ++i) {
circ_buf_put(cbuf, temp_array[i]);
}
for (uint8_t buff_el = 0; buff_el < end_buffer_size; ++buff_el) {
free(temp_array[buff_el]);
}
free(temp_array);
}
int circ_buf_get(cbuf_handle_t cbuf, char* data) {
assert(cbuf && data && cbuf->buffer);
@ -113,6 +289,20 @@ int circ_buf_get(cbuf_handle_t cbuf, char* data) {
return r;
}
int circ_buf_read(cbuf_handle_t cbuf, size_t position, char* data) {
assert(cbuf && data && cbuf->buffer && (position < circ_buf_size(cbuf)));
int r = -1;
if(!circ_buf_empty(cbuf)) {
strcpy(data, cbuf->buffer[(cbuf->tail + position) % cbuf->max]);
r = 0;
}
return r;
}
bool circ_buf_empty(cbuf_handle_t cbuf) {
assert(cbuf);
@ -124,3 +314,9 @@ bool circ_buf_full(cbuf_handle_t cbuf) {
return cbuf->full;
}
size_t circ_buf_element_size(cbuf_handle_t cbuf) {
assert(cbuf);
return cbuf->el_size;
}

34
lib/circ_buff.h

@ -8,14 +8,26 @@
#include <stdbool.h>
#define EOB "-1"
// Circular buffer structure
typedef struct circ_buf_t circ_buf_t;
// and handle type
typedef circ_buf_t* cbuf_handle_t;
#ifdef TEST //This is a test build
// Makes private functions reachable by the tester
#define unit_static
unit_static void advance_pointer(cbuf_handle_t cbuf);
unit_static void retreat_pointer(cbuf_handle_t cbuf);
unit_static void diff_bufs(cbuf_handle_t cbuf1, cbuf_handle_t cbuf2, char*** add1, char*** add2);
#else
#define unit_static static
#endif
// Initializes a circular buffer structure and returns the circular buffer handle.
// Pass in a storage buffer and size.
cbuf_handle_t circ_buf_init(char** buffer, size_t size);
cbuf_handle_t circ_buf_init(char** buffer, size_t size, size_t element_size);
// Frees a circular buffer structure. Does not free data buffer!
void circ_buf_free(cbuf_handle_t cbuf);
@ -23,22 +35,34 @@ void circ_buf_free(cbuf_handle_t cbuf);
// Resets the circular buffer to empty, head == tail. Data not cleared!
void circ_buf_reset(cbuf_handle_t cbuf);
// Adds data, even if the buffer is full. Old data is overwritten.
void circ_buf_put(cbuf_handle_t cbuf, char* data);
// Adds data to the end of the buffer, even if the buffer is full. Old data is overwritten.
void circ_buf_put(cbuf_handle_t cbuf, const char* data);
// Adds multiple entries to the buffer, keeping the data in ascending order according to the
// function compar provided in the parameters. If the buffer is full, smallest data are overwritten.
// Doesn't check for duplicates!
void circ_buf_mul_add(cbuf_handle_t cbuf, char** data, uint8_t size,
int (*compar)(const void* , const void*));
// Retrieves a value from the buffer.
int circ_buf_get(cbuf_handle_t cbuf, char* data);
// Reads a value from the buffer. Does NOT retrieve, size is not reduced!
int circ_buf_read(cbuf_handle_t cbuf, size_t position, char* data);
// Checks if the buffer is empty.
bool circ_buf_empty(cbuf_handle_t cbuf);
// Checks if the buffer is full.
bool circ_buf_full(cbuf_handle_t cbuf);
// Checks the capacity of the buffer.
// Returns the capacity of the buffer.
size_t circ_buf_capacity(cbuf_handle_t cbuf);
// Checks the number of elements stored in the buffer.
// Returns the number of elements stored in the buffer.
size_t circ_buf_size(cbuf_handle_t cbuf);
// Returns the size of each element.
size_t circ_buf_element_size(cbuf_handle_t cbuf);
#endif //CIRC_BUFF_H_

231
test/test_circ_buff.c

@ -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);
}

Loading…
Cancel
Save