source: mainline/uspace/lib/pcut/src/report/report.c@ 1d6dd2a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1d6dd2a was 1d6dd2a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Remove unnecessary includes from <stdio.h>.

  • Property mode set to 100644
File size: 6.9 KB
Line 
1/*
2 * Copyright (c) 2013 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file
30 *
31 * Common functions for test results reporting.
32 */
33
34#include "../internal.h"
35#ifdef __helenos__
36#include <str.h>
37#else
38#include <string.h>
39#endif
40#include <stdio.h>
41
42/** Currently used report ops. */
43static pcut_report_ops_t *report_ops = NULL;
44
45/** Call a report function if it is available.
46 *
47 * @param op Operation to be called on the pcut_report_ops_t.
48 * @param ... Arguments to the operation.
49 */
50#define REPORT_CALL(op, ...) \
51 if ((report_ops != NULL) && (report_ops->op != NULL)) report_ops->op(__VA_ARGS__)
52
53/** Call a report function if it is available.
54 *
55 * @param op Operation to be called on the pcut_report_ops_t.
56 */
57#define REPORT_CALL_NO_ARGS(op) \
58 if ((report_ops != NULL) && (report_ops->op != NULL)) report_ops->op()
59
60/** Print error message.
61 *
62 * NULL or empty message is silently ignored.
63 *
64 * The message is printed with a special 3-zero-byte prefix to be later
65 * parsed when reporting the results from a different process.
66 *
67 * @param msg The message to be printed.
68 */
69void pcut_print_fail_message(const char *msg) {
70 if (msg == NULL) {
71 return;
72 }
73 if (pcut_str_size(msg) == 0) {
74 return;
75 }
76
77 printf("%c%c%c%s\n%c", 0, 0, 0, msg, 0);
78}
79
80/** Size of buffer for storing error messages or extra test output. */
81#define BUFFER_SIZE 4096
82
83/** Buffer for stdout from the test. */
84static char buffer_for_extra_output[BUFFER_SIZE];
85
86/** Buffer for assertion and other error messages. */
87static char buffer_for_error_messages[BUFFER_SIZE];
88
89/** Parse output of a single test.
90 *
91 * @param full_output Full unparsed output.
92 * @param full_output_size Size of @p full_output in bytes.
93 * @param stdio_buffer Where to store normal output from the test.
94 * @param stdio_buffer_size Size of @p stdio_buffer in bytes.
95 * @param error_buffer Where to store error messages from the test.
96 * @param error_buffer_size Size of @p error_buffer in bytes.
97 */
98static void parse_command_output(const char *full_output, size_t full_output_size,
99 char *stdio_buffer, size_t stdio_buffer_size,
100 char *error_buffer, size_t error_buffer_size) {
101 memset(stdio_buffer, 0, stdio_buffer_size);
102 memset(error_buffer, 0, error_buffer_size);
103
104 /* Ensure that we do not read past the full_output. */
105 if (full_output[full_output_size - 1] != 0) {
106 /* FIXME: can this happen? */
107 return;
108 }
109
110 while (1) {
111 size_t message_length;
112
113 /* First of all, count number of zero bytes before the text. */
114 size_t cont_zeros_count = 0;
115 while (full_output[0] == 0) {
116 cont_zeros_count++;
117 full_output++;
118 full_output_size--;
119 if (full_output_size == 0) {
120 return;
121 }
122 }
123
124 /* Determine the length of the text after the zeros. */
125 message_length = pcut_str_size(full_output);
126
127 if (cont_zeros_count < 2) {
128 /* Okay, standard I/O. */
129 if (message_length > stdio_buffer_size) {
130 /* TODO: handle gracefully */
131 return;
132 }
133 memcpy(stdio_buffer, full_output, message_length);
134 stdio_buffer += message_length;
135 stdio_buffer_size -= message_length;
136 } else {
137 /* Error message. */
138 if (message_length > error_buffer_size) {
139 /* TODO: handle gracefully */
140 return;
141 }
142 memcpy(error_buffer, full_output, message_length);
143 error_buffer += message_length;
144 error_buffer_size -= message_length;
145 }
146
147 full_output += message_length + 1;
148 full_output_size -= message_length + 1;
149 }
150}
151
152/** Use given set of functions for error reporting.
153 *
154 * @param ops Functions to use.
155 */
156void pcut_report_register_handler(pcut_report_ops_t *ops) {
157 report_ops = ops;
158}
159
160/** Initialize the report.
161 *
162 * @param all_items List of all tests that could be run.
163 */
164void pcut_report_init(pcut_item_t *all_items) {
165 REPORT_CALL(init, all_items);
166}
167
168/** Report that a test suite was started.
169 *
170 * @param suite Suite that was just started.
171 */
172void pcut_report_suite_start(pcut_item_t *suite) {
173 REPORT_CALL(suite_start, suite);
174}
175
176/** Report that a test suite was completed.
177 *
178 * @param suite Suite that just completed.
179 */
180void pcut_report_suite_done(pcut_item_t *suite) {
181 REPORT_CALL(suite_done, suite);
182}
183
184/** Report that a test is about to start.
185 *
186 * @param test Test to be run just about now.
187 */
188void pcut_report_test_start(pcut_item_t *test) {
189 REPORT_CALL(test_start, test);
190}
191
192/** Report that a test was completed.
193 *
194 * @param test Test that just finished.
195 * @param outcome Outcome of the test.
196 * @param error_message Buffer with error message.
197 * @param teardown_error_message Buffer with error message from a tear-down function.
198 * @param extra_output Extra output from the test (stdout).
199 */
200void pcut_report_test_done(pcut_item_t *test, int outcome,
201 const char *error_message, const char *teardown_error_message,
202 const char *extra_output) {
203 REPORT_CALL(test_done, test, outcome, error_message, teardown_error_message,
204 extra_output);
205}
206
207/** Report that a test was completed with unparsed test output.
208 *
209 * @param test Test that just finished
210 * @param outcome Outcome of the test.
211 * @param unparsed_output Buffer with all the output from the test.
212 * @param unparsed_output_size Size of @p unparsed_output in bytes.
213 */
214void pcut_report_test_done_unparsed(pcut_item_t *test, int outcome,
215 const char *unparsed_output, size_t unparsed_output_size) {
216
217 parse_command_output(unparsed_output, unparsed_output_size,
218 buffer_for_extra_output, BUFFER_SIZE,
219 buffer_for_error_messages, BUFFER_SIZE);
220
221 pcut_report_test_done(test, outcome, buffer_for_error_messages, NULL, buffer_for_extra_output);
222}
223
224/** Close the report.
225 *
226 */
227void pcut_report_done(void) {
228 REPORT_CALL_NO_ARGS(done);
229}
230
Note: See TracBrowser for help on using the repository browser.