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
:
142 Error
<< argv
[0] << " failed in Framework::create()";
147 char *collection_to_run
= NULL
;
150 collection_to_run
= argv
[1];
152 else if (getenv("TEST_COLLECTION"))
154 collection_to_run
= getenv("TEST_COLLECTION");
157 if (collection_to_run
)
159 Out
<< "Only testing " << collection_to_run
;
162 char *wildcard
= NULL
;
168 for (collection_st
*next
= world
->collections
; next
->name
and (not signal
.is_shutdown()); next
++)
170 test_return_t collection_rc
= TEST_SUCCESS
;
174 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
177 stats
.collection_total
++;
179 collection_rc
= world
->startup(creators_ptr
);
181 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
183 collection_rc
= world
->runner()->pre(next
->pre
, creators_ptr
);
186 switch (collection_rc
)
193 Out
<< next
->name
<< " [ failed ]";
195 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
199 Out
<< next
->name
<< " [ skipping ]";
203 case TEST_MEMORY_ALLOCATION_FAILURE
:
204 test_assert(0, "Allocation failure, or unknown return");
207 Out
<< "Collection: " << next
->name
;
209 for (test_st
*run
= next
->tests
; run
->name
; run
++)
211 struct timeval start_time
, end_time
;
212 long int load_time
= 0;
214 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
219 test_return_t return_code
;
220 if (test_success(return_code
= world
->item
.startup(creators_ptr
)))
222 if (test_success(return_code
= world
->item
.flush(creators_ptr
, run
)))
224 // @note pre will fail is SKIPPED is returned
225 if (test_success(return_code
= world
->item
.pre(creators_ptr
)))
228 gettimeofday(&start_time
, NULL
);
229 assert(world
->runner());
230 assert(run
->test_fn
);
231 return_code
= world
->runner()->run(run
->test_fn
, creators_ptr
);
232 gettimeofday(&end_time
, NULL
);
233 load_time
= timedif(end_time
, start_time
);
237 // @todo do something if post fails
238 (void)world
->item
.post(creators_ptr
);
240 else if (return_code
== TEST_SKIPPED
)
242 else if (return_code
== TEST_FAILURE
)
244 Error
<< " item.flush(failure)";
245 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
248 else if (return_code
== TEST_SKIPPED
)
250 else if (return_code
== TEST_FAILURE
)
252 Error
<< " item.startup(failure)";
253 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
261 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
269 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
275 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
278 case TEST_MEMORY_ALLOCATION_FAILURE
:
279 test_assert(0, "Memory Allocation Error");
282 if (test_failed(world
->on_error(return_code
, creators_ptr
)))
284 Error
<< "Failed while running on_error()";
285 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
290 (void) world
->runner()->post(next
->post
, creators_ptr
);
293 if (failed
== false and skipped
== false)
295 stats
.collection_success
++;
300 stats
.collection_failed
++;
305 stats
.collection_skipped
++;
308 world
->shutdown(creators_ptr
);
312 if (not signal
.is_shutdown())
314 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
317 int exit_code
= EXIT_SUCCESS
;
318 shutdown_t status
= signal
.get_shutdown();
319 if (status
== SHUTDOWN_FORCED
)
321 Out
<< "Tests were aborted.";
322 exit_code
= EXIT_FAILURE
;
324 else if (stats
.collection_failed
)
326 Out
<< "Some test failed.";
327 exit_code
= EXIT_FAILURE
;
329 else if (stats
.collection_skipped
and stats
.collection_failed
and stats
.collection_success
)
331 Out
<< "Some tests were skipped.";
333 else if (stats
.collection_success
and stats
.collection_failed
== 0)
335 Out
<< "All tests completed successfully.";
342 Outn(); // Generate a blank to break up the messages if make check/test has been run