xref: /netbsd-src/external/gpl3/gcc/dist/libbacktrace/unittest.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1181254a7Smrg /* unittest.c -- Test for libbacktrace library
2*b1e83836Smrg    Copyright (C) 2018-2022 Free Software Foundation, Inc.
3181254a7Smrg 
4181254a7Smrg Redistribution and use in source and binary forms, with or without
5181254a7Smrg modification, are permitted provided that the following conditions are
6181254a7Smrg met:
7181254a7Smrg 
8181254a7Smrg     (1) Redistributions of source code must retain the above copyright
9181254a7Smrg     notice, this list of conditions and the following disclaimer.
10181254a7Smrg 
11181254a7Smrg     (2) Redistributions in binary form must reproduce the above copyright
12181254a7Smrg     notice, this list of conditions and the following disclaimer in
13181254a7Smrg     the documentation and/or other materials provided with the
14181254a7Smrg     distribution.
15181254a7Smrg 
16181254a7Smrg     (3) The name of the author may not be used to
17181254a7Smrg     endorse or promote products derived from this software without
18181254a7Smrg     specific prior written permission.
19181254a7Smrg 
20181254a7Smrg THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21181254a7Smrg IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22181254a7Smrg WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23181254a7Smrg DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24181254a7Smrg INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25181254a7Smrg (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26181254a7Smrg SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27181254a7Smrg HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28181254a7Smrg STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29181254a7Smrg IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30181254a7Smrg POSSIBILITY OF SUCH DAMAGE.  */
31181254a7Smrg 
32181254a7Smrg #include <assert.h>
33181254a7Smrg #include <stdio.h>
34181254a7Smrg #include <stdlib.h>
35181254a7Smrg #include <string.h>
36181254a7Smrg #include <unistd.h>
37181254a7Smrg 
38181254a7Smrg #include "filenames.h"
39181254a7Smrg 
40181254a7Smrg #include "backtrace.h"
41181254a7Smrg #include "backtrace-supported.h"
42181254a7Smrg 
43181254a7Smrg #include "testlib.h"
44181254a7Smrg 
45181254a7Smrg #include "internal.h"
46181254a7Smrg 
47181254a7Smrg static unsigned count;
48181254a7Smrg 
49181254a7Smrg static void
error_callback(void * vdata ATTRIBUTE_UNUSED,const char * msg ATTRIBUTE_UNUSED,int errnum ATTRIBUTE_UNUSED)50181254a7Smrg error_callback (void *vdata ATTRIBUTE_UNUSED, const char *msg ATTRIBUTE_UNUSED,
51181254a7Smrg 		int errnum ATTRIBUTE_UNUSED)
52181254a7Smrg {
53181254a7Smrg   ++count;
54181254a7Smrg }
55181254a7Smrg 
56181254a7Smrg static int
test1(void)57181254a7Smrg test1 (void)
58181254a7Smrg {
59181254a7Smrg   int res;
60181254a7Smrg   int failed;
61181254a7Smrg 
62181254a7Smrg   struct backtrace_vector vec;
63181254a7Smrg 
64181254a7Smrg   memset (&vec, 0, sizeof vec);
65181254a7Smrg 
66181254a7Smrg   backtrace_vector_grow (state, 100, error_callback, NULL, &vec);
67181254a7Smrg   vec.alc += vec.size;
68181254a7Smrg   vec.size = 0;
69181254a7Smrg 
70181254a7Smrg   count = 0;
71181254a7Smrg   res = backtrace_vector_release (state, &vec, error_callback, NULL);
72181254a7Smrg   failed = res != 1 || count != 0 || vec.base != NULL;
73181254a7Smrg 
74181254a7Smrg   printf ("%s: unittest backtrace_vector_release size == 0\n",
75181254a7Smrg 	  failed ? "FAIL": "PASS");
76181254a7Smrg 
77181254a7Smrg   if (failed)
78181254a7Smrg     ++failures;
79181254a7Smrg 
80181254a7Smrg   return failures;
81181254a7Smrg }
82181254a7Smrg 
83181254a7Smrg int
main(int argc ATTRIBUTE_UNUSED,char ** argv)84181254a7Smrg main (int argc ATTRIBUTE_UNUSED, char **argv)
85181254a7Smrg {
86181254a7Smrg   state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
87181254a7Smrg 				  error_callback_create, NULL);
88181254a7Smrg 
89181254a7Smrg   test1 ();
90181254a7Smrg 
91181254a7Smrg   exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
92181254a7Smrg }
93