bsdtestsummarize.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_APACHE_LICENSE_HEADER_START@
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @APPLE_APACHE_LICENSE_HEADER_END@
  19. */
  20. #include <math.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/param.h>
  25. int
  26. has_prefix(const char* str, const char* prefix)
  27. {
  28. return (strncmp(str, prefix, strlen(prefix)) == 0);
  29. }
  30. int
  31. print_summary(FILE* f, long total, long pass, long fail, long skip)
  32. {
  33. double dpass = 100.0 ,dfail = 0.0, dskip = 0.0;
  34. fprintf(f, "Total: %ld\n", total);
  35. if (total) {
  36. dpass = ((double)pass / (double)(total - skip)) * 10000.0;
  37. dpass = floor(dpass) / 100.0;
  38. dfail = ((double)fail / (double)(total - skip)) * 10000.0;
  39. dfail = ceil(dfail) / 100.0;
  40. dskip = ((double)skip / (double)total) * 10000.0;
  41. dskip = ceil(dskip) / 100.0;
  42. }
  43. fprintf(f, "Passed: %ld (%0.2lf%%)\nFailed: %ld (%0.2lf%%)\nSkipped: %ld (%0.2lf%%)\n\n", pass, dpass, fail, dfail, skip, dskip);
  44. return 0;
  45. }
  46. int
  47. main(int argc, char* argv[])
  48. {
  49. if (argc > 1) {
  50. fprintf(stderr, "%s: usage: summarize\n", argv[0]);
  51. exit(1);
  52. }
  53. /*
  54. FILE* f = fopen(argv[1], "w");
  55. if (f == NULL) {
  56. perror(argv[1]);
  57. exit(1);
  58. }
  59. */
  60. FILE* f = stdout;
  61. fprintf(f, "\n==================================================\n");
  62. fprintf(f, "[SUMMARY] Test Summary\n");
  63. fprintf(f, "==================================================\n\n");
  64. size_t len;
  65. char* ln, lastln[1024];
  66. int first_test = 1;
  67. long total = 0;
  68. long pass = 0;
  69. long fail = 0;
  70. long skip = 0;
  71. long total_total = 0;
  72. long total_pass = 0;
  73. long total_fail = 0;
  74. long total_skip = 0;
  75. for(;;) {
  76. ln = fgetln(stdin, &len);
  77. //if (ln) fprintf(stdout, "%.*s", (int)len, ln);
  78. if (ln == NULL || (has_prefix(ln, "[TEST]") &&
  79. strncmp(ln, lastln, MIN(len,1024)))) {
  80. if (total || !first_test) {
  81. print_summary(f, total, pass, fail, skip);
  82. first_test = 0;
  83. }
  84. total_total += total;
  85. total_pass += pass;
  86. total_fail += fail;
  87. total_skip += skip;
  88. total = 0;
  89. pass = 0;
  90. fail = 0;
  91. skip = 0;
  92. if (ln) {
  93. fprintf(f, "%.*s", (int)len, ln);
  94. strncpy(lastln, ln, MIN(len,1024));
  95. } else {
  96. fprintf(f, "[TOTAL]\n");
  97. print_summary(f, total_total, total_pass, total_fail, total_skip);
  98. break;
  99. }
  100. } else if (has_prefix(ln, "[PASS]")) {
  101. ++total;
  102. ++pass;
  103. } else if (has_prefix(ln, "[FAIL]")) {
  104. ++total;
  105. ++fail;
  106. } else if (has_prefix(ln, "[SKIP]")) {
  107. ++total;
  108. ++skip;
  109. }
  110. }
  111. return (total_fail ? EXIT_FAILURE : EXIT_SUCCESS);
  112. }