// 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; } } }