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: cc56ed4. Current time (UTC): 2024-08-30T17:14:30Z Lines: 81 81 100.0 %
Date: 2024-08-30 17:16:41 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             : const matrix_info files [ ] =
      77             : {
      78             :     { "A.mtx",
      79             :         4.6, 5.0,
      80             :         4.6, 5.0,
      81             :         5, 123456 },
      82             :      { "LFAT5.mtx",
      83             :         2.2, 2.0,
      84             :         2.2, 2.0,
      85             :         5, 123456 },
      86             :      { "cover.mtx",
      87             :         1.4, 1.0,
      88             :         2.4, 3.0,
      89             :         5, 123456 },
      90             :      { "full.mtx",
      91             :         3.0, 3.0,
      92             :         3.0, 3.0,
      93             :         5, 123456 },
      94             :      { "full_symmetric.mtx",
      95             :         4.0, 4.0,
      96             :         4.0, 4.0,
      97             :         5, 123456 },
      98             :      { "karate.mtx",
      99             :         3.0, 3.0,
     100             :         3.0, 3.0,
     101             :         5, 123456 },
     102             :      // Add karate two more times to test seed and nsamples
     103             :      { "karate.mtx",
     104             :         3.46666666667, 3.0,
     105             :         3.46666666667, 3.0,
     106             :         15, 123456 },
     107             :      { "karate.mtx",
     108             :         8.4, 6.0,
     109             :         8.4, 6.0,
     110             :         5, 87654432 },
     111             :      { "ldbc-cdlp-directed-example.mtx",
     112             :         2.2, 2.0,
     113             :         1.8, 2.0,
     114             :         5, 123456 },
     115             : //   { "ldbc-directed-example-bool.mtx",
     116             : //      2.5, 3.0,
     117             : //      3.8, 3.0,
     118             : //      10, 123456 },
     119             :     { "", 0.0, 0.0, 0.0, 0.0, 1, 0 }
     120             : } ;
     121             : 
     122             : //-----------------------------------------------------------------------------
     123             : // test_SampleDegree
     124             : //-----------------------------------------------------------------------------
     125             : 
     126           1 : void test_SampleDegree (void)
     127             : {
     128           1 :     setup ( ) ;
     129             : 
     130           1 :     for (int k = 0 ; ; k++)
     131           9 :     {
     132             : 
     133             :         // load the matrix as A
     134          10 :         const char *aname = files [k].name ;
     135          10 :         if (strlen (aname) == 0) break;
     136           9 :         TEST_CASE (aname) ;
     137           9 :         printf ("\n==================== Test case: %s\n", aname) ;
     138           9 :         snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
     139           9 :         FILE *f = fopen (filename, "r") ;
     140           9 :         TEST_CHECK (f != NULL) ;
     141           9 :         OK (LAGraph_MMRead (&A, f, msg)) ;
     142           9 :         OK (fclose (f)) ;
     143           9 :         TEST_MSG ("Loading of adjacency matrix failed") ;
     144             : 
     145             :         // construct the graph G with adjacency matrix A
     146           9 :         OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_DIRECTED, msg)) ;
     147           9 :         TEST_CHECK (A == NULL) ;
     148             : 
     149             :         // SampleDegree requires degrees to be precomputed
     150          18 :         ret_code = LAGr_SampleDegree (&mean, &median, G, 1,
     151           9 :             files [k].nsamples, files [k].seed, msg) ;
     152           9 :         TEST_CHECK (ret_code == LAGRAPH_NOT_CACHED) ;
     153           9 :         TEST_MSG ("SampleDegree without row degrees precomputed succeeded") ;
     154             : 
     155          18 :         ret_code = LAGr_SampleDegree (&mean, &median, G, 0,
     156           9 :             files [k].nsamples, files [k].seed, msg) ;
     157           9 :         TEST_CHECK (ret_code == LAGRAPH_NOT_CACHED) ;
     158           9 :         TEST_MSG ("SampleDegree without column degrees precomputed succeeded") ;
     159             : 
     160             :         // Compute and check the row samples
     161           9 :         OK (LAGraph_Cached_OutDegree (G, msg)) ;
     162           9 :         OK (LAGr_SampleDegree (&mean, &median, G, 1,
     163             :             files [k].nsamples, files [k].seed, msg)) ;
     164             : 
     165           9 :         TEST_CHECK (is_close(mean, files [k].row_mean)) ;
     166           9 :         TEST_MSG ("Row Mean Expected: %f", files [k].row_mean) ;
     167           9 :         TEST_MSG ("Row Mean Produced: %f", mean) ;
     168             : 
     169           9 :         TEST_CHECK (is_close(median, files [k].row_median)) ;
     170           9 :         TEST_MSG ("Row Median Expected: %f", files [k].row_median) ;
     171           9 :         TEST_MSG ("Row Median Produced: %f", median) ;
     172             : 
     173             :         // Compute the column samples
     174           9 :         OK (LAGraph_DeleteCached (G, msg)) ;
     175             : 
     176           9 :         OK (LAGraph_Cached_InDegree (G, msg)) ;
     177           9 :         OK (LAGr_SampleDegree (&mean, &median, G, 0,
     178             :             files [k].nsamples, files [k].seed, msg)) ;
     179             : 
     180           9 :         TEST_CHECK (is_close(mean, files [k].col_mean)) ;
     181           9 :         TEST_MSG ("Column Mean Expected: %f", files [k].col_mean) ;
     182           9 :         TEST_MSG ("Column Mean Produced: %f", mean) ;
     183             : 
     184           9 :         TEST_CHECK (is_close(median, files [k].col_median)) ;
     185           9 :         TEST_MSG ("Column Median Expected: %f", files [k].col_median) ;
     186           9 :         TEST_MSG ("Column Median Produced: %f", median) ;
     187             : 
     188           9 :         OK (LAGraph_Delete (&G, msg)) ;
     189             :     }
     190             : 
     191           1 :     teardown ( ) ;
     192           1 : }
     193             : 
     194             : //-----------------------------------------------------------------------------
     195             : // test_SampleDegree_brutal
     196             : //-----------------------------------------------------------------------------
     197             : 
     198             : #if LAGRAPH_SUITESPARSE
     199           1 : void test_SampleDegree_brutal (void)
     200             : {
     201           1 :     OK (LG_brutal_setup (msg)) ;
     202             : 
     203           1 :     for (int k = 0 ; ; k++)
     204           9 :     {
     205             : 
     206             :         // load the matrix as A
     207          10 :         const char *aname = files [k].name ;
     208          10 :         if (strlen (aname) == 0) break;
     209           9 :         TEST_CASE (aname) ;
     210           9 :         printf ("\n==================== Test case: %s\n", aname) ;
     211           9 :         snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
     212           9 :         FILE *f = fopen (filename, "r") ;
     213           9 :         TEST_CHECK (f != NULL) ;
     214           9 :         OK (LAGraph_MMRead (&A, f, msg)) ;
     215           9 :         OK (fclose (f)) ;
     216           9 :         TEST_MSG ("Loading of adjacency matrix failed") ;
     217             : 
     218             :         // construct the graph G with adjacency matrix A
     219           9 :         OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_DIRECTED, msg)) ;
     220           9 :         TEST_CHECK (A == NULL) ;
     221             : 
     222             :         // Compute and check the row samples
     223          67 :         LG_BRUTAL (LAGraph_Cached_OutDegree (G, msg)) ;
     224          18 :         LG_BRUTAL (LAGr_SampleDegree (&mean, &median, G, 1,
     225             :             files [k].nsamples, files [k].seed, msg)) ;
     226             : 
     227           9 :         TEST_CHECK (is_close(mean, files [k].row_mean)) ;
     228           9 :         TEST_CHECK (is_close(median, files [k].row_median)) ;
     229             : 
     230             :         // Compute the column samples
     231           9 :         LG_BRUTAL (LAGraph_DeleteCached (G, msg)) ;
     232             : 
     233          78 :         LG_BRUTAL (LAGraph_Cached_InDegree (G, msg)) ;
     234          18 :         LG_BRUTAL (LAGr_SampleDegree (&mean, &median, G, 0,
     235             :             files [k].nsamples, files [k].seed, msg)) ;
     236             : 
     237           9 :         TEST_CHECK (is_close(mean, files [k].col_mean)) ;
     238           9 :         TEST_CHECK (is_close(median, files [k].col_median)) ;
     239             : 
     240           9 :         OK (LAGraph_Delete (&G, msg)) ;
     241             :     }
     242             : 
     243           1 :     OK (LG_brutal_teardown (msg)) ;
     244           1 : }
     245             : #endif
     246             : 
     247             : //-----------------------------------------------------------------------------
     248             : // TEST_LIST: the list of tasks for this entire test
     249             : //-----------------------------------------------------------------------------
     250             : 
     251             : TEST_LIST =
     252             : {
     253             :     { "SampleDegree", test_SampleDegree },
     254             :     #if LAGRAPH_SUITESPARSE
     255             :     { "SampleDegree_brutal", test_SampleDegree_brutal },
     256             :     #endif
     257             :     { NULL, NULL }
     258             : } ;

Generated by: LCOV version 1.14