1  /* frecnt-2.c -- Test for decoder in libsframe.
       2  
       3     Copyright (C) 2022-2023 Free Software Foundation, Inc.
       4  
       5     This program is free software; you can redistribute it and/or modify
       6     it under the terms of the GNU General Public License as published by
       7     the Free Software Foundation; either version 3 of the License, or
       8     (at your option) any later version.
       9  
      10     This program is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13     GNU General Public License for more details.
      14  
      15     You should have received a copy of the GNU General Public License
      16     along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      17  
      18  #include "config.h"
      19  
      20  #include <stdio.h>
      21  #include <stdlib.h>
      22  #include <sys/stat.h>
      23  
      24  #include "sframe-api.h"
      25  
      26  /* DejaGnu should not use gnulib's vsnprintf replacement here.  */
      27  #undef vsnprintf
      28  #include <dejagnu.h>
      29  
      30  /*
      31   * SFrame info from the following source (2 fde 8 fres):
      32   * static int cnt;
      33   * int foo() { return ++cnt; }
      34   * int main() { return foo(); }
      35   */
      36  #define DATA	"DATA2"
      37  
      38  int
      39  main (void)
      40  {
      41    sframe_decoder_ctx *dctx = NULL;
      42    uint32_t nfres, fsize;
      43    int32_t fstart;
      44    unsigned char finfo;
      45    unsigned int i;
      46    int err = 0;
      47    FILE *fp;
      48    struct stat st;
      49    char *sf_buf;
      50    size_t sf_size;
      51  
      52  #define TEST(name, cond)                                                      \
      53    do                                                                          \
      54      {                                                                         \
      55        if (cond)                                                               \
      56  	pass (name);                                                          \
      57        else                                                                    \
      58  	fail (name);                                                          \
      59      }                                                                         \
      60      while (0)
      61  
      62    fp = fopen (DATA, "r");
      63    if (fp == NULL)
      64      goto setup_fail;
      65    if (fstat (fileno (fp), &st) < 0)
      66      {
      67        perror ("fstat");
      68        fclose (fp);
      69        goto setup_fail;
      70      }
      71    sf_buf = malloc (st.st_size);
      72    if (sf_buf == NULL)
      73      {
      74        perror ("malloc");
      75        goto setup_fail;
      76      }
      77  
      78    /* Execute tests.  */
      79    sf_size = fread (sf_buf, 1, st.st_size, fp);
      80    fclose (fp);
      81    TEST ("frecnt-2: Read data", sf_size != 0);
      82  
      83    dctx = sframe_decode (sf_buf, sf_size, &err);
      84    TEST ("frecnt-2: Decode setup", dctx != NULL);
      85  
      86    unsigned int fde_cnt = sframe_decoder_get_num_fidx (dctx);
      87    TEST ("frecnt-2: Decode FDE count", fde_cnt == 2);
      88  
      89    for (i = 0; i < fde_cnt; ++i)
      90      {
      91        err = sframe_decoder_get_funcdesc (dctx, i, &nfres, &fsize, &fstart,
      92  					 &finfo);
      93        TEST ("frecnt-2: Decode get FDE", err == 0);
      94        TEST ("frecnt-2: Decode get FRE", nfres == 4);
      95      }
      96  
      97    free (sf_buf);
      98    sf_buf = NULL;
      99  
     100    sframe_decoder_free (&dctx);
     101    return 0;
     102  
     103  setup_fail:
     104    sframe_decoder_free (&dctx);
     105    fail ("frecnt-2: Test setup");
     106    return 1;
     107  }