Tizen 2.1 base
[platform/upstream/gcd.git] / dispatch-1.0 / testing / summarize.c
1 /*
2  * Copyright (c) 2008-2009 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
21 #include "config/config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 int
28 has_prefix(const char* str, const char* prefix) {
29         return (strncmp(str, prefix, strlen(prefix)) == 0);
30 }
31
32 int
33 print_summary(FILE* f, long total, long pass, long fail) {
34         fprintf(f, "Total:  %ld\n", total);
35         fprintf(f, "Passed: %ld (%0.0lf%%)\n", pass, ((double)pass / (double)total) * (double)100.0);
36         fprintf(f, "Failed: %ld (%0.0lf%%)\n", fail, ((double)fail / (double)total) * (double)100.0);
37         fprintf(f, "\n");
38         return 0;
39 }
40
41 int main(int argc, char* argv[]) {
42         if (argc > 1) {
43                 fprintf(stderr, "%s: usage: summarize\n", argv[0]);
44                 exit(1);
45         }
46         
47         /*
48         FILE* f = fopen(argv[1], "w");
49         if (f == NULL) {
50                 perror(argv[1]);
51                 exit(1);
52         }
53         */
54         FILE* f = stdout;
55
56         fprintf(f, "\n==================================================\n");
57         fprintf(f, "[SUMMARY] Test Summary\n");
58         fprintf(f, "==================================================\n\n");
59         
60         size_t len;
61         char* ln;
62         long total = 0;
63         long pass = 0;
64         long fail = 0;
65         long total_total = 0;
66         long total_pass = 0;
67         long total_fail = 0;
68         for(;;) {
69                 ln = fgetln(stdin, &len);
70                 //if (ln) fprintf(stdout, "%.*s", (int)len, ln);
71                 if (ln == NULL || has_prefix(ln, "[TEST]")) {
72                         if (total) {
73                                 print_summary(f, total, pass, fail);
74                         }
75                         total_total += total;
76                         total_pass += pass;
77                         total_fail += fail;
78                         total = 0;
79                         pass = 0;
80                         fail = 0;
81                         if (ln) {
82                                 fprintf(f, "%.*s", (int)len, ln);
83                         } else {
84                                 fprintf(f, "[TOTAL]\n");
85                                 print_summary(f, total_total, total_pass, total_fail);
86                                 break;
87                         }
88                 } else if (has_prefix(ln, "[PASS]")) {
89                         ++total;
90                         ++pass;
91                 } else if (has_prefix(ln, "[FAIL]")) {
92                         ++total;
93                         ++fail;
94                 }
95         }
96         
97         return (total_fail ? EXIT_FAILURE : EXIT_SUCCESS);
98 }