From 45e065bc4fc93731ea9a0543462bc7cf9e6084d7 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 7 Jan 2009 15:59:57 +0000 Subject: Only two real changes. First, Bu::FString and Bu::FBasicString are in different files. This won't affect any programs at all anywhere. This will just make it easier to maintain and extend later. You still want to include "bu/fstring.h" and use Bu::FString in code. The other is kinda fun. I created a special format for unit tests, they use the extension .unit now and use the mkunit.sh script to convert them to c++ code. There are some nice features here too, maintaining unit tests is much, much easier, and we can have more features without making the code any harder to use. Also, it will be easier to have the unit tests generate reports and be run from a master program and the like. --- src/unit/archive.cpp | 145 ------------------------------------------ src/unit/archive.unit | 130 ++++++++++++++++++++++++++++++++++++++ src/unit/array.cpp | 101 ------------------------------ src/unit/array.unit | 80 ++++++++++++++++++++++++ src/unit/file.cpp | 117 ---------------------------------- src/unit/file.unit | 95 ++++++++++++++++++++++++++++ src/unit/fstring.cpp | 170 -------------------------------------------------- src/unit/fstring.unit | 140 +++++++++++++++++++++++++++++++++++++++++ src/unit/hash.cpp | 109 -------------------------------- src/unit/hash.unit | 86 +++++++++++++++++++++++++ src/unit/membuf.cpp | 60 ------------------ src/unit/membuf.unit | 45 +++++++++++++ src/unit/taf.cpp | 133 --------------------------------------- src/unit/taf.unit | 112 +++++++++++++++++++++++++++++++++ src/unit/xml.cpp | 39 ------------ src/unit/xml.unit | 20 ++++++ 16 files changed, 708 insertions(+), 874 deletions(-) delete mode 100644 src/unit/archive.cpp create mode 100644 src/unit/archive.unit delete mode 100644 src/unit/array.cpp create mode 100644 src/unit/array.unit delete mode 100644 src/unit/file.cpp create mode 100644 src/unit/file.unit delete mode 100644 src/unit/fstring.cpp create mode 100644 src/unit/fstring.unit delete mode 100644 src/unit/hash.cpp create mode 100644 src/unit/hash.unit delete mode 100644 src/unit/membuf.cpp create mode 100644 src/unit/membuf.unit delete mode 100644 src/unit/taf.cpp create mode 100644 src/unit/taf.unit delete mode 100644 src/unit/xml.cpp create mode 100644 src/unit/xml.unit (limited to 'src/unit') diff --git a/src/unit/archive.cpp b/src/unit/archive.cpp deleted file mode 100644 index 8e71f4b..0000000 --- a/src/unit/archive.cpp +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright (C) 2007-2008 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/unitsuite.h" -#include "bu/membuf.h" - -class Unit : public Bu::UnitSuite -{ -public: - Unit() - { - setName("Archive"); - addTest( Unit::testPrimitives ); - addTest( Unit::testContainers ); - } - - virtual ~Unit() - { - } - - void testPrimitives() - { - Bu::MemBuf mb; - { - Bu::Archive ar( mb, Bu::Archive::save ); - ar << (int8_t)1; - ar << (uint8_t)2; - ar << (int16_t)3; - ar << (uint16_t)4; - ar << (int32_t)5; - ar << (uint32_t)6; - ar << (int64_t)7; - ar << (uint64_t)8; - ar << (char)9; - ar << (unsigned char)10; - ar << (short)11; - ar << (unsigned short)12; - ar << (int)13; - ar << (unsigned int)14; - ar << (long)15; - ar << (unsigned long)16; - ar << (long long)17; - ar << (unsigned long long)18; - ar.close(); - } - mb.setPos( 0 ); - { - Bu::Archive ar( mb, Bu::Archive::load ); - int8_t p1; - uint8_t p2; - int16_t p3; - uint16_t p4; - int32_t p5; - uint32_t p6; - int64_t p7; - uint64_t p8; - char p9; - unsigned char p10; - short p11; - unsigned short p12; - int p13; - unsigned int p14; - long p15; - unsigned long p16; - long long p17; - unsigned long long p18; - ar >> p1; - ar >> p2; - ar >> p3; - ar >> p4; - ar >> p5; - ar >> p6; - ar >> p7; - ar >> p8; - ar >> p9; - ar >> p10; - ar >> p11; - ar >> p12; - ar >> p13; - ar >> p14; - ar >> p15; - ar >> p16; - ar >> p17; - ar >> p18; - unitTest( p1 == 1 ); - unitTest( p2 == 2 ); - unitTest( p3 == 3 ); - unitTest( p4 == 4 ); - unitTest( p5 == 5 ); - unitTest( p6 == 6 ); - unitTest( p7 == 7 ); - unitTest( p8 == 8 ); - unitTest( p9 == 9 ); - unitTest( p10 == 10 ); - unitTest( p11 == 11 ); - unitTest( p12 == 12 ); - unitTest( p13 == 13 ); - unitTest( p14 == 14 ); - unitTest( p15 == 15 ); - unitTest( p16 == 16 ); - unitTest( p17 == 17 ); - unitTest( p18 == 18 ); - ar.close(); - } - } - - void testContainers() - { - Bu::MemBuf mb; - { - Bu::Archive ar( mb, Bu::Archive::save ); - Bu::FString sStr("This is a test string."); - Bu::List lList; - lList.append( 10 ); - lList.append( 20 ); - lList.append( 30 ); - lList.append( 40 ); - ar << sStr; - ar << lList; - ar.close(); - } - mb.setPos( 0 ); - { - Bu::Archive ar( mb, Bu::Archive::load ); - Bu::FString sStr; - Bu::List lList; - ar >> sStr; - ar >> lList; - unitTest( sStr == "This is a test string." ); - unitTest( lList.getSize() == 4 ); - Bu::List::iterator i = lList.begin(); - unitTest( *i == 10 ); i++; - unitTest( *i == 20 ); i++; - unitTest( *i == 30 ); i++; - unitTest( *i == 40 ); - ar.close(); - } - } -}; - -int main( int argc, char *argv[] ){ return Unit().run( argc, argv ); } diff --git a/src/unit/archive.unit b/src/unit/archive.unit new file mode 100644 index 0000000..ecc589b --- /dev/null +++ b/src/unit/archive.unit @@ -0,0 +1,130 @@ +// vim: syntax=cpp +/* + * Copyright (C) 2007-2008 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/membuf.h" + +{=Init} + +{%testPrimitives} +{ + Bu::MemBuf mb; + { + Bu::Archive ar( mb, Bu::Archive::save ); + ar << (int8_t)1; + ar << (uint8_t)2; + ar << (int16_t)3; + ar << (uint16_t)4; + ar << (int32_t)5; + ar << (uint32_t)6; + ar << (int64_t)7; + ar << (uint64_t)8; + ar << (char)9; + ar << (unsigned char)10; + ar << (short)11; + ar << (unsigned short)12; + ar << (int)13; + ar << (unsigned int)14; + ar << (long)15; + ar << (unsigned long)16; + ar << (long long)17; + ar << (unsigned long long)18; + ar.close(); + } + mb.setPos( 0 ); + { + Bu::Archive ar( mb, Bu::Archive::load ); + int8_t p1; + uint8_t p2; + int16_t p3; + uint16_t p4; + int32_t p5; + uint32_t p6; + int64_t p7; + uint64_t p8; + char p9; + unsigned char p10; + short p11; + unsigned short p12; + int p13; + unsigned int p14; + long p15; + unsigned long p16; + long long p17; + unsigned long long p18; + ar >> p1; + ar >> p2; + ar >> p3; + ar >> p4; + ar >> p5; + ar >> p6; + ar >> p7; + ar >> p8; + ar >> p9; + ar >> p10; + ar >> p11; + ar >> p12; + ar >> p13; + ar >> p14; + ar >> p15; + ar >> p16; + ar >> p17; + ar >> p18; + unitTest( p1 == 1 ); + unitTest( p2 == 2 ); + unitTest( p3 == 3 ); + unitTest( p4 == 4 ); + unitTest( p5 == 5 ); + unitTest( p6 == 6 ); + unitTest( p7 == 7 ); + unitTest( p8 == 8 ); + unitTest( p9 == 9 ); + unitTest( p10 == 10 ); + unitTest( p11 == 11 ); + unitTest( p12 == 12 ); + unitTest( p13 == 13 ); + unitTest( p14 == 14 ); + unitTest( p15 == 15 ); + unitTest( p16 == 16 ); + unitTest( p17 == 17 ); + unitTest( p18 == 18 ); + ar.close(); + } +} + +{%testContainers} +{ + Bu::MemBuf mb; + { + Bu::Archive ar( mb, Bu::Archive::save ); + Bu::FString sStr("This is a test string."); + Bu::List lList; + lList.append( 10 ); + lList.append( 20 ); + lList.append( 30 ); + lList.append( 40 ); + ar << sStr; + ar << lList; + ar.close(); + } + mb.setPos( 0 ); + { + Bu::Archive ar( mb, Bu::Archive::load ); + Bu::FString sStr; + Bu::List lList; + ar >> sStr; + ar >> lList; + unitTest( sStr == "This is a test string." ); + unitTest( lList.getSize() == 4 ); + Bu::List::iterator i = lList.begin(); + unitTest( *i == 10 ); i++; + unitTest( *i == 20 ); i++; + unitTest( *i == 30 ); i++; + unitTest( *i == 40 ); + ar.close(); + } +} diff --git a/src/unit/array.cpp b/src/unit/array.cpp deleted file mode 100644 index f7dc0ae..0000000 --- a/src/unit/array.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (C) 2007-2008 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/unitsuite.h" -#include "bu/hash.h" -#include "bu/array.h" - -class Unit : public Bu::UnitSuite -{ -public: - Unit() - { - setName("Array"); - addTest( Unit::general ); - addTest( Unit::iterate1 ); - addTest( Unit::iterate2 ); - addTest( Unit::copy ); - } - - virtual ~Unit() - { - } - - void general() - { - Bu::Array ai; - - ai.append( 5 ); - ai.append( 10 ); - unitTest( ai.getSize() == 2 ); - unitTest( ai.getCapacity() == 10 ); - unitTest( ai[0] == 5 ); - unitTest( ai[1] == 10 ); - } - - void iterate1() - { - Bu::Array ai; - for( int j = 0; j < 10; j++ ) - ai.append( j ); - - int j = 0; - for( Bu::Array::iterator i = ai.begin(); i != ai.end(); i++ ) - unitTest( (*i) == j++ ); - - const Bu::Array &ci = ai; - j = 0; - for( Bu::Array::const_iterator i = ci.begin(); i != ci.end(); i++ ) - unitTest( (*i) == j++ ); - } - - void iterate2() - { - Bu::Array ai; - for( Bu::Array::iterator i = ai.begin(); i != ai.end(); i++ ) - unitFailed("Empty lists shouldn't be iterated through."); - } - - void copy() - { - typedef Bu::Hash StrHash; - typedef Bu::Array StrHashArray; - - StrHash h1; - h1["Hi"] = "Yo"; - h1["Bye"] = "Later"; - - StrHash h2; - h2["Test"] = "Bloop"; - h2["Foo"] = "ooF"; - - StrHashArray a1; - a1.append( h1 ); - a1.append( h2 ); - - StrHashArray a2(a1); - - unitTest( a2[0].get("Hi") == "Yo" ); - unitTest( a2[0].get("Bye") == "Later" ); - unitTest( a2[1].get("Test") == "Bloop" ); - unitTest( a2[1].get("Foo") == "ooF" ); - - StrHashArray a3; - a3 = a1; - - unitTest( a3[0].get("Hi") == "Yo" ); - unitTest( a3[0].get("Bye") == "Later" ); - unitTest( a3[1].get("Test") == "Bloop" ); - unitTest( a3[1].get("Foo") == "ooF" ); - } -}; - -int main( int argc, char *argv[] ) -{ - return Unit().run( argc, argv ); -} - diff --git a/src/unit/array.unit b/src/unit/array.unit new file mode 100644 index 0000000..d5fc573 --- /dev/null +++ b/src/unit/array.unit @@ -0,0 +1,80 @@ +// vim: syntax=cpp +/* + * Copyright (C) 2007-2008 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/hash.h" +#include "bu/array.h" + +{=Init} + +{%general} +{ + Bu::Array ai; + + ai.append( 5 ); + ai.append( 10 ); + unitTest( ai.getSize() == 2 ); + unitTest( ai.getCapacity() == 10 ); + unitTest( ai[0] == 5 ); + unitTest( ai[1] == 10 ); +} + +{%iterate1} +{ + Bu::Array ai; + for( int j = 0; j < 10; j++ ) + ai.append( j ); + + int j = 0; + for( Bu::Array::iterator i = ai.begin(); i != ai.end(); i++ ) + unitTest( (*i) == j++ ); + + const Bu::Array &ci = ai; + j = 0; + for( Bu::Array::const_iterator i = ci.begin(); i != ci.end(); i++ ) + unitTest( (*i) == j++ ); +} + +{%iterate2} +{ + Bu::Array ai; + for( Bu::Array::iterator i = ai.begin(); i != ai.end(); i++ ) + unitFailed("Empty lists shouldn't be iterated through."); +} + +{%copy} +{ + typedef Bu::Hash StrHash; + typedef Bu::Array StrHashArray; + + StrHash h1; + h1["Hi"] = "Yo"; + h1["Bye"] = "Later"; + + StrHash h2; + h2["Test"] = "Bloop"; + h2["Foo"] = "ooF"; + + StrHashArray a1; + a1.append( h1 ); + a1.append( h2 ); + + StrHashArray a2(a1); + + unitTest( a2[0].get("Hi") == "Yo" ); + unitTest( a2[0].get("Bye") == "Later" ); + unitTest( a2[1].get("Test") == "Bloop" ); + unitTest( a2[1].get("Foo") == "ooF" ); + + StrHashArray a3; + a3 = a1; + + unitTest( a3[0].get("Hi") == "Yo" ); + unitTest( a3[0].get("Bye") == "Later" ); + unitTest( a3[1].get("Test") == "Bloop" ); + unitTest( a3[1].get("Foo") == "ooF" ); +} diff --git a/src/unit/file.cpp b/src/unit/file.cpp deleted file mode 100644 index cc19fac..0000000 --- a/src/unit/file.cpp +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (C) 2007-2008 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/unitsuite.h" -#include "bu/file.h" - -#include -#include -#include - -class Unit : public Bu::UnitSuite -{ -public: - Unit() - { - setName("File"); - addTest( Unit::writeFull ); - addTest( Unit::readBlocks ); - addTest( Unit::readError1 ); - addTest( Unit::readError2 ); - } - - virtual ~Unit() { } - - // - // Tests go here - // - void writeFull() - { - Bu::File sf("testfile1", Bu::File::Write ); - for( int c = 0; c < 256; c++ ) - { - unsigned char ch = (unsigned char)c; - sf.write( &ch, 1 ); - unitTest( sf.tell() == c+1 ); - } - //unitTest( sf.canRead() == false ); - //unitTest( sf.canWrite() == true ); - //unitTest( sf.canSeek() == true ); - sf.close(); - struct stat sdat; - stat("testfile1", &sdat ); - unitTest( sdat.st_size == 256 ); - } - - void readBlocks() - { - Bu::File sf("testfile1", Bu::File::Read ); - unsigned char buf[50]; - size_t total = 0; - for(;;) - { - size_t s = sf.read( buf, 50 ); - for( size_t c = 0; c < s; c++ ) - { - unitTest( buf[c] == (unsigned char)(c+total) ); - } - total += s; - if( s < 50 ) - { - unitTest( total == 256 ); - unitTest( sf.isEOS() == true ); - break; - } - } - sf.close(); - } - - void readError1() - { - try - { - Bu::File sf("doesn'texist", Bu::File::Read ); - unitFailed("No exception thrown"); - } - catch( Bu::FileException &e ) - { - return; - } - } - - void readError2() - { - Bu::File sf("testfile1", Bu::File::Read ); - char buf[256]; - int r = sf.read( buf, 256 ); - unitTest( r == 256 ); - // You have to read past the end to set the EOS flag. - unitTest( sf.isEOS() == false ); - try - { - if( sf.read( buf, 5 ) > 0 ) - { - unitFailed("Non-zero read result"); - } - else - { - sf.close(); - } - } - catch( Bu::FileException &e ) - { - sf.close(); - return; - } - } -}; - -int main( int argc, char *argv[] ) -{ - return Unit().run( argc, argv ); -} - diff --git a/src/unit/file.unit b/src/unit/file.unit new file mode 100644 index 0000000..e6320ad --- /dev/null +++ b/src/unit/file.unit @@ -0,0 +1,95 @@ +// vim: syntax=cpp +/* + * Copyright (C) 2007-2008 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/file.h" + +#include +#include +#include + +{=Init} + +{%writeFull} +{ + Bu::File sf("testfile1", Bu::File::Write ); + for( int c = 0; c < 256; c++ ) + { + unsigned char ch = (unsigned char)c; + sf.write( &ch, 1 ); + unitTest( sf.tell() == c+1 ); + } + //unitTest( sf.canRead() == false ); + //unitTest( sf.canWrite() == true ); + //unitTest( sf.canSeek() == true ); + sf.close(); + struct stat sdat; + stat("testfile1", &sdat ); + unitTest( sdat.st_size == 256 ); +} + +{%readBlocks} +{ + Bu::File sf("testfile1", Bu::File::Read ); + unsigned char buf[50]; + size_t total = 0; + for(;;) + { + size_t s = sf.read( buf, 50 ); + for( size_t c = 0; c < s; c++ ) + { + unitTest( buf[c] == (unsigned char)(c+total) ); + } + total += s; + if( s < 50 ) + { + unitTest( total == 256 ); + unitTest( sf.isEOS() == true ); + break; + } + } + sf.close(); +} + +{%readError1} +{ + try + { + Bu::File sf("doesn'texist", Bu::File::Read ); + unitFailed("No exception thrown"); + } + catch( Bu::FileException &e ) + { + return; + } +} + +{%readError2} +{ + Bu::File sf("testfile1", Bu::File::Read ); + char buf[256]; + int r = sf.read( buf, 256 ); + unitTest( r == 256 ); + // You have to read past the end to set the EOS flag. + unitTest( sf.isEOS() == false ); + try + { + if( sf.read( buf, 5 ) > 0 ) + { + unitFailed("Non-zero read result"); + } + else + { + sf.close(); + } + } + catch( Bu::FileException &e ) + { + sf.close(); + return; + } +} diff --git a/src/unit/fstring.cpp b/src/unit/fstring.cpp deleted file mode 100644 index 9430a83..0000000 --- a/src/unit/fstring.cpp +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright (C) 2007-2008 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/fstring.h" -#include "bu/unitsuite.h" - -#include - -class Unit : public Bu::UnitSuite -{ -public: - Unit() - { - setName("FString"); - addTest( Unit::compare1 ); - addTest( Unit::compare2 ); - addTest( Unit::appendSingle ); - addTest( Unit::shared1 ); - addTest( Unit::insert ); - addTest( Unit::remove ); - addTest( Unit::add1 ); - addTest( Unit::add2 ); - addTest( Unit::add3 ); - addTest( Unit::add4 ); - addTest( Unit::add5 ); - addTest( Unit::add6 ); - addTest( Unit::subStr1 ); - } - - virtual ~Unit() - { - } - - void compare1() - { - Bu::FString b("Bob"); - unitTest( !(b == "Bobo") ); - unitTest( b == "Bob" ); - } - - void compare2() - { - Bu::FString b("Bobo"); - unitTest( !(b == "Bob") ); - unitTest( b == "Bobo" ); - } - - void appendSingle() - { - Bu::FString b; - for( char l = 'a'; l < 'g'; l++ ) - b += l; - unitTest( b == "abcdef" ); - unitTest( strcmp( b.getStr(), "abcdef" ) == 0 ); - } - - void shared1() - { - Bu::FString a("Hey there"); - Bu::FString b( a ); - unitTest( a.getStr() == b.getStr() ); - b += " guy"; - unitTest( a.getStr() != b.getStr() ); - a = b; - unitTest( a.getStr() == b.getStr() ); - } - - void insert() - { - Bu::FString a("abcd"); - a.insert( 2, "-!-", 3 ); - unitTest( a == "ab-!-cd" ); - - a.insert( 0, "!!", 2 ); - unitTest( a == "!!ab-!-cd" ); - - a.insert( -10, "789", 3 ); - unitTest( a == "789!!ab-!-cd" ); - - a.insert( 12, "89", 2 ); - unitTest( a == "789!!ab-!-cd89" ); - - a.insert( 1203, "12", 2 ); - unitTest( a == "789!!ab-!-cd8912" ); - } - - void remove() - { - Bu::FString a("abHEYcd"); - a.remove( 2, 3 ); - unitTest( a == "abcd" ); - a.remove( 2, 5 ); - unitTest( a == "ab" ); - a += "cdefghijklmnop"; - a.remove( 5, 1 ); - unitTest( a = "abcdeghijklmnop" ); - } - - void add1() - { - Bu::FString a("hi there"); - Bu::FString b(", yeah!"); - Bu::FString c = a + b; - - unitTest( c == "hi there, yeah!" ); - } - - void add2() - { - Bu::FString a("hi there"); - Bu::FString c = a + ", yeah!"; - - unitTest( c == "hi there, yeah!" ); - } - - void add3() - { - Bu::FString a("hi there"); - Bu::FString b(", yeah!"); - Bu::FString c = a + ", Mr. Man" + b; - - unitTest( c == "hi there, Mr. Man, yeah!" ); - } - - void add4() - { - Bu::FString b(", yeah!"); - Bu::FString c = "hi there" + b; - - unitTest( c == "hi there, yeah!" ); - } - - void add5() - { - Bu::FString b; - Bu::FString c = "sup?"; - b += "hey, " + c; - - unitTest( b == "hey, sup?" ); - } - - void add6() - { - Bu::FString a("Hello"); - char b[256] = {"Dude"}; - Bu::FString c = a + "/" + b; - - unitTest( c == "Hello/Dude" ); - } - - void subStr1() - { - Bu::FString a("abcdefghijklmnop"); - unitTest( a.getSubStr( 5, 3 ) == "fgh" ); - unitTest( a.getSubStr( 10 ) == "klmnop" ); - unitTest( a.getSubStr( 40 ) == "" ); - unitTest( a.getSubStr( -10 ) == "abcdefghijklmnop" ); - unitTest( a.getSubStr( -15, 4 ) == "abcd" ); - } -}; - -int main( int argc, char *argv[] ) -{ - return Unit().run( argc, argv ); -} - diff --git a/src/unit/fstring.unit b/src/unit/fstring.unit new file mode 100644 index 0000000..93065fe --- /dev/null +++ b/src/unit/fstring.unit @@ -0,0 +1,140 @@ +// vim: syntax=cpp +/* + * Copyright (C) 2007-2008 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/fstring.h" + +#include + +{=Init} + +{%compare1} +{ + Bu::FString b("Bob"); + unitTest( !(b == "Bobo") ); + unitTest( b == "Bob" ); +} + +{%compare2} +{ + Bu::FString b("Bobo"); + unitTest( !(b == "Bob") ); + unitTest( b == "Bobo" ); +} + +{%appendSingle} +{ + Bu::FString b; + for( char l = 'a'; l < 'g'; l++ ) + b += l; + unitTest( b == "abcdef" ); + unitTest( strcmp( b.getStr(), "abcdef" ) == 0 ); +} + +{%shared1} +{ + Bu::FString a("Hey there"); + Bu::FString b( a ); + unitTest( a.getStr() == b.getStr() ); + b += " guy"; + unitTest( a.getStr() != b.getStr() ); + a = b; + unitTest( a.getStr() == b.getStr() ); +} + +{%insert} +{ + Bu::FString a("abcd"); + a.insert( 2, "-!-", 3 ); + unitTest( a == "ab-!-cd" ); + + a.insert( 0, "!!", 2 ); + unitTest( a == "!!ab-!-cd" ); + + a.insert( -10, "789", 3 ); + unitTest( a == "789!!ab-!-cd" ); + + a.insert( 12, "89", 2 ); + unitTest( a == "789!!ab-!-cd89" ); + + a.insert( 1203, "12", 2 ); + unitTest( a == "789!!ab-!-cd8912" ); +} + +{%remove} +{ + Bu::FString a("abHEYcd"); + a.remove( 2, 3 ); + unitTest( a == "abcd" ); + a.remove( 2, 5 ); + unitTest( a == "ab" ); + a += "cdefghijklmnop"; + a.remove( 5, 1 ); + unitTest( a = "abcdeghijklmnop" ); +} + +{%add1} +{ + Bu::FString a("hi there"); + Bu::FString b(", yeah!"); + Bu::FString c = a + b; + + unitTest( c == "hi there, yeah!" ); +} + +{%add2} +{ + Bu::FString a("hi there"); + Bu::FString c = a + ", yeah!"; + + unitTest( c == "hi there, yeah!" ); +} + +{%add3} +{ + Bu::FString a("hi there"); + Bu::FString b(", yeah!"); + Bu::FString c = a + ", Mr. Man" + b; + + unitTest( c == "hi there, Mr. Man, yeah!" ); +} + +{%add4} +{ + Bu::FString b(", yeah!"); + Bu::FString c = "hi there" + b; + + unitTest( c == "hi there, yeah!" ); +} + +{%add5} +{ + Bu::FString b; + Bu::FString c = "sup?"; + b += "hey, " + c; + + unitTest( b == "hey, sup?" ); +} + +{%add6} +{ + Bu::FString a("Hello"); + char b[256] = {"Dude"}; + Bu::FString c = a + "/" + b; + + unitTest( c == "Hello/Dude" ); +} + +{%subStr1} +{ + Bu::FString a("abcdefghijklmnop"); + unitTest( a.getSubStr( 5, 3 ) == "fgh" ); + unitTest( a.getSubStr( 10 ) == "klmnop" ); + unitTest( a.getSubStr( 40 ) == "" ); + unitTest( a.getSubStr( -10 ) == "abcdefghijklmnop" ); + unitTest( a.getSubStr( -15, 4 ) == "abcd" ); +} diff --git a/src/unit/hash.cpp b/src/unit/hash.cpp deleted file mode 100644 index e04a656..0000000 --- a/src/unit/hash.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (C) 2007-2008 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/fstring.h" -#include "bu/hash.h" -#include "bu/unitsuite.h" - -#include - -class Unit : public Bu::UnitSuite -{ -private: - typedef Bu::Hash StrIntHash; - typedef Bu::Hash StrStrHash; - typedef Bu::Hash IntStrHash; - -public: - Unit() - { - setName("Hash"); - addTest( Unit::insert1 ); - addTest( Unit::insert2 ); - addTest( Unit::insert3 ); - addTest( Unit::probe1 ); - addTest( Unit::erase1 ); - } - - virtual ~Unit() - { - } - - void probe1() - { - StrIntHash h; - char buf[20]; - for(int i=1;i<10000;i++) - { - sprintf(buf,"%d",i); - Bu::FString sTmp(buf); - h[sTmp] = i; - unitTest( h.has(sTmp) ); - } - } - - void insert1() - { - StrIntHash h; - h["Hi"] = 42; - unitTest( h["Hi"] == 42 ); - } - - void insert2() - { - StrStrHash h; - h["Hi"] = "Yo"; - h["Bye"] = "Later"; - unitTest( h["Hi"].getValue() == "Yo" ); - - StrStrHash h2(h); - unitTest( h2["Hi"].getValue() = "Yo" ); - unitTest( h2["Bye"].getValue() = "Later" ); - - StrStrHash h3; - h3 = h; - unitTest( h3["Hi"].getValue() = "Yo" ); - unitTest( h3["Bye"].getValue() = "Later" ); - } - - void insert3() - { - IntStrHash h; - - for( unsigned int i=1; i<50; i++ ) - { - h[i] = "testing"; - unitTest( h.getSize() == i ); - } - } - - void erase1() - { - StrIntHash h; - h.insert("Number 1", 1 ); - h.insert("Number 2", 2 ); - h.insert("Number 3", 3 ); - h.erase("Number 2"); - h.get("Number 3"); - try { - h.get("Number 2"); - unitFailed("h.get(\"Number 2\") should have thrown an exception."); - } catch( Bu::HashException &e ) { } - - /* printf("\n"); - for( StrIntHash::iterator i = h.begin(); i != h.end(); i++ ) - { - printf(" - \"%s\" = %d\n", i.getKey().getStr(), i.getValue() ); - } */ - } -}; - -int main( int argc, char *argv[] ) -{ - return Unit().run( argc, argv ); -} - diff --git a/src/unit/hash.unit b/src/unit/hash.unit new file mode 100644 index 0000000..bd7da61 --- /dev/null +++ b/src/unit/hash.unit @@ -0,0 +1,86 @@ +// vim: syntax=cpp +/* + * Copyright (C) 2007-2008 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/fstring.h" +#include "bu/hash.h" + +#include + +typedef Bu::Hash StrIntHash; +typedef Bu::Hash StrStrHash; +typedef Bu::Hash IntStrHash; + +{=Init} + +{%probe1} +{ + StrIntHash h; + char buf[20]; + for(int i=1;i<10000;i++) + { + sprintf(buf,"%d",i); + Bu::FString sTmp(buf); + h[sTmp] = i; + unitTest( h.has(sTmp) ); + } +} + +{%insert1} +{ + StrIntHash h; + h["Hi"] = 42; + unitTest( h["Hi"] == 42 ); +} + +{%insert2} +{ + StrStrHash h; + h["Hi"] = "Yo"; + h["Bye"] = "Later"; + unitTest( h["Hi"].getValue() == "Yo" ); + + StrStrHash h2(h); + unitTest( h2["Hi"].getValue() = "Yo" ); + unitTest( h2["Bye"].getValue() = "Later" ); + + StrStrHash h3; + h3 = h; + unitTest( h3["Hi"].getValue() = "Yo" ); + unitTest( h3["Bye"].getValue() = "Later" ); +} + +{%insert3} +{ + IntStrHash h; + + for( unsigned int i=1; i<50; i++ ) + { + h[i] = "testing"; + unitTest( h.getSize() == i ); + } +} + +{%erase1} +{ + StrIntHash h; + h.insert("Number 1", 1 ); + h.insert("Number 2", 2 ); + h.insert("Number 3", 3 ); + h.erase("Number 2"); + h.get("Number 3"); + try { + h.get("Number 2"); + unitFailed("h.get(\"Number 2\") should have thrown an exception."); + } catch( Bu::HashException &e ) { } + +/* printf("\n"); + for( StrIntHash::iterator i = h.begin(); i != h.end(); i++ ) + { + printf(" - \"%s\" = %d\n", i.getKey().getStr(), i.getValue() ); + } */ +} diff --git a/src/unit/membuf.cpp b/src/unit/membuf.cpp deleted file mode 100644 index dc02aa3..0000000 --- a/src/unit/membuf.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) 2007-2008 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/unitsuite.h" -#include "bu/membuf.h" - -class Unit : public Bu::UnitSuite -{ -public: - Unit() - { - setName("MemBuf"); - addTest( Unit::testWriteRead01 ); - addTest( Unit::testOverwrite1 ); - } - - virtual ~Unit() - { - } - - void testWriteRead01() - { - Bu::MemBuf mb; - unitTest( mb.write("ab", 2 ) == 2 ); - unitTest( mb.write("cde", 3 ) == 3 ); - unitTest( mb.write("FG", 2 ) == 2 ); - - mb.setPos( 0 ); - - char buf[8]; - buf[7] = '\0'; - unitTest( mb.read( buf, 7 ) == 7 ); - unitTest( !strncmp( buf, "abcdeFG", 7 ) ); - unitTest( mb.read( buf, 7 ) == 0 ); - mb.seek( -3 ); - unitTest( mb.read( buf, 7 ) == 3 ); - unitTest( !strncmp( buf, "eFG", 3 ) ); - } - - void testOverwrite1() - { - Bu::MemBuf mb; - unitTest( mb.write("0123456789") == 10 ); - mb.setPos( 4 ); - unitTest( mb.write("-5-") == 3 ); - mb.setPos( 9 ); - mb.write("Hey!!!"); - unitTest( mb.tell() == 15 ); - char buf[50]; - mb.setPos( 0 ); - buf[mb.read( buf, 50 )] = '\0'; - unitTest( !strcmp( buf, "0123-5-78Hey!!!" ) ); - } -}; - -int main( int argc, char *argv[] ){ return Unit().run( argc, argv ); } diff --git a/src/unit/membuf.unit b/src/unit/membuf.unit new file mode 100644 index 0000000..aebf36c --- /dev/null +++ b/src/unit/membuf.unit @@ -0,0 +1,45 @@ +// vim: syntax=cpp +/* + * Copyright (C) 2007-2008 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/membuf.h" + +{=Init} + +{%testWriteRead01} +{ + Bu::MemBuf mb; + unitTest( mb.write("ab", 2 ) == 2 ); + unitTest( mb.write("cde", 3 ) == 3 ); + unitTest( mb.write("FG", 2 ) == 2 ); + + mb.setPos( 0 ); + + char buf[8]; + buf[7] = '\0'; + unitTest( mb.read( buf, 7 ) == 7 ); + unitTest( !strncmp( buf, "abcdeFG", 7 ) ); + unitTest( mb.read( buf, 7 ) == 0 ); + mb.seek( -3 ); + unitTest( mb.read( buf, 7 ) == 3 ); + unitTest( !strncmp( buf, "eFG", 3 ) ); +} + +{%testOverwrite1} +{ + Bu::MemBuf mb; + unitTest( mb.write("0123456789") == 10 ); + mb.setPos( 4 ); + unitTest( mb.write("-5-") == 3 ); + mb.setPos( 9 ); + mb.write("Hey!!!"); + unitTest( mb.tell() == 15 ); + char buf[50]; + mb.setPos( 0 ); + buf[mb.read( buf, 50 )] = '\0'; + unitTest( !strcmp( buf, "0123-5-78Hey!!!" ) ); +} diff --git a/src/unit/taf.cpp b/src/unit/taf.cpp deleted file mode 100644 index e4b3ccc..0000000 --- a/src/unit/taf.cpp +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (C) 2007-2008 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/unitsuite.h" -#include "bu/file.h" -#include "bu/tafreader.h" -#include "bu/tafwriter.h" -#include "bu/membuf.h" - -#include -#include - -class Unit : public Bu::UnitSuite -{ -public: - Unit() - { - setName("taf"); - addTest( Unit::read1 ); - addTest( Unit::encode1 ); - addTest( Unit::emptyStr1 ); - addTest( Unit::incomplete1 ); - } - - virtual ~Unit() - { - } - - void read1() - { -#define FN_TMP ("/tmp/tmpXXXXXXXX") - Bu::FString sFnTmp(FN_TMP); - Bu::File fOut = Bu::File::tempFile( sFnTmp ); - const char *data = -"{test: name=\"Bob\"}" -; - fOut.write(data,strlen(data)); - fOut.close(); - - Bu::File fIn(sFnTmp.getStr(), Bu::File::Read ); - Bu::TafReader tr(fIn); - - Bu::TafGroup *tn = tr.readGroup(); - unitTest( !strcmp("Bob", tn->getProperty("name").getStr()) ); - delete tn; - - unlink(sFnTmp.getStr()); -#undef FN_TMP - } - - void encode1() - { - Bu::MemBuf mb; - Bu::TafWriter tw( mb ); - - Bu::TafGroup g("Test data"); - Bu::FString sData( 256 ); - for( int j = 0; j < 256; j++ ) - sData[j] = (unsigned char)j; - g.addChild( new Bu::TafProperty("Encoded", sData) ); - tw.writeGroup( &g ); - - static const char *cmpdata = "{\"Test data\":\n \"Encoded\"=\"" - "\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07" - "\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F" - "\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17" - "\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F" - " !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCD" - "EFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghi" - "jklmnopqrstuvwxyz{|}~\\x7F" - "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87" - "\\x88\\x89\\x8A\\x8B\\x8C\\x8D\\x8E\\x8F" - "\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97" - "\\x98\\x99\\x9A\\x9B\\x9C\\x9D\\x9E\\x9F" - "\\xA0\\xA1\\xA2\\xA3\\xA4\\xA5\\xA6\\xA7" - "\\xA8\\xA9\\xAA\\xAB\\xAC\\xAD\\xAE\\xAF" - "\\xB0\\xB1\\xB2\\xB3\\xB4\\xB5\\xB6\\xB7" - "\\xB8\\xB9\\xBA\\xBB\\xBC\\xBD\\xBE\\xBF" - "\\xC0\\xC1\\xC2\\xC3\\xC4\\xC5\\xC6\\xC7" - "\\xC8\\xC9\\xCA\\xCB\\xCC\\xCD\\xCE\\xCF" - "\\xD0\\xD1\\xD2\\xD3\\xD4\\xD5\\xD6\\xD7" - "\\xD8\\xD9\\xDA\\xDB\\xDC\\xDD\\xDE\\xDF" - "\\xE0\\xE1\\xE2\\xE3\\xE4\\xE5\\xE6\\xE7" - "\\xE8\\xE9\\xEA\\xEB\\xEC\\xED\\xEE\\xEF" - "\\xF0\\xF1\\xF2\\xF3\\xF4\\xF5\\xF6\\xF7" - "\\xF8\\xF9\\xFA\\xFB\\xFC\\xFD\\xFE\\xFF\"\n}\n"; - unitTest( mb.getString() == cmpdata ); - mb.setPos( 0 ); - Bu::TafReader tr( mb ); - Bu::TafGroup *rg = tr.readGroup(); - unitTest( rg->getProperty("Encoded") == sData ); - delete rg; - } - - void emptyStr1() - { - Bu::MemBuf mb; - Bu::TafWriter tw( mb ); - - Bu::TafGroup g("Test Group"); - Bu::FString sVal; - g.addChild( new Bu::TafProperty("Lame", sVal) ); - tw.writeGroup( &g ); - - unitTest( - mb.getString() == "{\"Test Group\":\n \"Lame\"=\"\"\n}\n" ); - } - - void incomplete1() - { - try - { - Bu::MemBuf mb("{Lame: \"Hello=\""); - Bu::TafReader tr( mb ); - delete tr.readGroup(); - unitFailed("Should have thrown an exception, didn't."); - } - catch( Bu::TafException &e ) - { - // Woot - } - } -}; - -int main( int argc, char *argv[] ) -{ - return Unit().run( argc, argv ); -} - diff --git a/src/unit/taf.unit b/src/unit/taf.unit new file mode 100644 index 0000000..5588c85 --- /dev/null +++ b/src/unit/taf.unit @@ -0,0 +1,112 @@ +// vim: syntax=cpp +/* + * Copyright (C) 2007-2008 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/file.h" +#include "bu/tafreader.h" +#include "bu/tafwriter.h" +#include "bu/membuf.h" + +#include +#include + +{=Init} + +{%read1} +{ +#define FN_TMP ("/tmp/tmpXXXXXXXX") + Bu::FString sFnTmp(FN_TMP); + Bu::File fOut = Bu::File::tempFile( sFnTmp ); + const char *data = +"{test: name=\"Bob\"}" +; + fOut.write(data,strlen(data)); + fOut.close(); + + Bu::File fIn(sFnTmp.getStr(), Bu::File::Read ); + Bu::TafReader tr(fIn); + + Bu::TafGroup *tn = tr.readGroup(); + unitTest( !strcmp("Bob", tn->getProperty("name").getStr()) ); + delete tn; + + unlink(sFnTmp.getStr()); +#undef FN_TMP +} + +{%encode1} +{ + Bu::MemBuf mb; + Bu::TafWriter tw( mb ); + + Bu::TafGroup g("Test data"); + Bu::FString sData( 256 ); + for( int j = 0; j < 256; j++ ) + sData[j] = (unsigned char)j; + g.addChild( new Bu::TafProperty("Encoded", sData) ); + tw.writeGroup( &g ); + + static const char *cmpdata = "{\"Test data\":\n \"Encoded\"=\"" + "\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07" + "\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F" + "\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17" + "\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F" + " !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCD" + "EFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghi" + "jklmnopqrstuvwxyz{|}~\\x7F" + "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87" + "\\x88\\x89\\x8A\\x8B\\x8C\\x8D\\x8E\\x8F" + "\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97" + "\\x98\\x99\\x9A\\x9B\\x9C\\x9D\\x9E\\x9F" + "\\xA0\\xA1\\xA2\\xA3\\xA4\\xA5\\xA6\\xA7" + "\\xA8\\xA9\\xAA\\xAB\\xAC\\xAD\\xAE\\xAF" + "\\xB0\\xB1\\xB2\\xB3\\xB4\\xB5\\xB6\\xB7" + "\\xB8\\xB9\\xBA\\xBB\\xBC\\xBD\\xBE\\xBF" + "\\xC0\\xC1\\xC2\\xC3\\xC4\\xC5\\xC6\\xC7" + "\\xC8\\xC9\\xCA\\xCB\\xCC\\xCD\\xCE\\xCF" + "\\xD0\\xD1\\xD2\\xD3\\xD4\\xD5\\xD6\\xD7" + "\\xD8\\xD9\\xDA\\xDB\\xDC\\xDD\\xDE\\xDF" + "\\xE0\\xE1\\xE2\\xE3\\xE4\\xE5\\xE6\\xE7" + "\\xE8\\xE9\\xEA\\xEB\\xEC\\xED\\xEE\\xEF" + "\\xF0\\xF1\\xF2\\xF3\\xF4\\xF5\\xF6\\xF7" + "\\xF8\\xF9\\xFA\\xFB\\xFC\\xFD\\xFE\\xFF\"\n}\n"; + unitTest( mb.getString() == cmpdata ); + mb.setPos( 0 ); + Bu::TafReader tr( mb ); + Bu::TafGroup *rg = tr.readGroup(); + unitTest( rg->getProperty("Encoded") == sData ); + delete rg; +} + +{%emptyStr1} +{ + Bu::MemBuf mb; + Bu::TafWriter tw( mb ); + + Bu::TafGroup g("Test Group"); + Bu::FString sVal; + g.addChild( new Bu::TafProperty("Lame", sVal) ); + tw.writeGroup( &g ); + + unitTest( + mb.getString() == "{\"Test Group\":\n \"Lame\"=\"\"\n}\n" ); +} + +{%incomplete1} +{ + try + { + Bu::MemBuf mb("{Lame: \"Hello=\""); + Bu::TafReader tr( mb ); + delete tr.readGroup(); + unitFailed("Should have thrown an exception, didn't."); + } + catch( Bu::TafException &e ) + { + // Woot + } +} diff --git a/src/unit/xml.cpp b/src/unit/xml.cpp deleted file mode 100644 index e845cc1..0000000 --- a/src/unit/xml.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2007-2008 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/fstring.h" -#include "bu/unitsuite.h" -#include "bu/xmlreader.h" -#include "bu/membuf.h" - -class Unit : public Bu::UnitSuite -{ -public: - Unit() - { - setName("Xml"); - addTest( Unit::declaration ); - } - - virtual ~Unit() - { - } - - void declaration() - { - Bu::FString sXml(" "); - Bu::MemBuf buf( sXml ); - Bu::XmlReader xr( buf ); - } - -}; - -int main( int argc, char *argv[] ) -{ - return Unit().run( argc, argv ); -} - diff --git a/src/unit/xml.unit b/src/unit/xml.unit new file mode 100644 index 0000000..738ad66 --- /dev/null +++ b/src/unit/xml.unit @@ -0,0 +1,20 @@ +// vim: syntax=cpp +/* + * Copyright (C) 2007-2008 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/fstring.h" +#include "bu/xmlreader.h" +#include "bu/membuf.h" + +{=Init} + +{%declaration} +{ + Bu::FString sXml(" "); + Bu::MemBuf buf( sXml ); + Bu::XmlReader xr( buf ); +} -- cgit v1.2.3