1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <libtest/common.h>
28 #include <sys/types.h>
38 #ifndef __INTEL_COMPILER
39 #pragma GCC diagnostic ignored "-Wold-style-cast"
42 using namespace libtest
;
44 static void stats_print(Stats
*stats
)
46 if (stats
->collection_failed
== 0 and stats
->collection_success
== 0)
51 Out
<< "\tTotal Collections\t\t\t\t" << stats
->collection_total
;
52 Out
<< "\tFailed Collections\t\t\t\t" << stats
->collection_failed
;
53 Out
<< "\tSkipped Collections\t\t\t\t" << stats
->collection_skipped
;
54 Out
<< "\tSucceeded Collections\t\t\t\t" << stats
->collection_success
;
56 Out
<< "Total\t\t\t\t" << stats
->total
;
57 Out
<< "\tFailed\t\t\t" << stats
->failed
;
58 Out
<< "\tSkipped\t\t\t" << stats
->skipped
;
59 Out
<< "\tSucceeded\t\t" << stats
->success
;
62 static long int timedif(struct timeval a
, struct timeval b
)
66 us
= (long)(a
.tv_usec
- b
.tv_usec
);
68 s
= (long)(a
.tv_sec
- b
.tv_sec
);
73 static Framework
*world
= NULL
;
74 int main(int argc
, char *argv
[])
76 srandom((unsigned int)time(NULL
));
78 if (getenv("LIBTEST_QUIET"))
84 if (getenv("LIBTEST_TMP"))
86 snprintf(buffer
, sizeof(buffer
), "%s", getenv("LIBTEST_TMP"));
90 snprintf(buffer
, sizeof(buffer
), "%s", LIBTEST_TEMP
);
93 if (chdir(buffer
) == -1)
95 char getcwd_buffer
[1024];
96 char *dir
= getcwd(getcwd_buffer
, sizeof(getcwd_buffer
));
98 Error
<< "Unable to chdir() from " << dir
<< " to " << buffer
<< " errno:" << strerror(errno
);
102 if (libtest::libtool() == NULL
)
104 Error
<< "Failed to locate libtool";
108 world
= new Framework();
112 Error
<< "Failed to create Framework()";
116 libtest::SignalThread signal
;
117 if (not signal
.setup())
127 void *creators_ptr
= world
->create(error
);
135 Out
<< "SKIP " << argv
[0];
141 case TEST_MEMORY_ALLOCATION_FAILURE
:
146 char *collection_to_run
= NULL
;
149 collection_to_run
= argv
[1];
151 else if (getenv("TEST_COLLECTION"))
153 collection_to_run
= getenv("TEST_COLLECTION");
156 if (collection_to_run
)
158 Out
<< "Only testing " << collection_to_run
;
161 char *wildcard
= NULL
;
167 for (collection_st
*next
= world
->collections
; next
->name
and (not signal
.is_shutdown()); next
++)
169 test_return_t collection_rc
= TEST_SUCCESS
;
173 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
176 stats
.collection_total
++;
178 collection_rc
= world
->startup(creators_ptr
);
180 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
182 collection_rc
= world
->runner()->pre(next
->pre
, creators_ptr
);
185 switch (collection_rc
)
192 Out
<< next
->name
<< " [ failed ]";
194 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
198 Out
<< next
->name
<< " [ skipping ]";
202 case TEST_MEMORY_ALLOCATION_FAILURE
:
203 test_assert(0, "Allocation failure, or unknown return");
206 Out
<< "Collection: " << next
->name
;
208 for (test_st
*run
= next
->tests
; run
->name
; run
++)
210 struct timeval start_time
, end_time
;
211 long int load_time
= 0;
213 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
218 test_return_t return_code
;
219 if (test_success(return_code
= world
->item
.startup(creators_ptr
)))
221 if (test_success(return_code
= world
->item
.flush(creators_ptr
, run
)))
223 // @note pre will fail is SKIPPED is returned
224 if (test_success(return_code
= world
->item
.pre(creators_ptr
)))
227 gettimeofday(&start_time
, NULL
);
228 assert(world
->runner());
229 assert(run
->test_fn
);
230 return_code
= world
->runner()->run(run
->test_fn
, creators_ptr
);
231 gettimeofday(&end_time
, NULL
);
232 load_time
= timedif(end_time
, start_time
);
236 // @todo do something if post fails
237 (void)world
->item
.post(creators_ptr
);
239 else if (return_code
== TEST_SKIPPED
)
241 else if (return_code
== TEST_FAILURE
)
243 Error
<< " item.flush(failure)";
244 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
247 else if (return_code
== TEST_SKIPPED
)
249 else if (return_code
== TEST_FAILURE
)
251 Error
<< " item.startup(failure)";
252 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
260 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
268 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
274 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
277 case TEST_MEMORY_ALLOCATION_FAILURE
:
278 test_assert(0, "Memory Allocation Error");
281 if (test_failed(world
->on_error(return_code
, creators_ptr
)))
283 Error
<< "Failed while running on_error()";
284 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
289 (void) world
->runner()->post(next
->post
, creators_ptr
);
292 if (failed
== false and skipped
== false)
294 stats
.collection_success
++;
299 stats
.collection_failed
++;
304 stats
.collection_skipped
++;
307 world
->shutdown(creators_ptr
);
311 if (not signal
.is_shutdown())
313 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
316 int exit_code
= EXIT_SUCCESS
;
317 shutdown_t status
= signal
.get_shutdown();
318 if (status
== SHUTDOWN_FORCED
)
320 Out
<< "Tests were aborted.";
321 exit_code
= EXIT_FAILURE
;
323 else if (stats
.collection_failed
)
325 Out
<< "Some test failed.";
326 exit_code
= EXIT_FAILURE
;
328 else if (stats
.collection_skipped
and stats
.collection_failed
and stats
.collection_success
)
330 Out
<< "Some tests were skipped.";
332 else if (stats
.collection_success
and stats
.collection_failed
== 0)
334 Out
<< "All tests completed successfully.";
341 Outn(); // Generate a blank to break up the messages if make check/test has been run