Debian Codegraph @

libsframe/testsuite/libsframe.decode/frecnt-2.c

raw
1 /* frecnt-2.c -- Test for decoder in libsframe.
2
3 Copyright (C) 2022-2025 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 uint8_t rep_block_size;
52
53 #define TEST(name, cond) \
54 do \
55 { \
56 if (cond) \
57 pass (name); \
58 else \
59 fail (name); \
60 } \
61 while (0)
62
63 fp = fopen (DATA, "r");
64 if (fp == NULL)
65 goto setup_fail;
66 if (fstat (fileno (fp), &st) < 0)
67 {
68 perror ("fstat");
69 fclose (fp);
70 goto setup_fail;
71 }
72 sf_buf = malloc (st.st_size);
73 if (sf_buf == NULL)
74 {
75 perror ("malloc");
76 goto setup_fail;
77 }
78
79 /* Execute tests. */
80 sf_size = fread (sf_buf, 1, st.st_size, fp);
81 fclose (fp);
82 TEST ("frecnt-2: Read data", sf_size != 0);
83
84 dctx = sframe_decode (sf_buf, sf_size, &err);
85 TEST ("frecnt-2: Decode setup", dctx != NULL);
86
87 unsigned int fde_cnt = sframe_decoder_get_num_fidx (dctx);
88 TEST ("frecnt-2: Decode FDE count", fde_cnt == 2);
89
90 for (i = 0; i < fde_cnt; ++i)
91 {
92 err = sframe_decoder_get_funcdesc_v2 (dctx, i, &nfres, &fsize, &fstart,
93 &finfo, &rep_block_size);
94 TEST ("frecnt-2: Decode get FDE", err == 0);
95 TEST ("frecnt-2: Decode get FRE", nfres == 4);
96 }
97
98 free (sf_buf);
99 sf_buf = NULL;
100
101 sframe_decoder_free (&dctx);
102 return 0;
103
104 setup_fail:
105 sframe_decoder_free (&dctx);
106 fail ("frecnt-2: Test setup");
107 return 1;
108 }