From 11b5a91c5884d496744911f261ed6c2b053b9940 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 19 Aug 2010 06:28:17 +0000 Subject: Wow, it pretty much all works. the float format is a little funny, I treat it as a string, with a string header and then string data that is then turned into a float. It's pretty much how it's going to work, unless I come up with something revolutionary. --- src/unit/basic.unit | 19 ++++++++ src/unit/io.unit | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 src/unit/io.unit (limited to 'src/unit') diff --git a/src/unit/basic.unit b/src/unit/basic.unit index 744d679..d0309ee 100644 --- a/src/unit/basic.unit +++ b/src/unit/basic.unit @@ -18,6 +18,7 @@ #include "bu/sio.h" #include +#include using namespace Bu; @@ -120,6 +121,24 @@ suite Basic } } + test floats + { + Bu::MemBuf mb; + + Gats::Float( M_PI ).write( mb ); + + mb.setPos( 0 ); + + Gats::Object *pObj = Gats::Object::read( mb ); + unitTest( pObj != NULL ); + unitTest( pObj->getType() == Gats::typeFloat ); + Gats::Float *pFlt = dynamic_cast(pObj); + sio << "old = " << M_PI << ", new = " << pFlt->getValue() << sio.nl; + unitTest( pFlt->getValue() == M_PI ); + + delete pObj; + } + test dictionary { MemBuf mb; diff --git a/src/unit/io.unit b/src/unit/io.unit new file mode 100644 index 0000000..3e9c82c --- /dev/null +++ b/src/unit/io.unit @@ -0,0 +1,127 @@ +// vim: syntax=cpp +/* + * Copyright (C) 2007-2010 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 "gats/dictionary.h" +#include "gats/integer.h" +#include "gats/float.h" +#include "gats/list.h" +#include "gats/boolean.h" +#include "gats/string.h" +#include "gats/gatsstream.h" + +#include "bu/membuf.h" +#include "bu/list.h" +#include "bu/sio.h" + +#include + +using namespace Bu; + +suite Basic +{ + test basic + { + Bu::FString sTmpFileName("temp-XXXXXXXXX"); + Bu::File fIo = tempFile( sTmpFileName ); + + { + Gats::Dictionary dTest; + dTest.insert("age", 27 ); + dTest.insert("firstName", "Mike"); + dTest.insert("lastName", "Buland"); + dTest.insert("awake", true ); + + Gats::GatsStream sGats( fIo ); + sGats.writeObject( &dTest ); + } + + fIo.setPos( 0 ); + + { + Gats::GatsStream sGats( fIo ); + Gats::Object *pObj = sGats.readObject(); + unitTest( pObj != NULL ); + unitTest( pObj->getType() == Gats::typeDictionary ); + Gats::Dictionary *pDic = dynamic_cast(pObj); + unitTest( pDic->getSize() == 4 ); + unitTest( pDic->getInt("age") == 27 ); + unitTest( pDic->getStr("firstName") == "Mike" ); + unitTest( pDic->getStr("lastName") == "Buland" ); + unitTest( pDic->getBool("awake") == true ); + + delete pDic; + } + } + + test spacers + { + Bu::FString sTmpFileName("temp-XXXXXXXXX"); + Bu::File fIo = tempFile( sTmpFileName ); + + { + Gats::GatsStream sGats( fIo ); + Gats::Integer i( -157 ); + sGats.writeObject( &i ); + fIo.write( "\x00\x00\x00", 3 ); + Gats::String s("negative one hundred and fifty seven"); + sGats.writeObject( &s ); + } + + fIo.setPos( 0 ); + + { + Gats::GatsStream sGats( fIo ); + Gats::Object *pObj1 = sGats.readObject(); + unitTest( pObj1 != NULL ); + unitTest( pObj1->getType() == Gats::typeInteger ); + unitTest( dynamic_cast(pObj1)->getValue() == -157 ); + + Gats::Object *pObj2 = sGats.readObject(); + unitTest( pObj2 != NULL ); + unitTest( pObj2->getType() == Gats::typeString ); + unitTest( *dynamic_cast(pObj2) == + "negative one hundred and fifty seven" ); + + delete pObj1; + delete pObj2; + } + } + test biggerSpacers + { + Bu::FString sTmpFileName("temp-XXXXXXXXX"); + Bu::File fIo = tempFile( sTmpFileName ); + + { + Gats::GatsStream sGats( fIo ); + Gats::Integer i( -157 ); + sGats.writeObject( &i ); + fIo.write( "\x00\x00\x00\x00\x00\x00\x00\x00\x00", 9 ); + Gats::String s("negative one hundred and fifty seven"); + sGats.writeObject( &s ); + } + + fIo.setPos( 0 ); + + { + Gats::GatsStream sGats( fIo ); + Gats::Object *pObj1 = sGats.readObject(); + unitTest( pObj1 != NULL ); + unitTest( pObj1->getType() == Gats::typeInteger ); + unitTest( dynamic_cast(pObj1)->getValue() == -157 ); + + Gats::Object *pObj2 = sGats.readObject(); + unitTest( pObj2 != NULL ); + unitTest( pObj2->getType() == Gats::typeString ); + unitTest( *dynamic_cast(pObj2) == + "negative one hundred and fifty seven" ); + + delete pObj1; + delete pObj2; + } + } +} -- cgit v1.2.3