Line data Source code
1 : //------------------------------------------------------------------------------
2 : // LAGraph/src/test/test_Cached_AT.c: test LAGraph_Cached_AT
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 :
20 : //------------------------------------------------------------------------------
21 : // global variables
22 : //------------------------------------------------------------------------------
23 :
24 : LAGraph_Graph G = NULL ;
25 : char msg [LAGRAPH_MSG_LEN] ;
26 : GrB_Matrix A = NULL, B = NULL ;
27 : GrB_Type atype = 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 1 : void setup (void)
37 : {
38 1 : OK (LAGraph_Init (msg)) ;
39 1 : }
40 :
41 : //------------------------------------------------------------------------------
42 : // teardown: finalize a test
43 : //------------------------------------------------------------------------------
44 :
45 1 : void teardown (void)
46 : {
47 1 : OK (LAGraph_Finalize (msg)) ;
48 1 : }
49 :
50 : //------------------------------------------------------------------------------
51 : // test_Cached_AT: test LAGraph_Cached_AT
52 : //------------------------------------------------------------------------------
53 :
54 : typedef struct
55 : {
56 : LAGraph_Kind kind ;
57 : const char *name ;
58 : }
59 : matrix_info ;
60 :
61 : const matrix_info files [ ] =
62 : {
63 : LAGraph_ADJACENCY_DIRECTED, "cover.mtx",
64 : LAGraph_ADJACENCY_DIRECTED, "ldbc-directed-example.mtx",
65 : LAGraph_ADJACENCY_UNDIRECTED, "ldbc-undirected-example.mtx",
66 : LAGRAPH_UNKNOWN, ""
67 : } ;
68 :
69 : //-----------------------------------------------------------------------------
70 : // test_Cached_AT
71 : //-----------------------------------------------------------------------------
72 :
73 1 : void test_Cached_AT (void)
74 : {
75 1 : setup ( ) ;
76 :
77 1 : for (int k = 0 ; ; k++)
78 3 : {
79 :
80 : // load the matrix as A
81 4 : const char *aname = files [k].name ;
82 4 : int kind = files [k].kind ;
83 4 : if (strlen (aname) == 0) break;
84 3 : TEST_CASE (aname) ;
85 3 : snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
86 3 : FILE *f = fopen (filename, "r") ;
87 3 : TEST_CHECK (f != NULL) ;
88 3 : OK (LAGraph_MMRead (&A, f, msg)) ;
89 3 : OK (fclose (f)) ;
90 3 : TEST_MSG ("Loading of adjacency matrix failed") ;
91 :
92 : // construct the graph G with adjacency matrix A
93 3 : OK (LAGraph_New (&G, &A, kind, msg)) ;
94 3 : TEST_CHECK (A == NULL) ;
95 :
96 : // create the G->AT cached property
97 3 : int ok_result = (kind == LAGraph_ADJACENCY_UNDIRECTED) ?
98 3 : LAGRAPH_CACHE_NOT_NEEDED : GrB_SUCCESS ;
99 3 : int result = LAGraph_Cached_AT (G, msg) ;
100 3 : TEST_CHECK (result == ok_result) ;
101 :
102 : // try to create it again; this should safely do nothing
103 3 : result = LAGraph_Cached_AT (G, msg) ;
104 3 : TEST_CHECK (result == ok_result) ;
105 :
106 : // check the result
107 3 : if (kind == LAGraph_ADJACENCY_UNDIRECTED)
108 : {
109 1 : TEST_CHECK (G->AT == NULL) ;
110 : }
111 : else
112 : {
113 : // ensure G->A and G->AT are transposed of each other;
114 : // B = (G->AT)'
115 : GrB_Index nrows, ncols ;
116 2 : OK (GrB_Matrix_nrows (&nrows, G->A)) ;
117 2 : OK (GrB_Matrix_nrows (&ncols, G->A)) ;
118 2 : OK (LAGraph_Matrix_TypeName (atype_name, G->A, msg)) ;
119 2 : OK (LAGraph_TypeFromName (&atype, atype_name, msg)) ;
120 2 : OK (GrB_Matrix_new (&B, atype, nrows, ncols)) ;
121 2 : OK (GrB_transpose (B, NULL, NULL, G->AT, NULL)) ;
122 :
123 : // ensure B and G->A are the same
124 : bool ok ;
125 2 : OK (LAGraph_Matrix_IsEqual (&ok, G->A, B, msg)) ;
126 2 : TEST_CHECK (ok) ;
127 2 : TEST_MSG ("Test for G->A and B equal failed") ;
128 2 : OK (GrB_free (&B)) ;
129 : }
130 :
131 3 : OK (LAGraph_Delete (&G, msg)) ;
132 : }
133 :
134 1 : teardown ( ) ;
135 1 : }
136 :
137 : //-----------------------------------------------------------------------------
138 : // test_Cached_AT_brutal
139 : //-----------------------------------------------------------------------------
140 :
141 : #if LAGRAPH_SUITESPARSE
142 1 : void test_Cached_AT_brutal (void)
143 : {
144 1 : OK (LG_brutal_setup (msg)) ;
145 :
146 1 : for (int k = 0 ; ; k++)
147 3 : {
148 :
149 : // load the matrix as A
150 4 : const char *aname = files [k].name ;
151 4 : int kind = files [k].kind ;
152 4 : if (strlen (aname) == 0) break;
153 3 : TEST_CASE (aname) ;
154 3 : snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
155 3 : FILE *f = fopen (filename, "r") ;
156 3 : TEST_CHECK (f != NULL) ;
157 3 : OK (LAGraph_MMRead (&A, f, msg)) ;
158 3 : OK (fclose (f)) ;
159 3 : TEST_MSG ("Loading of adjacency matrix failed") ;
160 :
161 : // construct the graph G with adjacency matrix A
162 3 : OK (LAGraph_New (&G, &A, kind, msg)) ;
163 3 : TEST_CHECK (A == NULL) ;
164 :
165 : // create the G->AT cached property
166 13 : LG_BRUTAL (LAGraph_Cached_AT (G, msg)) ;
167 :
168 : // try to create it again; this should safely do nothing
169 3 : LG_BRUTAL (LAGraph_Cached_AT (G, msg)) ;
170 :
171 : // check the result
172 3 : if (kind == LAGraph_ADJACENCY_UNDIRECTED)
173 : {
174 1 : TEST_CHECK (G->AT == NULL) ;
175 : }
176 : else
177 : {
178 : // ensure G->A and G->AT are transposed of each other;
179 : // B = (G->AT)'
180 : GrB_Index nrows, ncols ;
181 2 : OK (GrB_Matrix_nrows (&nrows, G->A)) ;
182 2 : OK (GrB_Matrix_nrows (&ncols, G->A)) ;
183 2 : OK (LAGraph_Matrix_TypeName (atype_name, G->A, msg)) ;
184 2 : OK (LAGraph_TypeFromName (&atype, atype_name, msg)) ;
185 2 : OK (GrB_Matrix_new (&B, atype, nrows, ncols)) ;
186 2 : OK (GrB_transpose (B, NULL, NULL, G->AT, NULL)) ;
187 :
188 : // ensure B and G->A are the same
189 : bool ok ;
190 12 : LG_BRUTAL (LAGraph_Matrix_IsEqual (&ok, G->A, B, msg)) ;
191 2 : TEST_CHECK (ok) ;
192 2 : TEST_MSG ("Test for G->A and B equal failed") ;
193 2 : OK (GrB_free (&B)) ;
194 : }
195 :
196 3 : OK (LAGraph_Delete (&G, msg)) ;
197 : }
198 :
199 1 : OK (LG_brutal_teardown (msg)) ;
200 1 : }
201 : #endif
202 :
203 : //-----------------------------------------------------------------------------
204 : // TEST_LIST: the list of tasks for this entire test
205 : //-----------------------------------------------------------------------------
206 :
207 : TEST_LIST =
208 : {
209 : { "test_AT", test_Cached_AT },
210 : #if LAGRAPH_SUITESPARSE
211 : { "test_AT_brutal", test_Cached_AT_brutal },
212 : #endif
213 : { NULL, NULL }
214 : } ;
|