Skip to content

Commit

Permalink
Test that strings are max 8 characters.
Browse files Browse the repository at this point in the history
  • Loading branch information
joakim-hove committed Apr 20, 2018
1 parent d69d102 commit 6a07bb0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
17 changes: 8 additions & 9 deletions lib/ecl/tests/eclxx_kw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <stdexcept>
#include <fstream>

#include <ert/util/test_util.h>
#include <ert/util/test_util.hpp>

#include <ert/ecl/ecl_file.h>

Expand Down Expand Up @@ -57,38 +57,37 @@ void test_kw_vector_string() {
std::vector< const char* > vec = {
"short",
"sweet",
"padded ",
"verylongkeyword"
"padded "
};

std::vector<const char *> too_long = {"1234567890"};
ERT::EclKW< const char* > kw( "XYZ", vec );

test_assert_size_t_equal( kw.size(), vec.size() );

test_assert_string_equal( kw.at( 0 ), "short " );
test_assert_string_equal( kw.at( 1 ), "sweet " );
test_assert_string_equal( kw.at( 2 ), vec.at( 2 ) );
test_assert_string_equal( kw.at( 3 ), "verylong" );
test_assert_string_not_equal( kw.at( 2 ), "verylongkeyword" );

test_assert_throw( ERT::EclKW<const char*>("XY", too_long), std::range_error);
}

void test_kw_vector_std_string() {
std::vector< const char* > vec = {
"short",
"sweet",
"padded ",
"verylongkeyword"
};

std::vector<std::string> too_long = {"1234567890"};
ERT::EclKW< std::string > kw( "XYZ", vec );

test_assert_size_t_equal( kw.size(), vec.size() );

test_assert_string_equal( kw.at( 0 ).c_str(), "short " );
test_assert_string_equal( kw.at( 1 ).c_str(), "sweet " );
test_assert_string_equal( kw.at( 2 ).c_str(), vec.at( 2 ) );
test_assert_string_equal( kw.at( 3 ).c_str(), "verylong" );
test_assert_string_not_equal( kw.at( 2 ).c_str(), "verylongkeyword" );

test_assert_throw( ERT::EclKW<std::string>("XY", too_long), std::range_error);
}

void test_logical() {
Expand Down
15 changes: 12 additions & 3 deletions lib/include/ert/ecl/EclKW.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,11 @@ EclKW< const char* >::EclKW( const std::string& kw,
EclKW( kw, data.size() )
{
auto* ptr = this->get();
for( size_t i = 0; i < data.size(); ++i )
for( size_t i = 0; i < data.size(); ++i ) {
if (strlen(data[i]) > 8)
throw std::range_error("Strings must be maximum 8 characters long");
ecl_kw_iset_string8( ptr, i, data[ i ] );
}
}

template<> inline
Expand All @@ -215,8 +218,11 @@ EclKW< std::string >::EclKW( const std::string& kw,
EclKW( kw, data.size() )
{
auto* ptr = this->get();
for( size_t i = 0; i < data.size(); ++i )
for( size_t i = 0; i < data.size(); ++i ) {
if (data[i].size() > 8)
throw std::range_error("Strings must be maximum 8 characters long");
ecl_kw_iset_string8( ptr, i, data[ i ].c_str() );
}
}


Expand All @@ -227,8 +233,11 @@ EclKW< std::string >::EclKW( const std::string& kw,
EclKW( kw, data.size() )
{
auto* ptr = this->get();
for( size_t i = 0; i < data.size(); ++i )
for( size_t i = 0; i < data.size(); ++i) {
if (strlen(data[i]) > 8)
throw std::range_error("Strings must be maximum 8 characters long");
ecl_kw_iset_string8( ptr, i, data[ i ]);
}
}

template<> inline
Expand Down

0 comments on commit 6a07bb0

Please sign in to comment.