#include <Assertions.hh>
#include <TestCase.hh>
#include <TestCaseAdapter.hh>
#include <TestGadget.hh>
#include <TestSuite.hh>
#include <TestSuiteAdapter.hh>
#include <TestFixture.hh>
#include <TestManager.hh>
#include <ExceptionTrap.hh>
#include <BriefSessionLog.hh>
#include <VerboseSessionLog.hh>
#include <ExecutionTracer.hh>
#include <SessionReport.hh>
#include <Rect.hh>
//***************************************************************
class ClassicEqualsTest : public Scrunitizer::TestCase {
private:
Rect* r1;
Rect* r2;
Rect* r3;
public:
ClassicEqualsTest () : TestCase ("Equals Test") { }
void setUp () {
r1 = new Rect (0, 0, 10, 10);
r2 = new Rect (0, 0, 10, 10);
r3 = new Rect (0, 0, 10, 20);
}
void test () {
assert (r1->equals (*r2), "r1 == r2");
assert (r1->equals (*r1), "r1 == r1");
assert (r2->equals (*r1), "r1 == r2");
assert (! r1->equals (*r3), "r1 != r3");
assert (! r3->equals (*r1), "r1 != r3");
}
void tearDown () {
delete r1;
delete r2;
delete r3;
}
};
class DangerousCoversTest : public Scrunitizer::TestCase {
private:
Rect cover;
Rect notCovered;
Rect partlyCovered;
Rect fullyCovered;
public:
DangerousCoversTest () :
TestCase ("Covers Test"),
cover ( 0, 0, 100, 100),
notCovered (150, 150, 50, 50),
partlyCovered ( 50, 50, 100, 100),
fullyCovered ( 50, 50, 50, 50) {
}
void test () {
assert (! cover.covers (notCovered),
"! ( 0, 0, 100, 100) covers (150, 150, 200, 200)");
assert (! cover.covers (partlyCovered),
"! ( 0, 0, 100, 100) covers ( 50, 50, 150, 150)");
assert ( cover.covers (fullyCovered),
"( 0, 0, 100, 100) covers ( 50, 50, 100, 100)");
}
};
class ClassicCoversTest : public Scrunitizer::TestCase {
public:
ClassicCoversTest () : TestCase ("Covers Test") { }
void test () {
Rect cover ( 0, 0, 100, 100);
Rect notCovered (150, 150, 50, 50);
Rect partlyCovered ( 50, 50, 100, 100);
Rect fullyCovered ( 50, 50, 50, 50);
assert (! cover.covers (notCovered),
"! ( 0, 0, 100, 100) covers (150, 150, 200, 200)");
assert (! cover.covers (partlyCovered),
"! ( 0, 0, 100, 100) covers ( 50, 50, 150, 150)");
assert ( cover.covers (fullyCovered),
"( 0, 0, 100, 100) covers ( 50, 50, 100, 100)");
}
};
//***************************************************************
void EqualsTestFunction () {
Rect r1 (0, 0, 10, 10);
Rect r2 (0, 0, 10, 10);
Rect r3 (0, 0, 10, 20);
assert (r1.equals (r2), "r1 == r2");
assert (r1.equals (r1), "r1 == r1");
assert (r2.equals (r1), "r1 == r2");
assert (! r1.equals (r3), "r1 != r3");
assert (! r3.equals (r1), "r1 != r3");
}
void CoversTestFunction () {
Rect cover ( 0, 0, 100, 100);
Rect notCovered (150, 150, 50, 50);
Rect partlyCovered ( 50, 50, 100, 100);
Rect fullyCovered ( 50, 50, 50, 50);
assert (! cover.covers (notCovered),
"! ( 0, 0, 100, 100) covers (150, 150, 200, 200)");
assert (! cover.covers (partlyCovered),
"! ( 0, 0, 100, 100) covers ( 50, 50, 150, 150)");
assert ( cover.covers (fullyCovered),
"( 0, 0, 100, 100) covers ( 50, 50, 100, 100)");
}
//***************************************************************
void testIntersectionPred () {
Rect centerRect (0, 0, 10, 10);
Rect testRectNW (-10, -10, 10, 10);
Rect testRectN ( 0, -10, 10, 10);
Rect testRectNE ( 10, -10, 10, 10);
Rect testRectW (-10, 0, 10, 10);
Rect testRectC ( 0, 0, 10, 10);
Rect testRectE ( 10, 0, 10, 10);
Rect testRectSW (-10, 10, 10, 10);
Rect testRectS ( 0, 10, 10, 10);
Rect testRectSE ( 10, 10, 10, 10);
assert (! centerRect.intersects (testRectNW),
"Failure in NW intersection");
assert (! centerRect.intersects (testRectN ),
"Failure in N intersection");
assert (! centerRect.intersects (testRectNE),
"Failure in NE intersection");
assert (! centerRect.intersects (testRectW),
"Failure in W intersection");
assert ( centerRect.intersects (testRectC ),
"Failure in C intersection");
assert (! centerRect.intersects (testRectE),
"Failure in E intersection");
assert (! centerRect.intersects (testRectSW),
"Failure in SW intersection");
assert (! centerRect.intersects (testRectS ),
"Failure in S intersection");
assert (! centerRect.intersects (testRectSE),
"Failure in SE intersection");
}
//***************************************************************
void testNWIntersection () {
Rect centerRect (0, 0, 10, 10);
Rect testRectNW (-10, -10, 10, 10);
assert (! centerRect.intersects (testRectNW),
"Failure in NW intersection");
}
void testNIntersection () {
Rect centerRect (0, 0, 10, 10);
Rect testRectN ( 0, -10, 10, 10);
assert (! centerRect.intersects (testRectN ),
"Failure in N intersection");
}
void testNEIntersection () {
Rect centerRect (0, 0, 10, 10);
Rect testRectNE ( 10, -10, 10, 10);
assert (! centerRect.intersects (testRectNE),
"Failure in NE intersection");
}
void testWIntersection () {
Rect centerRect (0, 0, 10, 10);
Rect testRectW (-10, 0, 10, 10);
assert (! centerRect.intersects (testRectW),
"Failure in W intersection");
}
void testCIntersection () {
Rect centerRect (0, 0, 10, 10);
Rect testRectC ( 0, 0, 10, 10);
assert ( centerRect.intersects (testRectC ),
"Failure in C intersection");
}
void testEIntersection () {
Rect centerRect (0, 0, 10, 10);
Rect testRectE ( 10, 0, 10, 10);
assert (! centerRect.intersects (testRectE),
"Failure in E intersection");
}
void testSWIntersection () {
Rect centerRect (0, 0, 10, 10);
Rect testRectSW (-10, 10, 10, 10);
assert (! centerRect.intersects (testRectSW),
"Failure in SW intersection");
}
void testSIntersection () {
Rect centerRect (0, 0, 10, 10);
Rect testRectS ( 0, 10, 10, 10);
assert (! centerRect.intersects (testRectS ),
"Failure in S intersection");
}
void testSEIntersection () {
Rect centerRect (0, 0, 10, 10);
Rect testRectSE ( 10, 10, 10, 10);
assert (! centerRect.intersects (testRectSE),
"Failure in SE intersection");
}
//***************************************************************
void testIntersects (int xc, int yc, bool expectedResult) {
Rect centerRect (0, 0, 10, 10);
Rect testRect (xc * 10, yc * 10, 10, 10);
assert (centerRect.intersects (testRect) == expectedResult,
"Failure in intersection test");
}
void testCovers (int xc, int yc, bool expectedResult) {
Rect centerRect (0, 0, 10, 10);
Rect testRect (xc * 10, yc * 10, 10, 10);
assert (centerRect.covers (testRect) == expectedResult,
"Failure in coverage test");
}
//***************************************************************
class RectangleTester {
public:
void testIntersects (int cx, int cy, bool expectedResult) {
Rect centerRect (0, 0, 10, 10);
Rect testRect (cx * 10, cy * 10, 10, 10);
assert (centerRect.intersects (testRect) == expectedResult,
"Failure in intersection test");
}
void testCovers (int cx, int cy, bool expectedResult) {
Rect centerRect (0, 0, 10, 10);
Rect testRect (cx * 10, cy * 10, 10, 10);
assert (centerRect.covers (testRect) == expectedResult,
"Failure in coverage test");
}
};
//***************************************************************
class BasicRectTester : public Scrunitizer::TestFixture {
protected:
virtual void setUpCenterRect (Rect& r) {
r.reshape (0, 0, 10, 10);
}
virtual void setUpTestRect (Rect& r, int cx, int cy) {
r.reshape (cx * 10, cy * 10, 10, 10);
}
virtual bool expectedIntersectsResult (int cx, int cy) {
return cx == 0 && cy == 0;
}
virtual bool expectedCoversResult (int cx, int cy) {
return cx == 0 && cy == 0;
}
public:
void testIntersects (int cx, int cy) {
Rect centerRect;
Rect testRect;
setUpCenterRect (centerRect);
setUpTestRect (testRect, cx, cy);
assert (centerRect.intersects (testRect) == expectedIntersectsResult (cx, cy),
"Failure in intersection test");
}
void testCovers (int cx, int cy) {
Rect centerRect (0, 0, 10, 10);
Rect testRect;
setUpCenterRect (centerRect);
setUpTestRect (testRect, cx, cy);
assert (centerRect.covers (testRect) == expectedCoversResult (cx, cy),
"Failure in coverage test");
}
public:
static Scrunitizer::TestSuite* makePredicateTests (
const string& suiteName,
const string& testNamePrefix,
void (BasicRectTester::*testFunc)(int,int),
BasicRectTester* fixture
);
};
Scrunitizer::TestSuite* BasicRectTester::makePredicateTests (
const string& suiteName,
const string& testNamePrefix,
void (BasicRectTester::*testFunc)(int,int),
BasicRectTester* fixture
) {
using Scrunitizer::apply;
using Scrunitizer::makeTestGadget;
Scrunitizer::TestSuite* suite = new Scrunitizer::TestSuite (suiteName);
suite->add (makeTestGadget (testNamePrefix + "NW", fixture, apply (testFunc, -1, -1)));
suite->add (makeTestGadget (testNamePrefix + "N", fixture, apply (testFunc, 0, -1)));
suite->add (makeTestGadget (testNamePrefix + "NE", fixture, apply (testFunc, 1, -1)));
suite->add (makeTestGadget (testNamePrefix + "W", fixture, apply (testFunc, -1, 0)));
suite->add (makeTestGadget (testNamePrefix + "C", fixture, apply (testFunc, 0, 0)));
suite->add (makeTestGadget (testNamePrefix + "E", fixture, apply (testFunc, 1, 0)));
suite->add (makeTestGadget (testNamePrefix + "SW", fixture, apply (testFunc, -1, 1)));
suite->add (makeTestGadget (testNamePrefix + "S", fixture, apply (testFunc, 0, 1)));
suite->add (makeTestGadget (testNamePrefix + "SE", fixture, apply (testFunc, 1, 1)));
return suite;
}
class GrownRectTester : public BasicRectTester {
protected:
virtual void setUpTestRect (Rect& r, int cx, int cy) {
r.reshape (cx * 10 - 2, cy * 10 - 2, 10 + 4, 10 + 4);
}
virtual bool expectedIntersectsResult (int cx, int cy) {
return true;
}
virtual bool expectedCoversResult (int cx, int cy) {
return false;
}
};
//***************************************************************
Scrunitizer::TestSuite* buildTestSuite (
Scrunitizer::TestManager& manager
) {
using Scrunitizer::apply;
using Scrunitizer::bind;
using Scrunitizer::makeTestCase;
using Scrunitizer::makeTestSuite;
using Scrunitizer::makeTestGadget;
Scrunitizer::TestSuite* top = new Scrunitizer::TestSuite ("Rectangle Tests");
Scrunitizer::TestSuite* classic = new Scrunitizer::TestSuite ("Classic Tests");
classic->add (new ClassicEqualsTest ());
classic->add (new ClassicCoversTest ());
top->add (classic);
Scrunitizer::TestSuite* functionBased = new Scrunitizer::TestSuite ("Function based Tests");
functionBased->add (makeTestCase ("Function based equals Test", EqualsTestFunction));
functionBased->add (makeTestCase ("Function based covers Test", CoversTestFunction));
top->add (functionBased);
Scrunitizer::TestSuite* allInOneFunc = new Scrunitizer::TestSuite ("All in one functions");
allInOneFunc->add (makeTestCase ("All in one intersection predicate test", testIntersectionPred));
top->add (allInOneFunc);
Scrunitizer::TestSuite* exhFuncBased = new Scrunitizer::TestSuite ("Exhaustive function based");
exhFuncBased->add (makeTestCase ("NW intersection test", testNWIntersection));
exhFuncBased->add (makeTestCase ("N intersection test", testNIntersection));
exhFuncBased->add (makeTestCase ("NE intersection test", testNEIntersection));
exhFuncBased->add (makeTestCase ("W intersection test", testWIntersection));
exhFuncBased->add (makeTestCase ("C intersection test", testCIntersection));
exhFuncBased->add (makeTestCase ("E intersection test", testEIntersection));
exhFuncBased->add (makeTestCase ("SW intersection test", testSWIntersection));
exhFuncBased->add (makeTestCase ("S intersection test", testSIntersection));
exhFuncBased->add (makeTestCase ("SE intersection test", testSEIntersection));
top->add (exhFuncBased);
Scrunitizer::TestSuite* paramFuncBased = new Scrunitizer::TestSuite("Parameterised function based");
paramFuncBased->add (makeTestCase ("NW intersection test", bind (testIntersects, -1, -1, false)));
paramFuncBased->add (makeTestCase ("N intersection test", bind (testIntersects, 0, -1, false)));
paramFuncBased->add (makeTestCase ("NE intersection test", bind (testIntersects, 1, -1, false)));
paramFuncBased->add (makeTestCase ("W intersection test", bind (testIntersects, -1, 0, false)));
paramFuncBased->add (makeTestCase ("C intersection test", bind (testIntersects, 0, 0, true )));
paramFuncBased->add (makeTestCase ("E intersection test", bind (testIntersects, 1, 0, false)));
paramFuncBased->add (makeTestCase ("SW intersection test", bind (testIntersects, -1, 1, false)));
paramFuncBased->add (makeTestCase ("S intersection test", bind (testIntersects, 0, 1, false)));
paramFuncBased->add (makeTestCase ("SE intersection test", bind (testIntersects, 1, 1, false)));
paramFuncBased->add (makeTestCase ("NW coverage test", bind (testCovers, -1, -1, false)));
paramFuncBased->add (makeTestCase ("N coverage test", bind (testCovers, 0, -1, false)));
paramFuncBased->add (makeTestCase ("NE coverage test", bind (testCovers, 1, -1, false)));
paramFuncBased->add (makeTestCase ("W coverage test", bind (testCovers, -1, 0, false)));
paramFuncBased->add (makeTestCase ("C coverage test", bind (testCovers, 0, 0, true )));
paramFuncBased->add (makeTestCase ("E coverage test", bind (testCovers, 1, 0, false)));
paramFuncBased->add (makeTestCase ("SW coverage test", bind (testCovers, -1, 1, false)));
paramFuncBased->add (makeTestCase ("S coverage test", bind (testCovers, 0, 1, false)));
paramFuncBased->add (makeTestCase ("SE coverage test", bind (testCovers, 1, 1, false)));
top->add (paramFuncBased);
Scrunitizer::TestSuite* objBased = new Scrunitizer::TestSuite ("Parametrized object based Tests");
objBased->add (makeTestGadget ("NW", apply (&RectangleTester::testIntersects, -1, -1, false)));
objBased->add (makeTestGadget ("N", apply (&RectangleTester::testIntersects, 0, -1, false)));
objBased->add (makeTestGadget ("NE", apply (&RectangleTester::testIntersects, 1, -1, false)));
objBased->add (makeTestGadget ("W", apply (&RectangleTester::testIntersects, -1, 0, false)));
objBased->add (makeTestGadget ("C", apply (&RectangleTester::testIntersects, 0, 0, true )));
objBased->add (makeTestGadget ("E", apply (&RectangleTester::testIntersects, 1, 0, false)));
objBased->add (makeTestGadget ("SW", apply (&RectangleTester::testIntersects, -1, 1, false)));
objBased->add (makeTestGadget ("S", apply (&RectangleTester::testIntersects, 0, 1, false)));
objBased->add (makeTestGadget ("SE", apply (&RectangleTester::testIntersects, 1, 1, false)));
objBased->add (makeTestGadget ("NW", apply (&RectangleTester::testCovers, -1, -1, false)));
objBased->add (makeTestGadget ("N", apply (&RectangleTester::testCovers, 0, -1, false)));
objBased->add (makeTestGadget ("NE", apply (&RectangleTester::testCovers, 1, -1, false)));
objBased->add (makeTestGadget ("W", apply (&RectangleTester::testCovers, -1, 0, false)));
objBased->add (makeTestGadget ("C", apply (&RectangleTester::testCovers, 0, 0, true )));
objBased->add (makeTestGadget ("E", apply (&RectangleTester::testCovers, 1, 0, false)));
objBased->add (makeTestGadget ("SW", apply (&RectangleTester::testCovers, -1, 1, false)));
objBased->add (makeTestGadget ("S", apply (&RectangleTester::testCovers, 0, 1, false)));
objBased->add (makeTestGadget ("SE", apply (&RectangleTester::testCovers, 1, 1, false)));
top->add (objBased);
BasicRectTester* basicRectFixture = new BasicRectTester ();
GrownRectTester* grownRectFixture = new GrownRectTester ();
manager.takeFixture (basicRectFixture);
manager.takeFixture (grownRectFixture);
top->add (BasicRectTester::makePredicateTests (
"Basic intersection test", "Basic Intersection ",
&BasicRectTester::testIntersects, basicRectFixture));
top->add (BasicRectTester::makePredicateTests (
"Grown rect intersection test", "Overlapping Rect Instersection ",
&BasicRectTester::testIntersects, grownRectFixture));
top->add (BasicRectTester::makePredicateTests (
"Basic covrage test", "Basic Coverage ",
&BasicRectTester::testCovers, basicRectFixture));
top->add (BasicRectTester::makePredicateTests (
"Grown rect coverage test", "Overlapping Rect Coverage ",
&BasicRectTester::testCovers, grownRectFixture));
manager.addCallGate (new Scrunitizer::ExceptionTrap<Rect::NoExtent> ("Rectangle without extend"));
return top;
}
//***************************************************************
int main (int argc, char** argv) {
using namespace Scrunitizer;
TestManager manager;
BriefSessionLog briefLogger (cerr);
VerboseSessionLog verboseLogger (cerr, false);
SessionReport screenReport (cerr, "Class 'Rect' Test Report", false);
SessionReport fileReport (cout, "Class 'Rect' Test Report", false, false);
ExecutionTracer tracer (manager, cout);
manager.addObserver (&briefLogger);
manager.addObserver (&verboseLogger);
manager.addObserver (&tracer);
manager.addObserver (&screenReport);
manager.addObserver (&fileReport);
ArgVector args (argv + 1, argv + argc);
try {
manager.configureFrom (args);
}
catch (LogFileNameMissing& ex) {
cerr << "ERROR: File name missing after option " << ex.getOptionId () << endl;
exit (1);
}
catch (CannotOpenLog& ex) {
cerr << "ERROR: Cannot open log file \"" << ex.getFileName () << "\"" << endl;
exit (1);
}
if (args.size () > 0) {
cerr << "ERROR: Unknown options:";
ArgVector::iterator pivot = args.begin ();
while (pivot != args.end ()) {
cerr << " " << *pivot++;
}
cerr << endl;
exit (1);
}
Scrunitizer::TestSuite* suite;
try {
suite = buildTestSuite (manager);
}
catch (IllegalTestSetUp) {
cerr << "ERROR: Illegal test set-up!" << endl;
exit (1);
}
cerr << endl
<< "************************ Rectangle Test Demo **********************" << endl;
manager.conduct (*suite);
cerr << "**************************** End of Demo **************************" << endl
<< endl;
return 0;
}