setjmp_test.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #pragma GCC diagnostic ignored "-Wmissing-noreturn"
  25. static void
  26. jmp0 (void)
  27. {
  28. grub_longjmp (jmp_point, 0);
  29. }
  30. static void
  31. jmp1 (void)
  32. {
  33. grub_longjmp (jmp_point, 1);
  34. }
  35. static void
  36. jmp2 (void)
  37. {
  38. grub_longjmp (jmp_point, 2);
  39. }
  40. static void
  41. setjmp_test (void)
  42. {
  43. int val;
  44. expected = 0;
  45. ctr = 0;
  46. val = grub_setjmp (jmp_point);
  47. grub_test_assert (val == expected, "setjmp returned %d instead of %d",
  48. val, expected);
  49. switch (ctr++)
  50. {
  51. case 0:
  52. expected = 1;
  53. jmp0 ();
  54. case 1:
  55. expected = 1;
  56. jmp1 ();
  57. case 2:
  58. expected = 2;
  59. jmp2 ();
  60. case 3:
  61. return;
  62. }
  63. grub_test_assert (0, "setjmp didn't return enough times");
  64. }
  65. /* Register example_test method as a functional test. */
  66. GRUB_FUNCTIONAL_TEST (setjmp_test, setjmp_test);