1
0

report.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Test result reporting
  3. *
  4. * Copyright (c) Siemens AG, 2014
  5. *
  6. * Authors:
  7. * Jan Kiszka <jan.kiszka@siemens.com>
  8. * Andrew Jones <drjones@redhat.com>
  9. *
  10. * This work is licensed under the terms of the GNU LGPL, version 2.
  11. */
  12. #include "libcflat.h"
  13. #include "asm/spinlock.h"
  14. static unsigned int tests, failures, xfailures, skipped;
  15. static char prefixes[256];
  16. static struct spinlock lock;
  17. #define PREFIX_DELIMITER ": "
  18. void report_pass(void)
  19. {
  20. spin_lock(&lock);
  21. tests++;
  22. spin_unlock(&lock);
  23. }
  24. void report_prefix_pushf(const char *prefix_fmt, ...)
  25. {
  26. va_list va;
  27. unsigned int len;
  28. int start;
  29. spin_lock(&lock);
  30. len = strlen(prefixes);
  31. assert_msg(len < sizeof(prefixes), "%d >= %zu", len, sizeof(prefixes));
  32. start = len;
  33. va_start(va, prefix_fmt);
  34. len += vsnprintf(&prefixes[len], sizeof(prefixes) - len, prefix_fmt,
  35. va);
  36. va_end(va);
  37. assert_msg(len < sizeof(prefixes), "%d >= %zu", len, sizeof(prefixes));
  38. assert_msg(!strstr(&prefixes[start], PREFIX_DELIMITER),
  39. "Prefix \"%s\" contains delimiter \"" PREFIX_DELIMITER "\"",
  40. &prefixes[start]);
  41. len += snprintf(&prefixes[len], sizeof(prefixes) - len,
  42. PREFIX_DELIMITER);
  43. assert_msg(len < sizeof(prefixes), "%d >= %zu", len, sizeof(prefixes));
  44. spin_unlock(&lock);
  45. }
  46. void report_prefix_push(const char *prefix)
  47. {
  48. report_prefix_pushf("%s", prefix);
  49. }
  50. void report_prefix_pop(void)
  51. {
  52. char *p, *q;
  53. spin_lock(&lock);
  54. if (!*prefixes)
  55. return;
  56. for (p = prefixes, q = strstr(p, PREFIX_DELIMITER) + 2;
  57. *q;
  58. p = q, q = strstr(p, PREFIX_DELIMITER) + 2)
  59. ;
  60. *p = '\0';
  61. spin_unlock(&lock);
  62. }
  63. static void va_report(const char *msg_fmt,
  64. bool pass, bool xfail, bool skip, va_list va)
  65. {
  66. const char *prefix = skip ? "SKIP"
  67. : xfail ? (pass ? "XPASS" : "XFAIL")
  68. : (pass ? "PASS" : "FAIL");
  69. spin_lock(&lock);
  70. tests++;
  71. printf("%s: ", prefix);
  72. puts(prefixes);
  73. vprintf(msg_fmt, va);
  74. puts("\n");
  75. if (skip)
  76. skipped++;
  77. else if (xfail && !pass)
  78. xfailures++;
  79. else if (xfail || !pass)
  80. failures++;
  81. spin_unlock(&lock);
  82. }
  83. void report(const char *msg_fmt, bool pass, ...)
  84. {
  85. va_list va;
  86. va_start(va, pass);
  87. va_report(msg_fmt, pass, false, false, va);
  88. va_end(va);
  89. }
  90. void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...)
  91. {
  92. va_list va;
  93. va_start(va, pass);
  94. va_report(msg_fmt, pass, xfail, false, va);
  95. va_end(va);
  96. }
  97. void report_skip(const char *msg_fmt, ...)
  98. {
  99. va_list va;
  100. va_start(va, msg_fmt);
  101. va_report(msg_fmt, false, false, true, va);
  102. va_end(va);
  103. }
  104. void report_info(const char *msg_fmt, ...)
  105. {
  106. va_list va;
  107. spin_lock(&lock);
  108. puts("INFO: ");
  109. puts(prefixes);
  110. va_start(va, msg_fmt);
  111. vprintf(msg_fmt, va);
  112. va_end(va);
  113. puts("\n");
  114. spin_unlock(&lock);
  115. }
  116. int report_summary(void)
  117. {
  118. spin_lock(&lock);
  119. printf("SUMMARY: %d tests", tests);
  120. if (failures)
  121. printf(", %d unexpected failures", failures);
  122. if (xfailures)
  123. printf(", %d expected failures", xfailures);
  124. if (skipped)
  125. printf(", %d skipped", skipped);
  126. printf("\n");
  127. if (tests == skipped)
  128. /* Blame AUTOTOOLS for using 77 for skipped test and QEMU for
  129. * mangling error codes in a way that gets 77 if we ... */
  130. return 77 >> 1;
  131. return failures > 0 ? 1 : 0;
  132. spin_unlock(&lock);
  133. }
  134. void report_abort(const char *msg_fmt, ...)
  135. {
  136. va_list va;
  137. spin_lock(&lock);
  138. puts("ABORT: ");
  139. puts(prefixes);
  140. va_start(va, msg_fmt);
  141. vprintf(msg_fmt, va);
  142. va_end(va);
  143. puts("\n");
  144. spin_unlock(&lock);
  145. report_summary();
  146. abort();
  147. }