// 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 "bu/membuf.h" #include "bu/list.h" #include "bu/sio.h" #include #include using namespace Bu; suite Basic { test integer { Bu::List lInts; Bu::MemBuf mb; int64_t i = 1; for( int x = 0; x < 256; x++ ) { lInts.append( i ); Gats::Integer( i ).write( mb ); i = -(i<<1)-i; } mb.setPos( 0 ); for( Bu::List::iterator j = lInts.begin(); j; j++ ) { Gats::Object *pObj = Gats::Object::read( mb ); if( pObj->getType() != Gats::typeInteger ) unitFailed("Bad type read."); if( dynamic_cast(pObj)->getValue() != *j ) unitFailed("Bad number."); } } test string { Bu::List lStrs; Bu::MemBuf mb; lStrs.append( Bu::String() ); Gats::String("").write( mb ); { int iMax = 0; for( int j = 1; j <= (1<<16); j=j<<1 ) iMax += j; setStepCount( iMax ); } for( int j = 1; j <= (1<<16); j=j<<1 ) { Bu::String s( j ); for( int x = 0; x < j; x++ ) { s[x] = (unsigned char)(random()%256); } incProgress( j ); Gats::String( s ).write( mb ); lStrs.append( s ); } mb.setPos( 0 ); for( Bu::List::iterator i = lStrs.begin(); i; i++ ) { Gats::Object *pObj = Gats::Object::read( mb ); if( pObj->getType() != Gats::typeString ) unitFailed("Bad type read."); if( *dynamic_cast(pObj) != *i ) unitFailed("Bad string."); } } test boolean { Bu::List lBs; Bu::MemBuf mb; for( int j = 0; j < 1024; j++ ) { if( random()%2 == 0 ) { lBs.append( true ); Gats::Boolean( true ).write( mb ); } else { lBs.append( false ); Gats::Boolean( false ).write( mb ); } } mb.setPos( 0 ); for( Bu::List::iterator i = lBs.begin(); i; i++ ) { Gats::Object *pObj = Gats::Object::read( mb ); if( pObj->getType() != Gats::typeBoolean ) unitFailed("Bad type read."); if( dynamic_cast(pObj)->getValue() != *i ) unitFailed("Bad string."); } } 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; { Gats::Dictionary dict; dict.insert("bool", new Gats::Boolean(true) ); dict.insert("int", 33403055 ); dict.insert("str", "Hey there" ); dict.write( mb ); } mb.setPos( 0 ); { Gats::Object *pRead = Gats::Object::read( mb ); unitTest( pRead != NULL ); Gats::Dictionary *pDict = dynamic_cast(pRead); unitTest( pDict != NULL ); unitTest( pDict->getBool("bool") == true ); unitTest( pDict->getInt("int") == 33403055 ); unitTest( pDict->getStr("str") == "Hey there" ); unitTest( pDict->getSize() == 3 ); delete pDict; } } }