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"))
82 else if (getenv("JENKINS_URL"))
88 if (getenv("LIBTEST_TMP"))
90 snprintf(buffer
, sizeof(buffer
), "%s", getenv("LIBTEST_TMP"));
94 snprintf(buffer
, sizeof(buffer
), "%s", LIBTEST_TEMP
);
97 if (chdir(buffer
) == -1)
99 char getcwd_buffer
[1024];
100 char *dir
= getcwd(getcwd_buffer
, sizeof(getcwd_buffer
));
102 Error
<< "Unable to chdir() from " << dir
<< " to " << buffer
<< " errno:" << strerror(errno
);
106 if (libtest::libtool() == NULL
)
108 Error
<< "Failed to locate libtool";
112 world
= new Framework();
116 Error
<< "Failed to create Framework()";
120 libtest::SignalThread signal
;
121 if (not signal
.setup())
131 void *creators_ptr
= world
->create(error
);
139 Out
<< "SKIP " << argv
[0];
145 case TEST_MEMORY_ALLOCATION_FAILURE
:
150 char *collection_to_run
= NULL
;
153 collection_to_run
= argv
[1];
155 else if (getenv("TEST_COLLECTION"))
157 if (strlen(getenv("TEST_COLLECTION")))
159 collection_to_run
= getenv("TEST_COLLECTION");
163 if (collection_to_run
)
165 Out
<< "Only testing " << collection_to_run
;
168 char *wildcard
= NULL
;
174 for (collection_st
*next
= world
->collections
; next
->name
and (not signal
.is_shutdown()); next
++)
176 test_return_t collection_rc
= TEST_SUCCESS
;
180 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
183 stats
.collection_total
++;
185 collection_rc
= world
->startup(creators_ptr
);
187 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
189 collection_rc
= world
->runner()->pre(next
->pre
, creators_ptr
);
192 switch (collection_rc
)
199 Out
<< next
->name
<< " [ failed ]";
201 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
205 Out
<< next
->name
<< " [ skipping ]";
209 case TEST_MEMORY_ALLOCATION_FAILURE
:
210 test_assert(0, "Allocation failure, or unknown return");
213 Out
<< "Collection: " << next
->name
;
215 for (test_st
*run
= next
->tests
; run
->name
; run
++)
217 struct timeval start_time
, end_time
;
218 long int load_time
= 0;
220 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
225 test_return_t return_code
;
226 if (test_success(return_code
= world
->item
.startup(creators_ptr
)))
228 if (test_success(return_code
= world
->item
.flush(creators_ptr
, run
)))
230 // @note pre will fail is SKIPPED is returned
231 if (test_success(return_code
= world
->item
.pre(creators_ptr
)))
234 gettimeofday(&start_time
, NULL
);
235 assert(world
->runner());
236 assert(run
->test_fn
);
237 return_code
= world
->runner()->run(run
->test_fn
, creators_ptr
);
238 gettimeofday(&end_time
, NULL
);
239 load_time
= timedif(end_time
, start_time
);
243 // @todo do something if post fails
244 (void)world
->item
.post(creators_ptr
);
246 else if (return_code
== TEST_SKIPPED
)
248 else if (return_code
== TEST_FAILURE
)
250 Error
<< " item.flush(failure)";
251 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
254 else if (return_code
== TEST_SKIPPED
)
256 else if (return_code
== TEST_FAILURE
)
258 Error
<< " item.startup(failure)";
259 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
267 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
275 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
281 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
284 case TEST_MEMORY_ALLOCATION_FAILURE
:
285 test_assert(0, "Memory Allocation Error");
288 if (test_failed(world
->on_error(return_code
, creators_ptr
)))
290 Error
<< "Failed while running on_error()";
291 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
296 (void) world
->runner()->post(next
->post
, creators_ptr
);
299 if (failed
== false and skipped
== false)
301 stats
.collection_success
++;
306 stats
.collection_failed
++;
311 stats
.collection_skipped
++;
314 world
->shutdown(creators_ptr
);
318 if (not signal
.is_shutdown())
320 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
323 int exit_code
= EXIT_SUCCESS
;
324 shutdown_t status
= signal
.get_shutdown();
325 if (status
== SHUTDOWN_FORCED
)
327 Out
<< "Tests were aborted.";
328 exit_code
= EXIT_FAILURE
;
330 else if (stats
.collection_failed
)
332 Out
<< "Some test failed.";
333 exit_code
= EXIT_FAILURE
;
335 else if (stats
.collection_skipped
and stats
.collection_failed
and stats
.collection_success
)
337 Out
<< "Some tests were skipped.";
339 else if (stats
.collection_success
and stats
.collection_failed
== 0)
341 Out
<< "All tests completed successfully.";
348 Outn(); // Generate a blank to break up the messages if make check/test has been run