So I got the circular buffer implement in C exact copy as the template from embeddedartistry: https://github.com/embeddedartistry/embedded-resources/tree/master/examples/c/circular_buffer.
Currently, trying to use "Unity Unit Testing" Libraries -(http://www.throwtheswitch.org/unity) to write test cases for the circular buffer. The project complied successfully and execute in Clion. First 4 test cases were able to run however I kept getting error at GetElement, AddFirst Element and FullFunctionReturnTrue didn't execute with the message "Process finished with exit code 139 (interrupted by signal 11: SIGSEGV). And the error from debugger is: "exception exc_bad_access (code=1 address=0x0)" t
#include "unity.h"
#include "cbuf.h"
#include <stdio.h>
#include <stdlib.h>
mismatch value address
static size_t size = 5;
static uint8_t* buffer = NULL;
cbuf_handle_t test_cbuf;
static uint8_t data[] = {0,1,2,3,4};
static size_t elements_in_cbuf;
static size_t num;
//*************Fill Buffer********************//
static void fill_buffer()
{
for(size_t i = 0; i < size; i++)
{
circular_buf_put(test_cbuf, data[i]);
}
}
static void fill_buffer_num_elements()
{
for(size_t i = 0; i < num; i++)
{
circular_buf_put(test_cbuf, data[i]);
}
}
//*****************TEST-SETUP***************//
//void test_Subtraction_should_ReturnTwo(void) {
// TEST_ASSERT_EQUAL(2,log_subtraction());
//}
void test_CircularBuffer_Initialization(void){
test_cbuf = circular_buf_init(buffer, size);
TEST_ASSERT_NOT_NULL(test_cbuf);
}
void test_CircularBuffer_EmptyonInit(void){
test_cbuf = circular_buf_init(buffer, size);
TEST_ASSERT_TRUE(circular_buf_empty(test_cbuf));
TEST_ASSERT_FALSE(circular_buf_full(test_cbuf));
}
void test_Capacity_Return_CorrectValue(void){
TEST_ASSERT_EQUAL(size, circular_buf_capacity(test_cbuf));
}
//Errors started from here//
void test_CircularBuffer_SizeFuncReturnsZero_WhenCbufEmpty(void){
elements_in_cbuf = circular_buf_size(test_cbuf);
TEST_ASSERT_EQUAL_size_t(0, elements_in_cbuf);
}
void test_CircularBuffer_AddFirstElementTo_Cbuf(void){
circular_buf_put(test_cbuf,data[0]);
TEST_ASSERT_EQUAL_UINT8(data[0],buffer[0]);
}
void test_CircularBuffer_FullFunctionReturn_TrueAtFull(void){
fill_buffer();
TEST_ASSERT_TRUE(circular_buf_full(test_cbuf));
}
question from:
https://stackoverflow.com/questions/66055472/exception-exc-bad-access-code-1-address-0x0 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…