Remove complex arrays
This can be done with structs in an array_t
This commit is contained in:
209
src/safe_array.c
209
src/safe_array.c
@@ -240,212 +240,3 @@ void array_2d_free(array_2d_t *x)
|
||||
}
|
||||
free(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function generates a complex safe array
|
||||
*
|
||||
* @param rows The number of rows in the array
|
||||
* @param size The element size (sizeof(type))
|
||||
* @return array_c_s_t* The pointer to the complex array struct
|
||||
*/
|
||||
array_c_s_t *array_c_s_init(size_t rows, size_t size)
|
||||
{
|
||||
array_c_s_t *x = calloc(sizeof(array_c_s_t) + (2 * rows * size) - 1, 1);
|
||||
if (!x)
|
||||
{
|
||||
fprintf(stderr, "%s: Failed to allocate memory for array", __func__);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
x->rows = rows;
|
||||
x->size = size;
|
||||
x->ptr = x->data;
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function gets the number of rows in the complex safe array
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
* @return size_t The number of rows
|
||||
*/
|
||||
size_t array_c_s_get_rows(array_c_s_t *x)
|
||||
{
|
||||
if (!x)
|
||||
{
|
||||
fprintf(stderr, "%s: ptr dereferences to NULL\n", __func__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return x->rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function gets an imaginary element from the complex safe array
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
* @param row The row index of the element to get
|
||||
* @return void* The ptr to the imaginary element
|
||||
*/
|
||||
void* array_c_get_imag(array_c_s_t *x, size_t row)
|
||||
{
|
||||
if (row >= x->rows)
|
||||
{
|
||||
fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (char *)x->ptr + (2 * row * x->size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function gets a real element from the complex safe array
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
* @param row The row index of the element to get
|
||||
* @return void* The ptr to the real element
|
||||
*/
|
||||
void* array_c_get_real(array_c_s_t *x, size_t row)
|
||||
{
|
||||
if (row >= x->rows)
|
||||
{
|
||||
fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (char *)x->ptr + ((2 * row + 1) * x->size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function sets an element in the complex safe array
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
* @param row The row index of the element to set
|
||||
* @param real The ptr to the real value of the element
|
||||
* @param imaginary The ptr to the imaginary value of the element
|
||||
*/
|
||||
int array_c_s_set(array_c_s_t *x, size_t row, void *real, void *imaginary)
|
||||
{
|
||||
if (row >= x->rows)
|
||||
{
|
||||
fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy((char *)x->ptr + ((2 * row + 1) * x->size), real, x->size);
|
||||
memcpy((char *)x->ptr + ( 2 * row * x->size), imaginary, x->size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function frees the complex safe array, sets the pointer to NULL and the number of rows to 0
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
*/
|
||||
void array_c_s_free(array_c_s_t *x)
|
||||
{
|
||||
if (!x)
|
||||
{
|
||||
fprintf(stderr, "%s: ptr dereferences to NULL\n", __func__);
|
||||
}
|
||||
|
||||
free(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function generates a complex safe array in polar form
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
* @param rows The number of complex elements in the array
|
||||
* @param init_val The initial value of the array
|
||||
* @return int The number of elements in the array
|
||||
*/
|
||||
array_c_p_t *array_c_p_init(size_t rows, size_t size)
|
||||
{
|
||||
array_c_p_t* x = calloc(sizeof(array_c_p_t) + (2 * rows * size) - 1, 1);
|
||||
if (!x)
|
||||
{
|
||||
fprintf(stderr, "%s: Failed to allocate memory for array", __func__);
|
||||
return NULL;
|
||||
}
|
||||
x->size = size;
|
||||
x->rows = rows;
|
||||
x->ptr = x->data;
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function gets the number of rows in the complex safe array
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
* @return size_t The number of rows
|
||||
*/
|
||||
size_t array_c_p_get_rows(array_c_p_t *x)
|
||||
{
|
||||
if (!x)
|
||||
{
|
||||
fprintf(stderr, "%s: ptr dereferences to NULL\n", __func__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return x->rows;
|
||||
}
|
||||
|
||||
void* array_c_get_mag(array_c_p_t *x, size_t row)
|
||||
{
|
||||
if (row >= x->rows)
|
||||
{
|
||||
fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (char *)x->ptr + (2 * row * x->size);
|
||||
}
|
||||
|
||||
void* array_c_get_phase(array_c_p_t *x, size_t row)
|
||||
{
|
||||
if (row >= x->rows)
|
||||
{
|
||||
fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (char *)x->ptr + ((2 * row + 1) * x->size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function sets an element in the complex safe array
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
* @param row The row index of the element to set
|
||||
* @param magnitude The magnitude value of the element
|
||||
* @param phase The phase value of the element
|
||||
*/
|
||||
int array_c_p_set(array_c_p_t *x, size_t row, void* magnitude, void* phase)
|
||||
{
|
||||
if (row >= x->rows)
|
||||
{
|
||||
fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy((char *)x->ptr + (2 * row * x->size), magnitude, x->size);
|
||||
memcpy((char *)x->ptr + ((2 * row + 1) * x->size), phase, x->size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function frees the complex safe array, sets the pointer to NULL and the number of rows to 0
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
*/
|
||||
void array_c_p_free(array_c_p_t *x)
|
||||
{
|
||||
if (!x)
|
||||
{
|
||||
fprintf(stderr, "%s: ptr dereferences to NULL\n", __func__);
|
||||
}
|
||||
|
||||
free(x);
|
||||
}
|
||||
@@ -18,11 +18,6 @@
|
||||
|
||||
#define ARRAY_GET(arr, index, type) (*(type *)array_get(arr, index))
|
||||
#define ARRAY_2D_GET(arr, row, col, type) (*(type *)array_2d_get(arr, row, col))
|
||||
#define ARRAY_C_GET_IMAG(arr, row, type) (*(type *)array_c_get_imag(arr, row))
|
||||
#define ARRAY_C_GET_REAL(arr, row, type) (*(type *)array_c_get_real(arr, row))
|
||||
#define ARRAY_C_GET_MAG(arr, row, type) (*(type *)array_c_get_mag(arr, row))
|
||||
#define ARRAY_C_GET_PHASE(arr, row, type) (*(type *)array_c_get_phase(arr, row))
|
||||
|
||||
|
||||
typedef struct array_s
|
||||
{
|
||||
@@ -41,22 +36,6 @@ typedef struct array_2d_s
|
||||
uint8_t data[1];
|
||||
} array_2d_t;
|
||||
|
||||
typedef struct array_c_s
|
||||
{
|
||||
size_t rows;
|
||||
size_t size;
|
||||
void* ptr;
|
||||
uint8_t data[1];
|
||||
} array_c_s_t;
|
||||
|
||||
typedef struct array_c_p_s
|
||||
{
|
||||
size_t rows;
|
||||
size_t size;
|
||||
void* ptr;
|
||||
uint8_t data[1];
|
||||
} array_c_p_t;
|
||||
|
||||
// Array.c
|
||||
array_t *array_init(size_t size, size_t rows);
|
||||
size_t array_get_rows(array_t *arr);
|
||||
@@ -70,17 +49,3 @@ size_t array_2d_get_cols(array_2d_t *x);
|
||||
void *array_2d_get(array_2d_t *x, size_t row, size_t col);
|
||||
int array_2d_set(array_2d_t *x, size_t row, size_t col, void *val);
|
||||
void array_2d_free(array_2d_t *x);
|
||||
|
||||
array_c_s_t *array_c_s_init(size_t rows, size_t size);
|
||||
size_t array_c_s_get_rows(array_c_s_t *x);
|
||||
void* array_c_get_real(array_c_s_t *x, size_t row);
|
||||
void* array_c_get_imag(array_c_s_t *x, size_t row);
|
||||
int array_c_s_set(array_c_s_t *x, size_t row, void *real, void *imaginary);
|
||||
void array_c_s_free(array_c_s_t *x);
|
||||
|
||||
array_c_p_t *array_c_p_init(size_t rows, size_t size);
|
||||
size_t array_c_p_get_rows(array_c_p_t *x);
|
||||
void *array_c_get_mag(array_c_p_t *x, size_t row);
|
||||
void *array_c_get_phase(array_c_p_t *x, size_t row);
|
||||
int array_c_p_set(array_c_p_t *x, size_t row, void* magnitude, void* phase);
|
||||
void array_c_p_free(array_c_p_t *x);
|
||||
@@ -86,91 +86,3 @@ TEST(array, Set_Get_array_2d)
|
||||
|
||||
array_2d_free(x);
|
||||
}
|
||||
|
||||
TEST(array, array_c_s_init)
|
||||
{
|
||||
array_c_s_t* x = array_c_s_init(2, sizeof(double));
|
||||
EXPECT_EQ(array_c_s_get_rows(x), 2);
|
||||
EXPECT_EQ(ARRAY_C_GET_REAL(x, 0, double), 0);
|
||||
EXPECT_EQ(ARRAY_C_GET_REAL(x, 1, double), 0);
|
||||
EXPECT_EQ(ARRAY_C_GET_IMAG(x, 0, double), 0);
|
||||
EXPECT_EQ(ARRAY_C_GET_IMAG(x, 1, double), 0);
|
||||
array_c_s_free(x);
|
||||
}
|
||||
|
||||
TEST(array, Set_Get_array_C_S)
|
||||
{
|
||||
array_c_s_t* x = array_c_s_init(2, sizeof(double));
|
||||
|
||||
double _real = 1;
|
||||
double _imaginary = 2;
|
||||
array_c_s_set(x, 0, &_real, &_imaginary);
|
||||
_real = 3;
|
||||
_imaginary = 4;
|
||||
array_c_s_set(x, 1, &_real, &_imaginary);
|
||||
|
||||
testing::internal::CaptureStderr();
|
||||
array_c_s_set(x, 2, &_real, &_imaginary);
|
||||
std::string output = testing::internal::GetCapturedStderr();
|
||||
EXPECT_EQ(output, "array_c_s_set: index out of bounds: 2 > 2\n");
|
||||
|
||||
EXPECT_EQ(ARRAY_C_GET_REAL(x, 0, double), 1);
|
||||
EXPECT_EQ(ARRAY_C_GET_IMAG(x, 0, double), 2);
|
||||
EXPECT_EQ(ARRAY_C_GET_REAL(x, 1, double), 3);
|
||||
EXPECT_EQ(ARRAY_C_GET_IMAG(x, 1, double), 4);
|
||||
|
||||
testing::internal::CaptureStderr();
|
||||
array_c_get_real(x, 2);
|
||||
output = testing::internal::GetCapturedStderr();
|
||||
EXPECT_EQ(output, "array_c_get_real: index out of bounds: 2 > 2\n");
|
||||
testing::internal::CaptureStderr();
|
||||
array_c_get_imag(x, 2);
|
||||
output = testing::internal::GetCapturedStderr();
|
||||
EXPECT_EQ(output, "array_c_get_imag: index out of bounds: 2 > 2\n");
|
||||
|
||||
array_c_s_free(x);
|
||||
}
|
||||
|
||||
TEST(array, array_c_p_init)
|
||||
{
|
||||
array_c_p_t* x = array_c_p_init(2, sizeof(double));
|
||||
EXPECT_EQ(array_c_p_get_rows(x), 2);
|
||||
EXPECT_EQ(ARRAY_C_GET_MAG(x, 0, double), 0);
|
||||
EXPECT_EQ(ARRAY_C_GET_MAG(x, 1, double), 0);
|
||||
EXPECT_EQ(ARRAY_C_GET_PHASE(x, 0, double), 0);
|
||||
EXPECT_EQ(ARRAY_C_GET_PHASE(x, 1, double), 0);
|
||||
array_c_p_free(x);
|
||||
}
|
||||
|
||||
TEST(array, Set_Get_array_C_P)
|
||||
{
|
||||
array_c_p_t* x = array_c_p_init(2, sizeof(double));
|
||||
|
||||
double _mag = 1;
|
||||
double _phase = 2;
|
||||
array_c_p_set(x, 0, &_mag, &_phase);
|
||||
_mag = 3;
|
||||
_phase = 4;
|
||||
array_c_p_set(x, 1, &_mag, &_phase);
|
||||
|
||||
testing::internal::CaptureStderr();
|
||||
array_c_p_set(x, 2, &_mag, &_phase);
|
||||
std::string output = testing::internal::GetCapturedStderr();
|
||||
EXPECT_EQ(output, "array_c_p_set: index out of bounds: 2 > 2\n");
|
||||
|
||||
EXPECT_EQ(ARRAY_C_GET_MAG(x, 0, double), 1);
|
||||
EXPECT_EQ(ARRAY_C_GET_PHASE(x, 0, double), 2);
|
||||
EXPECT_EQ(ARRAY_C_GET_MAG(x, 1, double), 3);
|
||||
EXPECT_EQ(ARRAY_C_GET_PHASE(x, 1, double), 4);
|
||||
|
||||
testing::internal::CaptureStderr();
|
||||
array_c_get_phase(x, 2);
|
||||
output = testing::internal::GetCapturedStderr();
|
||||
EXPECT_EQ(output, "array_c_get_phase: index out of bounds: 2 > 2\n");
|
||||
testing::internal::CaptureStderr();
|
||||
array_c_get_mag(x, 2);
|
||||
output = testing::internal::GetCapturedStderr();
|
||||
EXPECT_EQ(output, "array_c_get_mag: index out of bounds: 2 > 2\n");
|
||||
|
||||
array_c_p_free(x);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user