您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

test.c 4.0KB

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