Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

test.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2011 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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. /** @file
  20. *
  21. * Self-test infrastructure
  22. *
  23. */
  24. /* Forcibly enable assertions */
  25. #undef NDEBUG
  26. #include <stddef.h>
  27. #include <stdio.h>
  28. #include <errno.h>
  29. #include <assert.h>
  30. #include <ipxe/test.h>
  31. #include <ipxe/init.h>
  32. #include <ipxe/image.h>
  33. /** Current self-test set */
  34. static struct self_test *current_tests;
  35. /**
  36. * Report test result
  37. *
  38. * @v success Test succeeded
  39. * @v file Test code file
  40. * @v line Test code line
  41. */
  42. void test_ok ( int success, const char *file, unsigned int line ) {
  43. /* Sanity check */
  44. assert ( current_tests != NULL );
  45. /* Increment test counter */
  46. current_tests->total++;
  47. /* Report failure if applicable */
  48. if ( ! success ) {
  49. current_tests->failures++;
  50. printf ( "FAILURE: \"%s\" test failed at %s line %d\n",
  51. current_tests->name, file, line );
  52. }
  53. }
  54. /**
  55. * Run self-test set
  56. *
  57. */
  58. static void run_tests ( struct self_test *tests ) {
  59. unsigned int old_assertion_failures = assertion_failures;
  60. /* Sanity check */
  61. assert ( current_tests == NULL );
  62. /* Record current test set */
  63. current_tests = tests;
  64. /* Run tests */
  65. tests->exec();
  66. /* Clear current test set */
  67. current_tests = NULL;
  68. /* Record number of assertion failures */
  69. tests->assertion_failures =
  70. ( assertion_failures - old_assertion_failures );
  71. /* Print test set summary */
  72. if ( tests->failures || tests->assertion_failures ) {
  73. printf ( "FAILURE: \"%s\" %d of %d tests failed",
  74. tests->name, tests->failures, tests->total );
  75. if ( tests->assertion_failures ) {
  76. printf ( " with %d assertion failures",
  77. tests->assertion_failures );
  78. }
  79. printf ( "\n" );
  80. } else {
  81. printf ( "OK: \"%s\" %d tests passed\n",
  82. tests->name, tests->total );
  83. }
  84. }
  85. /**
  86. * Run all self-tests
  87. *
  88. * @ret rc Return status code
  89. */
  90. static int run_all_tests ( void ) {
  91. struct self_test *tests;
  92. unsigned int failures = 0;
  93. unsigned int assertions = 0;
  94. unsigned int total = 0;
  95. /* Run all compiled-in self-tests */
  96. printf ( "Starting self-tests\n" );
  97. for_each_table_entry ( tests, SELF_TESTS )
  98. run_tests ( tests );
  99. /* Print overall summary */
  100. for_each_table_entry ( tests, SELF_TESTS ) {
  101. total += tests->total;
  102. failures += tests->failures;
  103. assertions += tests->assertion_failures;
  104. }
  105. if ( failures || assertions ) {
  106. printf ( "FAILURE: %d of %d tests failed",
  107. failures, total );
  108. if ( assertions ) {
  109. printf ( " with %d assertion failures", assertions );
  110. }
  111. printf ( "\n" );
  112. return -EINPROGRESS;
  113. } else {
  114. printf ( "OK: all %d tests passed\n", total );
  115. return 0;
  116. }
  117. }
  118. static int test_image_probe ( struct image *image __unused ) {
  119. return -ENOTTY;
  120. }
  121. static int test_image_exec ( struct image *image __unused ) {
  122. return run_all_tests();
  123. }
  124. static struct image_type test_image_type = {
  125. .name = "self-tests",
  126. .probe = test_image_probe,
  127. .exec = test_image_exec,
  128. };
  129. static struct image test_image = {
  130. .refcnt = REF_INIT ( ref_no_free ),
  131. .name = "<TESTS>",
  132. .type = &test_image_type,
  133. };
  134. static void test_init ( void ) {
  135. int rc;
  136. /* Register self-tests image */
  137. if ( ( rc = register_image ( &test_image ) ) != 0 ) {
  138. DBG ( "Could not register self-test image: %s\n",
  139. strerror ( rc ) );
  140. /* No way to report failure */
  141. return;
  142. }
  143. }
  144. /** Self-test initialisation function */
  145. struct init_fn test_init_fn __init_fn ( INIT_EARLY ) = {
  146. .initialise = test_init,
  147. };