Line data Source code
1 : //------------------------------------------------------------------------------
2 : // LAGraph/src/test/test_IsEqual.c: test LAGraph_*_IsEqual
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 : int status ;
25 : GrB_Info info ;
26 : char msg [LAGRAPH_MSG_LEN] ;
27 : GrB_Matrix A = NULL, B = NULL ;
28 : GrB_Vector u = NULL, v = NULL ;
29 : GrB_Type atype = NULL ;
30 : #define LEN 512
31 : char filename [LEN+1] ;
32 : char atype_name [LAGRAPH_MAX_NAME_LEN] ;
33 :
34 : //------------------------------------------------------------------------------
35 : // test matrices
36 : //------------------------------------------------------------------------------
37 :
38 : typedef struct
39 : {
40 : bool isequal ;
41 : bool isequal0 ;
42 : const char *matrix1 ;
43 : const char *matrix2 ;
44 : }
45 : matrix_info ;
46 :
47 : const matrix_info files [ ] =
48 : {
49 : // iseq matrix1 matrix2
50 : { 0, 0, "A.mtx" , "cover.mtx" },
51 : { 0, 1, "A.mtx" , "A2.mtx" },
52 : { 0, 0, "cover.mtx" , "cover_structure.mtx" },
53 : { 0, 0, "cover.mtx" , "cover_structure.mtx" },
54 : { 1, 1, "LFAT5.mtx" , "LFAT5.mtx" },
55 : { 0, 0, "sample2.mtx" , "sample.mtx" },
56 : { 1, 1, "sample.mtx" , "sample.mtx" },
57 : { 1, 1, "matrix_int32.mtx", "matrix_int32.mtx" },
58 : { 1, 1, "matrix_int32.mtx", "matrix_int32.mtx" },
59 : { 0, 0, "matrix_int32.mtx", "matrix_int64.mtx" },
60 : { 0, 0, "matrix_int32.mtx", "matrix_int64.mtx" },
61 : { 1, 1, "west0067.mtx" , "west0067_jumbled.mtx" },
62 : { 1, 1, "west0067.mtx" , "west0067_noheader.mtx"},
63 : { 0, 0, "LFAT5.mtx" , "west0067.mtx" },
64 : { 0, 0, "empty.mtx" , "full.mtx" },
65 : { 1, 1, "full.mtx" , "full_noheader.mtx" },
66 : { 0, 0, "" , "" }
67 : } ;
68 :
69 : //------------------------------------------------------------------------------
70 : // setup: start a test
71 : //------------------------------------------------------------------------------
72 :
73 3 : void setup (void)
74 : {
75 3 : OK (LAGraph_Init (msg)) ;
76 : // OK (LG_SET_BURBLE (true)) ;
77 3 : }
78 :
79 : //------------------------------------------------------------------------------
80 : // teardown: finalize a test
81 : //------------------------------------------------------------------------------
82 :
83 3 : void teardown (void)
84 : {
85 3 : OK (LAGraph_Finalize (msg)) ;
86 3 : }
87 :
88 : //------------------------------------------------------------------------------
89 : // test_IsEqual: test LAGraph_Matrix_IsEqual
90 : //------------------------------------------------------------------------------
91 :
92 1 : void test_IsEqual (void)
93 : {
94 :
95 : //--------------------------------------------------------------------------
96 : // start up the test
97 : //--------------------------------------------------------------------------
98 :
99 1 : setup ( ) ;
100 1 : printf ("\nTesting IsEqual:\n") ;
101 :
102 1 : for (int k = 0 ; ; k++)
103 16 : {
104 :
105 : //----------------------------------------------------------------------
106 : // load in the kth pair of files
107 : //----------------------------------------------------------------------
108 :
109 17 : const char *aname = files [k].matrix1 ;
110 17 : const char *bname = files [k].matrix2 ;
111 17 : const bool isequal = files [k].isequal ;
112 17 : const bool isequal0 = files [k].isequal0 ;
113 17 : if (strlen (aname) == 0) break;
114 16 : TEST_CASE (aname) ;
115 16 : printf ("test %2d: %s %s\n", k, aname, bname) ;
116 :
117 16 : snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
118 16 : FILE *f = fopen (filename, "r") ;
119 16 : TEST_CHECK (f != NULL) ;
120 16 : OK (LAGraph_MMRead (&A, f, msg)) ;
121 16 : OK (fclose (f)) ;
122 16 : TEST_MSG ("Failed to load %s\n", aname) ;
123 : GrB_Index ancols ;
124 16 : OK (GrB_Matrix_ncols (&ancols, A)) ;
125 :
126 16 : snprintf (filename, LEN, LG_DATA_DIR "%s", bname) ;
127 16 : f = fopen (filename, "r") ;
128 16 : TEST_CHECK (f != NULL) ;
129 16 : OK (LAGraph_MMRead (&B, f, msg)) ;
130 16 : OK (fclose (f)) ;
131 16 : TEST_MSG ("Failed to load %s\n", bname) ;
132 : GrB_Index bncols ;
133 16 : OK (GrB_Matrix_ncols (&bncols, B)) ;
134 :
135 : //----------------------------------------------------------------------
136 : // compare the two matrices
137 : //----------------------------------------------------------------------
138 :
139 16 : bool same = false ;
140 :
141 16 : OK (LAGraph_Matrix_IsEqual (&same, A, B, msg)) ;
142 16 : TEST_CHECK (same == isequal) ;
143 :
144 16 : OK (LAGraph_Matrix_IsEqual (&same, A, A, msg)) ;
145 16 : TEST_CHECK (same == true) ;
146 :
147 : //----------------------------------------------------------------------
148 : // compare the two matrices with a given op
149 : //----------------------------------------------------------------------
150 :
151 16 : OK (LAGraph_Matrix_TypeName (atype_name, A, msg)) ;
152 16 : OK (LAGraph_TypeFromName (&atype, atype_name, msg)) ;
153 :
154 16 : GrB_BinaryOp op = NULL ;
155 16 : if (atype == GrB_BOOL ) op = GrB_EQ_BOOL ;
156 12 : else if (atype == GrB_INT8 ) op = GrB_EQ_INT8 ;
157 12 : else if (atype == GrB_INT16 ) op = GrB_EQ_INT16 ;
158 12 : else if (atype == GrB_INT32 ) op = GrB_EQ_INT32 ;
159 5 : else if (atype == GrB_INT64 ) op = GrB_EQ_INT64 ;
160 5 : else if (atype == GrB_UINT8 ) op = GrB_EQ_UINT8 ;
161 5 : else if (atype == GrB_UINT16) op = GrB_EQ_UINT16 ;
162 5 : else if (atype == GrB_UINT32) op = GrB_EQ_UINT32 ;
163 5 : else if (atype == GrB_UINT64) op = GrB_EQ_UINT64 ;
164 5 : else if (atype == GrB_FP32 ) op = GrB_EQ_FP32 ;
165 5 : else if (atype == GrB_FP64 ) op = GrB_EQ_FP64 ;
166 :
167 16 : OK (LAGraph_Matrix_IsEqualOp (&same, A, B, op, msg)) ;
168 16 : TEST_CHECK (same == isequal) ;
169 :
170 16 : OK (LAGraph_Matrix_IsEqualOp (&same, A, A, op, msg)) ;
171 16 : TEST_CHECK (same == true) ;
172 :
173 : //----------------------------------------------------------------------
174 : // compare two vectors
175 : //----------------------------------------------------------------------
176 :
177 16 : OK (GrB_Vector_new (&u, atype, ancols)) ;
178 16 : OK (GrB_Vector_new (&v, atype, bncols)) ;
179 16 : OK (GrB_Col_extract (u, NULL, NULL, A, GrB_ALL, ancols, 0,
180 : GrB_DESC_T0)) ;
181 16 : OK (GrB_Col_extract (v, NULL, NULL, B, GrB_ALL, bncols, 0,
182 : GrB_DESC_T0)) ;
183 :
184 16 : OK (LAGraph_Vector_IsEqual (&same, u, v, msg)) ;
185 16 : TEST_CHECK (same == isequal0) ;
186 :
187 16 : OK (LAGraph_Vector_IsEqual (&same, u, u, msg)) ;
188 16 : TEST_CHECK (same == true) ;
189 :
190 16 : OK (LAGraph_Vector_IsEqual (&same, u, u, msg)) ;
191 16 : TEST_CHECK (same == true) ;
192 :
193 16 : OK (GrB_free (&u)) ;
194 16 : OK (GrB_free (&v)) ;
195 16 : OK (GrB_free (&A)) ;
196 16 : OK (GrB_free (&B)) ;
197 : }
198 :
199 : //--------------------------------------------------------------------------
200 : // finish the test
201 : //--------------------------------------------------------------------------
202 :
203 1 : teardown ( ) ;
204 1 : }
205 :
206 : //------------------------------------------------------------------------------
207 : // test_IsEqual_brutal:
208 : //------------------------------------------------------------------------------
209 :
210 : #if LG_BRUTAL_TESTS
211 1 : void test_IsEqual_brutal (void)
212 : {
213 :
214 : //--------------------------------------------------------------------------
215 : // start up the test
216 : //--------------------------------------------------------------------------
217 :
218 1 : OK (LG_brutal_setup (msg)) ;
219 1 : printf ("\nTesting IsEqual:\n") ;
220 :
221 1 : for (int k = 0 ; ; k++)
222 16 : {
223 :
224 : //----------------------------------------------------------------------
225 : // load in the kth pair of files
226 : //----------------------------------------------------------------------
227 :
228 17 : const char *aname = files [k].matrix1 ;
229 17 : const char *bname = files [k].matrix2 ;
230 17 : const bool isequal = files [k].isequal ;
231 17 : const bool isequal0 = files [k].isequal0 ;
232 17 : if (strlen (aname) == 0) break;
233 16 : TEST_CASE (aname) ;
234 16 : printf ("test %2d: %s %s\n", k, aname, bname) ;
235 :
236 16 : snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
237 16 : FILE *f = fopen (filename, "r") ;
238 16 : TEST_CHECK (f != NULL) ;
239 16 : OK (LAGraph_MMRead (&A, f, msg)) ;
240 16 : OK (fclose (f)) ;
241 16 : TEST_MSG ("Failed to load %s\n", aname) ;
242 : GrB_Index ancols ;
243 16 : OK (GrB_Matrix_ncols (&ancols, A)) ;
244 :
245 16 : snprintf (filename, LEN, LG_DATA_DIR "%s", bname) ;
246 16 : f = fopen (filename, "r") ;
247 16 : TEST_CHECK (f != NULL) ;
248 16 : OK (LAGraph_MMRead (&B, f, msg)) ;
249 16 : OK (fclose (f)) ;
250 16 : TEST_MSG ("Failed to load %s\n", bname) ;
251 : GrB_Index bncols ;
252 16 : OK (GrB_Matrix_ncols (&bncols, B)) ;
253 :
254 : //----------------------------------------------------------------------
255 : // compare the two matrices
256 : //----------------------------------------------------------------------
257 :
258 16 : bool same = false ;
259 :
260 59 : LG_BRUTAL (LAGraph_Matrix_IsEqual (&same, A, B, msg)) ;
261 16 : TEST_CHECK (same == isequal) ;
262 :
263 16 : LG_BRUTAL (LAGraph_Matrix_IsEqual (&same, A, A, msg)) ;
264 16 : TEST_CHECK (same == true) ;
265 :
266 : //----------------------------------------------------------------------
267 : // compare the two matrices with a given op
268 : //----------------------------------------------------------------------
269 :
270 16 : LG_BRUTAL (LAGraph_Matrix_TypeName (atype_name, A, msg)) ;
271 16 : LG_BRUTAL (LAGraph_TypeFromName (&atype, atype_name, msg)) ;
272 :
273 16 : GrB_BinaryOp op = NULL ;
274 16 : if (atype == GrB_BOOL ) op = GrB_EQ_BOOL ;
275 12 : else if (atype == GrB_INT8 ) op = GrB_EQ_INT8 ;
276 12 : else if (atype == GrB_INT16 ) op = GrB_EQ_INT16 ;
277 12 : else if (atype == GrB_INT32 ) op = GrB_EQ_INT32 ;
278 5 : else if (atype == GrB_INT64 ) op = GrB_EQ_INT64 ;
279 5 : else if (atype == GrB_UINT8 ) op = GrB_EQ_UINT8 ;
280 5 : else if (atype == GrB_UINT16) op = GrB_EQ_UINT16 ;
281 5 : else if (atype == GrB_UINT32) op = GrB_EQ_UINT32 ;
282 5 : else if (atype == GrB_UINT64) op = GrB_EQ_UINT64 ;
283 5 : else if (atype == GrB_FP32 ) op = GrB_EQ_FP32 ;
284 5 : else if (atype == GrB_FP64 ) op = GrB_EQ_FP64 ;
285 :
286 79 : LG_BRUTAL (LAGraph_Matrix_IsEqualOp (&same, A, B, op, msg)) ;
287 16 : TEST_CHECK (same == isequal) ;
288 :
289 16 : LG_BRUTAL (LAGraph_Matrix_IsEqualOp (&same, A, A, op, msg)) ;
290 16 : TEST_CHECK (same == true) ;
291 :
292 : //----------------------------------------------------------------------
293 : // compare two vectors
294 : //----------------------------------------------------------------------
295 :
296 48 : LG_BRUTAL (GrB_Vector_new (&u, atype, ancols)) ;
297 48 : LG_BRUTAL (GrB_Vector_new (&v, atype, bncols)) ;
298 78 : LG_BRUTAL (GrB_Col_extract (u, NULL, NULL, A, GrB_ALL, ancols, 0,
299 : GrB_DESC_T0)) ;
300 86 : LG_BRUTAL (GrB_Col_extract (v, NULL, NULL, B, GrB_ALL, bncols, 0,
301 : GrB_DESC_T0)) ;
302 :
303 63 : LG_BRUTAL (LAGraph_Vector_IsEqual (&same, u, v, msg)) ;
304 16 : TEST_CHECK (same == isequal0) ;
305 :
306 16 : LG_BRUTAL (LAGraph_Vector_IsEqual (&same, u, u, msg)) ;
307 16 : TEST_CHECK (same == true) ;
308 :
309 16 : LG_BRUTAL (LAGraph_Vector_IsEqual (&same, u, u, msg)) ;
310 16 : TEST_CHECK (same == true) ;
311 :
312 16 : LG_BRUTAL (GrB_free (&u)) ;
313 16 : OK (GrB_free (&v)) ;
314 16 : OK (GrB_free (&A)) ;
315 16 : OK (GrB_free (&B)) ;
316 : }
317 :
318 : //--------------------------------------------------------------------------
319 : // finish the test
320 : //--------------------------------------------------------------------------
321 :
322 1 : OK (LG_brutal_teardown (msg)) ;
323 1 : }
324 : #endif
325 :
326 : //------------------------------------------------------------------------------
327 : // test_IsEqual_failures: test error handling of LAGraph_Matrix_IsEqual*
328 : //------------------------------------------------------------------------------
329 :
330 : typedef int myint ;
331 :
332 1 : void test_IsEqual_failures (void)
333 : {
334 1 : setup ( ) ;
335 1 : printf ("\nTest IsEqual: error handling and special cases\n") ;
336 :
337 1 : bool same = false ;
338 : // not a failure, but a special case:
339 1 : OK (LAGraph_Matrix_IsEqual (&same, NULL, NULL, msg)) ;
340 1 : TEST_CHECK (same == true) ;
341 :
342 1 : OK (LAGraph_Vector_IsEqual (&same, NULL, NULL, msg)) ;
343 1 : TEST_CHECK (same == true) ;
344 :
345 1 : int result = LAGraph_Matrix_IsEqual (NULL, NULL, NULL, msg) ;
346 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
347 1 : printf ("msg: %s\n", msg) ;
348 :
349 1 : result = LAGraph_Matrix_IsEqual (NULL, NULL, NULL, msg) ;
350 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
351 1 : printf ("msg: %s\n", msg) ;
352 :
353 1 : OK (GrB_Matrix_new (&A, GrB_BOOL, 2, 2)) ;
354 1 : OK (GrB_Matrix_new (&B, GrB_BOOL, 2, 2)) ;
355 :
356 1 : OK (GrB_Vector_new (&u, GrB_BOOL, 2)) ;
357 1 : OK (GrB_Vector_new (&v, GrB_BOOL, 2)) ;
358 :
359 1 : result = LAGraph_Matrix_IsEqual (NULL, A, B, msg) ;
360 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
361 1 : printf ("msg: %s\n", msg) ;
362 :
363 1 : result = LAGraph_Matrix_IsEqualOp (&same, A, B, NULL, msg) ;
364 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
365 1 : printf ("msg: %s\n", msg) ;
366 :
367 1 : result = LAGraph_Vector_IsEqual (NULL, u, v, msg) ;
368 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
369 1 : printf ("msg: %s\n", msg) ;
370 :
371 1 : result = LAGraph_Vector_IsEqualOp (&same, u, v, NULL, msg) ;
372 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
373 1 : printf ("msg: %s\n", msg) ;
374 :
375 1 : result = LAGraph_Matrix_IsEqual (NULL, A, B, msg) ;
376 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
377 1 : printf ("msg: %s\n", msg) ;
378 :
379 1 : result = LAGraph_Matrix_IsEqual (NULL, A, B, msg) ;
380 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
381 1 : printf ("msg: %s\n", msg) ;
382 :
383 1 : OK (LAGraph_Matrix_IsEqual (&same, A, B, msg)) ;
384 1 : TEST_CHECK (same == true) ;
385 :
386 1 : OK (GrB_free (&u)) ;
387 1 : OK (GrB_free (&v)) ;
388 1 : OK (GrB_free (&A)) ;
389 1 : OK (GrB_free (&B)) ;
390 1 : teardown ( ) ;
391 1 : }
392 :
393 : //------------------------------------------------------------------------------
394 : // test_Vector_IsEqual: test LAGraph_Vector_isEqual
395 : //------------------------------------------------------------------------------
396 :
397 1 : void test_Vector_IsEqual (void)
398 : {
399 1 : setup ( ) ;
400 :
401 1 : bool same = false ;
402 1 : OK (LAGraph_Vector_IsEqualOp (&same, NULL, NULL, GrB_EQ_BOOL, msg)) ;
403 1 : TEST_CHECK (same == true) ;
404 :
405 1 : OK (GrB_Vector_new (&u, GrB_BOOL, 3)) ;
406 1 : OK (GrB_Vector_new (&v, GrB_BOOL, 2)) ;
407 :
408 1 : OK (LAGraph_Vector_IsEqualOp (&same, u, v, GrB_EQ_BOOL, msg)) ;
409 1 : TEST_CHECK (same == false) ;
410 :
411 1 : OK (GrB_free (&u)) ;
412 1 : OK (GrB_Vector_new (&u, GrB_BOOL, 2)) ;
413 :
414 1 : OK (LAGraph_Vector_IsEqualOp (&same, u, v, GrB_EQ_BOOL, msg)) ;
415 1 : TEST_CHECK (same == true) ;
416 :
417 1 : OK (GrB_Vector_setElement (u, true, 0)) ;
418 1 : OK (GrB_Vector_setElement (v, true, 1)) ;
419 1 : OK (LAGraph_Vector_IsEqualOp (&same, u, v, GrB_EQ_BOOL, msg)) ;
420 1 : TEST_CHECK (same == false) ;
421 :
422 1 : OK (LAGraph_Vector_IsEqual (&same, u, v, msg)) ;
423 1 : TEST_CHECK (same == false) ;
424 :
425 1 : OK (GrB_free (&u)) ;
426 1 : OK (GrB_free (&v)) ;
427 :
428 1 : OK (GrB_Vector_new (&u, GrB_BOOL, 3)) ;
429 1 : OK (GrB_Vector_new (&v, GrB_FP32, 3)) ;
430 1 : OK (LAGraph_Vector_IsEqual (&same, u, v, msg)) ;
431 1 : TEST_CHECK (same == false) ;
432 :
433 1 : OK (GrB_free (&u)) ;
434 1 : OK (GrB_free (&v)) ;
435 :
436 1 : teardown ( ) ;
437 1 : }
438 :
439 : //------------------------------------------------------------------------------
440 : // TEST_LIST: the list of tasks for this entire test
441 : //------------------------------------------------------------------------------
442 :
443 : TEST_LIST =
444 : {
445 : { "IsEqual", test_IsEqual },
446 : { "Vector_IsEqual", test_Vector_IsEqual },
447 : { "IsEqual_failures", test_IsEqual_failures },
448 : #if LG_BRUTAL_TESTS
449 : { "IsEqual_brutal", test_IsEqual_brutal },
450 : #endif
451 : { NULL, NULL }
452 : } ;
|