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

          Line data    Source code
       1             : //----------------------------------------------------------------------------
       2             : // LAGraph/experimental/test/test_AllKtest.c: test cases for all-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 ;
      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_AllKTruss (void)
      55             : {
      56           1 :     LAGraph_Init (msg) ;
      57             : 
      58           1 :     for (int id = 0 ; ; id++)
      59           9 :     {
      60             : 
      61             :         // load the matrix as A
      62          10 :         const char *aname = files [id].name ;
      63          10 :         uint32_t ntriangles = files [id].ntriangles ;
      64          10 :         if (strlen (aname) == 0) break;
      65           9 :         printf ("\n================================== %s:\n", aname) ;
      66           9 :         TEST_CASE (aname) ;
      67           9 :         snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
      68           9 :         FILE *f = fopen (filename, "r") ;
      69           9 :         TEST_CHECK (f != NULL) ;
      70           9 :         OK (LAGraph_MMRead (&A, f, msg)) ;
      71           9 :         TEST_MSG ("Loading of adjacency matrix failed") ;
      72           9 :         fclose (f) ;
      73             : 
      74             :         // construct an undirected graph G with adjacency matrix A
      75           9 :         OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_UNDIRECTED, msg)) ;
      76           9 :         TEST_CHECK (A == NULL) ;
      77             : 
      78             :         // check for self-edges
      79           9 :         OK (LAGraph_Cached_NSelfEdges (G, msg)) ;
      80           9 :         if (G->nself_edges != 0)
      81             :         {
      82             :             // remove self-edges
      83           2 :             printf ("graph has %g self edges\n", (double) G->nself_edges) ;
      84           2 :             OK (LAGraph_DeleteSelfEdges (G, msg)) ;
      85           2 :             printf ("now has %g self edges\n", (double) G->nself_edges) ;
      86           2 :             TEST_CHECK (G->nself_edges == 0) ;
      87             :         }
      88             : 
      89             :         // compute each k-truss
      90           9 :         bool ok = false ;
      91             :         GrB_Index n ;
      92             :         int64_t kmax ;
      93           9 :         OK (GrB_Matrix_nrows (&n, G->A)) ;
      94             :         GrB_Matrix *Cset ;
      95             :         int64_t *ntris, *nedges, *nsteps ;
      96           9 :         OK (LAGraph_Calloc ((void **) &Cset  , n, sizeof (GrB_Matrix), msg)) ;
      97           9 :         OK (LAGraph_Malloc ((void **) &ntris , n, sizeof (int64_t), msg)) ;
      98           9 :         OK (LAGraph_Malloc ((void **) &nedges, n, sizeof (int64_t), msg)) ;
      99           9 :         OK (LAGraph_Malloc ((void **) &nsteps, n, sizeof (int64_t), msg)) ;
     100             : 
     101           9 :         OK (LAGraph_AllKTruss (Cset, &kmax, ntris, nedges, nsteps, G, msg)) ;
     102           9 :         printf ("all k-truss: kmax %g\n", (double) kmax) ;
     103             : 
     104             :         // compute each k-truss using LAGraph_KTruss, and compare
     105          48 :         for (int k = 3 ; k < n ; k++)
     106             :         {
     107             :             // printf ("\n%d-truss:\n", k) ;
     108          48 :             TEST_CHECK (k <= kmax) ;
     109             :             // compute the k-truss
     110          48 :             OK (LAGraph_KTruss (&C1, G, k, msg)) ;
     111             : 
     112             :             // check the result
     113             :             GrB_Index nvals ;
     114          48 :             OK (GrB_Matrix_nvals (&nvals, C1)) ;
     115          48 :             OK (LAGraph_Matrix_IsEqual (&ok, C1, Cset [k], msg)) ;
     116          48 :             TEST_CHECK (ok) ;
     117             : 
     118             :             // count the triangles in the 3-truss
     119          48 :             uint32_t nt = 0 ;
     120          48 :             OK (GrB_reduce (&nt, NULL, GrB_PLUS_MONOID_UINT32, C1, NULL)) ;
     121          48 :             nt = nt / 6 ;
     122          48 :             if (k == 3)
     123             :             {
     124           9 :                 TEST_CHECK (nt == ntriangles) ;
     125             :             }
     126          48 :             TEST_CHECK (nt == ntris [k]) ;
     127          48 :             TEST_CHECK (nvals == 2 * nedges [k]) ;
     128          48 :             TEST_CHECK (nsteps [k] >= 0) ;
     129             : 
     130             :             // free C1, and break if C1 is empty
     131          48 :             OK (GrB_free (&C1)) ;
     132          48 :             if (nvals == 0)
     133             :             {
     134           9 :                 TEST_CHECK (k == kmax) ;
     135           9 :                 break ;
     136             :             }
     137             :         }
     138             : 
     139             :         // convert to directed with symmetric structure and recompute
     140           9 :         G->kind = LAGraph_ADJACENCY_DIRECTED ;
     141           9 :         G->is_symmetric_structure = LAGraph_TRUE ;
     142             :         int64_t k2 ;
     143             :         GrB_Matrix *Cset2 ;
     144             :         int64_t *ntris2, *nedges2, *nsteps2 ;
     145           9 :         OK (LAGraph_Calloc ((void **) &Cset2  , n, sizeof (GrB_Matrix), msg)) ;
     146           9 :         OK (LAGraph_Malloc ((void **) &ntris2 , n, sizeof (int64_t), msg)) ;
     147           9 :         OK (LAGraph_Malloc ((void **) &nedges2, n, sizeof (int64_t), msg)) ;
     148           9 :         OK (LAGraph_Malloc ((void **) &nsteps2, n, sizeof (int64_t), msg)) ;
     149             : 
     150           9 :         OK (LAGraph_AllKTruss (Cset2, &k2, ntris2, nedges2, nsteps2, G, msg)) ;
     151           9 :         TEST_CHECK (k2 == kmax) ;
     152          84 :         for (int k = 0 ; k <= kmax ; k++)
     153             :         {
     154          75 :             TEST_CHECK (ntris2  [k] == ntris  [k]) ;
     155          75 :             TEST_CHECK (nedges2 [k] == nedges [k]) ;
     156          75 :             TEST_CHECK (nsteps2 [k] == nsteps [k]) ;
     157          75 :             if (k < 3)
     158             :             {
     159          27 :                 TEST_CHECK (Cset [k] == NULL) ;
     160          27 :                 TEST_CHECK (Cset2 [k] == NULL) ;
     161             :             }
     162             :             else
     163             :             {
     164          48 :                 OK (LAGraph_Matrix_IsEqual (&ok, Cset [k], Cset2 [k], msg)) ;
     165             :             }
     166             : //          if (!ok)
     167             : //          {
     168             : //              GxB_print (Cset [k], 2) ;
     169             : //              GxB_print (Cset2 [k], 2) ;
     170             : //          }
     171          75 :             TEST_CHECK (ok) ;
     172          75 :             OK (GrB_free (&(Cset [k]))) ;
     173          75 :             OK (GrB_free (&(Cset2 [k]))) ;
     174             :         }
     175             : 
     176           9 :         LAGraph_Free ((void **) &Cset, NULL) ;
     177           9 :         LAGraph_Free ((void **) &ntris, NULL) ;
     178           9 :         LAGraph_Free ((void **) &nedges, NULL) ;
     179           9 :         LAGraph_Free ((void **) &nsteps, NULL) ;
     180             : 
     181           9 :         LAGraph_Free ((void **) &Cset2, NULL) ;
     182           9 :         LAGraph_Free ((void **) &ntris2, NULL) ;
     183           9 :         LAGraph_Free ((void **) &nedges2, NULL) ;
     184           9 :         LAGraph_Free ((void **) &nsteps2, NULL) ;
     185             : 
     186           9 :         OK (LAGraph_Delete (&G, msg)) ;
     187             :     }
     188             : 
     189           1 :     LAGraph_Finalize (msg) ;
     190           1 : }
     191             : 
     192             : //------------------------------------------------------------------------------
     193             : // test_AllKTruss_errors
     194             : //------------------------------------------------------------------------------
     195             : 
     196           1 : void test_allktruss_errors (void)
     197             : {
     198           1 :     LAGraph_Init (msg) ;
     199             : 
     200           1 :     snprintf (filename, LEN, LG_DATA_DIR "%s", "karate.mtx") ;
     201           1 :     FILE *f = fopen (filename, "r") ;
     202           1 :     TEST_CHECK (f != NULL) ;
     203           1 :     OK (LAGraph_MMRead (&A, f, msg)) ;
     204           1 :     TEST_MSG ("Loading of adjacency matrix failed") ;
     205           1 :     fclose (f) ;
     206             : 
     207             :     // construct an undirected graph G with adjacency matrix A
     208           1 :     OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_UNDIRECTED, msg)) ;
     209           1 :     TEST_CHECK (A == NULL) ;
     210             : 
     211           1 :     OK (LAGraph_Cached_NSelfEdges (G, msg)) ;
     212             : 
     213             :     GrB_Index n ;
     214             :     int64_t kmax ;
     215           1 :     OK (GrB_Matrix_nrows (&n, G->A)) ;
     216             :     int64_t *ntris, *nedges, *nsteps ;
     217             :     GrB_Matrix *Cset ;
     218           1 :     OK (LAGraph_Calloc ((void **) &Cset  , n, sizeof (GrB_Matrix), msg)) ;
     219           1 :     OK (LAGraph_Malloc ((void **) &ntris , n, sizeof (int64_t), msg)) ;
     220           1 :     OK (LAGraph_Malloc ((void **) &nedges, n, sizeof (int64_t), msg)) ;
     221           1 :     OK (LAGraph_Malloc ((void **) &nsteps, n, sizeof (int64_t), msg)) ;
     222             : 
     223             :     // kmax is NULL
     224           1 :     int result = LAGraph_AllKTruss (Cset, NULL, ntris, nedges, nsteps, G, msg) ;
     225           1 :     printf ("\nresult: %d %s\n", result, msg) ;
     226           1 :     TEST_CHECK (result == GrB_NULL_POINTER) ;
     227             : 
     228             :     // G is invalid
     229           1 :     result = LAGraph_AllKTruss (Cset, &kmax, ntris, nedges, nsteps, NULL, msg) ;
     230           1 :     printf ("\nresult: %d %s\n", result, msg) ;
     231           1 :     TEST_CHECK (result == GrB_NULL_POINTER) ;
     232             : 
     233             :     // G may have self edges
     234           1 :     G->nself_edges = LAGRAPH_UNKNOWN ;
     235           1 :     result = LAGraph_AllKTruss (Cset, &kmax, ntris, nedges, nsteps, G, msg) ;
     236           1 :     printf ("\nresult: %d %s\n", result, msg) ;
     237           1 :     TEST_CHECK (result == -1004) ;
     238             : 
     239             :     // G is undirected
     240           1 :     G->nself_edges = 0 ;
     241           1 :     G->kind = LAGraph_ADJACENCY_DIRECTED ;
     242           1 :     G->is_symmetric_structure = LAGraph_FALSE ;
     243           1 :     result = LAGraph_AllKTruss (Cset, &kmax, ntris, nedges, nsteps, G, msg) ;
     244           1 :     printf ("\nresult: %d %s\n", result, msg) ;
     245           1 :     TEST_CHECK (result == -1005) ;
     246             : 
     247           1 :     LAGraph_Free ((void **) &Cset, NULL) ;
     248           1 :     LAGraph_Free ((void **) &ntris, NULL) ;
     249           1 :     LAGraph_Free ((void **) &nedges, NULL) ;
     250           1 :     LAGraph_Free ((void **) &nsteps, NULL) ;
     251             : 
     252           1 :     OK (LAGraph_Delete (&G, msg)) ;
     253           1 :     LAGraph_Finalize (msg) ;
     254           1 : }
     255             : 
     256             : //****************************************************************************
     257             : 
     258             : TEST_LIST = {
     259             :     {"allktruss", test_AllKTruss},
     260             :     {"allktruss_errors", test_allktruss_errors},
     261             :     {NULL, NULL}
     262             : };

Generated by: LCOV version 1.14