1 
2 #define LOG_ERROR 1
3 #define LOG_WARN 1
4 #define LOG_INFO 0
5 #define LOG_TEST 1
6 
7 #include "Mongoose_Test.hpp"
8 #include "Mongoose_Internal.hpp"
9 #include "Mongoose_IO.hpp"
10 #include "Mongoose_EdgeCutProblem.hpp"
11 
12 using namespace Mongoose;
13 
14 /* Custom memory management functions allow for memory testing. */
15 int AllowedMallocs;
16 
myMalloc(size_t size)17 void *myMalloc(size_t size)
18 {
19     if(AllowedMallocs <= 0) return NULL;
20     AllowedMallocs--;
21     return malloc(size);
22 }
23 
myCalloc(size_t count,size_t size)24 void *myCalloc(size_t count, size_t size)
25 {
26     if(AllowedMallocs <= 0) return NULL;
27     AllowedMallocs--;
28     return calloc(count, size);
29 }
30 
myRealloc(void * ptr,size_t newSize)31 void *myRealloc(void *ptr, size_t newSize)
32 {
33     if(AllowedMallocs <= 0) return NULL;
34     AllowedMallocs--;
35     return realloc(ptr, newSize);
36 }
37 
myFree(void * ptr)38 void myFree(void *ptr)
39 {
40     if(ptr != NULL) free(ptr);
41 }
42 
main(int argn,char ** argv)43 int main(int argn, char** argv)
44 {
45     (void)argn; // Unused variable
46     (void)argv; // Unused variable
47 
48     SuiteSparse_start();
49 
50     // Set Logger to report all messages and turn off timing info
51     Logger::setDebugLevel(All);
52     Logger::setTimingFlag(false);
53 
54     // Test Graph(n, nz) static constructor
55     Graph *G2 = Graph::create(10, 20);
56     EdgeCutProblem *prob = EdgeCutProblem::create(G2);
57 
58     prob->clearMarkArray(LONG_MAX);
59     Int markValue = prob->getMarkValue();
60     assert(markValue == 1);
61 
62     prob->clearMarkArray(LONG_MAX-1);
63     prob->clearMarkArray();
64     markValue = prob->getMarkValue();
65     assert(markValue >= 1);
66     prob->~EdgeCutProblem();
67 
68     MM_typecode matcode;
69     cs *M4 = read_matrix("../Matrix/bcspwr01.mtx", matcode);
70     M4->x = NULL;
71     Graph *G7 = Graph::create(M4);
72     assert(G7 != NULL);
73 
74     // Tests to increase coverage
75     /* Override SuiteSparse memory management with custom testers. */
76     SuiteSparse_config.malloc_func = myMalloc;
77     SuiteSparse_config.calloc_func = myCalloc;
78     SuiteSparse_config.realloc_func = myRealloc;
79     SuiteSparse_config.free_func = myFree;
80 
81     // Simulate failure to allocate return arrays
82     AllowedMallocs = 0;
83     EdgeCutProblem *G3 = EdgeCutProblem::create(G7);
84     assert(G3 == NULL);
85 
86     AllowedMallocs = 1;
87     EdgeCutProblem *G4 = EdgeCutProblem::create(G7);
88     assert(G4 == NULL);
89 
90     AllowedMallocs = 5;
91     EdgeCutProblem *G5 = EdgeCutProblem::create(G7);
92     assert(G5 == NULL);
93 
94     AllowedMallocs = 10;
95     EdgeCutProblem *G6 = EdgeCutProblem::create(G7);
96     assert(G6 == NULL);
97 
98     G7->~Graph();
99 
100     SuiteSparse_finish();
101 
102     return 0;
103 }
104