Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double conversion #1411

Merged
merged 5 commits into from
May 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# DGtal 1.0.1

## Bug Fixes

- *Helpers*

- Fixing double conversion bug in class Parameters, related to
English/French decimal point inconsistency between `atof` and
`boost::program_options` (Jacques-Olivier Lachaud,
[#1411](https://github.com/DGtal-team/DGtal/pull/1411))


# DGtal 1.0

Expand Down
24 changes: 21 additions & 3 deletions src/DGtal/helpers/Parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,39 @@ namespace DGtal
template <>
struct ValueConverter< std::string, double >{
static double cast( const std::string& value )
{ return atof( value.c_str() ); }
{
// note (JOL): cannot use atof (C) since it uses a different locale as program_options (C++).
double val;
std::istringstream iss( value );
iss >> val;
return val;
}
};

/// Specialized definitions of a class for converting type X toward type Y.
template <>
struct ValueConverter< std::string, float >{
static float cast( const std::string& value )
{ return (float) atof( value.c_str() ); }
{
// note (JOL): cannot use atof (C) since it uses a different locale as program_options (C++).
float val;
std::istringstream iss( value );
iss >> val;
return val;
}
};

/// Specialized definitions of a class for converting type X toward type Y.
template <>
struct ValueConverter< std::string, int >{
static int cast( const std::string& value )
{ return atoi( value.c_str() ); }
{
// note (JOL): cannot use atoi (C) since it uses a different locale as program_options (C++).
int val;
std::istringstream iss( value );
iss >> val;
return val;
}
};
/// Specialized definitions of a class for converting type X toward type Y.
template < typename X >
Expand Down
1 change: 1 addition & 0 deletions tests/helpers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
SET(DGTAL_TESTS_SRC_HELPERS
testParametricShape
testImplicitShape
testParameters
)

FOREACH(FILE ${DGTAL_TESTS_SRC_HELPERS})
Expand Down
74 changes: 74 additions & 0 deletions tests/helpers/testParameters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
**/

/**
* @file testParameters.cpp
* @ingroup Tests
* @author Jacques-Olivier Lachaud (\c [email protected] )
* Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
*
* @date 2019/05/14
*
* Functions for testing class CubicalComplex.
*
* This file is part of the DGtal library.
*/

///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include "DGtal/base/Common.h"
#include "DGtal/helpers/Parameters.h"

#include "DGtalCatch.h"
///////////////////////////////////////////////////////////////////////////////

using namespace std;
using namespace DGtal;


///////////////////////////////////////////////////////////////////////////////
// Functions for testing class Parameters
///////////////////////////////////////////////////////////////////////////////

SCENARIO( "Parameters decimal conversion tests", "[parameters]" )
{
GIVEN( "A Parameters object" ) {
Parameters params;
WHEN( "initialized with strings" ) {
params( "foo", "bar" )( "Laurel", "Hardy" );
THEN( "it does store strings" ) {
REQUIRE( params[ "foo" ].as<string>() == "bar" );
REQUIRE( params[ "Laurel" ].as<string>() == "Hardy" );
}
}
WHEN( "initialized with integers" ) {
params( "prime", 7 )( "negative-int", -2 );
THEN( "it does store ints" ) {
REQUIRE( params[ "prime" ].as<int>() == 7 );
REQUIRE( params[ "negative-int" ].as<int>() == -2 );
}
}
WHEN( "initialized with doubles" ) {
params( "pi", 3.14159 )( "planck", 6.62607004e-34 )( "g", 9.80665 );
THEN( "it does store ints" ) {
REQUIRE( params[ "pi" ].as<double>() == Approx( 3.14159 ) );
REQUIRE( params[ "planck" ].as<double>() == Approx( 6.62607004e-34 ) );
REQUIRE( params[ "g" ].as<double>() == Approx( 9.80665 ) );
}
}
}
}