Debian Codegraph @

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

raw
1 /* frecnt-1.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 (1 fde 4 fres):
32 * static int cnt;
33 * int main() { cnt++; return (cnt); }
34 */
35 #define DATA "DATA1"
36
37 int
38 main (void)
39 {
40 sframe_decoder_ctx *dctx = NULL;
41 uint32_t nfres, fsize;
42 int32_t fstart;
43 unsigned char finfo;
44 int err = 0;
45 FILE *fp;
46 struct stat st;
47 char *sf_buf;
48 size_t sf_size;
49 uint8_t rep_block_size;
50
51 #define TEST(name, cond) \
52 do \
53 { \
54 if (cond) \
55 pass (name); \
56 else \
57 fail (name); \
58 } \
59 while (0)
60
61 /* Test Setup. */
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-1: Read data", sf_size != 0);
82
83 dctx = sframe_decode (sf_buf, sf_size, &err);
84 TEST ("frecnt-1: Decoder setup", dctx != NULL);
85
86 unsigned int fde_cnt = sframe_decoder_get_num_fidx (dctx);
87 TEST ("frecnt-1: Decoder FDE count", fde_cnt == 1);
88
89 err = sframe_decoder_get_funcdesc_v2 (dctx, 0, &nfres, &fsize, &fstart,
90 &finfo, &rep_block_size);
91 TEST ("frecnt-1: Decoder get FDE", err == 0);
92 TEST ("frecnt-1: Decoder FRE count", nfres == 4);
93
94 free (sf_buf);
95 sf_buf = NULL;
96
97 sframe_decoder_free (&dctx);
98 return 0;
99
100 setup_fail:
101 sframe_decoder_free (&dctx);
102 fail ("frecnt-1: Test setup");
103 return 1;
104 }