C++ Library containing a lot of needful things: Stack Trace, Command Line Parser, Resource Handling, Configuration Files, Unix Command Execution, Directories, Regular Expressions, Tokenizer, Function Trace, Standard Extensions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
8.1 KiB

20 years ago
/** @file
$Id$
$Date$
$Author$
@copy © Marc Wäckerlin
@license LGPL, see file <a href="license.html">COPYING</a>
$Log$
Revision 1.3 2004/12/20 13:23:00 marc
new tests for string exceptions
Revision 1.2 2004/10/13 11:19:22 marc
remove stdout, print stack trace
20 years ago
Revision 1.1 2004/10/07 09:31:30 marc
new feature
*/
#include <mrw/string.hxx>
#include <mrw/list.hxx>
#include <mrw/vector.hxx>
#include <mrw/deque.hxx>
#include <mrw/set.hxx>
#include <mrw/map.hxx>
#include <mrw/multiset.hxx>
#include <mrw/multimap.hxx>
#include <mrw/stacktrace.hxx>
20 years ago
#include <cppunit/TestFixture.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/XmlOutputter.h>
#include <fstream>
20 years ago
class StdExtTest: public CppUnit::TestFixture {
public:
void StringConv() {
std::string s("Integer=");
int i(-7382);
CPPUNIT_ASSERT_EQUAL(std::string("Integer=-7382"), s+mrw::string(i));
CPPUNIT_ASSERT_EQUAL(i, mrw::to<int>(mrw::string(i)));
20 years ago
}
void StringShift() {
std::string s("Integer=");
int i(4);
CPPUNIT_ASSERT_EQUAL(std::string("Integer=4 test 45 xx"),
(s<<i<<" test "<<4<<(long)5<<" xx"));
20 years ago
int i2 = 0;
std::string s2, s3;
CPPUNIT_ASSERT_NO_THROW(s>>s2>>s3>>i2);
CPPUNIT_ASSERT_EQUAL(std::string("Integer=4"), s2);
CPPUNIT_ASSERT_EQUAL(std::string("test"), s3);
CPPUNIT_ASSERT_EQUAL(45, i2);
CPPUNIT_ASSERT_EQUAL(std::string(" xx"), s);
20 years ago
s2=""; s3="";
s>>s2;
CPPUNIT_ASSERT_EQUAL(std::string("xx"), s2);
CPPUNIT_ASSERT_EQUAL(std::string(), s);
20 years ago
}
void StringAdd() {
std::string s;
s=s+(signed short)1+(signed int)2+(signed long)3+(signed char)'A'+
(unsigned short)1+(unsigned int)2+(unsigned long)3+(unsigned char)'A'+'c';
CPPUNIT_ASSERT_EQUAL(std::string("1236512365c"), s);
20 years ago
s=(signed short)-4+s;
s=(signed int)5+s;
s=(signed long)6+s;
s=(signed char)8+s;
s=(unsigned short)4+s;
s=(unsigned int)5+s;
s=(unsigned long)6+s;
s=(unsigned char)8+s;
s='a'+s;
CPPUNIT_ASSERT_EQUAL(std::string("a8654865-41236512365c"), s);
20 years ago
s+=(signed short)-4;
s+=(signed int)5 ;
s+=(signed long)6;
s+=(signed char)8 ;
s+=(unsigned short)4;
s+=(unsigned int)5 ;
s+=(unsigned long)6;
s+=(unsigned char)8 ;
s+='a';
CPPUNIT_ASSERT_EQUAL(std::string("a8654865-41236512365c-45684568a"), s);
20 years ago
}
void ListShift() {
std::list<int> l;
l<<1<<2<<3<<4<<5<<6<<7<<8;
int i1(0), i2(0), i3(0), i4(0);
l>>i1>>i2>>i3>>i4;
// now: i1==1 i2==2 i3==3 i4==4 l=={5, 6, 7, 8}
CPPUNIT_ASSERT(i1==1 && i2==2 && i3==3 && i4==4);
CPPUNIT_ASSERT_EQUAL(std::list<int>::size_type(4), l.size());
20 years ago
for (int i=0; i<4; (l.pop_front(), ++i)) {
CPPUNIT_ASSERT_EQUAL(i+5, l.front());
20 years ago
}
bool exc(false);
try {
l>>i1;
} catch (mrw::length_error&) {
exc=true;
}
CPPUNIT_ASSERT(exc);
}
void VectorShift() {
std::vector<int> l;
CPPUNIT_ASSERT_NO_THROW(l<<1<<2<<3<<4<<5<<6<<7<<8);
CPPUNIT_ASSERT_EQUAL(std::vector<int>::size_type(8), l.size());
20 years ago
int i1(0), i2(0), i3(0), i4(0);
CPPUNIT_ASSERT_NO_THROW(l>>i1>>i2>>i3>>i4);
CPPUNIT_ASSERT_EQUAL(std::vector<int>::size_type(4), l.size());
20 years ago
// now: i1==1 i2==2 i3==3 i4==4 l=={5, 6, 7, 8}
CPPUNIT_ASSERT(i1==1 && i2==2 && i3==3 && i4==4);
CPPUNIT_ASSERT(l.size()==4);
for (int i=0; i<4; (l.erase(l.begin()), ++i)) {
CPPUNIT_ASSERT(l.front()==i+5);
}
bool exc(false);
try {
l>>i1;
} catch (mrw::length_error&) {
exc=true;
}
CPPUNIT_ASSERT(exc);
}
void DequeShift() {
std::deque<int> l;
CPPUNIT_ASSERT_NO_THROW(l<<1<<2<<3<<4<<5<<6<<7<<8);
20 years ago
int i1(0), i2(0), i3(0), i4(0);
CPPUNIT_ASSERT_NO_THROW(l>>i1>>i2>>i3>>i4);
20 years ago
// now: i1==1 i2==2 i3==3 i4==4 l=={5, 6, 7, 8}
CPPUNIT_ASSERT(i1==1 && i2==2 && i3==3 && i4==4);
CPPUNIT_ASSERT_EQUAL(std::deque<int>::size_type(4), l.size());
20 years ago
for (int i=0; i<4; (l.erase(l.begin()), ++i)) {
CPPUNIT_ASSERT_EQUAL(i+5, l.front());
20 years ago
}
bool exc(false);
try {
l>>i1;
} catch (mrw::length_error&) {
exc=true;
}
CPPUNIT_ASSERT(exc);
}
void SetShift() {
std::set<int> s;
bool exc(false);
try {
s<<1<<2<<3<<4<<5<<6<<7<<8<<8;
} catch (mrw::invalid_argument& e) {
mrw::StackTrace::createSymtable();
20 years ago
exc=true;
}
CPPUNIT_ASSERT(exc);
int i1(0), i2(0), i3(0), i4(0);
s>>i1>>i2>>i3>>i4;
// now: i1==1 i2==2 i3==3 i4==4 s=={5, 6, 7, 8}
CPPUNIT_ASSERT(i1==1 && i2==2 && i3==3 && i4==4);
CPPUNIT_ASSERT_EQUAL(std::set<int>::size_type(4), s.size());
20 years ago
for (int i=0; i<4; ++i) {
CPPUNIT_ASSERT(s.find(i+5)!=s.end());
}
s.erase(s.begin(), s.end());
exc=false;
try {
s>>i1;
} catch (mrw::length_error&) {
exc=true;
}
CPPUNIT_ASSERT(exc);
}
void MapShift() {
typedef std::map<int, std::string> Map;
Map s;
20 years ago
bool exc(false);
try {
s<<std::make_pair(1, std::string("one"))
<<std::make_pair(2, std::string("two"))
<<std::make_pair(2, std::string("two"));
} catch (mrw::invalid_argument& e) {
exc=true;
}
CPPUNIT_ASSERT(exc);
std::pair<int, std::string> i1, i2;
s>>i1>>i2;
// now: i1==1 i2==2 i3==3 i4==4 s=={5, 6, 7, 8}
CPPUNIT_ASSERT(i1==std::make_pair(1, std::string("one")) &&
i2==std::make_pair(2, std::string("two")));
CPPUNIT_ASSERT_EQUAL(Map::size_type(0), s.size());
20 years ago
exc=false;
try {
s>>i1;
} catch (mrw::length_error&) {
exc=true;
}
CPPUNIT_ASSERT(exc);
}
void MultisetShift() {
std::multiset<int> s;
s<<1<<2<<3<<4<<5<<6<<7<<8<<9;
int i1(0), i2(0), i3(0), i4(0);
s>>i1>>i2>>i3>>i4;
// now: i1==1 i2==2 i3==3 i4==4 s=={5, 6, 7, 8}
CPPUNIT_ASSERT(i1==1 && i2==2 && i3==3 && i4==4);
CPPUNIT_ASSERT_EQUAL(std::multiset<int>::size_type(5), s.size());
20 years ago
for (int i=0; i<5; ++i) {
CPPUNIT_ASSERT(s.find(i+5)!=s.end());
}
s.erase(s.begin(), s.end());
bool exc(false);
try {
s>>i1;
} catch (mrw::length_error&) {
exc=true;
}
CPPUNIT_ASSERT(exc);
}
void MultimapShift() {
typedef std::multimap<int, std::string> Map;
Map s;
20 years ago
s<<std::make_pair(1, std::string("one"))
<<std::make_pair(2, std::string("two"))
<<std::make_pair(2, std::string("two"));
std::pair<int, std::string> i1, i2;
s>>i1>>i2;
// now: i1==1 i2==2 i3==3 i4==4 s=={5, 6, 7, 8}
CPPUNIT_ASSERT(i1==std::make_pair(1, std::string("one")) &&
i2==std::make_pair(2, std::string("two")));
CPPUNIT_ASSERT_EQUAL(Map::size_type(1), s.size());
20 years ago
s>>i1;
CPPUNIT_ASSERT(std::make_pair(2, std::string("two"))==i1);
20 years ago
bool exc(false);
try {
s>>i1;
} catch (mrw::length_error&) {
exc=true;
}
CPPUNIT_ASSERT(exc);
}
void StringException1() {
std::string s("Hello World");
int hello;
s>>hello; // not an int, exception expected
}
void StringException2() {
std::string s("Hello World");
mrw::to<int>(s); // not an int, exception expected
}
20 years ago
CPPUNIT_TEST_SUITE(StdExtTest);
CPPUNIT_TEST(StringConv);
CPPUNIT_TEST(StringShift);
CPPUNIT_TEST(StringAdd);
CPPUNIT_TEST(ListShift);
CPPUNIT_TEST(VectorShift);
CPPUNIT_TEST(DequeShift);
CPPUNIT_TEST(SetShift);
CPPUNIT_TEST(MapShift);
CPPUNIT_TEST(MultisetShift);
CPPUNIT_TEST(MultimapShift);
CPPUNIT_TEST_EXCEPTION(StringException1, mrw::invalid_argument);
CPPUNIT_TEST_EXCEPTION(StringException2, mrw::invalid_argument);
20 years ago
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(StdExtTest);
int main(int argc, char** argv) try {
std::ofstream ofs((*argv+std::string(".xml")).c_str());
20 years ago
CppUnit::TextUi::TestRunner runner;
runner.setOutputter(new CppUnit::XmlOutputter(&runner.result(), ofs));
20 years ago
runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
return runner.run() ? 0 : 1;
} catch (std::exception& e) {
std::cerr<<"***Exception: "<<e.what()<<std::endl;
return 1;
}