Skip to content

Commit

Permalink
More testing of record
Browse files Browse the repository at this point in the history
  • Loading branch information
whaeck committed Apr 9, 2024
1 parent 4f3e8a4 commit a727a8f
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/tools/disco/Character.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Character : public BaseFixedWidthField< Width > {
template < typename Iterator >
static std::string read( Iterator& iter, const Iterator& end ) {

return read< std::string >( iter, end );
return read< std::string >( iter, end );
}

template< typename Representation, typename Iterator >
Expand Down
2 changes: 1 addition & 1 deletion src/tools/disco/Real.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Real : public BaseFixedWidthField< Width > {
template < typename Iterator >
static double read( Iterator& iter, const Iterator& end ) {

return read< double >( iter, end );
return read< double >( iter, end );
}
};

Expand Down
38 changes: 22 additions & 16 deletions src/tools/disco/Record.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,49 @@ template<>
struct Record<> {

template< typename Iterator >
static void read( Iterator& iter, const Iterator& end ){
static void read( Iterator& iter, const Iterator& end ) {

while ( iter != end and not ( isNewLine( iter )
or isEndOfFile( iter ) ) ){ ++iter; }
std::advance( iter, isNewLine( iter ) and iter != end );
while ( iter != end && ! ( isNewLine( iter )|| isEndOfFile( iter ) ) ) {

++iter;
}
std::advance( iter, isNewLine( iter ) && iter != end );
}

template< typename Iterator, typename... Args >
static void write( Iterator& it, const Args&... ){
static void write( Iterator& it, const Args&... ) {

*it++='\n';
}

};

template<>
struct Record< RetainCarriage > {

template< typename Iterator >
static void read( Iterator&, const Iterator& ){}
static void read( Iterator&, const Iterator& ) {}

template< typename Iterator, typename... Args >
static void write( Iterator&, const Args&... ){}
static void write( Iterator&, const Args&... ) {}

};

template< unsigned int w, typename... Components >
struct Record< Column< w >, Components... > {

template< typename Iterator, typename... Args >
static void read( Iterator& it, const Iterator& end, Args&... args ){
static void read( Iterator& it, const Iterator& end, Args&... args ) {

Column< w >::read( it, end );
Record< Components... >::read( it, end, args... );
}

template< typename Iterator, typename... Args >
static void write( Iterator& it, const Args&... args ){
static void write( Iterator& it, const Args&... args ) {

Column< w >::write( it );
Record< Components... >::write( it, args... );
}

};

template< typename Component, typename... Components >
Expand All @@ -68,29 +71,32 @@ struct Record< Component, Components... > {
using Subrecord = Record< Components... >;

template< typename Iterator >
static void read( Iterator& it, const Iterator& end ){
static void read( Iterator& it, const Iterator& end ) {

Component::read( it, end );
Subrecord::read( it, end );
}

template< typename Iterator, typename SinkType, typename... Args >
static void read( Iterator& it, const Iterator& end, SinkType& sink, Args&... args ){
static void read( Iterator& it, const Iterator& end, SinkType& sink, Args&... args ) {

sink = Component::template read< SinkType >( it, end );
Subrecord::read( it, end, args... );
}

template< typename Iterator >
static void write( Iterator& it ){
static void write( Iterator& it ) {

Component::write( it );
Subrecord::write( it );
}

template< typename Iterator, typename SourceType, typename... Args >
static void write( Iterator& it, const SourceType& source, const Args&... args ){
static void write( Iterator& it, const SourceType& source, const Args&... args ) {

Component::write( source, it );
Subrecord::write( it, args... );
}

};

} // disco namespace
Expand Down
112 changes: 82 additions & 30 deletions src/tools/disco/Record/test/Record.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,87 @@ using namespace njoy::tools::disco;

SCENARIO( "Record" ) {

std::string source;;
auto iter = source.begin();
auto end = source.end();

double value;
source = " 2.0000E+00\n";
iter = source.begin();
end = source.end();
Record< Scientific< 11, 4 > >::read( iter, end, value );
CHECK( 2 == value );
CHECK( iter == end );

std::vector< double > vector( 2 );
source = " 1.0000E-11 2.0000E+07\n";
iter = source.begin();
end = source.end();
Record< Scientific< 11, 4 >, Scientific< 11, 4 > >::read( iter, end, vector[0], vector[1] );
CHECK( 1e-11 == vector[0] );
CHECK( 2e+7 == vector[1] );
CHECK( iter == end );

std::tuple< double, int, std::string > tuple;
source = " 1.0000E-11 2abcd\n";
iter = source.begin();
end = source.end();
Record< Scientific< 11, 4 >, Integer< 11 >, Character< 4 > >::read( iter, end, std::get<0>(tuple), std::get<1>(tuple), std::get<2>(tuple) );
CHECK( 1e-11 == std::get<0>(tuple) );
CHECK( 2 == std::get<1>(tuple) );
CHECK( "abcd" == std::get<2>(tuple) );
CHECK( iter == end );
GIVEN( "a line of data" ) {

THEN( "data can be read from it" ) {

std::string source;
auto iter = source.begin();
auto end = source.end();

double value;
source = " 2.0000E+00\n";
iter = source.begin();
end = source.end();
Record< Scientific< 11, 4 > >::read( iter, end, value );
CHECK( 2 == value );
CHECK( iter == end );

std::vector< double > vector( 2 );
source = " 1.0000E-11 2.0000E+07\n";
iter = source.begin();
end = source.end();
Record< Scientific< 11, 4 >, Scientific< 11, 4 > >::read( iter, end, vector[0], vector[1] );
CHECK( 1e-11 == vector[0] );
CHECK( 2e+7 == vector[1] );
CHECK( iter == end );

std::tuple< double, int, std::string > tuple;
source = " 1.0000E-11 2abcd\n";
iter = source.begin();
end = source.end();
Record< Scientific< 11, 4 >,
Integer< 11 >,
Character< 4 > >::read( iter, end, std::get<0>(tuple), std::get<1>(tuple), std::get<2>(tuple) );
CHECK( 1e-11 == std::get<0>(tuple) );
CHECK( 2 == std::get<1>(tuple) );
CHECK( "abcd" == std::get<2>(tuple) );
CHECK( iter == end );
}
}

GIVEN( "a line of data" ) {

WHEN( "reading only part of the data" ) {

THEN( "the entire line is consumed" ) {

std::string source;
auto iter = source.begin();
auto end = source.end();

source = " 1.0000E-11 1.0000E-10 1.0000E-09\n";
iter = source.begin();
end = source.end();
double value1;
double value2;
Record< Scientific< 11, 4 >,
Scientific< 11, 4 >,
Scientific< 11, 4 > >::read( iter, end, value1, value2 );
CHECK( 1e-11 == value1 );
CHECK( 1e-10 == value2 );
CHECK( iter == end );
}
}
}

GIVEN( "data" ) {

WHEN( "writing the data" ) {

THEN( "the line is written as expected" ) {

std::string buffer;
auto iter = std::back_inserter( buffer );

double f = 2;
int i = 4;
std::string s = "abc";
Record< Scientific< 11, 4 >,
Integer< 4 >,
Character< 5 > >::write( iter, f, i, s );
CHECK( " 2.0000E+00 4 abc\n" == buffer );
}
}
}
} // SCENARIO

0 comments on commit a727a8f

Please sign in to comment.