LCOV - code coverage report
Current view: top level - src/utility - LAGraph_CheckGraph.c (source / functions) Hit Total Coverage
Test: LAGraph code coverage report. Commit id: 7b9d2ee. Current time (UTC): 2025-06-03T21:57:17Z Lines: 33 33 100.0 %
Date: 2025-06-03 22:02:40 Functions: 1 1 100.0 %

          Line data    Source code
       1             : //------------------------------------------------------------------------------
       2             : // LAGraph_CheckGraph: check if a graph is valid
       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 "LG_internal.h"
      19             : 
      20       38021 : int LAGraph_CheckGraph
      21             : (
      22             :     // input/output:
      23             :     LAGraph_Graph G,    // graph to check
      24             :     char *msg
      25             : )
      26             : {
      27             : 
      28             :     //--------------------------------------------------------------------------
      29             :     // clear the msg and check basic components
      30             :     //--------------------------------------------------------------------------
      31             : 
      32       38021 :     LG_CLEAR_MSG_AND_BASIC_ASSERT (G, msg) ;
      33       38002 :     GrB_Matrix A = G->A ;
      34       38002 :     LAGraph_Kind kind = G->kind ;
      35             : 
      36             :     //--------------------------------------------------------------------------
      37             :     // ensure the matrix is square for directed or undirected graphs
      38             :     //--------------------------------------------------------------------------
      39             : 
      40             :     GrB_Index nrows, ncols ;
      41       38002 :     if (kind == LAGraph_ADJACENCY_UNDIRECTED ||
      42             :         kind == LAGraph_ADJACENCY_DIRECTED)
      43             :     {
      44       38002 :         GRB_TRY (GrB_Matrix_nrows (&nrows, A)) ;
      45       38002 :         GRB_TRY (GrB_Matrix_ncols (&ncols, A)) ;
      46       38002 :         LG_ASSERT_MSG (nrows == ncols, LAGRAPH_INVALID_GRAPH,
      47             :             "adjacency matrix must be square") ;
      48             :     }
      49             : 
      50             :     int32_t fmt ;
      51       38001 :     GRB_TRY (GrB_get (A, &fmt, GrB_STORAGE_ORIENTATION_HINT)) ;
      52             :     #if LAGRAPH_SUITESPARSE
      53             :         // only by-row format is supported when using SuiteSparse
      54       38001 :         LG_ASSERT_MSG (fmt == GrB_ROWMAJOR, LAGRAPH_INVALID_GRAPH,
      55             :             "only by-row format supported") ;
      56             :     #endif
      57             : 
      58             :     //--------------------------------------------------------------------------
      59             :     // check the cached properties
      60             :     //--------------------------------------------------------------------------
      61             : 
      62       38000 :     GrB_Matrix AT = G->AT ;
      63       38000 :     if (AT != NULL)
      64             :     {
      65             :         GrB_Index nrows2, ncols2;
      66        6905 :         GRB_TRY (GrB_Matrix_nrows (&nrows2, AT)) ;
      67        6899 :         GRB_TRY (GrB_Matrix_ncols (&ncols2, AT)) ;
      68        6899 :         LG_ASSERT_MSG (nrows == ncols2 && ncols == nrows2,
      69             :             LAGRAPH_INVALID_GRAPH, "G->AT matrix has the wrong dimensions") ;
      70             : 
      71        6897 :         GRB_TRY (GrB_get (AT, &fmt, GrB_STORAGE_ORIENTATION_HINT)) ;
      72             :         #if LAGRAPH_SUITESPARSE
      73             :             // only by-row format is supported when using SuiteSparse
      74        6897 :             LG_ASSERT_MSG (fmt == GrB_ROWMAJOR, LAGRAPH_INVALID_GRAPH,
      75             :                 "only by-row format supported") ;
      76             :         #endif
      77             : 
      78             :         // ensure the types of A and AT are the same
      79             :         char atype [LAGRAPH_MAX_NAME_LEN] ;
      80             :         char ttype [LAGRAPH_MAX_NAME_LEN] ;
      81        6895 :         LG_TRY (LAGraph_Matrix_TypeName (atype, A, msg)) ;
      82        6895 :         LG_TRY (LAGraph_Matrix_TypeName (ttype, AT, msg)) ;
      83        6895 :         LG_ASSERT_MSG (MATCHNAME (atype, ttype),
      84             :             LAGRAPH_INVALID_GRAPH, "A and AT must have the same type") ;
      85             :     }
      86             : 
      87       37994 :     GrB_Vector out_degree = G->out_degree ;
      88       37994 :     if (out_degree != NULL)
      89             :     {
      90             :         GrB_Index m ;
      91       20430 :         GRB_TRY (GrB_Vector_size (&m, out_degree)) ;
      92       20428 :         LG_ASSERT_MSG (m == nrows, LAGRAPH_INVALID_GRAPH,
      93             :             "out_degree invalid size") ;
      94             :         char rtype [LAGRAPH_MAX_NAME_LEN] ;
      95       20427 :         LG_TRY (LAGraph_Vector_TypeName (rtype, out_degree, msg)) ;
      96       20427 :         LG_ASSERT_MSG (MATCHNAME (rtype, "int64_t"),
      97             :             LAGRAPH_INVALID_GRAPH,
      98             :             "out_degree has wrong type; must be GrB_INT64") ;
      99             :     }
     100             : 
     101       37992 :     GrB_Vector in_degree = G->in_degree ;
     102       37992 :     if (in_degree != NULL)
     103             :     {
     104             :         GrB_Index n ;
     105        6345 :         GRB_TRY (GrB_Vector_size (&n, in_degree)) ;
     106        6343 :         LG_ASSERT_MSG (n == ncols, LAGRAPH_INVALID_GRAPH,
     107             :             "in_degree invalid size") ;
     108             :         char ctype [LAGRAPH_MAX_NAME_LEN] ;
     109        6342 :         LG_TRY (LAGraph_Vector_TypeName (ctype, in_degree, msg)) ;
     110        6342 :         LG_ASSERT_MSG (MATCHNAME (ctype, "int64_t"),
     111             :             LAGRAPH_INVALID_GRAPH,
     112             :             "in_degree has wrong type; must be GrB_INT64") ;
     113             :     }
     114             : 
     115       37990 :     return (GrB_SUCCESS) ;
     116             : }

Generated by: LCOV version 1.14