You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

setjmp_test.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * setjmp()/longjmp() tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <stddef.h>
  32. #include <assert.h>
  33. #include <setjmp.h>
  34. #include <ipxe/test.h>
  35. /** A setjmp()/longjmp() test */
  36. struct setjmp_test {
  37. /** Jump buffer */
  38. jmp_buf env;
  39. /** Expected value */
  40. int expected;
  41. /** Test code file */
  42. const char *file;
  43. /** Test code line */
  44. unsigned int line;
  45. };
  46. /** Expected jump */
  47. static struct setjmp_test *jumped;
  48. /**
  49. * Report a setjmp() test result
  50. *
  51. * @v test setjmp()/longjmp() test
  52. *
  53. * This has to be implemented as a macro since if it were a function
  54. * then the context saved by setjmp() would be invalidated when the
  55. * function returned.
  56. */
  57. #define setjmp_ok( test ) do { \
  58. int value; \
  59. /* Sanity check */ \
  60. assert ( jumped == NULL ); \
  61. /* Initialise test */ \
  62. (test)->expected = 0; \
  63. (test)->file = __FILE__; \
  64. (test)->line = __LINE__; \
  65. /* Perform setjmp() */ \
  66. value = setjmp ( (test)->env ); \
  67. /* Report setjmp()/longjmp() result */ \
  68. setjmp_return_ok ( (test), value ); \
  69. } while ( 0 )
  70. /**
  71. * Report a setjmp()/longjmp() test result
  72. *
  73. * @v test setjmp()/longjmp() test
  74. * @v value Value returned from setjmp()
  75. *
  76. * This function ends up reporting results from either setjmp() or
  77. * longjmp() tests (since calls to longjmp() will return via the
  78. * corresponding setjmp()). It therefore uses the test code file and
  79. * line stored in the test structure, which will represent the line
  80. * from which either setjmp() or longjmp() was called.
  81. */
  82. static void setjmp_return_ok ( struct setjmp_test *test, int value ) {
  83. /* Determine whether this was reached via setjmp() or longjmp() */
  84. if ( value == 0 ) {
  85. /* This is the initial call to setjmp() */
  86. okx ( test->expected == 0, test->file, test->line );
  87. okx ( jumped == NULL, test->file, test->line );
  88. } else {
  89. /* This is reached via a call to longjmp() */
  90. okx ( value == test->expected, test->file, test->line );
  91. okx ( jumped == test, test->file, test->line );
  92. }
  93. /* Clear expected jump */
  94. jumped = NULL;
  95. }
  96. /**
  97. * Report a longjmp() test result
  98. *
  99. * @v test setjmp()/longjmp() test
  100. * @v file Test code file
  101. * @v line Test code line
  102. */
  103. static void __attribute__ (( noreturn ))
  104. longjmp_okx ( struct setjmp_test *test, int value,
  105. const char *file, unsigned int line ) {
  106. /* Record expected value. A zero passed to longjmp() should
  107. * result in setjmp() returning a value of one.
  108. */
  109. test->expected = ( value ? value : 1 );
  110. /* Record test code file and line */
  111. test->file = file;
  112. test->line = line;
  113. /* Record expected jump */
  114. jumped = test;
  115. /* Perform longjmp(). Should return via setjmp_okx() */
  116. longjmp ( test->env, value );
  117. /* longjmp() should never return */
  118. assert ( 0 );
  119. }
  120. #define longjmp_ok( test, value ) \
  121. longjmp_okx ( test, value, __FILE__, __LINE__ )
  122. /**
  123. * Perform setjmp()/longjmp() self-tests
  124. *
  125. */
  126. static void setjmp_test_exec ( void ) {
  127. static struct setjmp_test alpha;
  128. static struct setjmp_test beta;
  129. static int iteration;
  130. /* This is one of the very few situations in which the
  131. * "for-case" pattern is justified.
  132. */
  133. for ( iteration = 0 ; iteration < 10 ; iteration++ ) {
  134. DBGC ( jumped, "SETJMP test iteration %d\n", iteration );
  135. switch ( iteration ) {
  136. case 0: setjmp_ok ( &alpha ); break;
  137. case 1: setjmp_ok ( &beta ); break;
  138. case 2: longjmp_ok ( &alpha, 0 );
  139. case 3: longjmp_ok ( &alpha, 1 );
  140. case 4: longjmp_ok ( &alpha, 2 );
  141. case 5: longjmp_ok ( &beta, 17 );
  142. case 6: longjmp_ok ( &beta, 29 );
  143. case 7: longjmp_ok ( &alpha, -1 );
  144. case 8: longjmp_ok ( &beta, 0 );
  145. case 9: longjmp_ok ( &beta, 42 );
  146. }
  147. }
  148. }
  149. /** setjmp()/longjmp() self-test */
  150. struct self_test setjmp_test __self_test = {
  151. .name = "setjmp",
  152. .exec = setjmp_test_exec,
  153. };