Removed SimpleString from CppUnitLite - now just uses std::string and boost::lexical_cast

release/4.3a0
Alex Cunningham 2012-07-18 15:43:54 +00:00
parent 90fd122876
commit 6266a2c56d
9 changed files with 61 additions and 256 deletions

View File

@ -13,14 +13,10 @@
#include "Failure.h"
#include <stdio.h>
#include <string.h>
Failure::Failure (const SimpleString& theTestName,
const SimpleString& theFileName,
Failure::Failure (const std::string& theTestName,
const std::string& theFileName,
long theLineNumber,
const SimpleString& theCondition)
const std::string& theCondition)
: message (theCondition),
testName (theTestName),
fileName (theFileName),
@ -28,9 +24,9 @@ Failure::Failure (const SimpleString& theTestName,
{
}
Failure::Failure (const SimpleString& theTestName,
const SimpleString& theFileName,
const SimpleString& theCondition)
Failure::Failure (const std::string& theTestName,
const std::string& theFileName,
const std::string& theCondition)
: message (theCondition),
testName (theTestName),
fileName (theFileName),
@ -39,33 +35,16 @@ Failure::Failure (const SimpleString& theTestName,
}
Failure::Failure (const SimpleString& theTestName,
const SimpleString& theFileName,
Failure::Failure (const std::string& theTestName,
const std::string& theFileName,
long theLineNumber,
const SimpleString& expected,
const SimpleString& actual)
: testName (theTestName),
const std::string& expected,
const std::string& actual)
: message("expected " + expected + " but was: " + actual),
testName (theTestName),
fileName (theFileName),
lineNumber (theLineNumber)
{
const char *part1 = "expected ";
const char *part3 = " but was: ";
char *stage = new char [strlen (part1)
+ expected.size ()
+ strlen (part3)
+ actual.size ()
+ 1];
sprintf(stage, "%s%s%s%s",
part1,
expected.asCharString(),
part3,
actual.asCharString());
message = SimpleString(stage);
delete stage;
}

View File

@ -24,32 +24,31 @@
#ifndef FAILURE_H
#define FAILURE_H
#include "SimpleString.h"
#include <string>
class Failure
{
public:
Failure (const SimpleString& theTestName,
const SimpleString& theFileName,
Failure (const std::string& theTestName,
const std::string& theFileName,
long theLineNumber,
const SimpleString& theCondition);
const std::string& theCondition);
Failure (const SimpleString& theTestName,
const SimpleString& theFileName,
Failure (const std::string& theTestName,
const std::string& theFileName,
long theLineNumber,
const SimpleString& expected,
const SimpleString& actual);
const std::string& expected,
const std::string& actual);
Failure (const SimpleString& theTestName,
const SimpleString& theFileName,
const SimpleString& theCondition);
Failure (const std::string& theTestName,
const std::string& theFileName,
const std::string& theCondition);
SimpleString message;
SimpleString testName;
SimpleString fileName;
std::string message;
std::string testName;
std::string fileName;
long lineNumber;
};

View File

@ -1,115 +0,0 @@
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
#include "SimpleString.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
static const int DEFAULT_SIZE = 20;
SimpleString::SimpleString ()
: buffer_(new char [1])
{
buffer_ [0] = '\0';
}
SimpleString::SimpleString (const char *otherBuffer)
: buffer_ (new char [strlen (otherBuffer) + 1])
{
strcpy (buffer_, otherBuffer);
}
SimpleString::SimpleString (const SimpleString& other)
{
buffer_ = new char [other.size() + 1];
strcpy(buffer_, other.buffer_);
}
SimpleString SimpleString::operator= (const SimpleString& other)
{
delete [] buffer_;
buffer_ = new char [other.size() + 1];
strcpy(buffer_, other.buffer_);
return *this;
}
SimpleString SimpleString::operator+ (const SimpleString& other)
{
SimpleString ret;
delete [] ret.buffer_;
ret.buffer_ = new char [size() + other.size() + 1];
strcpy(ret.buffer_, buffer_);
strcat(ret.buffer_, other.buffer_);
return ret;
}
char *SimpleString::asCharString () const
{
return buffer_;
}
int SimpleString::size() const
{
return strlen (buffer_);
}
SimpleString::~SimpleString ()
{
delete [] buffer_;
}
bool operator== (const SimpleString& left, const SimpleString& right)
{
return !strcmp (left.asCharString (), right.asCharString ());
}
SimpleString StringFrom (bool value)
{
char buffer [sizeof ("false") + 1];
sprintf (buffer, "%s", value ? "true" : "false");
return SimpleString(buffer);
}
SimpleString StringFrom (const char *value)
{
return SimpleString(value);
}
SimpleString StringFrom (long value)
{
char buffer [DEFAULT_SIZE];
sprintf (buffer, "%ld", value);
return SimpleString(buffer);
}
SimpleString StringFrom (double value)
{
char buffer [DEFAULT_SIZE];
sprintf (buffer, "%lg", value);
return SimpleString(buffer);
}
SimpleString StringFrom (const SimpleString& value)
{
return SimpleString(value);
}

View File

@ -1,57 +0,0 @@
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
///////////////////////////////////////////////////////////////////////////////
//
// SIMPLESTRING.H
//
// One of the design goals of CppUnitLite is to compilation with very old C++
// compilers. For that reason, I've added a simple string class that provides
// only the operations needed in CppUnitLite.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef SIMPLE_STRING
#define SIMPLE_STRING
class SimpleString
{
friend bool operator== (const SimpleString& left, const SimpleString& right);
public:
SimpleString ();
SimpleString (const char *value);
SimpleString (const SimpleString& other);
~SimpleString ();
SimpleString operator= (const SimpleString& other);
SimpleString operator+ (const SimpleString& other);
char *asCharString () const;
int size() const;
private:
char *buffer_;
};
SimpleString StringFrom (bool value);
SimpleString StringFrom (const char *value);
SimpleString StringFrom (long value);
SimpleString StringFrom (double value);
SimpleString StringFrom (const SimpleString& other);
#endif

View File

@ -10,20 +10,20 @@
* -------------------------------------------------------------------------- */
#include "Test.h"
#include "TestRegistry.h"
#include "TestResult.h"
#include "Failure.h"
#include <boost/lexical_cast.hpp>
Test::Test (const SimpleString& testName)
Test::Test (const std::string& testName)
: name_ (testName)
{
TestRegistry::addTest (this);
}
Test::Test (const SimpleString& testName, const SimpleString& filename, long lineNumber, bool safeCheck = true)
Test::Test (const std::string& testName, const std::string& filename, long lineNumber, bool safeCheck = true)
: name_(testName), filename_(filename), lineNumber_(lineNumber), safeCheck_(safeCheck)
{
TestRegistry::addTest (this);
@ -40,31 +40,31 @@ void Test::setNext(Test *test)
next_ = test;
}
bool Test::check(long expected, long actual, TestResult& result, const SimpleString& fileName, long lineNumber)
bool Test::check(long expected, long actual, TestResult& result, const std::string& fileName, long lineNumber)
{
if (expected == actual)
return true;
result.addFailure (
Failure (
name_,
StringFrom (__FILE__),
boost::lexical_cast<std::string> (__FILE__),
__LINE__,
StringFrom (expected),
StringFrom (actual)));
boost::lexical_cast<std::string> (expected),
boost::lexical_cast<std::string> (actual)));
return false;
}
bool Test::check(const SimpleString& expected, const SimpleString& actual, TestResult& result, const SimpleString& fileName, long lineNumber)
bool Test::check(const std::string& expected, const std::string& actual, TestResult& result, const std::string& fileName, long lineNumber)
{
if (expected == actual)
return true;
result.addFailure (
Failure (
name_,
StringFrom (__FILE__),
boost::lexical_cast<std::string> (__FILE__),
__LINE__,
expected,
actual));

View File

@ -23,7 +23,7 @@
#include <cmath>
#include "SimpleString.h"
#include <boost/lexical_cast.hpp>
class TestResult;
@ -32,8 +32,8 @@ class TestResult;
class Test
{
public:
Test (const SimpleString& testName);
Test (const SimpleString& testName, const SimpleString& filename, long lineNumber, bool safeCheck);
Test (const std::string& testName);
Test (const std::string& testName, const std::string& filename, long lineNumber, bool safeCheck);
virtual ~Test() {};
virtual void run (TestResult& result) = 0;
@ -41,19 +41,19 @@ public:
void setNext(Test *test);
Test *getNext () const;
SimpleString getName() const {return name_;}
SimpleString getFilename() const {return filename_;}
std::string getName() const {return name_;}
std::string getFilename() const {return filename_;}
long getLineNumber() const {return lineNumber_;}
bool safe() const {return safeCheck_;}
protected:
bool check (long expected, long actual, TestResult& result, const SimpleString& fileName, long lineNumber);
bool check (const SimpleString& expected, const SimpleString& actual, TestResult& result, const SimpleString& fileName, long lineNumber);
bool check (long expected, long actual, TestResult& result, const std::string& fileName, long lineNumber);
bool check (const std::string& expected, const std::string& actual, TestResult& result, const std::string& fileName, long lineNumber);
SimpleString name_;
std::string name_;
Test *next_;
SimpleString filename_;
std::string filename_;
long lineNumber_; /// This is the line line number of the test, rather than the a single check
bool safeCheck_;
@ -102,17 +102,17 @@ protected:
#define THROWS_EXCEPTION(condition)\
{ try { condition; \
result_.addFailure (Failure (name_, __FILE__,__LINE__, SimpleString("Didn't throw: ") + StringFrom(#condition))); \
result_.addFailure (Failure (name_, __FILE__,__LINE__, std::string("Didn't throw: ") + boost::lexical_cast<std::string>(#condition))); \
return; } \
catch (...) {} }
#define CHECK_EXCEPTION(condition, exception_name)\
{ try { condition; \
result_.addFailure (Failure (name_, __FILE__,__LINE__, SimpleString("Didn't throw: ") + StringFrom(#condition))); \
result_.addFailure (Failure (name_, __FILE__,__LINE__, std::string("Didn't throw: ") + boost::lexical_cast<std::string>(#condition))); \
return; } \
catch (exception_name&) {} \
catch (...) { \
result_.addFailure (Failure (name_, __FILE__,__LINE__, SimpleString("Wrong exception: ") + StringFrom(#condition) + StringFrom(", expected: ") + StringFrom(#exception_name))); \
result_.addFailure (Failure (name_, __FILE__,__LINE__, std::string("Wrong exception: ") + boost::lexical_cast<std::string>(#condition) + boost::lexical_cast<std::string>(", expected: ") + boost::lexical_cast<std::string>(#exception_name))); \
return; } }
#define EQUALITY(expected,actual)\
@ -120,21 +120,21 @@ protected:
result_.addFailure(Failure(name_, __FILE__, __LINE__, #expected, #actual)); }
#define CHECK_EQUAL(expected,actual)\
{ if ((expected) == (actual)) return; result_.addFailure(Failure(name_, __FILE__, __LINE__, StringFrom(expected), StringFrom(actual))); }
{ if ((expected) == (actual)) return; result_.addFailure(Failure(name_, __FILE__, __LINE__, boost::lexical_cast<std::string>(expected), boost::lexical_cast<std::string>(actual))); }
#define LONGS_EQUAL(expected,actual)\
{ long actualTemp = actual; \
long expectedTemp = expected; \
if ((expectedTemp) != (actualTemp)) \
{ result_.addFailure (Failure (name_, __FILE__, __LINE__, StringFrom(expectedTemp), \
StringFrom(actualTemp))); return; } }
{ result_.addFailure (Failure (name_, __FILE__, __LINE__, boost::lexical_cast<std::string>(expectedTemp), \
boost::lexical_cast<std::string>(actualTemp))); return; } }
#define DOUBLES_EQUAL(expected,actual,threshold)\
{ double actualTemp = actual; \
double expectedTemp = expected; \
if (fabs ((expectedTemp)-(actualTemp)) > threshold) \
{ result_.addFailure (Failure (name_, __FILE__, __LINE__, \
StringFrom((double)expectedTemp), StringFrom((double)actualTemp))); return; } }
boost::lexical_cast<std::string>((double)expectedTemp), boost::lexical_cast<std::string>((double)actualTemp))); return; } }
/* EXPECTs: tests will continue running after a failure */
@ -146,15 +146,15 @@ StringFrom((double)expectedTemp), StringFrom((double)actualTemp))); return; } }
{ long actualTemp = actual; \
long expectedTemp = expected; \
if ((expectedTemp) != (actualTemp)) \
{ result_.addFailure (Failure (name_, __FILE__, __LINE__, StringFrom(expectedTemp), \
StringFrom(actualTemp))); } }
{ result_.addFailure (Failure (name_, __FILE__, __LINE__, boost::lexical_cast<std::string>(expectedTemp), \
boost::lexical_cast<std::string>(actualTemp))); } }
#define EXPECT_DOUBLES_EQUAL(expected,actual,threshold)\
{ double actualTemp = actual; \
double expectedTemp = expected; \
if (fabs ((expectedTemp)-(actualTemp)) > threshold) \
{ result_.addFailure (Failure (name_, __FILE__, __LINE__, \
StringFrom((double)expectedTemp), StringFrom((double)actualTemp))); } }
boost::lexical_cast<std::string>((double)expectedTemp), boost::lexical_cast<std::string>((double)actualTemp))); } }
#define FAIL(text) \

View File

@ -17,7 +17,6 @@
#include "Failure.h"
#include "TestResult.h"
#include "TestRegistry.h"
#include "SimpleString.h"
void TestRegistry::addTest (Test *test)
{
@ -65,12 +64,12 @@ int TestRegistry::run (TestResult& result)
// catch standard exceptions and derivatives
result.addFailure(
Failure(test->getName(), test->getFilename(), test->getLineNumber(),
SimpleString("Exception: ") + SimpleString(e.what())));
std::string("Exception: ") + std::string(e.what())));
} catch (...) {
// catch all other exceptions
result.addFailure(
Failure(test->getName(), test->getFilename(), test->getLineNumber(),
SimpleString("ExceptionThrown!")));
"ExceptionThrown!"));
}
}
else {

View File

@ -32,16 +32,16 @@ void TestResult::addFailure (const Failure& failure)
if (failure.lineNumber < 0) // allow for no line number
fprintf (stdout, "%s%s%s%s\n",
"Failure: \"",
failure.message.asCharString (),
failure.message.c_str (),
"\" in ",
failure.fileName.asCharString ());
failure.fileName.c_str ());
else
fprintf (stdout, "%s%s%ld%s%s%s\n",
failure.fileName.asCharString(), // Format matches Eclipse error flagging
failure.fileName.c_str(), // Format matches Eclipse error flagging
":",
failure.lineNumber,
": Failure: \"",
failure.message.asCharString(),
failure.message.c_str(),
"\" ");
failureCount++;

View File

@ -381,7 +381,7 @@ TEST(ISAM2, optimize2) {
bool isam_check(const planarSLAM::Graph& fullgraph, const Values& fullinit, const ISAM2& isam, Test& test, TestResult& result) {
TestResult& result_ = result;
const SimpleString name_ = test.getName();
const std::string name_ = test.getName();
Values actual = isam.calculateEstimate();
Ordering ordering = isam.getOrdering(); // *fullgraph.orderingCOLAMD(fullinit).first;