From bc85a65ae30086f432deb7844ae9872da5bec542 Mon Sep 17 00:00:00 2001 From: Tyler Dauwalder Date: Thu, 11 Jul 2002 03:34:41 +0000 Subject: [PATCH] Initial checking of unified unit testing app and example test. + Currently, only statically linked tests are supported. + None of the existing tests have been integrated into it yet; that will come after I add dynamic linking. + The example test has multithreaded and singlethreaded examples + The name of the unit testing app is open for negotiation if UnitTester is unacceptable for some reason. :-) git-svn-id: file:///srv/svn/repos/haiku/trunk/current@78 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/tests/ExampleTest.cpp | 83 +++++++++++++++++++++++++++++++++++++++ src/tests/ExampleTest.h | 22 +++++++++++ src/tests/UnitTester.cpp | 41 +++++++++++++++++++ src/tests/UnitTester.h | 14 +++++++ 4 files changed, 160 insertions(+) create mode 100644 src/tests/ExampleTest.cpp create mode 100644 src/tests/ExampleTest.h create mode 100644 src/tests/UnitTester.cpp create mode 100644 src/tests/UnitTester.h diff --git a/src/tests/ExampleTest.cpp b/src/tests/ExampleTest.cpp new file mode 100644 index 0000000000..9484ac5e71 --- /dev/null +++ b/src/tests/ExampleTest.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +ExampleTest::ExampleTest(std::string name) + : BThreadedTestCase(name) + , fLocker(new BLocker()) +{ +} + +CppUnit::Test* +ExampleTest::Suite() { + CppUnit::TestSuite *suite = new CppUnit::TestSuite("Yo"); + BThreadedTestCaller *caller; + + // Add a multithreaded test + ExampleTest *test = new ExampleTest("This name is never used, just so you know :-)"); + caller = new BThreadedTestCaller("MultiThreaded Test #1", test); + caller->addThread("A", &ExampleTest::TestFunc1); + caller->addThread("B", &ExampleTest::TestFunc2); + caller->addThread("C", &ExampleTest::TestFunc3); + suite->addTest(caller); + + // And another + caller = new BThreadedTestCaller("MultiThreaded Test #2"); + caller->addThread("Thread1", &ExampleTest::TestFunc1); + caller->addThread("Thread2", &ExampleTest::TestFunc1); + caller->addThread("Thread3", &ExampleTest::TestFunc1); + suite->addTest(caller); + + // And some single threaded ones + suite->addTest(new CppUnit::TestCaller("SingleThreaded Test #1", &ExampleTest::TestFunc1)); + suite->addTest(new CppUnit::TestCaller("SingleThreaded Test #2", &ExampleTest::TestFunc2)); + + return suite; +} + +const int sleeptime = 10000; + +void +ExampleTest::TestFunc1() { + for (int i = 0; i < 10; i++) { + // Get the lock and do our business + NextSubTest(); + fLocker->Lock(); +// printf("TestFunc1() -- %d + 10 = %d\n", fNum, fNum+10); + fNum += 10; + fLocker->Unlock(); + snooze(sleeptime); + } +} + +void +ExampleTest::TestFunc2() { + for (int i = 0; i < 13; i++) { + // Get the lock and do our business + NextSubTest(); + fLocker->Lock(); +// printf("TestFunc2() -- %d * 2 = %d\n", fNum, fNum*2); + fNum *= 2; + fLocker->Unlock(); + snooze(sleeptime); + } +} + +void +ExampleTest::TestFunc3() { + for (int i = 0; i < 15; i++) { + // Get the lock and do our business + NextSubTest(); + fLocker->Lock(); +// printf("TestFunc3() -- %d - 5 = %d\n", fNum, fNum-5); + fNum += 10; + fLocker->Unlock(); + snooze(sleeptime); + } +} + diff --git a/src/tests/ExampleTest.h b/src/tests/ExampleTest.h new file mode 100644 index 0000000000..6c22060188 --- /dev/null +++ b/src/tests/ExampleTest.h @@ -0,0 +1,22 @@ +#ifndef _example_test_h_ +#define _example_test_h_ + +#include +#include + +class ExampleTest : public BThreadedTestCase { +public: + ExampleTest(std::string name = ""); + virtual ~ExampleTest() { delete fLocker; } + + static Test* Suite(); + + void TestFunc1(); // num += 10 + void TestFunc2(); // num *= 2 + void TestFunc3(); // num -= 5 +protected: + BLocker *fLocker; + int fNum; +}; + +#endif // _example_test_h_ diff --git a/src/tests/UnitTester.cpp b/src/tests/UnitTester.cpp new file mode 100644 index 0000000000..07a95ec6b5 --- /dev/null +++ b/src/tests/UnitTester.cpp @@ -0,0 +1,41 @@ +#include "UnitTester.h" +#include +#include + +// ##### Include headers for statically linked tests here ##### +#include + +UnitTesterShell shell("OpenBeOS Unit Testing Framework", new LockerSyncObject); + +int main(int argc, char *argv[]) { + // ##### Add test suites for statically linked tests here ##### + shell.AddSuite( "Example", &ExampleTest::Suite ); + + return shell.Run(argc, argv); +} + +UnitTesterShell::UnitTesterShell(const std::string &description, SyncObject *syncObject) + : BTestShell(description, syncObject) +{ +} + +void +UnitTesterShell::PrintDescription(int argc, char *argv[]) { + std::string AppName = argv[0]; + cout << endl; + cout << "This program is the central testing framework for the purpose" << endl; + cout << "of testing and verifying the various kits, classes, functions," << endl; + cout << "and the like that comprise OpenBeOS." << endl; + + if (AppName.rfind("UnitTester.R5") != std::string::npos) { + cout << endl; + cout << "Judging by its name (UnitTester.R5), this copy was" << endl; + cout << "probably linked against Be Inc.'s R5 implementations" << endl; + cout << "for the sake of comparison." << endl; + } else if (AppName.rfind("UnitTester.OpenBeOS") != std::string::npos) { + cout << endl; + cout << "Judging by its name (UnitTester.OpenBeOS), this copy was probably" << endl; + cout << "linked against our OpenBeOS implementations." << endl; + } +} + diff --git a/src/tests/UnitTester.h b/src/tests/UnitTester.h new file mode 100644 index 0000000000..7e12c7e6c0 --- /dev/null +++ b/src/tests/UnitTester.h @@ -0,0 +1,14 @@ +#ifndef __testing_is_delightful_h__ +#define __testing_is_delightful_h__ + +#include + +class UnitTesterShell : public BTestShell { +public: + UnitTesterShell(const std::string &description = "", SyncObject *syncObject = 0); + virtual void PrintDescription(int argc, char *argv[]); +}; + +//extern UnitTesterShell shell; + +#endif // __testing_is_delightful_h__