Line data Source code
1 : //----------------------------------------------------------------------------
2 : // LAGraph/src/test/test_cdlp.c: test cases for CDLP
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 : // todo: write a simple cdlp method, as LG_check_cdlp, and compare its results
19 : // with LAGraph_cdlp
20 :
21 : #include <stdio.h>
22 : #include <acutest.h>
23 :
24 : #include <LAGraphX.h>
25 : #include <LAGraph_test.h>
26 :
27 : char msg [LAGRAPH_MSG_LEN] ;
28 : LAGraph_Graph G = NULL ;
29 : GrB_Matrix A = NULL ;
30 : #define LEN 512
31 : char filename [LEN+1] ;
32 :
33 : typedef struct
34 : {
35 : const char *name ;
36 : }
37 : matrix_info ;
38 :
39 : const matrix_info files [ ] =
40 : {
41 : { "A.mtx" },
42 : { "jagmesh7.mtx" },
43 : { "west0067.mtx" }, // unsymmetric
44 : { "bcsstk13.mtx" },
45 : { "karate.mtx" },
46 : { "ldbc-cdlp-undirected-example.mtx" },
47 : { "ldbc-undirected-example-bool.mtx" },
48 : { "ldbc-undirected-example-unweighted.mtx" },
49 : { "ldbc-undirected-example.mtx" },
50 : { "ldbc-wcc-example.mtx" },
51 : { "" },
52 : } ;
53 :
54 : //****************************************************************************
55 1 : void test_cdlp (void)
56 : {
57 1 : LAGraph_Init (msg) ;
58 :
59 1 : for (int k = 0 ; ; k++)
60 10 : {
61 : // load the matrix as A
62 11 : const char *aname = files [k].name ;
63 11 : if (strlen (aname) == 0) break;
64 10 : printf ("\n================================== %s:\n", aname) ;
65 10 : TEST_CASE (aname) ;
66 10 : snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
67 10 : FILE *f = fopen (filename, "r") ;
68 10 : TEST_CHECK (f != NULL) ;
69 10 : OK (LAGraph_MMRead (&A, f, msg)) ;
70 10 : fclose (f) ;
71 :
72 : // construct a directed graph G with adjacency matrix A
73 10 : OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_DIRECTED, msg)) ;
74 10 : TEST_CHECK (A == NULL) ;
75 :
76 : // check for self-edges
77 10 : OK (LAGraph_DeleteSelfEdges (G, msg)) ;
78 10 : OK (LAGraph_Cached_IsSymmetricStructure (G, msg)) ;
79 :
80 10 : GrB_Vector c = NULL ;
81 :
82 : // compute the communities with LAGraph_cdlp
83 10 : OK (LAGraph_cdlp (&c, G, 100, msg)) ;
84 : GrB_Index n ;
85 10 : OK (GrB_Vector_size (&n, c)) ;
86 10 : LAGraph_PrintLevel pr = (n <= 100) ? LAGraph_COMPLETE : LAGraph_SHORT ;
87 10 : printf ("\ncdlp (computed result):\n") ;
88 10 : OK (LAGraph_Vector_Print (c, pr, stdout, msg)) ;
89 :
90 : // compute with another method
91 10 : GrB_Vector cgood = NULL ;
92 10 : OK (LAGraph_cdlp_withsort(&cgood, G, 100, msg)) ;
93 10 : OK (GrB_wait (cgood, GrB_MATERIALIZE)) ;
94 10 : printf ("\ncdlp (known result):\n") ;
95 10 : OK (LAGraph_Vector_Print (cgood, pr, stdout, msg)) ;
96 10 : bool ok = false ;
97 10 : OK (LAGraph_Vector_IsEqual (&ok, c, cgood, msg)) ;
98 10 : TEST_CHECK (ok) ;
99 10 : OK (GrB_free (&cgood)) ;
100 :
101 10 : printf ("\ncdlp:\n") ;
102 10 : OK (LAGraph_Vector_Print (c, pr, stdout, msg)) ;
103 10 : OK (GrB_free (&c)) ;
104 :
105 10 : OK (LAGraph_Delete (&G, msg)) ;
106 : }
107 :
108 1 : LAGraph_Finalize (msg) ;
109 1 : }
110 :
111 : //------------------------------------------------------------------------------
112 : // test_errors
113 : //------------------------------------------------------------------------------
114 :
115 1 : void test_errors (void)
116 : {
117 1 : LAGraph_Init (msg) ;
118 :
119 1 : snprintf (filename, LEN, LG_DATA_DIR "%s", "karate.mtx") ;
120 1 : FILE *f = fopen (filename, "r") ;
121 1 : TEST_CHECK (f != NULL) ;
122 1 : OK (LAGraph_MMRead (&A, f, msg)) ;
123 1 : TEST_MSG ("Loading of adjacency matrix failed") ;
124 1 : fclose (f) ;
125 :
126 : // construct an undirected graph G with adjacency matrix A
127 1 : OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_UNDIRECTED, msg)) ;
128 1 : TEST_CHECK (A == NULL) ;
129 :
130 1 : OK (LAGraph_DeleteSelfEdges (G, msg)) ;
131 1 : OK (LAGraph_Cached_IsSymmetricStructure (G, msg)) ;
132 :
133 1 : GrB_Vector c = NULL ;
134 :
135 : // c is NULL
136 1 : int result = LAGraph_cdlp (NULL, G, 100, msg) ;
137 1 : printf ("\nresult: %d\n", result) ;
138 1 : TEST_CHECK (result == GrB_NULL_POINTER) ;
139 :
140 1 : OK (LAGraph_Delete (&G, msg)) ;
141 1 : LAGraph_Finalize (msg) ;
142 1 : }
143 :
144 : //****************************************************************************
145 :
146 : TEST_LIST = {
147 : {"cdlp", test_cdlp},
148 : {"cdlp_errors", test_errors},
149 : {NULL, NULL}
150 : };
|