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

          Line data    Source code
       1             : //------------------------------------------------------------------------------
       2             : // LAGraph/src/test/test_SampleDegree.c:  test LAGr_SampleDegree
       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             : LAGraph_Graph G = NULL ;
      25             : char msg [LAGRAPH_MSG_LEN] ;
      26             : GrB_Matrix A = NULL ;
      27             : double mean, median ;
      28             : int ret_code ;
      29             : #define LEN 512
      30             : char filename [LEN+1] ;
      31             : 
      32             : //------------------------------------------------------------------------------
      33             : // setup: start a test
      34             : //------------------------------------------------------------------------------
      35             : 
      36           1 : void setup (void)
      37             : {
      38           1 :     OK (LAGraph_Init (msg)) ;
      39           1 : }
      40             : 
      41             : //------------------------------------------------------------------------------
      42             : // teardown: finalize a test
      43             : //------------------------------------------------------------------------------
      44             : 
      45           1 : void teardown (void)
      46             : {
      47           1 :     OK (LAGraph_Finalize (msg)) ;
      48           1 : }
      49             : 
      50             : //------------------------------------------------------------------------------
      51             : // is_close: check whether two floats are close
      52             : //------------------------------------------------------------------------------
      53             : 
      54          72 : bool is_close (double a, double b)
      55             : {
      56          72 :     double abs_diff = fabs(a - b) ;
      57          72 :     return abs_diff < 1e-6 ;
      58             : }
      59             : 
      60             : //------------------------------------------------------------------------------
      61             : // test_SampleDegree:  test LAGr_SampleDegree
      62             : //------------------------------------------------------------------------------
      63             : 
      64             : typedef struct
      65             : {
      66             :     const char *name ;
      67             :     const double row_mean ;
      68             :     const double row_median ;
      69             :     const double col_mean ;
      70             :     const double col_median ;
      71             :     const int64_t nsamples ;
      72             :     const uint64_t seed ;
      73             : }
      74             : matrix_info ;
      75             : 
      76             : // NOTE: these expected results depend on the random number generator used
      77             : const matrix_info files [ ] =
      78             : {
      79             :     { "A.mtx",
      80             :         5.0, 5.0,
      81             :         5.0, 5.0,
      82             :         5, 123456 },
      83             :      { "LFAT5.mtx",
      84             :         2.6, 2.0,
      85             :         2.6, 2.0,
      86             :         5, 123456 },
      87             :      { "cover.mtx",
      88             :         1.6, 1.0,
      89             :         2.4, 3.0,
      90             :         5, 123456 },
      91             :      { "full.mtx",
      92             :         3.0, 3.0,
      93             :         3.0, 3.0,
      94             :         5, 123456 },
      95             :      { "full_symmetric.mtx",
      96             :         4.0, 4.0,
      97             :         4.0, 4.0,
      98             :         5, 123456 },
      99             :      { "karate.mtx",
     100             :         4.2, 4.0,
     101             :         4.2, 4.0,
     102             :         5, 123456 },
     103             :      // Add karate two more times to test seed and nsamples
     104             :      { "karate.mtx",
     105             :         4.93333333, 4.0,
     106             :         4.93333333, 4.0,
     107             :         15, 123456 },
     108             :      { "karate.mtx",
     109             :         3.2, 2.0,
     110             :         3.2, 2.0,
     111             :         5, 87654432 },
     112             :      { "ldbc-cdlp-directed-example.mtx",
     113             :         2.0, 2.0,
     114             :         2.0, 1.0,
     115             :         5, 123456 },
     116             :     { "", 0.0, 0.0, 0.0, 0.0, 1, 0 }
     117             : } ;
     118             : 
     119             : //-----------------------------------------------------------------------------
     120             : // test_SampleDegree
     121             : //-----------------------------------------------------------------------------
     122             : 
     123           1 : void test_SampleDegree (void)
     124             : {
     125           1 :     setup ( ) ;
     126             : 
     127           1 :     for (int k = 0 ; ; k++)
     128           9 :     {
     129             : 
     130             :         // load the matrix as A
     131          10 :         const char *aname = files [k].name ;
     132          10 :         if (strlen (aname) == 0) break;
     133           9 :         TEST_CASE (aname) ;
     134           9 :         printf ("\n==================== Test case: %s\n", aname) ;
     135           9 :         snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
     136           9 :         FILE *f = fopen (filename, "r") ;
     137           9 :         TEST_CHECK (f != NULL) ;
     138           9 :         OK (LAGraph_MMRead (&A, f, msg)) ;
     139           9 :         OK (fclose (f)) ;
     140           9 :         TEST_MSG ("Loading of adjacency matrix failed") ;
     141             : 
     142             :         // construct the graph G with adjacency matrix A
     143           9 :         OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_DIRECTED, msg)) ;
     144           9 :         TEST_CHECK (A == NULL) ;
     145             : 
     146             :         // SampleDegree requires degrees to be precomputed
     147          18 :         ret_code = LAGr_SampleDegree (&mean, &median, G, 1,
     148           9 :             files [k].nsamples, files [k].seed, msg) ;
     149           9 :         TEST_CHECK (ret_code == LAGRAPH_NOT_CACHED) ;
     150           9 :         TEST_MSG ("SampleDegree without row degrees precomputed succeeded") ;
     151             : 
     152          18 :         ret_code = LAGr_SampleDegree (&mean, &median, G, 0,
     153           9 :             files [k].nsamples, files [k].seed, msg) ;
     154           9 :         TEST_CHECK (ret_code == LAGRAPH_NOT_CACHED) ;
     155           9 :         TEST_MSG ("SampleDegree without column degrees precomputed succeeded") ;
     156             : 
     157             :         // Compute and check the row samples
     158           9 :         OK (LAGraph_Cached_OutDegree (G, msg)) ;
     159           9 :         OK (LAGr_SampleDegree (&mean, &median, G, 1,
     160             :             files [k].nsamples, files [k].seed, msg)) ;
     161             : 
     162           9 :         TEST_CHECK (is_close(mean, files [k].row_mean)) ;
     163           9 :         TEST_MSG ("Row Mean Expected: %f", files [k].row_mean) ;
     164           9 :         TEST_MSG ("Row Mean Produced: %f", mean) ;
     165             : 
     166           9 :         TEST_CHECK (is_close(median, files [k].row_median)) ;
     167           9 :         TEST_MSG ("Row Median Expected: %f", files [k].row_median) ;
     168           9 :         TEST_MSG ("Row Median Produced: %f", median) ;
     169             : 
     170             :         // Compute the column samples
     171           9 :         OK (LAGraph_DeleteCached (G, msg)) ;
     172             : 
     173           9 :         OK (LAGraph_Cached_InDegree (G, msg)) ;
     174           9 :         OK (LAGr_SampleDegree (&mean, &median, G, 0,
     175             :             files [k].nsamples, files [k].seed, msg)) ;
     176             : 
     177           9 :         TEST_CHECK (is_close(mean, files [k].col_mean)) ;
     178           9 :         TEST_MSG ("Column Mean Expected: %f", files [k].col_mean) ;
     179           9 :         TEST_MSG ("Column Mean Produced: %f", mean) ;
     180             : 
     181           9 :         TEST_CHECK (is_close(median, files [k].col_median)) ;
     182           9 :         TEST_MSG ("Column Median Expected: %f", files [k].col_median) ;
     183           9 :         TEST_MSG ("Column Median Produced: %f", median) ;
     184             : 
     185           9 :         OK (LAGraph_Delete (&G, msg)) ;
     186             :     }
     187             : 
     188           1 :     teardown ( ) ;
     189           1 : }
     190             : 
     191             : //-----------------------------------------------------------------------------
     192             : // test_SampleDegree_brutal
     193             : //-----------------------------------------------------------------------------
     194             : 
     195             : #if LG_BRUTAL_TESTS
     196           1 : void test_SampleDegree_brutal (void)
     197             : {
     198           1 :     OK (LG_brutal_setup (msg)) ;
     199             : 
     200           1 :     for (int k = 0 ; ; k++)
     201           9 :     {
     202             : 
     203             :         // load the matrix as A
     204          10 :         const char *aname = files [k].name ;
     205          10 :         if (strlen (aname) == 0) break;
     206           9 :         TEST_CASE (aname) ;
     207           9 :         printf ("\n==================== Test case: %s\n", aname) ;
     208           9 :         snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
     209           9 :         FILE *f = fopen (filename, "r") ;
     210           9 :         TEST_CHECK (f != NULL) ;
     211           9 :         OK (LAGraph_MMRead (&A, f, msg)) ;
     212           9 :         OK (fclose (f)) ;
     213           9 :         TEST_MSG ("Loading of adjacency matrix failed") ;
     214             : 
     215             :         // construct the graph G with adjacency matrix A
     216           9 :         OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_DIRECTED, msg)) ;
     217           9 :         TEST_CHECK (A == NULL) ;
     218             : 
     219             :         // Compute and check the row samples
     220          67 :         LG_BRUTAL (LAGraph_Cached_OutDegree (G, msg)) ;
     221          18 :         LG_BRUTAL (LAGr_SampleDegree (&mean, &median, G, 1,
     222             :             files [k].nsamples, files [k].seed, msg)) ;
     223             : 
     224           9 :         TEST_CHECK (is_close(mean, files [k].row_mean)) ;
     225           9 :         TEST_MSG ("Row Mean Expected: %f", files [k].row_mean) ;
     226           9 :         TEST_MSG ("Row Mean Produced: %f", mean) ;
     227             : 
     228           9 :         TEST_CHECK (is_close(median, files [k].row_median)) ;
     229           9 :         TEST_MSG ("Row Median Expected: %f", files [k].row_median) ;
     230           9 :         TEST_MSG ("Row Median Produced: %f", median) ;
     231             : 
     232             :         // Compute the column samples
     233           9 :         LG_BRUTAL (LAGraph_DeleteCached (G, msg)) ;
     234             : 
     235          78 :         LG_BRUTAL (LAGraph_Cached_InDegree (G, msg)) ;
     236          18 :         LG_BRUTAL (LAGr_SampleDegree (&mean, &median, G, 0,
     237             :             files [k].nsamples, files [k].seed, msg)) ;
     238             : 
     239           9 :         TEST_CHECK (is_close(mean, files [k].col_mean)) ;
     240           9 :         TEST_MSG ("Column Mean Expected: %f", files [k].col_mean) ;
     241           9 :         TEST_MSG ("Column Mean Produced: %f", mean) ;
     242             : 
     243           9 :         TEST_CHECK (is_close(median, files [k].col_median)) ;
     244           9 :         TEST_MSG ("Column Median Expected: %f", files [k].col_median) ;
     245           9 :         TEST_MSG ("Column Median Produced: %f", median) ;
     246             : 
     247           9 :         OK (LAGraph_Delete (&G, msg)) ;
     248             :     }
     249             : 
     250           1 :     OK (LG_brutal_teardown (msg)) ;
     251           1 : }
     252             : #endif
     253             : 
     254             : //-----------------------------------------------------------------------------
     255             : // TEST_LIST: the list of tasks for this entire test
     256             : //-----------------------------------------------------------------------------
     257             : 
     258             : TEST_LIST =
     259             : {
     260             :     { "SampleDegree", test_SampleDegree },
     261             :     #if LG_BRUTAL_TESTS
     262             :     { "SampleDegree_brutal", test_SampleDegree_brutal },
     263             :     #endif
     264             :     { NULL, NULL }
     265             : } ;

Generated by: LCOV version 1.14