]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - libsframe/testsuite/libsframe.decode/frecnt-1.c
libsframe: testsuite: fix memory leaks in testcases
[thirdparty/binutils-gdb.git] / libsframe / testsuite / libsframe.decode / frecnt-1.c
CommitLineData
19e559f1
WP
1/* frecnt-1.c -- Test for decoder in libsframe.
2
3 Copyright (C) 2022 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
37int
38main (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
50#define TEST(name, cond) \
51 do \
52 { \
53 if (cond) \
54 pass (name); \
55 else \
56 fail (name); \
57 } \
58 while (0)
59
60 /* Test Setup. */
61 fp = fopen (DATA, "r");
62 if (fp == NULL)
63 goto setup_fail;
64 if (fstat (fileno (fp), &st) < 0)
65 {
66 perror ("fstat");
67 fclose (fp);
68 goto setup_fail;
69 }
70 sf_buf = malloc (st.st_size);
71 if (sf_buf == NULL)
72 {
73 perror ("malloc");
74 goto setup_fail;
75 }
76
77 /* Execute tests. */
78 sf_size = fread (sf_buf, 1, st.st_size, fp);
79 fclose (fp);
80 TEST ("frecnt-1: Read data", sf_size != 0);
81
82 dctx = sframe_decode (sf_buf, sf_size, &err);
83 TEST ("frecnt-1: Decoder setup", dctx != NULL);
84
85 unsigned int fde_cnt = sframe_decoder_get_num_fidx (dctx);
86 TEST ("frecnt-1: Decoder FDE count", fde_cnt == 1);
87
88 err = sframe_decoder_get_funcdesc (dctx, 0, &nfres, &fsize, &fstart, &finfo);
89 TEST ("frecnt-1: Decoder get FDE", err == 0);
90 TEST ("frecnt-1: Decoder FRE count", nfres == 4);
91
28f1a767
IB
92 free (sf_buf);
93 sf_buf = NULL;
94
19e559f1
WP
95 sframe_decoder_free (&dctx);
96 return 0;
97
98setup_fail:
99 sframe_decoder_free (&dctx);
100 fail ("frecnt-1: Test setup");
101 return 1;
102}