Line data Source code
1 : //------------------------------------------------------------------------------
2 : // LAGraph/src/test/test_Graph_Print.c: test LAGraph_Graph_Print
3 : //------------------------------------------------------------------------------
4 :
5 : // LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
6 : // SPDX-License-Identifier: BSD-2-Clause
7 : //
8 : // For additional details (including references to third party source code and
9 : // other files) see the LICENSE file or contact permission@sei.cmu.edu. See
10 : // Contributors.txt for a full list of contributors. Created, in part, with
11 : // funding and support from the U.S. Government (see Acknowledgments.txt file).
12 : // DM22-0790
13 :
14 : // Contributed by Timothy A. Davis, Texas A&M University
15 :
16 : //------------------------------------------------------------------------------
17 :
18 : #include "LAGraph_test.h"
19 : #include "LG_internal.h"
20 :
21 : //------------------------------------------------------------------------------
22 : // global variables
23 : //------------------------------------------------------------------------------
24 :
25 : LAGraph_Graph G = NULL ;
26 : char msg [LAGRAPH_MSG_LEN] ;
27 : GrB_Matrix A = NULL, AT = NULL ;
28 : #define LEN 512
29 : char filename [LEN+1] ;
30 : char atype_name [LAGRAPH_MAX_NAME_LEN] ;
31 :
32 : //------------------------------------------------------------------------------
33 : // setup: start a test
34 : //------------------------------------------------------------------------------
35 :
36 2 : void setup (void)
37 : {
38 2 : OK (LAGraph_Init (msg)) ;
39 2 : }
40 :
41 : //------------------------------------------------------------------------------
42 : // teardown: finalize a test
43 : //------------------------------------------------------------------------------
44 :
45 2 : void teardown (void)
46 : {
47 2 : OK (LAGraph_Finalize (msg)) ;
48 2 : }
49 :
50 : //------------------------------------------------------------------------------
51 : // prwhat: print what should be printed
52 : //------------------------------------------------------------------------------
53 :
54 97 : const char *prwhat (int pr)
55 : {
56 97 : switch (pr)
57 : {
58 16 : case 0: return ("nothing") ;
59 16 : case 1: return ("terse") ;
60 16 : case 2: return ("summary") ;
61 16 : case 3: return ("all") ;
62 16 : case 4: return ("summary (doubles in full precision)") ;
63 16 : case 5: return ("all (doubles in full precision)") ;
64 1 : default: ;
65 : }
66 1 : return (NULL) ;
67 : }
68 :
69 : //------------------------------------------------------------------------------
70 : // test_Graph_Print: test LAGraph_Graph_Print
71 : //------------------------------------------------------------------------------
72 :
73 : typedef struct
74 : {
75 : LAGraph_Kind kind ;
76 : int nself_edges ;
77 : const char *name ;
78 : }
79 : matrix_info ;
80 :
81 : const matrix_info files [ ] =
82 : {
83 : LAGraph_ADJACENCY_DIRECTED, 0, "cover.mtx",
84 : LAGraph_ADJACENCY_DIRECTED, 0, "ldbc-directed-example.mtx",
85 : LAGraph_ADJACENCY_UNDIRECTED, 0, "ldbc-undirected-example.mtx",
86 : LAGraph_ADJACENCY_DIRECTED, 2, "west0067.mtx",
87 : LAGRAPH_UNKNOWN, 0, ""
88 : } ;
89 :
90 1 : void test_Graph_Print (void)
91 : {
92 1 : setup ( ) ;
93 :
94 1 : for (int k = 0 ; ; k++)
95 4 : {
96 :
97 : // load the adjacency matrix as A
98 5 : const char *aname = files [k].name ;
99 5 : LAGraph_Kind kind = files [k].kind ;
100 5 : if (strlen (aname) == 0) break;
101 4 : TEST_CASE (aname) ;
102 4 : snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
103 4 : FILE *f = fopen (filename, "r") ;
104 4 : TEST_CHECK (f != NULL) ;
105 4 : OK (LAGraph_MMRead (&A, f, msg)) ;
106 4 : OK (fclose (f)) ;
107 4 : TEST_MSG ("Loading of adjacency matrix failed") ;
108 :
109 4 : OK (LAGraph_Matrix_TypeName (atype_name, A, msg)) ;
110 4 : if (MATCHNAME (atype_name, "double"))
111 : {
112 3 : OK (GrB_Matrix_setElement (A, 3.14159265358979323, 0, 1)) ;
113 : }
114 :
115 : // create the graph
116 4 : OK (LAGraph_New (&G, &A, kind, msg)) ;
117 4 : TEST_CHECK (A == NULL) ; // A has been moved into G->A
118 :
119 : // display the graph
120 12 : for (int trial = 0 ; trial <= 1 ; trial++)
121 : {
122 8 : printf ("\n############################# TRIAL: %d\n", trial) ;
123 56 : for (int pr = 0 ; pr <= 5 ; pr++)
124 : {
125 48 : printf ("\n########### %s: pr: %d (%s)\n",
126 : aname, pr, prwhat (pr)) ;
127 48 : LAGraph_PrintLevel prl = pr ;
128 48 : OK (LAGraph_Graph_Print (G, prl, stdout, msg)) ;
129 : }
130 8 : int ok_result = (kind == LAGraph_ADJACENCY_UNDIRECTED) ?
131 8 : LAGRAPH_CACHE_NOT_NEEDED : GrB_SUCCESS ;
132 8 : int result = LAGraph_Cached_AT (G, msg) ;
133 8 : TEST_CHECK (result == ok_result) ;
134 8 : OK (LAGraph_Cached_IsSymmetricStructure (G, msg)) ;
135 8 : OK (LAGraph_Cached_NSelfEdges (G, msg)) ;
136 8 : TEST_CHECK (G->nself_edges == files [k].nself_edges) ;
137 : }
138 :
139 : // free the graph
140 4 : OK (LAGraph_Delete (&G, msg)) ;
141 4 : TEST_CHECK (G == NULL) ;
142 : }
143 :
144 1 : TEST_CHECK (prwhat (999) == NULL) ;
145 1 : teardown ( ) ;
146 1 : }
147 :
148 : //------------------------------------------------------------------------------
149 : // test_Graph_Print_failures: test error handling of LAGraph_Graph_Print
150 : //------------------------------------------------------------------------------
151 :
152 1 : void test_Graph_Print_failures (void)
153 : {
154 1 : setup ( ) ;
155 :
156 : // G cannot be NULL
157 1 : int result = LAGraph_New (NULL, NULL, 0, msg) ;
158 1 : printf ("\nresult: %d, msg: %s\n", result, msg) ;
159 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
160 :
161 : // create a graph with no adjacency matrix; this is OK, since the intent is
162 : // to create a graph for which the adjacency matrix can be defined later,
163 : // via assigning it to G->A. However, the graph will be declared invalid
164 : // by LAGraph_CheckGraph since G->A is NULL.
165 1 : OK (LAGraph_New (&G, NULL, LAGraph_ADJACENCY_UNDIRECTED, msg)) ;
166 :
167 : // G->A is NULL
168 1 : LAGraph_PrintLevel pr = LAGraph_COMPLETE_VERBOSE ;
169 1 : result = LAGraph_Graph_Print (G, pr, stdout, msg) ;
170 1 : printf ("result: %d, msg: %s\n", result, msg) ;
171 1 : TEST_CHECK (result == LAGRAPH_INVALID_GRAPH) ;
172 :
173 1 : OK (LAGraph_Delete (&G, msg)) ;
174 1 : TEST_CHECK (G == NULL) ;
175 :
176 : // valid graph
177 1 : OK (GrB_Matrix_new (&A, GrB_FP32, 5, 5)) ;
178 1 : OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_UNDIRECTED, msg)) ;
179 1 : result = LAGraph_Graph_Print (G, pr, stdout, msg) ;
180 1 : printf ("result: %d, msg: %s\n", result, msg) ;
181 1 : TEST_CHECK (result == GrB_SUCCESS) ;
182 :
183 : // mangled G->kind
184 1 : G->kind = -1 ;
185 1 : result = LAGraph_Graph_Print (G, pr, stdout, msg) ;
186 1 : printf ("result: %d, msg: %s\n", result, msg) ;
187 1 : TEST_CHECK (result == LAGRAPH_INVALID_GRAPH) ;
188 1 : G->kind = LAGraph_ADJACENCY_UNDIRECTED ;
189 :
190 : // G->AT has the wrong size
191 1 : OK (GrB_Matrix_new (&(G->AT), GrB_FP32, 6, 5)) ;
192 1 : result = LAGraph_Graph_Print (G, pr, stdout, msg) ;
193 1 : printf ("result: %d, msg: %s\n", result, msg) ;
194 1 : TEST_CHECK (result == LAGRAPH_INVALID_GRAPH) ;
195 :
196 1 : OK (GrB_free (&G->AT)) ;
197 1 : OK (GrB_Matrix_new (&(G->AT), GrB_FP32, 5, 5)) ;
198 :
199 : #if LAGRAPH_SUITESPARSE
200 : // G->AT must be held by row, not by column
201 1 : OK (GxB_set (G->AT, GxB_FORMAT, GxB_BY_COL)) ;
202 1 : result = LAGraph_Graph_Print (G, pr, stdout, msg) ;
203 1 : printf ("result: %d, msg: %s\n", result, msg) ;
204 1 : TEST_CHECK (result == LAGRAPH_INVALID_GRAPH) ;
205 : #endif
206 :
207 : // G->A and G->AT must have the same types
208 1 : OK (GrB_free (&G->AT)) ;
209 1 : OK (GrB_Matrix_new (&(G->AT), GrB_FP64, 5, 5)) ;
210 1 : result = LAGraph_Graph_Print (G, pr, stdout, msg) ;
211 1 : printf ("result: %d, msg: %s\n", result, msg) ;
212 1 : TEST_CHECK (result == LAGRAPH_INVALID_GRAPH) ;
213 :
214 1 : OK (LAGraph_Delete (&G, msg)) ;
215 1 : TEST_CHECK (G == NULL) ;
216 :
217 1 : OK (LAGraph_Delete (NULL, msg)) ;
218 1 : teardown ( ) ;
219 1 : }
220 :
221 : //-----------------------------------------------------------------------------
222 : // test_Graph_Print_brutal
223 : //-----------------------------------------------------------------------------
224 :
225 : #if LAGRAPH_SUITESPARSE
226 1 : void test_Graph_Print_brutal (void)
227 : {
228 1 : OK (LG_brutal_setup (msg)) ;
229 :
230 1 : for (int k = 0 ; ; k++)
231 4 : {
232 :
233 : // load the adjacency matrix as A
234 5 : const char *aname = files [k].name ;
235 5 : LAGraph_Kind kind = files [k].kind ;
236 5 : if (strlen (aname) == 0) break;
237 4 : TEST_CASE (aname) ;
238 4 : snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
239 4 : FILE *f = fopen (filename, "r") ;
240 4 : TEST_CHECK (f != NULL) ;
241 4 : OK (LAGraph_MMRead (&A, f, msg)) ;
242 4 : OK (fclose (f)) ;
243 4 : TEST_MSG ("Loading of adjacency matrix failed") ;
244 :
245 4 : OK (LAGraph_Matrix_TypeName (atype_name, A, msg)) ;
246 4 : if (MATCHNAME (atype_name, "double"))
247 : {
248 3 : OK (GrB_Matrix_setElement (A, 3.14159265358979323, 0, 1)) ;
249 : }
250 4 : OK (GrB_wait (A, GrB_MATERIALIZE)) ;
251 :
252 : // create the graph
253 4 : OK (LAGraph_New (&G, &A, kind, msg)) ;
254 4 : OK (LAGraph_CheckGraph (G, msg)) ;
255 :
256 : // display the graph
257 12 : for (int trial = 0 ; trial <= 1 ; trial++)
258 : {
259 8 : printf ("\n############################# TRIAL: %d\n", trial) ;
260 56 : for (int pr = 0 ; pr <= 5 ; pr++)
261 : {
262 48 : printf ("\n########### %s: pr: %d (%s)\n",
263 : aname, pr, prwhat (pr)) ;
264 48 : LAGraph_PrintLevel prl = pr ;
265 48 : if (pr == 3 || pr == 5)
266 : {
267 16 : printf ("skipped for brutal tests\n") ;
268 : }
269 : else
270 : {
271 114 : LG_BRUTAL (LAGraph_Graph_Print (G, prl, stdout, msg)) ;
272 : }
273 : }
274 8 : int ok_result = (kind == LAGraph_ADJACENCY_UNDIRECTED) ?
275 8 : LAGRAPH_CACHE_NOT_NEEDED : GrB_SUCCESS ;
276 8 : int result = LAGraph_Cached_AT (G, msg) ;
277 8 : TEST_CHECK (result == ok_result) ;
278 8 : OK (LAGraph_Cached_IsSymmetricStructure (G, msg)) ;
279 8 : OK (LAGraph_Cached_NSelfEdges (G, msg)) ;
280 : }
281 :
282 : // free the graph
283 4 : OK (LAGraph_Delete (&G, msg)) ;
284 : }
285 :
286 1 : OK (LG_brutal_teardown (msg)) ;
287 1 : }
288 : #endif
289 :
290 : //-----------------------------------------------------------------------------
291 : // TEST_LIST: the list of tasks for this entire test
292 : //-----------------------------------------------------------------------------
293 :
294 : TEST_LIST =
295 : {
296 : { "Graph_Print", test_Graph_Print },
297 : #if LAGRAPH_SUITESPARSE
298 : { "Graph_Print_brutal", test_Graph_Print_brutal },
299 : #endif
300 : { "Graph_Print_failures", test_Graph_Print_failures },
301 : { NULL, NULL }
302 : } ;
|