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 if (strlen(getenv("TEST_COLLECTION")))
155 collection_to_run
= getenv("TEST_COLLECTION");
159 if (collection_to_run
)
161 Out
<< "Only testing " << collection_to_run
;
164 char *wildcard
= NULL
;
170 for (collection_st
*next
= world
->collections
; next
->name
and (not signal
.is_shutdown()); next
++)
172 test_return_t collection_rc
= TEST_SUCCESS
;
176 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
179 stats
.collection_total
++;
181 collection_rc
= world
->startup(creators_ptr
);
183 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
185 collection_rc
= world
->runner()->pre(next
->pre
, creators_ptr
);
188 switch (collection_rc
)
195 Out
<< next
->name
<< " [ failed ]";
197 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
201 Out
<< next
->name
<< " [ skipping ]";
205 case TEST_MEMORY_ALLOCATION_FAILURE
:
206 test_assert(0, "Allocation failure, or unknown return");
209 Out
<< "Collection: " << next
->name
;
211 for (test_st
*run
= next
->tests
; run
->name
; run
++)
213 struct timeval start_time
, end_time
;
214 long int load_time
= 0;
216 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
221 test_return_t return_code
;
222 if (test_success(return_code
= world
->item
.startup(creators_ptr
)))
224 if (test_success(return_code
= world
->item
.flush(creators_ptr
, run
)))
226 // @note pre will fail is SKIPPED is returned
227 if (test_success(return_code
= world
->item
.pre(creators_ptr
)))
230 gettimeofday(&start_time
, NULL
);
231 assert(world
->runner());
232 assert(run
->test_fn
);
233 return_code
= world
->runner()->run(run
->test_fn
, creators_ptr
);
234 gettimeofday(&end_time
, NULL
);
235 load_time
= timedif(end_time
, start_time
);
239 // @todo do something if post fails
240 (void)world
->item
.post(creators_ptr
);
242 else if (return_code
== TEST_SKIPPED
)
244 else if (return_code
== TEST_FAILURE
)
246 Error
<< " item.flush(failure)";
247 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
250 else if (return_code
== TEST_SKIPPED
)
252 else if (return_code
== TEST_FAILURE
)
254 Error
<< " item.startup(failure)";
255 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
263 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
271 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
277 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
280 case TEST_MEMORY_ALLOCATION_FAILURE
:
281 test_assert(0, "Memory Allocation Error");
284 if (test_failed(world
->on_error(return_code
, creators_ptr
)))
286 Error
<< "Failed while running on_error()";
287 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
292 (void) world
->runner()->post(next
->post
, creators_ptr
);
295 if (failed
== false and skipped
== false)
297 stats
.collection_success
++;
302 stats
.collection_failed
++;
307 stats
.collection_skipped
++;
310 world
->shutdown(creators_ptr
);
314 if (not signal
.is_shutdown())
316 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
319 int exit_code
= EXIT_SUCCESS
;
320 shutdown_t status
= signal
.get_shutdown();
321 if (status
== SHUTDOWN_FORCED
)
323 Out
<< "Tests were aborted.";
324 exit_code
= EXIT_FAILURE
;
326 else if (stats
.collection_failed
)
328 Out
<< "Some test failed.";
329 exit_code
= EXIT_FAILURE
;
331 else if (stats
.collection_skipped
and stats
.collection_failed
and stats
.collection_success
)
333 Out
<< "Some tests were skipped.";
335 else if (stats
.collection_success
and stats
.collection_failed
== 0)
337 Out
<< "All tests completed successfully.";
344 Outn(); // Generate a blank to break up the messages if make check/test has been run