LCOV - code coverage report
Current view: top level - experimental/test - test_KTruss.c (source / functions) Hit Total Coverage
Test: LAGraph code coverage report. Commit id: cc56ed4. Current time (UTC): 2024-08-30T17:14:30Z Lines: 89 89 100.0 %
Date: 2024-08-30 17:16:41 Functions: 2 2 100.0 %

          Line data    Source code
       1             : //----------------------------------------------------------------------------
       2             : // LAGraph/experimental/test/test_KTruss.c: test cases for k-truss
       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 <stdio.h>
      19             : #include <acutest.h>
      20             : 
      21             : #include "LAGraphX.h"
      22             : #include "LAGraph_test.h"
      23             : #include "LG_Xtest.h"
      24             : 
      25             : char msg [LAGRAPH_MSG_LEN] ;
      26             : LAGraph_Graph G = NULL ;
      27             : GrB_Matrix A = NULL ;
      28             : GrB_Matrix C1 = NULL, C2 = NULL ;
      29             : #define LEN 512
      30             : char filename [LEN+1] ;
      31             : 
      32             : typedef struct
      33             : {
      34             :     uint32_t ntriangles ;
      35             :     const char *name ;
      36             : }
      37             : matrix_info ;
      38             : 
      39             : const matrix_info files [ ] =
      40             : {
      41             :     {     11, "A.mtx" },
      42             :     {   2016, "jagmesh7.mtx" },
      43             : //  { 342300, "bcsstk13.mtx" },
      44             :     {     45, "karate.mtx" },
      45             :     {      6, "ldbc-cdlp-undirected-example.mtx" },
      46             :     {      4, "ldbc-undirected-example-bool.mtx" },
      47             :     {      4, "ldbc-undirected-example-unweighted.mtx" },
      48             :     {      4, "ldbc-undirected-example.mtx" },
      49             :     {      5, "ldbc-wcc-example.mtx" },
      50             :     { 0, "" },
      51             : } ;
      52             : 
      53             : //****************************************************************************
      54           1 : void test_ktruss (void)
      55             : {
      56           1 :     LAGraph_Init (msg) ;
      57             : 
      58           1 :     for (int id = 0 ; ; id++)
      59           8 :     {
      60             : 
      61             :         // load the matrix as A
      62           9 :         const char *aname = files [id].name ;
      63           9 :         uint32_t ntriangles = files [id].ntriangles ;
      64           9 :         if (strlen (aname) == 0) break;
      65           8 :         printf ("\n================================== %s:\n", aname) ;
      66           8 :         TEST_CASE (aname) ;
      67           8 :         snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
      68           8 :         FILE *f = fopen (filename, "r") ;
      69           8 :         TEST_CHECK (f != NULL) ;
      70           8 :         OK (LAGraph_MMRead (&A, f, msg)) ;
      71           8 :         TEST_MSG ("Loading of adjacency matrix failed") ;
      72             : 
      73             :         // construct an undirected graph G with adjacency matrix A
      74           8 :         OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_UNDIRECTED, msg)) ;
      75           8 :         TEST_CHECK (A == NULL) ;
      76             : 
      77             :         // check for self-edges
      78           8 :         OK (LAGraph_Cached_NSelfEdges (G, msg)) ;
      79           8 :         if (G->nself_edges != 0)
      80             :         {
      81             :             // remove self-edges
      82           1 :             printf ("graph has %g self edges\n", (double) G->nself_edges) ;
      83           1 :             OK (LAGraph_DeleteSelfEdges (G, msg)) ;
      84           1 :             printf ("now has %g self edges\n", (double) G->nself_edges) ;
      85           1 :             TEST_CHECK (G->nself_edges == 0) ;
      86             :         }
      87             : 
      88             :         // compute each k-truss until the result is empty
      89             :         bool ok ;
      90             :         GrB_Index n ;
      91           8 :         OK (GrB_Matrix_nrows (&n, G->A)) ;
      92          21 :         for (int k = 3 ; k < n ; k++)
      93             :         {
      94             :             // compute the k-truss
      95          21 :             printf ("\n%d-truss:\n", k) ;
      96          21 :             OK (LAGraph_KTruss (&C1, G, k, msg)) ;
      97             : 
      98             :             // compute it again to check the result
      99          21 :             OK (LG_check_ktruss (&C2, G, k, msg)) ;
     100          21 :             OK (LAGraph_Matrix_IsEqual (&ok, C1, C2, msg)) ;
     101          21 :             TEST_CHECK (ok) ;
     102             : 
     103             :             // count the triangles in the 3-truss
     104          21 :             if (k == 3)
     105             :             {
     106           8 :                 uint32_t nt = 0 ;
     107           8 :                 OK (GrB_reduce (&nt, NULL, GrB_PLUS_MONOID_UINT32, C1, NULL)) ;
     108           8 :                 nt = nt / 6 ;
     109           8 :                 TEST_CHECK (nt == ntriangles) ;
     110             :             }
     111             : 
     112             :             // free C1 and C2, and break if C1 is empty
     113             :             GrB_Index nvals ;
     114          21 :             OK (GrB_Matrix_nvals (&nvals, C1)) ;
     115          21 :             OK (GrB_free (&C1)) ;
     116          21 :             OK (GrB_free (&C2)) ;
     117          21 :             if (nvals == 0) break ;
     118             :         }
     119             : 
     120             :         // convert to directed with symmetric structure and recompute
     121           8 :         G->kind = LAGraph_ADJACENCY_DIRECTED ;
     122           8 :         G->is_symmetric_structure = LAGraph_TRUE ;
     123           8 :         OK (LAGraph_KTruss (&C1, G, 3, msg)) ;
     124           8 :         OK (LG_check_ktruss (&C2, G, 3, msg)) ;
     125           8 :         OK (LAGraph_Matrix_IsEqual (&ok, C1, C2, msg)) ;
     126           8 :         TEST_CHECK (ok) ;
     127           8 :         OK (GrB_free (&C1)) ;
     128           8 :         OK (GrB_free (&C2)) ;
     129             : 
     130           8 :         OK (LAGraph_Delete (&G, msg)) ;
     131             :     }
     132             : 
     133           1 :     LAGraph_Finalize (msg) ;
     134           1 : }
     135             : 
     136             : //------------------------------------------------------------------------------
     137             : // test_ktruss_error
     138             : //------------------------------------------------------------------------------
     139             : 
     140           1 : void test_ktruss_errors (void)
     141             : {
     142           1 :     LAGraph_Init (msg) ;
     143             : 
     144           1 :     snprintf (filename, LEN, LG_DATA_DIR "%s", "karate.mtx") ;
     145           1 :     FILE *f = fopen (filename, "r") ;
     146           1 :     TEST_CHECK (f != NULL) ;
     147           1 :     OK (LAGraph_MMRead (&A, f, msg)) ;
     148           1 :     TEST_MSG ("Loading of adjacency matrix failed") ;
     149             : 
     150             :     // construct an undirected graph G with adjacency matrix A
     151           1 :     OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_UNDIRECTED, msg)) ;
     152           1 :     TEST_CHECK (A == NULL) ;
     153             : 
     154           1 :     OK (LAGraph_Cached_NSelfEdges (G, msg)) ;
     155             : 
     156             :     // C is NULL
     157           1 :     int result = LAGraph_KTruss (NULL, G, 3, msg) ;
     158           1 :     printf ("\nresult: %d %s\n", result, msg) ;
     159           1 :     TEST_CHECK (result == GrB_NULL_POINTER) ;
     160             : 
     161             :     // k is invalid
     162           1 :     result = LAGraph_KTruss (&C1, G, 2, msg) ;
     163           1 :     printf ("\nresult: %d %s\n", result, msg) ;
     164           1 :     TEST_CHECK (result == GrB_INVALID_VALUE) ;
     165           1 :     TEST_CHECK (C1 == NULL) ;
     166             : 
     167             :     // G is invalid
     168           1 :     result = LAGraph_KTruss (&C1, NULL, 3, msg) ;
     169           1 :     printf ("\nresult: %d %s\n", result, msg) ;
     170           1 :     TEST_CHECK (result == GrB_NULL_POINTER) ;
     171           1 :     TEST_CHECK (C1 == NULL) ;
     172             : 
     173             :     // G may have self edges
     174           1 :     G->nself_edges = LAGRAPH_UNKNOWN ;
     175           1 :     result = LAGraph_KTruss (&C1, G, 3, msg) ;
     176           1 :     printf ("\nresult: %d %s\n", result, msg) ;
     177           1 :     TEST_CHECK (result == -1004) ;
     178           1 :     TEST_CHECK (C1 == NULL) ;
     179             : 
     180             :     // G is undirected
     181           1 :     G->nself_edges = 0 ;
     182           1 :     G->kind = LAGraph_ADJACENCY_DIRECTED ;
     183           1 :     G->is_symmetric_structure = LAGraph_FALSE ;
     184           1 :     result = LAGraph_KTruss (&C1, G, 3, msg) ;
     185           1 :     printf ("\nresult: %d %s\n", result, msg) ;
     186           1 :     TEST_CHECK (result == -1005) ;
     187           1 :     TEST_CHECK (C1 == NULL) ;
     188             : 
     189           1 :     result = LG_check_ktruss (&C1, G, 3, msg) ;
     190           1 :     printf ("\nresult: %d %s\n", result, msg) ;
     191           1 :     TEST_CHECK (result == -1005) ;
     192           1 :     TEST_CHECK (C1 == NULL) ;
     193             : 
     194           1 :     OK (LAGraph_Delete (&G, msg)) ;
     195           1 :     LAGraph_Finalize (msg) ;
     196           1 : }
     197             : 
     198             : //****************************************************************************
     199             : 
     200             : TEST_LIST = {
     201             :     {"ktruss", test_ktruss},
     202             :     {"ktruss_errors", test_ktruss_errors},
     203             :     {NULL, NULL}
     204             : };

Generated by: LCOV version 1.14