Skip to content

Commit

Permalink
Refs #8550. Read method functionality, plus tests for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbekasov committed Dec 4, 2013
1 parent 787b53b commit de36ec3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
Expand Up @@ -4,6 +4,10 @@
#include "MantidAPI/Column.h"
#include "MantidKernel/System.h"

#include <boost/algorithm/string/join.hpp>
#include <boost/tokenizer.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string/trim.hpp>

namespace Mantid
{
Expand Down Expand Up @@ -75,7 +79,28 @@ namespace DataObjects
/// Set item from a string value
virtual void read(size_t index, const std::string & text)
{
// TODO: implement
std::vector<Type> newValues;

boost::char_separator<char> delim(",");
boost::tokenizer< boost::char_separator<char> > elements(text, delim);

for ( auto it = elements.begin(); it != elements.end(); it++ )
{
std::string element(*it);

boost::trim(element);

try
{
newValues.push_back( boost::lexical_cast<Type>(element) );
}
catch(boost::bad_lexical_cast&)
{
throw std::invalid_argument("Unable to convert one of the elements: " + element);
}
}

m_data.at(index) = newValues;
}

/// Specialized type check
Expand Down Expand Up @@ -112,7 +137,7 @@ namespace DataObjects
/// Sets the new column size.
virtual void resize(size_t count)
{
// TODO: implement
m_data.resize(count);
}

/// Inserts an item.
Expand All @@ -129,13 +154,13 @@ namespace DataObjects
/// Pointer to a data element
virtual void* void_pointer(size_t index)
{
// TODO: implement
return &m_data.at(index);
}

/// Pointer to a data element
virtual const void* void_pointer(size_t index) const
{
// TODO: implement
return &m_data.at(index);
}

private:
Expand Down
42 changes: 42 additions & 0 deletions Code/Mantid/Framework/DataObjects/test/VectorColumnTest.h
Expand Up @@ -7,6 +7,15 @@

using Mantid::DataObjects::VectorColumn;

template<class Type>
class VectorColumnTestHelper : public VectorColumn<Type>
{
public:
using VectorColumn<Type>::resize;
using VectorColumn<Type>::insert;
using VectorColumn<Type>::remove;
};

class VectorColumnTest : public CxxTest::TestSuite
{
public:
Expand All @@ -21,6 +30,39 @@ class VectorColumnTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS( col.type(), "vector_int");
}

void test_read()
{
VectorColumnTestHelper<int> col;

col.resize(5);

// Simple case
TS_ASSERT_THROWS_NOTHING( col.read(0,"1,2,3") );
std::vector<int> v1;
v1.push_back(1); v1.push_back(2); v1.push_back(3);
TS_ASSERT_EQUALS( col.cell< std::vector<int> >(0), v1 );

// Check if trimming works
TS_ASSERT_THROWS_NOTHING( col.read(1," 4, 5, 6") );
std::vector<int> v2;
v2.push_back(4); v2.push_back(5); v2.push_back(6);
TS_ASSERT_EQUALS( col.cell< std::vector<int> >(1), v2 );

// Single element
TS_ASSERT_THROWS_NOTHING( col.read(2,"7") );
std::vector<int> v3;
v3.push_back(7);
TS_ASSERT_EQUALS( col.cell< std::vector<int> >(2), v3 );

// Empty string
TS_ASSERT_THROWS_NOTHING( col.read(3,"") );
std::vector<int> v4;
TS_ASSERT_EQUALS( col.cell< std::vector<int> >(3), v4 );
TS_ASSERT_EQUALS( col.cell< std::vector<int> >(4), v4 );

// Non-convertable characters
TS_ASSERT_THROWS( col.read(4,"1,2,a,3"), std::invalid_argument);
}

};

Expand Down

0 comments on commit de36ec3

Please sign in to comment.