setjmp_test.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 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. #include <grub/test.h>
  19. #include <grub/dl.h>
  20. #include <grub/setjmp.h>
  21. GRUB_MOD_LICENSE ("GPLv3+");
  22. static grub_jmp_buf jmp_point;
  23. static int expected, ctr;
  24. /* This fixes GCC7 "unintentional fallthrough" warning */
  25. static void jmp0 (void) __attribute__ ((noreturn));
  26. static void jmp1 (void) __attribute__ ((noreturn));
  27. static void jmp2 (void) __attribute__ ((noreturn));
  28. static void
  29. jmp0 (void)
  30. {
  31. grub_longjmp (jmp_point, 0);
  32. }
  33. static void
  34. jmp1 (void)
  35. {
  36. grub_longjmp (jmp_point, 1);
  37. }
  38. static void
  39. jmp2 (void)
  40. {
  41. grub_longjmp (jmp_point, 2);
  42. }
  43. static void
  44. setjmp_test (void)
  45. {
  46. int val;
  47. expected = 0;
  48. ctr = 0;
  49. val = grub_setjmp (jmp_point);
  50. grub_test_assert (val == expected, "setjmp returned %d instead of %d",
  51. val, expected);
  52. switch (ctr++)
  53. {
  54. case 0:
  55. expected = 1;
  56. jmp0 ();
  57. case 1:
  58. expected = 1;
  59. jmp1 ();
  60. case 2:
  61. expected = 2;
  62. jmp2 ();
  63. case 3:
  64. return;
  65. }
  66. grub_test_assert (0, "setjmp didn't return enough times");
  67. }
  68. /* Register example_test method as a functional test. */
  69. GRUB_FUNCTIONAL_TEST (setjmp_test, setjmp_test);