From c2e0fc5b77738b7b5d5d137a9bdd9efe9f7ec742 Mon Sep 17 00:00:00 2001 From: Billy Bao Date: Mon, 19 Feb 2024 12:41:48 -0800 Subject: [PATCH 1/4] add igo_common test --- tests/test_igo_common.cpp | 58 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/test_igo_common.cpp diff --git a/tests/test_igo_common.cpp b/tests/test_igo_common.cpp new file mode 100644 index 0000000..34cb8f2 --- /dev/null +++ b/tests/test_igo_common.cpp @@ -0,0 +1,58 @@ +#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->PAb, (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->PAb->B, 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->PAb); + 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, 0); +} + +TEST(CommonTests, TestInitNull) { + igo_common* igo_cm = NULL; + int ret = igo_init(igo_cm); + ASSERT_EQ(ret, 0); +} \ No newline at end of file From 429c816046c0ed11a7e71c1b27e7d5d1f2efac53 Mon Sep 17 00:00:00 2001 From: Billy Bao Date: Mon, 19 Feb 2024 14:48:09 -0800 Subject: [PATCH 2/4] add NULL checks for igo_init and igo_finish --- src/igo.c | 7 +++++++ tests/test_igo_common.cpp | 7 ++----- 2 files changed, 9 insertions(+), 5 deletions(-) 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/tests/test_igo_common.cpp b/tests/test_igo_common.cpp index 34cb8f2..86e4246 100644 --- a/tests/test_igo_common.cpp +++ b/tests/test_igo_common.cpp @@ -16,20 +16,17 @@ TEST(CommonTests, TestInitTypical) { 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->PAb, (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->PAb->B, 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->PAb); free(igo_cm->x); free(igo_cm->y); cholmod_finish(igo_cm->cholmod_cm); @@ -48,11 +45,11 @@ TEST(CommonTests, TestFinishTypical) { TEST(CommonTests, TestFinishNull) { igo_common* igo_cm = NULL; int ret = igo_finish(igo_cm); - ASSERT_EQ(ret, 0); + ASSERT_EQ(ret, 1); } TEST(CommonTests, TestInitNull) { igo_common* igo_cm = NULL; int ret = igo_init(igo_cm); ASSERT_EQ(ret, 0); -} \ No newline at end of file +} From 45cc02f450232e7351e955801704574efa37dc41 Mon Sep 17 00:00:00 2001 From: Billy Bao Date: Mon, 19 Feb 2024 14:51:58 -0800 Subject: [PATCH 3/4] documentation updates for NULL handling in igo_init/igo_finish --- src/igo.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/igo.h b/src/igo.h index 0194c72..7dbb40e 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 (returning 1). Attempting to finish a NULL pointer performs no operation on it instead. */ int igo_finish ( /* --- inouts --- */ igo_common* igo_cm From d65716035ccbd674c60de2b2882d950646d6312e Mon Sep 17 00:00:00 2001 From: Billy Bao Date: Mon, 26 Feb 2024 14:51:59 -0800 Subject: [PATCH 4/4] minor doc change --- src/igo.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/igo.h b/src/igo.h index 7dbb40e..efe9d58 100644 --- a/src/igo.h +++ b/src/igo.h @@ -109,7 +109,7 @@ int igo_init ( igo_common* igo_cm ) ; -/* Always succeeds (returning 1). Attempting to finish a NULL pointer performs no operation on it instead. */ +/* 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