xvfb-wrapper.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright © 2014 Ran Benita <ran234@gmail.com>
  3. * Copyright © 2023 Pierre Le Marre <dev@wismill.eu>
  4. * SPDX-License-Identifier: MIT
  5. */
  6. /*
  7. *This is a wrapper around X11 tests to make it faster to use for the simple
  8. * type of test cases.
  9. *
  10. * Use with the X11_TEST macro like this:
  11. *
  12. * X11_TEST(some_test) {
  13. * return 0;
  14. * }
  15. *
  16. * int main(void) {
  17. * return x11_tests_run(void);
  18. * }
  19. *
  20. */
  21. #pragma once
  22. #include "config.h"
  23. typedef int (* x11_test_func_t)(const char* display, void *private);
  24. int xvfb_wrapper(x11_test_func_t f, void *private);
  25. int x11_tests_run(void);
  26. struct test_function {
  27. const char *name; /* function name */
  28. const char *file; /* file name */
  29. x11_test_func_t func; /* test function */
  30. } __attribute__((aligned(16)));
  31. /**
  32. * Defines a struct test_function in a custom ELF section that we can then
  33. * loop over in x11_tests_run() to extract the tests. This removes the
  34. * need of manually adding the tests to a suite or listing them somewhere.
  35. */
  36. #define TEST_ELF_SECTION test_func_sec
  37. #if defined(__APPLE__) && defined(__MACH__)
  38. #define SET_TEST_ELF_SECTION(_section) \
  39. __attribute__((retain,used)) \
  40. __attribute__((section("__DATA," STRINGIFY(_section))))
  41. /* Custom section pointers. See: https://stackoverflow.com/a/22366882 */
  42. #define DECLARE_TEST_ELF_SECTION_POINTERS(_section) \
  43. extern const struct test_function CONCAT2(__start_, _section)[] \
  44. __asm("section$start$__DATA$" STRINGIFY2(_section)); \
  45. extern const struct test_function CONCAT2(__stop_, _section)[] \
  46. __asm("section$end$__DATA$" STRINGIFY2(_section))
  47. #else
  48. #define SET_TEST_ELF_SECTION(_section) \
  49. __attribute__((retain,used)) \
  50. __attribute__((section(STRINGIFY(_section))))
  51. #define DECLARE_TEST_ELF_SECTION_POINTERS(_section) \
  52. extern const struct test_function \
  53. CONCAT2(__start_, _section)[], CONCAT2(__stop_, _section)[]
  54. #endif
  55. #define X11_TEST(_func) \
  56. static int _func(const char* display, void *private); \
  57. static const struct test_function _test_##_func \
  58. SET_TEST_ELF_SECTION(TEST_ELF_SECTION) = { \
  59. .name = #_func, \
  60. .func = (_func), \
  61. .file = __FILE__, \
  62. }; \
  63. static int _func(const char* display, void *private)