diff --git a/lib/ecl/tests/eclxx_kw.cpp b/lib/ecl/tests/eclxx_kw.cpp index 7104da56f7..7c3e30215c 100644 --- a/lib/ecl/tests/eclxx_kw.cpp +++ b/lib/ecl/tests/eclxx_kw.cpp @@ -20,7 +20,7 @@ #include #include -#include +#include #include @@ -57,10 +57,10 @@ void test_kw_vector_string() { std::vector< const char* > vec = { "short", "sweet", - "padded ", - "verylongkeyword" + "padded " }; + std::vector too_long = {"1234567890"}; ERT::EclKW< const char* > kw( "XYZ", vec ); test_assert_size_t_equal( kw.size(), vec.size() ); @@ -68,8 +68,8 @@ void test_kw_vector_string() { 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("XY", too_long), std::range_error); } void test_kw_vector_std_string() { @@ -77,9 +77,8 @@ void test_kw_vector_std_string() { "short", "sweet", "padded ", - "verylongkeyword" }; - + std::vector too_long = {"1234567890"}; ERT::EclKW< std::string > kw( "XYZ", vec ); test_assert_size_t_equal( kw.size(), vec.size() ); @@ -87,8 +86,8 @@ void test_kw_vector_std_string() { 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("XY", too_long), std::range_error); } void test_logical() { diff --git a/lib/include/ert/ecl/EclKW.hpp b/lib/include/ert/ecl/EclKW.hpp index 214c6ecaa9..68f834428d 100644 --- a/lib/include/ert/ecl/EclKW.hpp +++ b/lib/include/ert/ecl/EclKW.hpp @@ -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 @@ -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() ); + } } @@ -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