sourcehook/test/main.cpp
author Pavol Marko <pavolmarko@gmail.com>
Sat Apr 30 15:48:46 2005 +0000 (2005-04-30)
changeset 42 390a2aec00e4
parent 41 534eac4c53ef
child 45 04770941f5a9
permissions -rw-r--r--
Some fixes, new test system
     1 // Hello BAIL!
     2 // This is a test file
     3 
     4 #include <stdio.h>
     5 #include <iostream>
     6 #include <string>
     7 #include <list>
     8 
     9 using namespace std;
    10 
    11 class Test
    12 {
    13 	typedef bool (*TestProto)(std::string&);
    14 	TestProto m_Func;
    15 	std::string m_Name;
    16 
    17 	static std::list<Test *> ms_Tests;
    18 public:
    19 	Test(TestProto func, const char *name) : m_Func(func), m_Name(name)
    20 	{
    21 		ms_Tests.push_back(this);
    22 	}
    23 
    24 	bool operator()()
    25 	{
    26 		std::string error;
    27 		if (!m_Func(error))
    28 		{
    29 			cout << "Test" << m_Name << " FAILED: " << error << endl;
    30 			return false;
    31 		}
    32 		else
    33 		{
    34 			cout << "Test" << m_Name << " passed" << endl;
    35 			return true;
    36 		}
    37 	}
    38 
    39 	static void DoTests()
    40 	{
    41 		int passed=0, failed=0;
    42 		for (std::list<Test*>::iterator iter = ms_Tests.begin(); iter != ms_Tests.end(); ++iter)
    43 		{
    44 			if ((**iter)())
    45 				++passed;
    46 			else
    47 				++failed;
    48 
    49 			cout << endl << "----" << endl << "Passed: " << passed << endl << "Failed: " << failed << endl;
    50 			cout << "Total: " << passed + failed << endl;
    51 		}
    52 	}
    53 };
    54 
    55 std::list<Test *> Test::ms_Tests;
    56 
    57 #define DO_TEST(x) \
    58 	bool Test##x(std::string &error); \
    59 	Test g_Test##x(Test##x, #x);
    60 
    61 DO_TEST(Basic);
    62 
    63 int main(int argc, char *argv[])
    64 {
    65 	std::string error;
    66 	int passed=0, failed=0;
    67 
    68 	Test::DoTests();
    69 
    70 	cout << "Press enter to continue" << endl;
    71 
    72 	char x;
    73 	cin.read(&x, 1);
    74 }