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
This commit is contained in:
@@ -0,0 +1,83 @@
|
|||||||
|
#include <ExampleTest.h>
|
||||||
|
#include <ThreadedTestCaller.h>
|
||||||
|
#include <cppunit/Test.h>
|
||||||
|
#include <cppunit/TestCaller.h>
|
||||||
|
#include <cppunit/TestSuite.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <kernel/OS.h>
|
||||||
|
|
||||||
|
ExampleTest::ExampleTest(std::string name)
|
||||||
|
: BThreadedTestCase(name)
|
||||||
|
, fLocker(new BLocker())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
CppUnit::Test*
|
||||||
|
ExampleTest::Suite() {
|
||||||
|
CppUnit::TestSuite *suite = new CppUnit::TestSuite("Yo");
|
||||||
|
BThreadedTestCaller<ExampleTest> *caller;
|
||||||
|
|
||||||
|
// Add a multithreaded test
|
||||||
|
ExampleTest *test = new ExampleTest("This name is never used, just so you know :-)");
|
||||||
|
caller = new BThreadedTestCaller<ExampleTest>("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<ExampleTest>("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<ExampleTest>("SingleThreaded Test #1", &ExampleTest::TestFunc1));
|
||||||
|
suite->addTest(new CppUnit::TestCaller<ExampleTest>("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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef _example_test_h_
|
||||||
|
#define _example_test_h_
|
||||||
|
|
||||||
|
#include <ThreadedTestCase.h>
|
||||||
|
#include <Locker.h>
|
||||||
|
|
||||||
|
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_
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
#include "UnitTester.h"
|
||||||
|
#include <LockerSyncObject.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
// ##### Include headers for statically linked tests here #####
|
||||||
|
#include <ExampleTest.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef __testing_is_delightful_h__
|
||||||
|
#define __testing_is_delightful_h__
|
||||||
|
|
||||||
|
#include <TestShell.h>
|
||||||
|
|
||||||
|
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__
|
||||||
Reference in New Issue
Block a user