test.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB 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. * GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef GRUB_TEST_HEADER
  19. #define GRUB_TEST_HEADER
  20. #include <grub/dl.h>
  21. #include <grub/list.h>
  22. #include <grub/misc.h>
  23. #include <grub/types.h>
  24. #include <grub/symbol.h>
  25. struct grub_test
  26. {
  27. /* The next test. */
  28. struct grub_test *next;
  29. /* The test name. */
  30. char *name;
  31. /* The test main function. */
  32. void (*main) (void);
  33. };
  34. typedef struct grub_test *grub_test_t;
  35. extern grub_test_t grub_test_list;
  36. void grub_test_register (const char *name, void (*test) (void));
  37. void grub_test_unregister (const char *name);
  38. /* Execute a test and print results. */
  39. int grub_test_run (grub_test_t test);
  40. /* Test `cond' for nonzero; log failure otherwise. */
  41. void grub_test_nonzero (int cond, const char *file,
  42. const char *func, grub_uint32_t line,
  43. const char *fmt, ...)
  44. __attribute__ ((format (printf, 5, 6)));
  45. /* Macro to fill in location details and an optional error message. */
  46. #define grub_test_assert(cond, ...) \
  47. grub_test_nonzero(cond, GRUB_FILE, __FUNCTION__, __LINE__, \
  48. ## __VA_ARGS__, \
  49. "assert failed: %s", #cond)
  50. /* Macro to define a unit test. */
  51. #define GRUB_UNIT_TEST(name, funp) \
  52. void grub_unit_test_init (void) \
  53. { \
  54. grub_test_register (name, funp); \
  55. } \
  56. \
  57. void grub_unit_test_fini (void) \
  58. { \
  59. grub_test_unregister (name); \
  60. }
  61. /* Macro to define a functional test. */
  62. #define GRUB_FUNCTIONAL_TEST(name, funp) \
  63. GRUB_MOD_INIT(functional_test_##funp) \
  64. { \
  65. grub_test_register (name, funp); \
  66. } \
  67. \
  68. GRUB_MOD_FINI(functional_test_##funp) \
  69. { \
  70. grub_test_unregister (name); \
  71. }
  72. #endif /* ! GRUB_TEST_HEADER */