Library meant to simplify writing of tests. Very light-weight in the approach. Test syntax will be familiar to users of CUnit, however this is far more basic.

Dependents:   CircularBufferTest

Tests are expected to take the form of macro-based statements, such as:

// An empty buffer should yield getSize() of zero
TST_EQ( buffer.getSize(), 0, "Empty buffer: getSize()" );

You still have to do the hard work of coming up with the tests and writing code to implement them, but this library is intended to give you a "jump start" towards getting something with consistent output and keeping track of the number of passes/fails - just import the library, include the header and you're away.

See CircularBufferTest main.cpp for an example of how this library can be used

Files at this revision

API Documentation at this revision

Comitter:
johnb
Date:
Sun Jan 19 17:14:59 2014 +0000
Commit message:
Quick utility library to support testing

Changed in this revision

TestSupportLite.cpp Show annotated file Show diff for this revision Revisions of this file
TestSupportLite.hpp Show annotated file Show diff for this revision Revisions of this file
TestSupportSimple.cpp Show annotated file Show diff for this revision Revisions of this file
TestSupportSimple.hpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TestSupportLite.cpp	Sun Jan 19 17:14:59 2014 +0000
@@ -0,0 +1,42 @@
+/** 
+
+Copyright 2014 John Bailey
+   
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+*/
+
+#include "TestSupportLite.hpp"
+
+TestResults::TestResults() : m_passed( 0 ), m_total( 0 )
+{
+}
+
+void TestResults::registerResult( const bool p_passed )
+{
+    m_total++;
+    if( p_passed )
+    {
+        m_passed++;
+    }   
+}
+
+std::size_t TestResults::getCount( void ) const
+{
+    return m_total;
+}
+
+std::size_t TestResults::getPassed( void ) const
+{
+    return m_passed;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TestSupportLite.hpp	Sun Jan 19 17:14:59 2014 +0000
@@ -0,0 +1,85 @@
+/**
+   @file
+   @brief Support routines for writing very simple test harnesses
+
+   @author John Bailey 
+
+   @copyright Copyright 2014 John Bailey
+
+   @section LICENSE
+   
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+*/
+
+#if !defined TESTSUPPORTLITE_HPP
+#define      TESTSUPPORTLITE_HPP
+
+#include <cstring> // for size_t
+
+/** Boiler plate test support for the TST_EQ, TST_TRUE, etc macros 
+    \param _p Indicate whether or not the test passed
+    \param _t Description of the test
+*/
+#define TST_BOILER( _p, _t) do { TEST_OUT( _p, _t ); if( !_p ) { FAIL_OUT(); } } while ( 0 )
+
+/** Testing macro - expects _x and _q to be equal (using the equality operator)
+    \param _x Value to be compared
+    \param _y Value to be compared
+    \param _t Description of test
+*/
+#define TST_EQ( _x, _y, _t ) do { bool passed = ((_x)==(_y)); TST_BOILER( passed, _t ); } while( 0 )
+
+/** Testing macro - expects _x to be true
+    \param _x Value to be evaluated
+    \param _t Description of test
+*/
+#define TST_TRUE( _x, _t ) do { bool passed = (_x); TST_BOILER( passed, _t ); } while( 0 )
+
+/** Testing macro - expects _x to be false
+    \param _x Value to be evaluated
+    \param _t Description of test
+*/
+#define TST_FALSE( _x, _t ) TST_TRUE( !( _x ), _t )
+
+/** Tasks to be carried out when the tests are over */
+#define TST_DONE() do { DONE_OUT(); } while( 0 )
+
+/** Currently a very simple class just to encapsulate a few test statistics
+
+    Implemented as a class rather than a set of global variables to simplify
+    multiple instatitation
+*/
+class TestResults
+{
+    protected:
+        /** Number of tests that have passed */
+        std::size_t m_passed;
+        /** Total number of tests run so far */
+        std::size_t m_total;
+    public:
+        /* Default initialiser */
+        TestResults( void );
+        
+        /* Register that a test has been run
+           \parap p_passed Whether or not the test passed successfully */
+        void registerResult( const bool p_passed );
+        
+        /* Query the total number of tests run so far */
+        std::size_t getCount( void ) const;
+        
+        /* Query the number of tests passed so far */
+        std::size_t getPassed( void ) const;
+};
+
+#endif // !defined TESTSUPPORTLITE_HPP
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TestSupportSimple.cpp	Sun Jan 19 17:14:59 2014 +0000
@@ -0,0 +1,26 @@
+/** 
+
+Copyright 2014 John Bailey
+   
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+*/
+
+#include "TestSupportSimple.hpp"
+
+#if defined __CC_ARM
+#include "mbed.h"
+Serial pc(USBTX, USBRX); // tx, rx   
+#endif
+
+TestResults tst_glob;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TestSupportSimple.hpp	Sun Jan 19 17:14:59 2014 +0000
@@ -0,0 +1,54 @@
+/**
+   @file
+   @brief Set up TestSupportLite for simple tests
+
+   @author John Bailey 
+
+   @copyright Copyright 2014 John Bailey
+
+   @section LICENSE
+   
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+*/
+
+#if !defined TESTSUPPORTSIMPLE_HPP
+#define      TESTSUPPORTSIMPLE_HPP
+
+#if defined __CC_ARM
+#include "mbed.h"
+extern Serial pc;   
+#define TS_PRINTF( ... ) pc.printf(__VA_ARGS__)
+#else
+#include <stdio.h>
+#define TS_PRINTF( ... ) printf(__VA_ARGS__)
+#endif
+
+#include "TestSupportLite.hpp"
+
+extern TestResults tst_glob;
+
+/** Text to be displayed for each test being run */
+#define TEST_TXT "[%03d] {%c} %s\r\n"
+
+/** Text to be displayed at the end of the test run */
+#define DONE_TXT "----- [%d tests run, %d passed]\r\n"
+
+/** Text to be displayed for failing tests */
+#define FAIL_TXT "      Test at %s:%d\r\n"
+
+#define TEST_OUT( _p, _t ) do { tst_glob.registerResult( _p ); TS_PRINTF( TEST_TXT, tst_glob.getCount(), _p?'X':' ', _t ); } while (0)
+#define DONE_OUT() do { TS_PRINTF( DONE_TXT, tst_glob.getCount(), tst_glob.getPassed() ); } while ( 0 )
+#define FAIL_OUT() do { TS_PRINTF( FAIL_TXT, __FILE__, __LINE__ ); } while ( 0 )
+
+#endif
\ No newline at end of file