From 963d13b306aba88c86838f885d87de9aad9bb318 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Wed, 20 Feb 2013 00:35:45 +0100 Subject: digital/ucoolib/ucoolib/base/test: use an object to track running test --- digital/ucoolib/ucoolib/base/test/test.cc | 43 ++++++++++----------- digital/ucoolib/ucoolib/base/test/test.hh | 45 ++++++++++++++-------- .../ucoolib/ucoolib/base/test/test/test_test.cc | 26 +++++++------ digital/ucoolib/ucoolib/utils/test/test_fifo.cc | 14 +++---- 4 files changed, 71 insertions(+), 57 deletions(-) diff --git a/digital/ucoolib/ucoolib/base/test/test.cc b/digital/ucoolib/ucoolib/base/test/test.cc index 4a52929e..4d8a105f 100644 --- a/digital/ucoolib/ucoolib/base/test/test.cc +++ b/digital/ucoolib/ucoolib/base/test/test.cc @@ -33,9 +33,9 @@ namespace ucoo { -Test::Test (const char *test_suite) - : test_suite_ (test_suite), test_group_ ("none"), test_ (0), - test_nb_ (0), fail_nb_ (0) +TestSuite::TestSuite (const char *test_suite) + : test_suite_ (test_suite), test_group_ ("none"), + test_nb_ (0), fail_nb_ (0), in_test_ (false) { test_stream_setup (); if (UCOO_CONFIG_BASE_TEST_WAIT) @@ -55,33 +55,33 @@ Test::Test (const char *test_suite) } bool -Test::report () +TestSuite::report () { - assert (!test_); + assert (!in_test_); printf ("test results: %d tests, %d fails\n", test_nb_, fail_nb_); return fail_nb_ == 0; } void -Test::group (const char *test_group) +TestSuite::group (const char *test_group) { test_group_ = test_group; } -void -Test::begin (const char *test) +Test::Test (TestSuite &suite, const char *test) + : suite_ (suite), test_ (test), failed_ (false) { - assert (!test_); - test_ = test; - test_nb_++; + assert (!suite_.in_test_); + suite_.in_test_ = true; + suite_.test_nb_++; } -void -Test::pass () +Test::~Test (void) { - assert (test_); - printf ("%s:%s:%s:P: pass\n", test_suite_, test_group_, test_); - test_ = 0; + if (!failed_) + printf ("%s:%s:%s:P: pass\n", suite_.test_suite_, suite_.test_group_, + test_); + suite_.in_test_ = false; } void @@ -94,22 +94,21 @@ void Test::fail (const char *fail_fmt, ...) { va_list ap; - assert (test_); - printf ("%s:%s:%s:F: ", test_suite_, test_group_, test_); + assert (!failed_); + printf ("%s:%s:%s:F: ", suite_.test_suite_, suite_.test_group_, test_); va_start (ap, fail_fmt); vprintf (fail_fmt, ap); va_end (ap); putchar ('\n'); - fail_nb_++; - test_ = 0; + suite_.fail_nb_++; + failed_ = true; } void Test::info (const char *info_fmt, ...) { va_list ap; - assert (test_); - printf ("%s:%s:%s:I: ", test_suite_, test_group_, test_); + printf ("%s:%s:%s:I: ", suite_.test_suite_, suite_.test_group_, test_); va_start (ap, info_fmt); vprintf (info_fmt, ap); va_end (ap); diff --git a/digital/ucoolib/ucoolib/base/test/test.hh b/digital/ucoolib/ucoolib/base/test/test.hh index 6d8a613d..8515d312 100644 --- a/digital/ucoolib/ucoolib/base/test/test.hh +++ b/digital/ucoolib/ucoolib/base/test/test.hh @@ -44,20 +44,39 @@ test_stream (void); void test_stream_setup (void); -/// Test context. -class Test +/// Test suite context. +class TestSuite { public: /// Create a new test suite. - Test (const char *test_suite); + TestSuite (const char *test_suite); /// Report the overall test results, return true if all tests passed. bool report (); /// Enter a new test group. void group (const char *test_group); - /// Start a new test, should be followed by pass or fail. - void begin (const char *test); - /// End the current test with a pass status. - void pass (); + private: + friend class Test; + /// Test suite name. + const char *test_suite_; + /// Test group name. + const char *test_group_; + /// Number of attempted tests. + int test_nb_; + /// Number of test failures. + int fail_nb_; + /// Running a test. + bool in_test_; +}; + +/// Test context. +class Test +{ + public: + /// Start a new test, may be followed by fail, test stops at object + /// destruction. + Test (TestSuite &suite, const char *test); + /// Stop a test. + ~Test (void); /// End the current test with a fail status. void fail (); /// End the current test with a fail status and message. @@ -67,16 +86,12 @@ class Test void __attribute__ ((format (printf, 2, 3))) info (const char *info_fmt, ...); private: - /// Test suite name. - const char *test_suite_; - /// Test group name. - const char *test_group_; + /// Attached test suite. + TestSuite &suite_; /// Test name. const char *test_; - /// Number of attempted tests. - int test_nb_; - /// Number of test failures. - int fail_nb_; + /// Failed yet. + bool failed_; }; } // namespace ucoo diff --git a/digital/ucoolib/ucoolib/base/test/test/test_test.cc b/digital/ucoolib/ucoolib/base/test/test/test_test.cc index baf70a38..88bad02d 100644 --- a/digital/ucoolib/ucoolib/base/test/test/test_test.cc +++ b/digital/ucoolib/ucoolib/base/test/test/test_test.cc @@ -28,17 +28,21 @@ int main (int argc, const char **argv) { ucoo::arch_init (argc, argv); - ucoo::Test test ("test_test"); + ucoo::TestSuite test_suite ("test_test"); // Really, what a dumb test! - test.group ("group one"); - test.begin ("the first test"); - test.fail ("Oh no! test %d!", 123); - test.begin ("try again"); - test.info ("working harder..."); - test.pass (); - test.group ("group two"); - test.begin ("simple test"); - test.pass (); - return test.report () ? 0 : 1; + test_suite.group ("group one"); + { + ucoo::Test test (test_suite, "the first test"); + test.fail ("Oh no! test %d!", 123); + } + { + ucoo::Test test (test_suite, "try again"); + test.info ("working harder..."); + } + test_suite.group ("group two"); + { + ucoo::Test test (test_suite, "simple test"); + } + return test_suite.report () ? 0 : 1; } diff --git a/digital/ucoolib/ucoolib/utils/test/test_fifo.cc b/digital/ucoolib/ucoolib/utils/test/test_fifo.cc index 3f89276e..b905edea 100644 --- a/digital/ucoolib/ucoolib/utils/test/test_fifo.cc +++ b/digital/ucoolib/ucoolib/utils/test/test_fifo.cc @@ -29,20 +29,18 @@ int main (int argc, const char **argv) { ucoo::arch_init (argc, argv); - ucoo::Test test ("fifo"); + ucoo::TestSuite tsuite ("fifo"); { - test.begin ("push pop"); + ucoo::Test test (tsuite, "push pop"); ucoo::Fifo fifo; fifo.push (1); fifo.push (2); fifo.push (3); if (fifo.pop () != 1 || fifo.pop () != 2 || fifo.pop () != 3) test.fail (); - else - test.pass (); } { - test.begin ("full empty"); + ucoo::Test test (tsuite, "full empty"); ucoo::Fifo fifo; do { @@ -53,11 +51,10 @@ main (int argc, const char **argv) test_fail_break_unless (test, !fifo.empty () && !fifo.full ()); fifo.push (3); test_fail_break_unless (test, !fifo.empty () && fifo.full ()); - test.pass (); } while (0); } { - test.begin ("write read"); + ucoo::Test test (tsuite, "write read"); ucoo::Fifo fifo; static const int b[] = { 1, 2, 3, 4, 5 }; int c[8]; @@ -75,8 +72,7 @@ main (int argc, const char **argv) test_fail_break_unless (test, c[0] == 3 && c[1] == 4 && c[2] == 5); test_fail_break_unless (test, c[3] == 1 && c[4] == 2 && c[5] == 3 && c[6] == 4); - test.pass (); } while (0); } - return test.report () ? 0 : 1; + return tsuite.report () ? 0 : 1; } -- cgit v1.2.3