unittest.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* unittest.c -- Test for libbacktrace library
  2. Copyright (C) 2018-2021 Free Software Foundation, Inc.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are
  5. met:
  6. (1) Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. (2) Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the
  11. distribution.
  12. (3) The name of the author may not be used to
  13. endorse or promote products derived from this software without
  14. specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE. */
  26. #include <assert.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include "filenames.h"
  32. #include "backtrace.h"
  33. #include "backtrace-supported.h"
  34. #include "testlib.h"
  35. #include "internal.h"
  36. static unsigned count;
  37. static void
  38. error_callback (void *vdata ATTRIBUTE_UNUSED, const char *msg ATTRIBUTE_UNUSED,
  39. int errnum ATTRIBUTE_UNUSED)
  40. {
  41. ++count;
  42. }
  43. static int
  44. test1 (void)
  45. {
  46. int res;
  47. int failed;
  48. struct backtrace_vector vec;
  49. memset (&vec, 0, sizeof vec);
  50. backtrace_vector_grow (state, 100, error_callback, NULL, &vec);
  51. vec.alc += vec.size;
  52. vec.size = 0;
  53. count = 0;
  54. res = backtrace_vector_release (state, &vec, error_callback, NULL);
  55. failed = res != 1 || count != 0 || vec.base != NULL;
  56. printf ("%s: unittest backtrace_vector_release size == 0\n",
  57. failed ? "FAIL": "PASS");
  58. if (failed)
  59. ++failures;
  60. return failures;
  61. }
  62. int
  63. main (int argc ATTRIBUTE_UNUSED, char **argv)
  64. {
  65. state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
  66. error_callback_create, NULL);
  67. test1 ();
  68. exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
  69. }