Skip to content

Instantly share code, notes, and snippets.

@PeterHajdu
Created March 11, 2012 12:06
Show Gist options
  • Save PeterHajdu/2016204 to your computer and use it in GitHub Desktop.
Save PeterHajdu/2016204 to your computer and use it in GitHub Desktop.
DataContainer
*.swp
*.log
covsummary
*~
*gcov
*gcna
*gcda
*gcno
testFile
test.cpp
Implement a constructor for the DataContainer class which deserializes binary data.
Data format:
1 byte | <number of fields>
{
1 byte | <length of field name> (x)
x byte | <field name>
1 byte | <length of value> (y)
y byte | <value>
} *
Constructor declaration should be something like this:
DataContainer::DataContainer( const std::string& binaryData );
1. Start with a failing test implementation in testDataContainer.hpp
2. Implement the constructor in DataContainer class.
#include "DataContainer.hpp"
#include <algorithm>
ctx::DataContainer::DataContainer()
: m_data()
{
}
ctx::DataContainer::~DataContainer()
{
}
void
ctx::DataContainer::set( const std::string& key, const std::string& value )
{
m_data[ key ] = value;
}
std::string
ctx::DataContainer::get( const std::string& key ) const
{
HashTable::const_iterator it( m_data.find( key ) );
if ( m_data.end() == it )
{
return std::string("");
}
return it->second;
}
std::string
ctx::DataContainer::serialize() const
{
std::string serializedData;
char numberOfAttributes( m_data.size() );
serializedData.append( 1, numberOfAttributes );
std::for_each( m_data.begin(), m_data.end(), std::bind1st( std::ptr_fun( &DataContainer::serializeField ), serializedData ) );
return serializedData;
}
void
ctx::DataContainer::serializeField( std::string& buffer, const std::pair< const std::string, std::string > field )
{
buffer.append( 1, field.first.size() );
buffer.append( field.first );
buffer.append( 1, field.second.size() );
buffer.append( field.second );
}
#ifndef _DATACONTAINER_HPP_
#define _DATACONTAINER_HPP_
#include <string>
#include <map>
namespace ctx
{
class DataContainer
{
public:
DataContainer();
virtual void set( const std::string& key, const std::string& value );
virtual std::string get( const std::string& key ) const;
virtual std::string serialize() const;
virtual ~DataContainer();
private:
static void serializeField( std::string& buffer, const std::pair< const std::string, std::string > field );
typedef std::map< std::string, std::string > HashTable;
HashTable m_data;
DataContainer( const DataContainer& );
DataContainer& operator=( const DataContainer& );
};
}
#endif
SRC=DataContainer.cpp
TST=testDataContainer.hpp
include $(RULES)
#include <cxxtest/TestSuite.h>
#include <string>
#define private public
#include "DataContainer.hpp"
class TestDataContainer : public CxxTest::TestSuite
{
private:
ctx::DataContainer* m_dataContainer;
std::string m_dataName;
std::string m_dataValue;
public:
TestDataContainer()
: m_dataContainer( 0 )
, m_dataName( "appletree" )
, m_dataValue( "foo" )
{
}
void setUp()
{
m_dataContainer = new ctx::DataContainer();
}
void tearDown()
{
delete m_dataContainer;
}
void testSimpleParameterSetAndGet()
{
TS_ASSERT_EQUALS( "", m_dataContainer->get( m_dataName ) );
m_dataContainer->set( m_dataName, m_dataValue );
TS_ASSERT_EQUALS( m_dataValue, m_dataContainer->get( m_dataName ) );
}
void testSerializeData()
{
char position( 0 );
std::string serializedData( m_dataContainer->serialize() );
TS_ASSERT_EQUALS( 0, serializedData[ position ] );
m_dataContainer->set( m_dataName, m_dataValue );
serializedData = m_dataContainer->serialize();
TS_ASSERT_EQUALS( 1, serializedData[ position++ ] );
TS_ASSERT_EQUALS( m_dataName.size(), serializedData[ position++ ] );
TS_ASSERT_EQUALS( m_dataName, serializedData.substr( position, m_dataName.size() ) );
position+=m_dataName.size();
TS_ASSERT_EQUALS( m_dataValue.size(), serializedData[ position++ ] );
TS_ASSERT_EQUALS( m_dataValue, serializedData.substr( position, m_dataValue.size() ) );
position+=m_dataValue.size();
TS_ASSERT_EQUALS( position, serializedData.size() );
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment