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

          Line data    Source code
       1             : //------------------------------------------------------------------------------
       2             : // experimental/test/test_argminmax:  test LAGraph_argminmax
       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 Tim Davis, Texas A&M University
      15             : 
      16             : //------------------------------------------------------------------------------
      17             : 
      18             : #include <stdio.h>
      19             : #include <acutest.h>
      20             : #include <LAGraphX.h>
      21             : #include <LAGraph_test.h>
      22             : #include <LG_Xtest.h>
      23             : #include <LG_test.h>
      24             : #include <LAGraph.h>
      25             : #include <LG_internal.h>
      26             : 
      27             : char msg [LAGRAPH_MSG_LEN] ;
      28             : 
      29             : #define LEN 512
      30             : char filename [LEN+1] ;
      31             : 
      32             : typedef struct
      33             : {
      34             :     const char *name ;
      35             : }
      36             : matrix_info ;
      37             : 
      38             : const matrix_info files [ ] =
      39             : {
      40             :     { "structure.mtx" },
      41             :     { "karate.mtx" },
      42             :     { "west0067.mtx" },
      43             :     { "bcsstk13.mtx" },
      44             :     { "" },
      45             : } ;
      46             : 
      47           1 : void test_argminmax (void)
      48             : {
      49             : #if LAGRAPH_SUITESPARSE
      50             : 
      51             :     //--------------------------------------------------------------------------
      52             :     // start LAGraph
      53             :     //--------------------------------------------------------------------------
      54             : 
      55           1 :     LAGraph_Init (msg) ;
      56           1 :     GrB_Matrix A = NULL, C = NULL ;
      57           1 :     GrB_Vector x = NULL, p = NULL, x2 = NULL, p2 = NULL ;
      58             :     GrB_Index nrows, ncols ;
      59             : 
      60           1 :     for (int k = 0 ; ; k++)
      61           4 :     {
      62             :         // load the matrix as A
      63           5 :         const char *aname = files [k].name ;
      64           5 :         if (strlen (aname) == 0) break ;
      65             :         // printf ("\n %s: ==================================\n", aname) ;
      66           4 :         TEST_CASE (aname) ;
      67           4 :         snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
      68           4 :         FILE *f = fopen (filename, "r") ;
      69           4 :         TEST_CHECK (f != NULL) ;
      70           4 :         OK (LAGraph_MMRead (&A, f, msg)) ;
      71           4 :         TEST_MSG ("Loading of adjacency matrix failed") ;
      72           4 :         fclose (f) ;
      73           4 :         OK (GrB_Matrix_nrows (&nrows, A)) ;
      74           4 :         OK (GrB_Matrix_ncols (&ncols, A)) ;
      75             : 
      76          48 :         for (int ktype = 0 ; ktype < 11 ; ktype++)
      77             :         {
      78             :             GrB_Type type ;
      79          44 :             switch (ktype)
      80             :             {
      81           4 :                 case  0: type = GrB_BOOL    ; break ;
      82           4 :                 case  1: type = GrB_INT8    ; break ;
      83           4 :                 case  2: type = GrB_INT16   ; break ;
      84           4 :                 case  3: type = GrB_INT32   ; break ;
      85           4 :                 case  4: type = GrB_INT64   ; break ;
      86           4 :                 case  5: type = GrB_UINT8   ; break ;
      87           4 :                 case  6: type = GrB_UINT16  ; break ;
      88           4 :                 case  7: type = GrB_UINT32  ; break ;
      89           4 :                 case  8: type = GrB_UINT64  ; break ;
      90           4 :                 case  9: type = GrB_FP32    ; break ;
      91           4 :                 default:
      92           4 :                 case 10: type = GrB_FP64    ; break ;
      93             :             }
      94             : 
      95             :             // typecast A into a different type
      96          44 :             OK (GrB_Matrix_new (&C, type, nrows, ncols)) ;
      97          44 :             OK (GrB_assign (C, NULL, NULL, A,
      98             :                 GrB_ALL, nrows, GrB_ALL, ncols, NULL)) ;
      99             : 
     100             :             // printf ("\nA:\n") ;
     101             :             // OK (LAGraph_Matrix_Print (A, 2, stdout, msg)) ;
     102             : 
     103             :             // printf ("\nC:\n") ;
     104             :             // OK (LAGraph_Matrix_Print (C, 2, stdout, msg)) ;
     105             : 
     106         132 :             for (int is_min = 0 ; is_min <= 1 ; is_min++)
     107             :             {
     108         352 :                 for (int dim = 0 ; dim <= 2 ; dim++)
     109             :                 {
     110             :                     // printf ("\nis_min: %d dim: %d\n", is_min, dim) ;
     111             :                     // test the algorithm
     112         264 :                     OK (LAGraph_argminmax (&x, &p, C, dim, is_min, msg)) ;
     113             :                     // printf ("\nx:\n") ;
     114             :                     // OK (LAGraph_Vector_Print (x, 2, stdout, msg)) ;
     115             :                     // printf ("\np:\n") ;
     116             :                     // OK (LAGraph_Vector_Print (p, 2, stdout, msg)) ;
     117             :                     // check the result
     118         264 :                     OK (LG_check_argminmax (&x2, &p2, C, dim, is_min, msg)) ;
     119             :                     // printf ("\nx2:\n") ;
     120             :                     // OK (LAGraph_Vector_Print (x2, 2, stdout, msg)) ;
     121             :                     // printf ("\np2:\n") ;
     122             :                     // OK (LAGraph_Vector_Print (p2, 2, stdout, msg)) ;
     123         264 :                     bool isequal = false ;
     124             :                     // x and x2 must be equal, for all cases
     125         264 :                     OK (LAGraph_Vector_IsEqual (&isequal, x, x2, msg)) ;
     126         264 :                     TEST_CHECK (isequal) ;
     127         264 :                     uint64_t npvals = 0 ;
     128         264 :                     OK (GrB_Vector_nvals (&npvals, p)) ;
     129         264 :                     if (dim > 0 || npvals == 0)
     130             :                     {
     131             :                         // For dim=1 or dim=2 (row-wise or col-wise), p and p2
     132             :                         // must always match
     133         176 :                         OK (LAGraph_Vector_IsEqual (&isequal, p, p2, msg)) ;
     134         176 :                         TEST_CHECK (isequal) ;
     135             :                     }
     136             :                     else
     137             :                     {
     138             :                         // For dim=0, the result is a single scalar, with
     139             :                         // C(p2[0],p2[1]) being argmin/argmax of C.  The two
     140             :                         // methods may find different places where the min/max
     141             :                         // entry appears in C, if there are ties, so p and p2
     142             :                         // can differ.  Just make sure C(p2[0],p2[1]) is equal
     143             :                         // to x [0].
     144             :                         uint64_t i, j ;
     145          88 :                         TEST_CHECK (npvals == 2) ;
     146          88 :                         GrB_Info info = GrB_Vector_extractElement (&i, p2, 0) ;
     147          88 :                         TEST_CHECK (info >= GrB_SUCCESS) ;
     148          88 :                         info = GrB_Vector_extractElement (&j, p2, 1) ;
     149          88 :                         TEST_CHECK (info >= GrB_SUCCESS) ;
     150          88 :                         double x_1 = 0, x_2 = 1 ;
     151          88 :                         info = GrB_Matrix_extractElement (&x_1, C, i, j) ;
     152          88 :                         TEST_CHECK (info >= GrB_SUCCESS) ;
     153          88 :                         info = GrB_Vector_extractElement (&x_2, x, 0) ;
     154          88 :                         TEST_CHECK (info >= GrB_SUCCESS) ;
     155             :                         // printf ("x_1 %g x_2 %g\n", x_1, x_2) ;
     156          88 :                         TEST_CHECK (x_1 == x_2) ;
     157             :                     }
     158         264 :                     OK (GrB_free (&x)) ;
     159         264 :                     OK (GrB_free (&p)) ;
     160         264 :                     OK (GrB_free (&x2)) ;
     161         264 :                     OK (GrB_free (&p2)) ;
     162             :                 }
     163             :             }
     164          44 :             OK (GrB_free (&C)) ;
     165             :         }
     166           4 :         OK (GrB_free (&A)) ;
     167             :     }
     168             : 
     169             :     //--------------------------------------------------------------------------
     170             :     // finalize LAGraph
     171             :     //--------------------------------------------------------------------------
     172             : 
     173           1 :     LAGraph_Finalize (msg) ;
     174             : #endif
     175           1 : }
     176             : 
     177             : //----------------------------------------------------------------------------
     178             : // test_argminmax_errors
     179             : //----------------------------------------------------------------------------
     180             : 
     181           1 : void test_argminmax_errors (void)
     182             : {
     183             : #if LAGRAPH_SUITESPARSE
     184           1 :     LAGraph_Init (msg) ;
     185           1 :     GrB_Matrix A = NULL ;
     186           1 :     GrB_Vector x = NULL, p = NULL ;
     187           1 :     OK (LAGraph_Random_Matrix (&A, GrB_FP64, 5, 5, 0.5, 1, msg)) ;
     188           1 :     GrB_Info info = LG_check_argminmax (&x, &p, A, 3, true, msg) ;
     189           1 :     TEST_CHECK (info == GrB_INVALID_VALUE) ;
     190           1 :     GrB_free (&A) ;
     191           1 :     LAGraph_Finalize (msg) ;
     192             : #endif
     193           1 : }
     194             : 
     195             : //----------------------------------------------------------------------------
     196             : // the main program is created by acutest, and it runs a list of tests:
     197             : //----------------------------------------------------------------------------
     198             : 
     199             : TEST_LIST =
     200             : {
     201             :     {"argminmax", test_argminmax},
     202             :     {"argminmax_errors", test_argminmax_errors},
     203             :     {NULL, NULL}
     204             : } ;
     205             : 

Generated by: LCOV version 1.14