Skip to content

Commit dbb14fb

Browse files
author
Sebastiano Merlino
committed
Added statistics to test
1 parent 7da8766 commit dbb14fb

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

test/littletest.hpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//TODO: time extimations
2222
//TODO: personalized messages
2323
//TODO: statistics in runner closure
24+
//TODO: catch exceptions during set_up and tier_down
2425

2526
#ifndef _LITTLETEST_HPP_
2627
#define _LITTLETEST_HPP_
@@ -29,6 +30,7 @@
2930
#include <iostream>
3031
#include <sstream>
3132
#include <algorithm>
33+
#include <sys/time.h>
3234

3335
#define WARN 0
3436
#define CHECK 1
@@ -311,10 +313,12 @@ class test
311313
private:
312314
bool run_test(test_runner<suite_impl>* tr)
313315
{
316+
timeval before, after;
314317
static_cast<test_impl* >(this)->suite_set_up();
315318
bool result = false;
316319
try
317320
{
321+
gettimeofday(&before, NULL);
318322
(*static_cast<test_impl*>(this))(tr);
319323
result = true;
320324
}
@@ -334,6 +338,15 @@ class test
334338
if(tr->last_checkpoint_line != -1)
335339
std::cout << "Last checkpoint in " << tr->last_checkpoint_file << ":" << tr->last_checkpoint_line << std::endl;
336340
}
341+
gettimeofday(&after, NULL);
342+
343+
double duration = ((after.tv_sec * 1000 + (after.tv_usec / 1000.0)) -
344+
(before.tv_sec * 1000 + (before.tv_usec / 1000.0)));
345+
346+
tr->add_good_time(duration);
347+
348+
std::cout << "Time spent during \"" << test_impl::name << "\": " << duration << std::endl;
349+
337350
static_cast<test_impl* >(this)->suite_tier_down();
338351
return result;
339352
}
@@ -353,7 +366,8 @@ struct test_runner
353366
success_counter(0),
354367
failures_counter(0),
355368
last_checkpoint_file(""),
356-
last_checkpoint_line(-1)
369+
last_checkpoint_line(-1),
370+
good_time_total(0.0)
357371
{
358372
}
359373

@@ -379,6 +393,11 @@ struct test_runner
379393
test_runner& operator()()
380394
{
381395
std::cout << "** Runner terminated! **" << std::endl;
396+
std::cout << test_counter << " tests executed" << std::endl;
397+
std::cout << (failures_counter + success_counter) << " checks" << std::endl;
398+
std::cout << "-> " << success_counter << " failures" << std::endl;
399+
std::cout << "-> " << failures_counter << " failures" << std::endl;
400+
std::cout << "Total time spent in tests: " << good_time_total << std::endl;
382401
}
383402

384403
void add_failure()
@@ -397,13 +416,19 @@ struct test_runner
397416
last_checkpoint_line = line;
398417
}
399418

419+
void add_good_time(double t)
420+
{
421+
good_time_total += t;
422+
}
423+
400424
std::string last_checkpoint_file;
401425
int last_checkpoint_line;
402426

403427
private:
404428
int test_counter;
405429
int success_counter;
406430
int failures_counter;
431+
double good_time_total;
407432
};
408433

409434
#endif //_LITTLETEST_HPP_

0 commit comments

Comments
 (0)