xref: /netbsd-src/external/gpl3/gcc.old/dist/libbacktrace/unittest.c (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1627f7eb2Smrg /* unittest.c -- Test for libbacktrace library
2*4c3eb207Smrg    Copyright (C) 2018-2020 Free Software Foundation, Inc.
3627f7eb2Smrg 
4627f7eb2Smrg Redistribution and use in source and binary forms, with or without
5627f7eb2Smrg modification, are permitted provided that the following conditions are
6627f7eb2Smrg met:
7627f7eb2Smrg 
8627f7eb2Smrg     (1) Redistributions of source code must retain the above copyright
9627f7eb2Smrg     notice, this list of conditions and the following disclaimer.
10627f7eb2Smrg 
11627f7eb2Smrg     (2) Redistributions in binary form must reproduce the above copyright
12627f7eb2Smrg     notice, this list of conditions and the following disclaimer in
13627f7eb2Smrg     the documentation and/or other materials provided with the
14627f7eb2Smrg     distribution.
15627f7eb2Smrg 
16627f7eb2Smrg     (3) The name of the author may not be used to
17627f7eb2Smrg     endorse or promote products derived from this software without
18627f7eb2Smrg     specific prior written permission.
19627f7eb2Smrg 
20627f7eb2Smrg THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21627f7eb2Smrg IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22627f7eb2Smrg WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23627f7eb2Smrg DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24627f7eb2Smrg INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25627f7eb2Smrg (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26627f7eb2Smrg SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27627f7eb2Smrg HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28627f7eb2Smrg STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29627f7eb2Smrg IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30627f7eb2Smrg POSSIBILITY OF SUCH DAMAGE.  */
31627f7eb2Smrg 
32627f7eb2Smrg #include <assert.h>
33627f7eb2Smrg #include <stdio.h>
34627f7eb2Smrg #include <stdlib.h>
35627f7eb2Smrg #include <string.h>
36627f7eb2Smrg #include <unistd.h>
37627f7eb2Smrg 
38627f7eb2Smrg #include "filenames.h"
39627f7eb2Smrg 
40627f7eb2Smrg #include "backtrace.h"
41627f7eb2Smrg #include "backtrace-supported.h"
42627f7eb2Smrg 
43627f7eb2Smrg #include "testlib.h"
44627f7eb2Smrg 
45627f7eb2Smrg #include "internal.h"
46627f7eb2Smrg 
47627f7eb2Smrg static unsigned count;
48627f7eb2Smrg 
49627f7eb2Smrg static void
error_callback(void * vdata ATTRIBUTE_UNUSED,const char * msg ATTRIBUTE_UNUSED,int errnum ATTRIBUTE_UNUSED)50627f7eb2Smrg error_callback (void *vdata ATTRIBUTE_UNUSED, const char *msg ATTRIBUTE_UNUSED,
51627f7eb2Smrg 		int errnum ATTRIBUTE_UNUSED)
52627f7eb2Smrg {
53627f7eb2Smrg   ++count;
54627f7eb2Smrg }
55627f7eb2Smrg 
56627f7eb2Smrg static int
test1(void)57627f7eb2Smrg test1 (void)
58627f7eb2Smrg {
59627f7eb2Smrg   int res;
60627f7eb2Smrg   int failed;
61627f7eb2Smrg 
62627f7eb2Smrg   struct backtrace_vector vec;
63627f7eb2Smrg 
64627f7eb2Smrg   memset (&vec, 0, sizeof vec);
65627f7eb2Smrg 
66627f7eb2Smrg   backtrace_vector_grow (state, 100, error_callback, NULL, &vec);
67627f7eb2Smrg   vec.alc += vec.size;
68627f7eb2Smrg   vec.size = 0;
69627f7eb2Smrg 
70627f7eb2Smrg   count = 0;
71627f7eb2Smrg   res = backtrace_vector_release (state, &vec, error_callback, NULL);
72627f7eb2Smrg   failed = res != 1 || count != 0 || vec.base != NULL;
73627f7eb2Smrg 
74627f7eb2Smrg   printf ("%s: unittest backtrace_vector_release size == 0\n",
75627f7eb2Smrg 	  failed ? "FAIL": "PASS");
76627f7eb2Smrg 
77627f7eb2Smrg   if (failed)
78627f7eb2Smrg     ++failures;
79627f7eb2Smrg 
80627f7eb2Smrg   return failures;
81627f7eb2Smrg }
82627f7eb2Smrg 
83627f7eb2Smrg int
main(int argc ATTRIBUTE_UNUSED,char ** argv)84627f7eb2Smrg main (int argc ATTRIBUTE_UNUSED, char **argv)
85627f7eb2Smrg {
86627f7eb2Smrg   state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
87627f7eb2Smrg 				  error_callback_create, NULL);
88627f7eb2Smrg 
89627f7eb2Smrg   test1 ();
90627f7eb2Smrg 
91627f7eb2Smrg   exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
92627f7eb2Smrg }
93