Skip to content

Commit

Permalink
Adding a use case
Browse files Browse the repository at this point in the history
  • Loading branch information
whaeck committed Nov 9, 2023
1 parent afb2096 commit bc75099
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/tools/ranges/AnyIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,22 @@ class AnyIterator {

template < typename Iterator >
AnyIterator( const std::input_iterator_tag&, Iterator&& iter ) :
ptr_( new AnyInputIteratorModel< typename std::decay< Iterator >::type,
ptr_( new AnyInputIteratorModel< Iterator,
ValueType >( std::forward< Iterator >( iter ) ) ) {}

template < typename Iterator >
AnyIterator( const std::forward_iterator_tag&, Iterator&& iter ) :
ptr_( new AnyForwardIteratorModel< typename std::decay< Iterator >::type,
ptr_( new AnyForwardIteratorModel< Iterator,
ValueType >( std::forward< Iterator >( iter ) ) ) {}

template < typename Iterator >
AnyIterator( const std::bidirectional_iterator_tag&, Iterator&& iter ) :
ptr_( new AnyBidirectionalIteratorModel< typename std::decay< Iterator >::type,
ptr_( new AnyBidirectionalIteratorModel< Iterator,
ValueType >( std::forward< Iterator >( iter ) ) ) {}

template < typename Iterator >
AnyIterator( const std::random_access_iterator_tag&, Iterator&& iter ) :
ptr_( new AnyRandomAccessIteratorModel< typename std::decay< Iterator >::type,
ptr_( new AnyRandomAccessIteratorModel< Iterator,
ValueType >( std::forward< Iterator >( iter ) ) ) {}

public:
Expand Down
1 change: 1 addition & 0 deletions src/tools/ranges/AnyView/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_cpp_test( ranges.AnyView AnyView.test.cpp )
add_cpp_test( ranges.AnyView.usecase usecase.test.cpp )
70 changes: 70 additions & 0 deletions src/tools/ranges/AnyView/test/usecase.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// include Catch2
#include <catch2/catch_test_macros.hpp>

// what we are testing
#include "tools/ranges/AnyView.hpp"

// other includes
#include <forward_list>
#include <list>
#include <vector>
#include "tools/ranges/make_view.hpp"

// convenience typedefs
using namespace njoy::tools::ranges;

// a class with data
class Data {

std::vector< double > data_;

public:

Data( int size ) : data_( size ) {

for ( unsigned int i = 0; i < data_.size(); ++i ) {

data_[i] = i;
}
}

const std::vector< double >& data() const { return this->data_; }

auto view() const { return njoy::tools::ranges::make_view( this->data() ); }
};

SCENARIO( "use case" ) {

GIVEN( "a data object that returns a range" ) {

Data object( 1000 );

WHEN( "retrieving the view and converting it to an AnyView" ) {

AnyRandomAccessView< double > chunk = object.view();

THEN( "an AnyView can be constructed and members can be tested" ) {

CHECK( 1000 == chunk.size() );
CHECK( false == chunk.empty() );
CHECK( false == bool( chunk ) );

unsigned int counter = 0;
for ( auto value : chunk ) {

++counter;
}
CHECK( 1000 == counter );

CHECK( 0 == chunk[0] );
CHECK( 999 == chunk[999] );

CHECK( 0 == chunk.at( 0 ) );
CHECK( 999 == chunk.at( 999 ) );

CHECK( 0 == chunk.front() );
CHECK( 999 == chunk.back() );
} // THEN
} // WHEN
} // GIVEN
} // SCENARIO

0 comments on commit bc75099

Please sign in to comment.