Permalink
Browse files

added a poor-mans unit test framework. Bu I like it.

git-svn-id: https://ilk.uvt.nl/svn/sources/libticcutils/trunk@15110 12f355fe-0486-481a-ad91-c297ab22b4e3
  • Loading branch information...
kosloot committed Aug 13, 2012
1 parent 0a5db65 commit c4f4a76b6939fe758b9512199b1e58d8ea4adef3
Showing with 64 additions and 44 deletions.
  1. +1 −0 TODO
  2. +2 −1 include/ticcutils/Makefile.am
  3. +53 −0 include/ticcutils/UnitTest.h
  4. +8 −43 src/test.cxx
1 TODO
@@ -0,0 +1 @@
Add documentation
@@ -1,6 +1,7 @@
# $Id$
# $URL$

pkginclude_HEADERS = LogBuffer.h LogStream.h PrettyPrint.h StringOps.h
pkginclude_HEADERS = LogBuffer.h LogStream.h \
PrettyPrint.h StringOps.h UnitTest.h


@@ -0,0 +1,53 @@
#ifndef UNITTEST_H
#define UNITTEST_H

#define assert_eq( XX, YY ) test_eq<typeof XX, typeof YY>( __func__, __LINE__, XX, YY )
#define assert_true( YY ) test_true( __func__, __LINE__, (YY) )

const std::string OK = "\033[1;32m OK \033[0m";
const std::string FAIL = "\033[1;31m FAILED \033[0m";

int tests;
int fails;

void init_tests(){
tests = 0;
fails = 0;
}

template <typename T1, typename T2>
inline void test_eq( const char* F, int L,
const T1& s1, const T2& s2 ){
std::cout << "test: " << F << "(" << L << "): ";
++tests;
if ( s1 != s2 ){
++fails;
std::cout << FAIL << std::endl;
std::cerr << F << "(), line " << L << " : '" << s1 << "' != '"
<< s2 << "'" << std::endl;
}
else {
std::cout << OK << std::endl;
}
}

inline void test_true( const char* F, int L, bool b ){
std::cout << "test: " << F << "(" << L << "): ";
++tests;
if ( !b ){
++fails;
std::cout << FAIL << std::endl;
std::cerr << F << "(), line " << L << " : '" << b << "' != TRUE" << std::endl;
}
else {
std::cout << OK << std::endl;
}
}

inline int summarize_tests(){
std::cout << "performed " << tests << " tests, "
<< fails << " failures." << std::endl;
return fails;
}

#endif
@@ -4,55 +4,20 @@
#include "ticcutils/StringOps.h"
#include "ticcutils/PrettyPrint.h"

#define assert_eq( XX, YY ) test_eq<typeof XX>( __func__, __LINE__, XX, YY )
#define assert_true( YY ) test_true( __func__, __LINE__, (YY) )
#include "ticcutils/UnitTest.h"

using namespace std;
using namespace TiCC;

const string OK = "\033[1;32m OK \033[0m";
const string FAIL = "\033[1;31m FAILED \033[0m";

int tests = 0;
int fails = 0;

template <typename T>
void test_eq( const char* F, int L,
const T& s1, const T& s2 ){
cout << "test: " << F << " ";
++tests;
if ( s1 != s2 ){
++fails;
cerr << FAIL << endl;
cerr << F << "(), line " << L << " : '" << s1 << "' != '"
<< s2 << "'" << endl;
}
else {
cerr << OK << endl;
}
}

void test_true( const char* F, int L, bool b ){
cout << "test: " << F << " ";
++tests;
if ( !b ){
++fails;
cerr << FAIL << endl;
cerr << F << "(), line " << L << " : '" << b << "' != TRUE" << endl;
}
else {
cerr << "OK" << endl;
}
}

void test_trim(){
string val = " aha ";
string res = trim(val);
assert_eq( res, "aha" );
assert_eq( string(""), trim(" \r ") );
assert_true( "A" == trim("A") );
assert_true( "AHA" == trim("AHA") );
assert_true( "AHA" == trim("AHA\r\n") );
assert_eq( "", trim(" \r ") );
assert_true( trim(" \r ").empty() );
assert_eq( "A", trim("A") );
assert_eq( "AHA", trim("AHA") );
assert_eq( "AHA", trim("AHA\r\n") );
}

void test_trim_front(){
@@ -125,6 +90,7 @@ void test_lowercase(){
}

int main(){
init_tests();
test_trim();
test_trim_front();
test_trim_back();
@@ -136,6 +102,5 @@ int main(){
test_to_lower();
test_uppercase();
test_lowercase();
cout << "performed " << tests << " tests, " << fails << " failures." << endl;
return fails;
return summarize_tests();
}

0 comments on commit c4f4a76

Please sign in to comment.