LCOV - code coverage report
Current view: top level - src/test - test_IsEqual.c (source / functions) Hit Total Coverage
Test: LAGraph code coverage report. Commit id: 3b461aa. Current time (UTC): 2024-01-25T16:04:32Z Lines: 210 210 100.0 %
Date: 2024-01-25 16:05:28 Functions: 6 6 100.0 %

          Line data    Source code
       1             : //------------------------------------------------------------------------------
       2             : // LAGraph/src/test/test_IsEqual.c:  test LAGraph_*_IsEqual
       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             : int status ;
      25             : GrB_Info info ;
      26             : char msg [LAGRAPH_MSG_LEN] ;
      27             : GrB_Matrix A = NULL, B = NULL ;
      28             : GrB_Vector u = NULL, v = NULL ;
      29             : GrB_Type atype = NULL ;
      30             : #define LEN 512
      31             : char filename [LEN+1] ;
      32             : char atype_name [LAGRAPH_MAX_NAME_LEN] ;
      33             : 
      34             : //------------------------------------------------------------------------------
      35             : // test matrices
      36             : //------------------------------------------------------------------------------
      37             : 
      38             : typedef struct
      39             : {
      40             :     bool isequal ;
      41             :     bool isequal0 ;
      42             :     const char *matrix1 ;
      43             :     const char *matrix2 ;
      44             : }
      45             : matrix_info ;
      46             : 
      47             : const matrix_info files [ ] =
      48             : {
      49             :     //   iseq        matrix1             matrix2
      50             :     {    0, 0, "A.mtx"           , "cover.mtx" },
      51             :     {    0, 1, "A.mtx"           , "A2.mtx" },
      52             :     {    0, 0, "cover.mtx"       , "cover_structure.mtx" },
      53             :     {    0, 0, "cover.mtx"       , "cover_structure.mtx" },
      54             :     {    1, 1, "LFAT5.mtx"       , "LFAT5.mtx" },
      55             :     {    0, 0, "sample2.mtx"     , "sample.mtx" },
      56             :     {    1, 1, "sample.mtx"      , "sample.mtx" },
      57             :     {    1, 1, "matrix_int32.mtx", "matrix_int32.mtx" },
      58             :     {    1, 1, "matrix_int32.mtx", "matrix_int32.mtx" },
      59             :     {    0, 0, "matrix_int32.mtx", "matrix_int64.mtx" },
      60             :     {    0, 0, "matrix_int32.mtx", "matrix_int64.mtx" },
      61             :     {    1, 1, "west0067.mtx"    , "west0067_jumbled.mtx" },
      62             :     {    1, 1, "west0067.mtx"    , "west0067_noheader.mtx"},
      63             :     {    0, 0, "LFAT5.mtx"       , "west0067.mtx" },
      64             :     {    0, 0, "empty.mtx"       , "full.mtx" },
      65             :     {    1, 1, "full.mtx"        , "full_noheader.mtx" },
      66             :     {    0, 0, ""                , "" }
      67             : } ;
      68             : 
      69             : //------------------------------------------------------------------------------
      70             : // setup: start a test
      71             : //------------------------------------------------------------------------------
      72             : 
      73           3 : void setup (void)
      74             : {
      75           3 :     OK (LAGraph_Init (msg)) ;
      76           3 : }
      77             : 
      78             : //------------------------------------------------------------------------------
      79             : // teardown: finalize a test
      80             : //------------------------------------------------------------------------------
      81             : 
      82           3 : void teardown (void)
      83             : {
      84           3 :     OK (LAGraph_Finalize (msg)) ;
      85           3 : }
      86             : 
      87             : //------------------------------------------------------------------------------
      88             : // test_IsEqual: test LAGraph_Matrix_IsEqual
      89             : //------------------------------------------------------------------------------
      90             : 
      91           1 : void test_IsEqual (void)
      92             : {
      93             : 
      94             :     //--------------------------------------------------------------------------
      95             :     // start up the test
      96             :     //--------------------------------------------------------------------------
      97             : 
      98           1 :     setup ( ) ;
      99           1 :     printf ("\nTesting IsEqual:\n") ;
     100             : 
     101           1 :     for (int k = 0 ; ; k++)
     102          16 :     {
     103             : 
     104             :         //----------------------------------------------------------------------
     105             :         // load in the kth pair of files
     106             :         //----------------------------------------------------------------------
     107             : 
     108          17 :         const char *aname = files [k].matrix1 ;
     109          17 :         const char *bname = files [k].matrix2 ;
     110          17 :         const bool isequal = files [k].isequal ;
     111          17 :         const bool isequal0 = files [k].isequal0 ;
     112          17 :         if (strlen (aname) == 0) break;
     113          16 :         TEST_CASE (aname) ;
     114          16 :         printf ("test %2d: %s %s\n", k, aname, bname) ;
     115             : 
     116          16 :         snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
     117          16 :         FILE *f = fopen (filename, "r") ;
     118          16 :         TEST_CHECK (f != NULL) ;
     119          16 :         OK (LAGraph_MMRead (&A, f, msg)) ;
     120          16 :         OK (fclose (f)) ;
     121          16 :         TEST_MSG ("Failed to load %s\n", aname) ;
     122             :         GrB_Index ancols ;
     123          16 :         OK (GrB_Matrix_ncols (&ancols, A)) ;
     124             : 
     125          16 :         snprintf (filename, LEN, LG_DATA_DIR "%s", bname) ;
     126          16 :         f = fopen (filename, "r") ;
     127          16 :         TEST_CHECK (f != NULL) ;
     128          16 :         OK (LAGraph_MMRead (&B, f, msg)) ;
     129          16 :         OK (fclose (f)) ;
     130          16 :         TEST_MSG ("Failed to load %s\n", bname) ;
     131             :         GrB_Index bncols ;
     132          16 :         OK (GrB_Matrix_ncols (&bncols, B)) ;
     133             : 
     134             :         //----------------------------------------------------------------------
     135             :         // compare the two matrices
     136             :         //----------------------------------------------------------------------
     137             : 
     138          16 :         bool same = false ;
     139             : 
     140          16 :         OK (LAGraph_Matrix_IsEqual (&same, A, B, msg)) ;
     141          16 :         TEST_CHECK (same == isequal) ;
     142             : 
     143          16 :         OK (LAGraph_Matrix_IsEqual (&same, A, A, msg)) ;
     144          16 :         TEST_CHECK (same == true) ;
     145             : 
     146             :         //----------------------------------------------------------------------
     147             :         // compare the two matrices with a given op
     148             :         //----------------------------------------------------------------------
     149             : 
     150          16 :         OK (LAGraph_Matrix_TypeName (atype_name, A, msg)) ;
     151          16 :         OK (LAGraph_TypeFromName (&atype, atype_name, msg)) ;
     152             : 
     153          16 :         GrB_BinaryOp op = NULL ;
     154          16 :         if      (atype == GrB_BOOL  ) op = GrB_EQ_BOOL   ;
     155          12 :         else if (atype == GrB_INT8  ) op = GrB_EQ_INT8   ;
     156          12 :         else if (atype == GrB_INT16 ) op = GrB_EQ_INT16  ;
     157          12 :         else if (atype == GrB_INT32 ) op = GrB_EQ_INT32  ;
     158           5 :         else if (atype == GrB_INT64 ) op = GrB_EQ_INT64  ;
     159           5 :         else if (atype == GrB_UINT8 ) op = GrB_EQ_UINT8  ;
     160           5 :         else if (atype == GrB_UINT16) op = GrB_EQ_UINT16 ;
     161           5 :         else if (atype == GrB_UINT32) op = GrB_EQ_UINT32 ;
     162           5 :         else if (atype == GrB_UINT64) op = GrB_EQ_UINT64 ;
     163           5 :         else if (atype == GrB_FP32  ) op = GrB_EQ_FP32   ;
     164           5 :         else if (atype == GrB_FP64  ) op = GrB_EQ_FP64   ;
     165             : 
     166          16 :         OK (LAGraph_Matrix_IsEqualOp (&same, A, B, op, msg)) ;
     167          16 :         TEST_CHECK (same == isequal) ;
     168             : 
     169          16 :         OK (LAGraph_Matrix_IsEqualOp (&same, A, A, op, msg)) ;
     170          16 :         TEST_CHECK (same == true) ;
     171             : 
     172             :         //----------------------------------------------------------------------
     173             :         // compare two vectors
     174             :         //----------------------------------------------------------------------
     175             : 
     176          16 :         OK (GrB_Vector_new (&u, atype, ancols)) ;
     177          16 :         OK (GrB_Vector_new (&v, atype, bncols)) ;
     178          16 :         OK (GrB_Col_extract (u, NULL, NULL, A, GrB_ALL, ancols, 0,
     179             :             GrB_DESC_T0)) ;
     180          16 :         OK (GrB_Col_extract (v, NULL, NULL, B, GrB_ALL, bncols, 0,
     181             :             GrB_DESC_T0)) ;
     182             : 
     183          16 :         OK (LAGraph_Vector_IsEqual (&same, u, v, msg)) ;
     184          16 :         TEST_CHECK (same == isequal0) ;
     185             : 
     186          16 :         OK (LAGraph_Vector_IsEqual (&same, u, u, msg)) ;
     187          16 :         TEST_CHECK (same == true) ;
     188             : 
     189          16 :         OK (LAGraph_Vector_IsEqual (&same, u, u, msg)) ;
     190          16 :         TEST_CHECK (same == true) ;
     191             : 
     192          16 :         OK (GrB_free (&u)) ;
     193          16 :         OK (GrB_free (&v)) ;
     194          16 :         OK (GrB_free (&A)) ;
     195          16 :         OK (GrB_free (&B)) ;
     196             :     }
     197             : 
     198             :     //--------------------------------------------------------------------------
     199             :     // finish the test
     200             :     //--------------------------------------------------------------------------
     201             : 
     202           1 :     teardown ( ) ;
     203           1 : }
     204             : 
     205             : //------------------------------------------------------------------------------
     206             : // test_IsEqual_brutal:
     207             : //------------------------------------------------------------------------------
     208             : 
     209             : #if LAGRAPH_SUITESPARSE
     210           1 : void test_IsEqual_brutal (void)
     211             : {
     212             : 
     213             :     //--------------------------------------------------------------------------
     214             :     // start up the test
     215             :     //--------------------------------------------------------------------------
     216             : 
     217           1 :     OK (LG_brutal_setup (msg)) ;
     218           1 :     printf ("\nTesting IsEqual:\n") ;
     219           1 :     GxB_set (GxB_BURBLE, false) ;
     220             : 
     221           1 :     for (int k = 0 ; ; k++)
     222          16 :     {
     223             : 
     224             :         //----------------------------------------------------------------------
     225             :         // load in the kth pair of files
     226             :         //----------------------------------------------------------------------
     227             : 
     228          17 :         const char *aname = files [k].matrix1 ;
     229          17 :         const char *bname = files [k].matrix2 ;
     230          17 :         const bool isequal = files [k].isequal ;
     231          17 :         const bool isequal0 = files [k].isequal0 ;
     232          17 :         if (strlen (aname) == 0) break;
     233          16 :         TEST_CASE (aname) ;
     234          16 :         printf ("test %2d: %s %s\n", k, aname, bname) ;
     235             : 
     236          16 :         snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
     237          16 :         FILE *f = fopen (filename, "r") ;
     238          16 :         TEST_CHECK (f != NULL) ;
     239          16 :         OK (LAGraph_MMRead (&A, f, msg)) ;
     240          16 :         OK (fclose (f)) ;
     241          16 :         TEST_MSG ("Failed to load %s\n", aname) ;
     242             :         GrB_Index ancols ;
     243          16 :         OK (GrB_Matrix_ncols (&ancols, A)) ;
     244             : 
     245          16 :         snprintf (filename, LEN, LG_DATA_DIR "%s", bname) ;
     246          16 :         f = fopen (filename, "r") ;
     247          16 :         TEST_CHECK (f != NULL) ;
     248          16 :         OK (LAGraph_MMRead (&B, f, msg)) ;
     249          16 :         OK (fclose (f)) ;
     250          16 :         TEST_MSG ("Failed to load %s\n", bname) ;
     251             :         GrB_Index bncols ;
     252          16 :         OK (GrB_Matrix_ncols (&bncols, B)) ;
     253             : 
     254             :         //----------------------------------------------------------------------
     255             :         // compare the two matrices
     256             :         //----------------------------------------------------------------------
     257             : 
     258          16 :         bool same = false ;
     259             : 
     260          59 :         LG_BRUTAL (LAGraph_Matrix_IsEqual (&same, A, B, msg)) ;
     261          16 :         TEST_CHECK (same == isequal) ;
     262             : 
     263          16 :         LG_BRUTAL (LAGraph_Matrix_IsEqual (&same, A, A, msg)) ;
     264          16 :         TEST_CHECK (same == true) ;
     265             : 
     266             :         //----------------------------------------------------------------------
     267             :         // compare the two matrices with a given op
     268             :         //----------------------------------------------------------------------
     269             : 
     270          16 :         LG_BRUTAL (LAGraph_Matrix_TypeName (atype_name, A, msg)) ;
     271          16 :         LG_BRUTAL (LAGraph_TypeFromName (&atype, atype_name, msg)) ;
     272             : 
     273          16 :         GrB_BinaryOp op = NULL ;
     274          16 :         if      (atype == GrB_BOOL  ) op = GrB_EQ_BOOL   ;
     275          12 :         else if (atype == GrB_INT8  ) op = GrB_EQ_INT8   ;
     276          12 :         else if (atype == GrB_INT16 ) op = GrB_EQ_INT16  ;
     277          12 :         else if (atype == GrB_INT32 ) op = GrB_EQ_INT32  ;
     278           5 :         else if (atype == GrB_INT64 ) op = GrB_EQ_INT64  ;
     279           5 :         else if (atype == GrB_UINT8 ) op = GrB_EQ_UINT8  ;
     280           5 :         else if (atype == GrB_UINT16) op = GrB_EQ_UINT16 ;
     281           5 :         else if (atype == GrB_UINT32) op = GrB_EQ_UINT32 ;
     282           5 :         else if (atype == GrB_UINT64) op = GrB_EQ_UINT64 ;
     283           5 :         else if (atype == GrB_FP32  ) op = GrB_EQ_FP32   ;
     284           5 :         else if (atype == GrB_FP64  ) op = GrB_EQ_FP64   ;
     285             : 
     286          79 :         LG_BRUTAL (LAGraph_Matrix_IsEqualOp (&same, A, B, op, msg)) ;
     287          16 :         TEST_CHECK (same == isequal) ;
     288             : 
     289          16 :         LG_BRUTAL (LAGraph_Matrix_IsEqualOp (&same, A, A, op, msg)) ;
     290          16 :         TEST_CHECK (same == true) ;
     291             : 
     292             :         //----------------------------------------------------------------------
     293             :         // compare two vectors
     294             :         //----------------------------------------------------------------------
     295             : 
     296          48 :         LG_BRUTAL (GrB_Vector_new (&u, atype, ancols)) ;
     297          48 :         LG_BRUTAL (GrB_Vector_new (&v, atype, bncols)) ;
     298          78 :         LG_BRUTAL (GrB_Col_extract (u, NULL, NULL, A, GrB_ALL, ancols, 0,
     299             :             GrB_DESC_T0)) ;
     300          86 :         LG_BRUTAL (GrB_Col_extract (v, NULL, NULL, B, GrB_ALL, bncols, 0,
     301             :             GrB_DESC_T0)) ;
     302             : 
     303          63 :         LG_BRUTAL (LAGraph_Vector_IsEqual (&same, u, v, msg)) ;
     304          16 :         TEST_CHECK (same == isequal0) ;
     305             : 
     306          16 :         LG_BRUTAL (LAGraph_Vector_IsEqual (&same, u, u, msg)) ;
     307          16 :         TEST_CHECK (same == true) ;
     308             : 
     309          16 :         LG_BRUTAL (LAGraph_Vector_IsEqual (&same, u, u, msg)) ;
     310          16 :         TEST_CHECK (same == true) ;
     311             : 
     312          16 :         LG_BRUTAL (GrB_free (&u)) ;
     313          16 :         OK (GrB_free (&v)) ;
     314          16 :         OK (GrB_free (&A)) ;
     315          16 :         OK (GrB_free (&B)) ;
     316             :     }
     317             : 
     318             :     //--------------------------------------------------------------------------
     319             :     // finish the test
     320             :     //--------------------------------------------------------------------------
     321             : 
     322           1 :     OK (LG_brutal_teardown (msg)) ;
     323           1 : }
     324             : #endif
     325             : 
     326             : //------------------------------------------------------------------------------
     327             : // test_IsEqual_failures: test error handling of LAGraph_Matrix_IsEqual*
     328             : //------------------------------------------------------------------------------
     329             : 
     330             : typedef int myint ;
     331             : 
     332           1 : void test_IsEqual_failures (void)
     333             : {
     334           1 :     setup ( ) ;
     335           1 :     printf ("\nTest IsEqual: error handling and special cases\n") ;
     336             : 
     337           1 :     bool same = false ;
     338             :     // not a failure, but a special case:
     339           1 :     OK (LAGraph_Matrix_IsEqual (&same, NULL, NULL, msg)) ;
     340           1 :     TEST_CHECK (same == true) ;
     341             : 
     342           1 :     OK (LAGraph_Vector_IsEqual (&same, NULL, NULL, msg)) ;
     343           1 :     TEST_CHECK (same == true) ;
     344             : 
     345           1 :     int result = LAGraph_Matrix_IsEqual (NULL, NULL, NULL, msg) ;
     346           1 :     TEST_CHECK (result == GrB_NULL_POINTER) ;
     347           1 :     printf ("msg: %s\n", msg) ;
     348             : 
     349           1 :     result = LAGraph_Matrix_IsEqual (NULL, NULL, NULL, msg) ;
     350           1 :     TEST_CHECK (result == GrB_NULL_POINTER) ;
     351           1 :     printf ("msg: %s\n", msg) ;
     352             : 
     353           1 :     OK (GrB_Matrix_new (&A, GrB_BOOL, 2, 2)) ;
     354           1 :     OK (GrB_Matrix_new (&B, GrB_BOOL, 2, 2)) ;
     355             : 
     356           1 :     OK (GrB_Vector_new (&u, GrB_BOOL, 2)) ;
     357           1 :     OK (GrB_Vector_new (&v, GrB_BOOL, 2)) ;
     358             : 
     359           1 :     result = LAGraph_Matrix_IsEqual (NULL, A, B, msg) ;
     360           1 :     TEST_CHECK (result == GrB_NULL_POINTER) ;
     361           1 :     printf ("msg: %s\n", msg) ;
     362             : 
     363           1 :     result = LAGraph_Matrix_IsEqualOp (&same, A, B, NULL, msg) ;
     364           1 :     TEST_CHECK (result == GrB_NULL_POINTER) ;
     365           1 :     printf ("msg: %s\n", msg) ;
     366             : 
     367           1 :     result = LAGraph_Vector_IsEqual (NULL, u, v, msg) ;
     368           1 :     TEST_CHECK (result == GrB_NULL_POINTER) ;
     369           1 :     printf ("msg: %s\n", msg) ;
     370             : 
     371           1 :     result = LAGraph_Vector_IsEqualOp (&same, u, v, NULL, msg) ;
     372           1 :     TEST_CHECK (result == GrB_NULL_POINTER) ;
     373           1 :     printf ("msg: %s\n", msg) ;
     374             : 
     375           1 :     result = LAGraph_Matrix_IsEqual (NULL, A, B, msg) ;
     376           1 :     TEST_CHECK (result == GrB_NULL_POINTER) ;
     377           1 :     printf ("msg: %s\n", msg) ;
     378             : 
     379           1 :     result = LAGraph_Matrix_IsEqual (NULL, A, B, msg) ;
     380           1 :     TEST_CHECK (result == GrB_NULL_POINTER) ;
     381           1 :     printf ("msg: %s\n", msg) ;
     382             : 
     383           1 :     OK (LAGraph_Matrix_IsEqual (&same, A, B, msg)) ;
     384           1 :     TEST_CHECK (same == true) ;
     385             : 
     386           1 :     OK (GrB_free (&u)) ;
     387           1 :     OK (GrB_free (&v)) ;
     388           1 :     OK (GrB_free (&A)) ;
     389           1 :     OK (GrB_free (&B)) ;
     390           1 :     teardown ( ) ;
     391           1 : }
     392             : 
     393             : //------------------------------------------------------------------------------
     394             : // test_Vector_IsEqual: test LAGraph_Vector_isEqual
     395             : //------------------------------------------------------------------------------
     396             : 
     397           1 : void test_Vector_IsEqual (void)
     398             : {
     399           1 :     setup ( ) ;
     400             : 
     401           1 :     bool same = false ;
     402           1 :     OK (LAGraph_Vector_IsEqualOp (&same, NULL, NULL, GrB_EQ_BOOL, msg)) ;
     403           1 :     TEST_CHECK (same == true) ;
     404             : 
     405           1 :     OK (GrB_Vector_new (&u, GrB_BOOL, 3)) ;
     406           1 :     OK (GrB_Vector_new (&v, GrB_BOOL, 2)) ;
     407             : 
     408           1 :     OK (LAGraph_Vector_IsEqualOp (&same, u, v, GrB_EQ_BOOL, msg)) ;
     409           1 :     TEST_CHECK (same == false) ;
     410             : 
     411           1 :     OK (GrB_free (&u)) ;
     412           1 :     OK (GrB_Vector_new (&u, GrB_BOOL, 2)) ;
     413             : 
     414           1 :     OK (LAGraph_Vector_IsEqualOp (&same, u, v, GrB_EQ_BOOL, msg)) ;
     415           1 :     TEST_CHECK (same == true) ;
     416             : 
     417           1 :     OK (GrB_Vector_setElement (u, true, 0)) ;
     418           1 :     OK (GrB_Vector_setElement (v, true, 1)) ;
     419           1 :     OK (LAGraph_Vector_IsEqualOp (&same, u, v, GrB_EQ_BOOL, msg)) ;
     420           1 :     TEST_CHECK (same == false) ;
     421             : 
     422           1 :     OK (LAGraph_Vector_IsEqual (&same, u, v, msg)) ;
     423           1 :     TEST_CHECK (same == false) ;
     424             : 
     425           1 :     OK (GrB_free (&u)) ;
     426           1 :     OK (GrB_free (&v)) ;
     427             : 
     428           1 :     OK (GrB_Vector_new (&u, GrB_BOOL, 3)) ;
     429           1 :     OK (GrB_Vector_new (&v, GrB_FP32, 3)) ;
     430           1 :     OK (LAGraph_Vector_IsEqual (&same, u, v, msg)) ;
     431           1 :     TEST_CHECK (same == false) ;
     432             : 
     433           1 :     OK (GrB_free (&u)) ;
     434           1 :     OK (GrB_free (&v)) ;
     435             : 
     436           1 :     teardown ( ) ;
     437           1 : }
     438             : 
     439             : //------------------------------------------------------------------------------
     440             : // TEST_LIST: the list of tasks for this entire test
     441             : //------------------------------------------------------------------------------
     442             : 
     443             : TEST_LIST =
     444             : {
     445             :     { "IsEqual", test_IsEqual },
     446             :     { "Vector_IsEqual", test_Vector_IsEqual },
     447             :     { "IsEqual_failures", test_IsEqual_failures },
     448             :     #if LAGRAPH_SUITESPARSE
     449             :     { "IsEqual_brutal", test_IsEqual_brutal },
     450             :     #endif
     451             :     { NULL, NULL }
     452             : } ;

Generated by: LCOV version 1.14