Line data Source code
1 : //------------------------------------------------------------------------------
2 : // LAGraph/src/test/test_Matrix_Structure.c: test LAGraph_Matrix_Structure
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 : char msg [LAGRAPH_MSG_LEN] ;
26 : GrB_Matrix A = NULL, B = NULL, C = NULL ;
27 : GrB_Vector w = NULL, u = NULL, z = NULL ;
28 : #define LEN 512
29 : char filename [LEN+1] ;
30 : char btype_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 : // test_Matrix_Structure: test LAGraph_Matrix_Structure
52 : //------------------------------------------------------------------------------
53 :
54 : const char *files [ ] =
55 : {
56 : "cover",
57 : "lp_afiro",
58 : "matrix_fp32",
59 : ""
60 : } ;
61 :
62 1 : void test_Matrix_Structure (void)
63 : {
64 1 : setup ( ) ;
65 :
66 1 : for (int k = 0 ; ; k++)
67 3 : {
68 : // load the valued matrix as A
69 4 : const char *aname = files [k] ;
70 4 : if (strlen (aname) == 0) break;
71 3 : TEST_CASE (aname) ;
72 3 : snprintf (filename, LEN, LG_DATA_DIR "%s.mtx", aname) ;
73 3 : FILE *f = fopen (filename, "r") ;
74 3 : TEST_CHECK (f != NULL) ;
75 3 : OK (LAGraph_MMRead (&A, f, msg)) ;
76 3 : OK (fclose (f)) ;
77 3 : TEST_MSG ("Loading of valued matrix failed") ;
78 :
79 : // load the structure as B
80 3 : snprintf (filename, LEN, LG_DATA_DIR "%s_structure.mtx", aname) ;
81 3 : f = fopen (filename, "r") ;
82 3 : TEST_CHECK (f != NULL) ;
83 3 : OK (LAGraph_MMRead (&B, f, msg)) ;
84 3 : OK (LAGraph_Matrix_TypeName (btype_name, B, msg)) ;
85 3 : TEST_CHECK (MATCHNAME (btype_name, "bool")) ;
86 3 : OK (fclose (f)) ;
87 3 : TEST_MSG ("Loading of structure matrix failed") ;
88 :
89 : // C = structure (A)
90 3 : OK (LAGraph_Matrix_Structure (&C, A, msg)) ;
91 :
92 : // ensure B and C are the same
93 : bool ok ;
94 3 : OK (LAGraph_Matrix_IsEqual (&ok, C, B, msg)) ;
95 3 : TEST_CHECK (ok) ;
96 3 : TEST_MSG ("Test for C and B equal failed") ;
97 :
98 3 : OK (GrB_free (&A)) ;
99 3 : OK (GrB_free (&B)) ;
100 3 : OK (GrB_free (&C)) ;
101 : }
102 1 : teardown ( ) ;
103 1 : }
104 :
105 : //------------------------------------------------------------------------------
106 : // test_Matrix_Structure_brutal
107 : //------------------------------------------------------------------------------
108 :
109 : #if LAGRAPH_SUITESPARSE
110 1 : void test_Matrix_Structure_brutal (void)
111 : {
112 1 : OK (LG_brutal_setup (msg)) ;
113 :
114 1 : for (int k = 0 ; ; k++)
115 3 : {
116 : // load the valued matrix as A
117 4 : const char *aname = files [k] ;
118 4 : if (strlen (aname) == 0) break;
119 3 : TEST_CASE (aname) ;
120 3 : snprintf (filename, LEN, LG_DATA_DIR "%s.mtx", aname) ;
121 3 : FILE *f = fopen (filename, "r") ;
122 3 : TEST_CHECK (f != NULL) ;
123 3 : OK (LAGraph_MMRead (&A, f, msg)) ;
124 3 : OK (fclose (f)) ;
125 3 : TEST_MSG ("Loading of valued matrix failed") ;
126 :
127 : // load the structure as B
128 3 : snprintf (filename, LEN, LG_DATA_DIR "%s_structure.mtx", aname) ;
129 3 : f = fopen (filename, "r") ;
130 3 : TEST_CHECK (f != NULL) ;
131 3 : OK (LAGraph_MMRead (&B, f, msg)) ;
132 3 : OK (LAGraph_Matrix_TypeName (btype_name, B, msg)) ;
133 3 : TEST_CHECK (MATCHNAME (btype_name, "bool")) ;
134 3 : OK (fclose (f)) ;
135 3 : TEST_MSG ("Loading of structure matrix failed") ;
136 :
137 : // C = structure (A)
138 22 : LG_BRUTAL (LAGraph_Matrix_Structure (&C, A, msg)) ;
139 :
140 : // ensure B and C are the same
141 : bool ok ;
142 3 : OK (LAGraph_Matrix_IsEqual (&ok, C, B, msg)) ;
143 3 : TEST_CHECK (ok) ;
144 3 : TEST_MSG ("Test for C and B equal failed") ;
145 :
146 3 : OK (GrB_free (&A)) ;
147 3 : OK (GrB_free (&B)) ;
148 3 : OK (GrB_free (&C)) ;
149 : }
150 1 : OK (LG_brutal_teardown (msg)) ;
151 1 : }
152 : #endif
153 :
154 : //------------------------------------------------------------------------------
155 : // test_Matrix_Structure_failures: test LAGraph_Matrix_Structure error handling
156 : //------------------------------------------------------------------------------
157 :
158 1 : void test_Matrix_Structure_failures (void)
159 : {
160 1 : setup ( ) ;
161 :
162 1 : C = NULL ;
163 1 : int result = LAGraph_Matrix_Structure (NULL, NULL, msg) ;
164 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
165 1 : printf ("\nmsg: [%s]\n", msg) ;
166 1 : result = LAGraph_Matrix_Structure (&C, NULL, msg) ;
167 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
168 1 : printf ("msg: [%s]\n", msg) ;
169 1 : TEST_CHECK (C == NULL) ;
170 :
171 1 : teardown ( ) ;
172 1 : }
173 :
174 : //-----------------------------------------------------------------------------
175 : // TEST_LIST: the list of tasks for this entire test
176 : //-----------------------------------------------------------------------------
177 :
178 : TEST_LIST =
179 : {
180 : { "Matrix_Structure", test_Matrix_Structure },
181 : { "Matrix_Structure_failures", test_Matrix_Structure_failures },
182 : #if LAGRAPH_SUITESPARSE
183 : { "Matrix_Structure_brutal", test_Matrix_Structure_brutal },
184 : #endif
185 : { NULL, NULL }
186 : } ;
|