sourcehook/test/main.cpp
author Pavol Marko <pavolmarko@gmail.com>
Fri May 27 21:39:30 2005 +0000 (2005-05-27)
changeset 75 8d8efeb2ecc8
parent 60 d44f93f0677a
child 96 e023f17b7b73
permissions -rw-r--r--
Added TestBail
     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 bool g_Verbose;
    12 
    13 class Test
    14 {
    15 	typedef bool (*TestProto)(std::string&);
    16 	TestProto m_Func;
    17 	std::string m_Name;
    18 
    19 	static std::list<Test *> ms_Tests;
    20 public:
    21 	Test(TestProto func, const char *name) : m_Func(func), m_Name(name)
    22 	{
    23 		ms_Tests.push_back(this);
    24 	}
    25 
    26 	bool operator()()
    27 	{
    28 		std::string error;
    29 		if (!m_Func(error))
    30 		{
    31 			cout << "Test" << m_Name << " FAILED: " << error << endl;
    32 			return false;
    33 		}
    34 		else
    35 		{
    36 			cout << "Test" << m_Name << " passed" << endl;
    37 			return true;
    38 		}
    39 	}
    40 
    41 	static void DoTests()
    42 	{
    43 		int passed=0, failed=0;
    44 		for (std::list<Test*>::iterator iter = ms_Tests.begin(); iter != ms_Tests.end(); ++iter)
    45 		{
    46 			if ((**iter)())
    47 				++passed;
    48 			else
    49 				++failed;
    50 		}
    51 		cout << endl << "----" << endl << "Passed: " << passed << endl << "Failed: " << failed << endl;
    52 		cout << "Total: " << passed + failed << endl;
    53 	}
    54 };
    55 
    56 std::list<Test *> Test::ms_Tests;
    57 
    58 #define DO_TEST(x) \
    59 	bool Test##x(std::string &error); \
    60 	Test g_Test##x(Test##x, #x);
    61 
    62 DO_TEST(Basic);
    63 DO_TEST(VafmtAndOverload);
    64 DO_TEST(ThisPtrOffs);
    65 DO_TEST(PlugSys);
    66 DO_TEST(Bail);
    67 
    68 int main(int argc, char *argv[])
    69 {
    70 	std::string error;
    71 	int passed=0, failed=0;
    72 
    73 	g_Verbose = argc > 1 && strcmp(argv[1], "-v") == 0;
    74 
    75 	Test::DoTests();
    76 
    77 	cout << "Press enter to continue" << endl;
    78 
    79 	char x;
    80 	cin.read(&x, 1);
    81 }
    82