You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
242 lines
6.8 KiB
242 lines
6.8 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
|
|
#include "unity.h"
|
|
#include "circ_buff.h"
|
|
|
|
#define BUFFER_SIZE 8
|
|
#define BUFFER_ELEMENT_SIZE 278
|
|
|
|
void free_buffer(char** buffer) {
|
|
for(uint8_t i = 0; i < BUFFER_SIZE; ++i) {
|
|
free(buffer[i]);
|
|
}
|
|
}
|
|
|
|
void test_circ_buf_init(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));
|
|
}
|
|
|
|
TEST_ASSERT_NOT_NULL(circ_buf_init(buffer, BUFFER_SIZE));
|
|
|
|
free_buffer(buffer);
|
|
free(buffer);
|
|
}
|
|
|
|
void test_circ_buf_put(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);
|
|
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);
|
|
}
|
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_0", buffer[0], BUFFER_ELEMENT_SIZE);
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_1", buffer[1], BUFFER_ELEMENT_SIZE);
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_2", buffer[2], BUFFER_ELEMENT_SIZE);
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_3", buffer[3], BUFFER_ELEMENT_SIZE);
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_4", buffer[4], BUFFER_ELEMENT_SIZE);
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_5", buffer[5], BUFFER_ELEMENT_SIZE);
|
|
|
|
for(uint8_t i = 0; i < BUFFER_SIZE; ++i) {
|
|
snprintf(temp, BUFFER_ELEMENT_SIZE, "string_%d", i);
|
|
|
|
circ_buf_put(circ_buf, temp);
|
|
}
|
|
|
|
free(temp);
|
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_2", buffer[0], BUFFER_ELEMENT_SIZE);
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_3", buffer[1], BUFFER_ELEMENT_SIZE);
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_4", buffer[2], BUFFER_ELEMENT_SIZE);
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_5", buffer[3], BUFFER_ELEMENT_SIZE);
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_6", buffer[4], BUFFER_ELEMENT_SIZE);
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_7", buffer[5], BUFFER_ELEMENT_SIZE);
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_0", buffer[6], BUFFER_ELEMENT_SIZE);
|
|
TEST_ASSERT_EQUAL_STRING_LEN("string_1", buffer[7], BUFFER_ELEMENT_SIZE);
|
|
|
|
circ_buf_free(circ_buf);
|
|
free_buffer(buffer);
|
|
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);
|
|
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_get(circ_buf, actual);
|
|
snprintf(test_against, BUFFER_ELEMENT_SIZE, "string_%d", i);
|
|
|
|
TEST_ASSERT_EQUAL_STRING_LEN(test_against, actual, BUFFER_ELEMENT_SIZE);
|
|
}
|
|
|
|
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 *));
|
|
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);
|
|
|
|
TEST_ASSERT_TRUE(circ_buf_empty(circ_buf));
|
|
|
|
for(uint8_t i = 0; i < BUFFER_SIZE; ++i) {
|
|
circ_buf_put(circ_buf, "Lorem ipsum");
|
|
}
|
|
|
|
TEST_ASSERT_FALSE(circ_buf_empty(circ_buf));
|
|
|
|
char* temp = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char));
|
|
|
|
for(uint8_t i = 0; i < BUFFER_SIZE; ++i) {
|
|
circ_buf_get(circ_buf, temp);
|
|
}
|
|
|
|
free(temp);
|
|
|
|
TEST_ASSERT_TRUE(circ_buf_empty(circ_buf));
|
|
|
|
circ_buf_free(circ_buf);
|
|
free_buffer(buffer);
|
|
free(buffer);
|
|
}
|
|
|
|
|
|
void test_circ_buf_full(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);
|
|
|
|
for(uint8_t i = 0; i < (BUFFER_SIZE - 1); ++i) {
|
|
circ_buf_put(circ_buf, "Lorem ipsum");
|
|
TEST_ASSERT_FALSE(circ_buf_full(circ_buf));
|
|
}
|
|
|
|
circ_buf_put(circ_buf, "Lorem ipsum");
|
|
TEST_ASSERT_TRUE(circ_buf_full(circ_buf));
|
|
|
|
circ_buf_free(circ_buf);
|
|
free_buffer(buffer);
|
|
free(buffer);
|
|
}
|
|
|
|
|
|
void test_circ_buf_capacity(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);
|
|
|
|
TEST_ASSERT_EQUAL_INT8(BUFFER_SIZE, circ_buf_capacity(circ_buf));
|
|
|
|
circ_buf_put(circ_buf, "Lorem ipsum");
|
|
circ_buf_put(circ_buf, "Lorem ipsum");
|
|
|
|
TEST_ASSERT_EQUAL_INT8(BUFFER_SIZE, circ_buf_capacity(circ_buf));
|
|
|
|
char* temp = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char));
|
|
circ_buf_get(circ_buf, temp);
|
|
free(temp);
|
|
|
|
TEST_ASSERT_EQUAL_INT8(BUFFER_SIZE, circ_buf_capacity(circ_buf));
|
|
|
|
circ_buf_free(circ_buf);
|
|
free_buffer(buffer);
|
|
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);
|
|
|
|
TEST_ASSERT_EQUAL_INT8(0, circ_buf_size(circ_buf));
|
|
|
|
for(uint8_t i = 0; i < BUFFER_SIZE; ++i) {
|
|
circ_buf_put(circ_buf, "Lorem ipsum");
|
|
TEST_ASSERT_EQUAL_INT8(i + 1, circ_buf_size(circ_buf));
|
|
}
|
|
|
|
char* temp = (char *) malloc(BUFFER_ELEMENT_SIZE * sizeof(char));
|
|
|
|
for(uint8_t i = 0; i < BUFFER_SIZE; ++i) {
|
|
circ_buf_get(circ_buf, temp);
|
|
TEST_ASSERT_EQUAL_INT8(BUFFER_SIZE - i - 1, circ_buf_size(circ_buf));
|
|
}
|
|
|
|
free(temp);
|
|
|
|
circ_buf_free(circ_buf);
|
|
free_buffer(buffer);
|
|
free(buffer);
|
|
}
|
|
|
|
void test_circ_buf_reset(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);
|
|
|
|
for(uint8_t i = 0; i < BUFFER_SIZE - 3; ++i) {
|
|
circ_buf_put(circ_buf, "Lorem ipsum");
|
|
}
|
|
|
|
circ_buf_reset(circ_buf);
|
|
|
|
TEST_ASSERT_EQUAL_INT8(0, circ_buf_size(circ_buf));
|
|
TEST_ASSERT_EQUAL_INT8(BUFFER_SIZE, circ_buf_capacity(circ_buf));
|
|
TEST_ASSERT_TRUE(circ_buf_empty(circ_buf));
|
|
TEST_ASSERT_FALSE(circ_buf_full(circ_buf));
|
|
|
|
circ_buf_free(circ_buf);
|
|
free_buffer(buffer);
|
|
free(buffer);
|
|
}
|
|
|