diff --git a/src/igo.c b/src/igo.c index adc2800..3c5ddba 100644 --- a/src/igo.c +++ b/src/igo.c @@ -8,6 +8,10 @@ int igo_init ( /* --- inouts --- */ igo_common* igo_cm ) { + if (igo_cm == NULL) { + return 0; + } + igo_cm->cholmod_cm = malloc(sizeof(cholmod_common)); cholmod_start(igo_cm->cholmod_cm); @@ -44,6 +48,9 @@ int igo_finish ( /* --- inouts --- */ igo_common* igo_cm ) { + if (igo_cm == NULL) { + return 1; + } igo_free_sparse(&(igo_cm->A), igo_cm); igo_free_dense(&(igo_cm->b), igo_cm); igo_free_factor(&(igo_cm->L), igo_cm); diff --git a/src/igo.h b/src/igo.h index 0194c72..efe9d58 100644 --- a/src/igo.h +++ b/src/igo.h @@ -103,11 +103,13 @@ typedef struct igo_common_struct { /* Primary functions */ /* ---------------------------------------------------------- */ +/* Returns 0 if given a NULL pointer, 1 otherwise. */ int igo_init ( /* --- inouts --- */ igo_common* igo_cm ) ; +/* Always succeeds and returns 1. Attempting to finish a NULL pointer performs no operation on it instead. */ int igo_finish ( /* --- inouts --- */ igo_common* igo_cm diff --git a/tests/test_igo_common.cpp b/tests/test_igo_common.cpp new file mode 100644 index 0000000..86e4246 --- /dev/null +++ b/tests/test_igo_common.cpp @@ -0,0 +1,55 @@ +#include + +extern "C" { +#include +} + +TEST(CommonTests, TestInitTypical) { + igo_common* igo_cm = (igo_common*) malloc(sizeof(igo_common)); + int ret = igo_init(igo_cm); + ASSERT_EQ(ret, 1); + ASSERT_NE(igo_cm, (void*)NULL); + // check cholmod initialization + ASSERT_NE(igo_cm->cholmod_cm, (void*)NULL); + ASSERT_EQ(igo_cm->cholmod_cm->status, 0); + // check matrices + ASSERT_NE(igo_cm->A, (void*)NULL); + ASSERT_NE(igo_cm->b, (void*)NULL); + ASSERT_NE(igo_cm->L, (void*)NULL); + ASSERT_NE(igo_cm->x, (void*)NULL); + ASSERT_NE(igo_cm->y, (void*)NULL); + // cleanup + cholmod_free_sparse(&igo_cm->A->A, igo_cm->cholmod_cm); + cholmod_free_dense(&igo_cm->b->B, igo_cm->cholmod_cm); + cholmod_free_factor(&igo_cm->L->L, igo_cm->cholmod_cm); + cholmod_free_dense(&igo_cm->x->B, igo_cm->cholmod_cm); + cholmod_free_dense(&igo_cm->y->B, igo_cm->cholmod_cm); + free(igo_cm->A); + free(igo_cm->b); + free(igo_cm->L); + free(igo_cm->x); + free(igo_cm->y); + cholmod_finish(igo_cm->cholmod_cm); + free(igo_cm); +} + +TEST(CommonTests, TestFinishTypical) { + igo_common* igo_cm = (igo_common*) malloc(sizeof(igo_common)); + igo_init(igo_cm); + int ret = igo_finish(igo_cm); + ASSERT_EQ(ret, 1); + ASSERT_EQ(igo_cm->cholmod_cm, (void*)NULL); + free(igo_cm); +} + +TEST(CommonTests, TestFinishNull) { + igo_common* igo_cm = NULL; + int ret = igo_finish(igo_cm); + ASSERT_EQ(ret, 1); +} + +TEST(CommonTests, TestInitNull) { + igo_common* igo_cm = NULL; + int ret = igo_init(igo_cm); + ASSERT_EQ(ret, 0); +}