Browse Source

Added tests for the 3 string convertion functions and improved the 3 functions to pass the tests.

master
DomtronVox 1 year ago
parent
commit
2285008ac4
3 changed files with 92 additions and 10 deletions
  1. +18
    -7
      src/Configuration/ConfigStore.cpp
  2. +3
    -3
      src/Configuration/ConfigStore.h
  3. +71
    -0
      tests/test_ConfigStore.cpp

+ 18
- 7
src/Configuration/ConfigStore.cpp View File

@ -5,30 +5,41 @@
#include <iostream>
#include <sstream>
//define the convertion functions
bool toDecimal(std::string value) {
//convert string to double
// @return bool indicating success
bool toDecimal(std::string value, double& float_var) {
std::istringstream stream(value);
double float_var;
stream >> std::noskipws >> float_var; // noskipws considers leading whitespace invalid
// Check the entire string was consumed and if either failbit or badbit is set
return stream.eof() && !stream.fail();
}
bool toInt(std::string value) {
//convert string to integer
// @return bool indicating success
bool toInt(std::string value, int& int_var) {
std::istringstream stream(value);
int int_var;
stream >> std::noskipws >> int_var; // noskipws considers leading whitespace invalid
// Check the entire string was consumed and if either failbit or badbit is set
return stream.eof() && !stream.fail();
}
bool toBool(std::string value) {
//convert string to boolean
// @return bool indicating success
bool toBool(std::string value, bool& bool_var) {
//force lowercase
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
if ( value == "true" || value == "false") {
if ( value == "true" || value == "1") {
bool_var = true;
return true;
} else if (value == "false" || value == "0") {
bool_var = false;
return true;
}
//if all those fail then the value isn't a boolean so successes fails
return false;
}


+ 3
- 3
src/Configuration/ConfigStore.h View File

@ -8,9 +8,9 @@
#include <unordered_map>
//functions to test string values for different basic types
bool toDecimal(std::string value);
bool toInt(std::string value);
bool toBool(std::string value);
bool toDecimal(std::string value, double& float_var);
bool toInt(std::string value, int& int_var);
bool toBool(std::string value, bool& bool_var);
class ConfigStore {


+ 71
- 0
tests/test_ConfigStore.cpp View File

@ -58,6 +58,77 @@ int main (int argc, char* argv[]) {
//TODO need to test iterators
TEST_BEGIN( "toDecimal: test decimal converter" );
double decimal1, decimal2, decimal3, decimal4, decimal5;
bool dtest1 = toDecimal("1", decimal1)
, dtest2 = toDecimal("1.5", decimal2)
, dtest3 = toDecimal("1.0", decimal3)
, dtest4 = toDecimal("test", decimal4)
, dtest5 = toDecimal("test1", decimal5);
ASSERT_THROW( dtest1 == true );
ASSERT_THROW( dtest2 == true );
ASSERT_THROW( dtest3 == true );
ASSERT_THROW( dtest4 == false );
ASSERT_THROW( dtest5 == false );
ASSERT_THROW( decimal1 == 1 );
ASSERT_THROW( decimal2 == 1.5 );
ASSERT_THROW( decimal3 == 1.0 );
ASSERT_THROW( decimal4 == 0 );
ASSERT_THROW( decimal5 == 0 );
TEST_END();
TEST_BEGIN( "toInt: test integer converter" );
int integer1, integer2, integer3, integer4, integer5;
bool itest1 = toInt("1", integer1)
, itest2 = toInt("100", integer2)
, itest3 = toInt("123.4", integer3)
, itest4 = toInt("test", integer4)
, itest5 = toInt("test1", integer5);
ASSERT_THROW( itest1 == true );
ASSERT_THROW( itest2 == true );
ASSERT_THROW( itest3 == false );
ASSERT_THROW( itest4 == false );
ASSERT_THROW( itest5 == false );
ASSERT_THROW( integer1 == 1 );
ASSERT_THROW( integer2 == 100 );
ASSERT_THROW( integer3 == 123 );
ASSERT_THROW( integer4 == 0 );
ASSERT_THROW( integer5 == 0 );
TEST_END();
TEST_BEGIN( "toBool: test boolean converter" );
bool bool1, bool2, bool3, bool4, bool5, bool6;
bool btest1 = toBool("false", bool1)
, btest2 = toBool("true", bool2)
, btest3 = toBool("FaLsE", bool3)
, btest4 = toBool("1", bool4)
, btest5 = toBool("0", bool5)
, btest6 = toBool("0test", bool6);
ASSERT_THROW( btest1 == true );
ASSERT_THROW( btest2 == true );
ASSERT_THROW( btest3 == true );
ASSERT_THROW( btest4 == true );
ASSERT_THROW( btest5 == true );
ASSERT_THROW( btest6 == false );
ASSERT_THROW( bool1 == false );
ASSERT_THROW( bool2 == true );
ASSERT_THROW( bool3 == false );
ASSERT_THROW( bool4 == true );
ASSERT_THROW( bool5 == false );
ASSERT_THROW( bool6 == false );
TEST_END();
return 0;
}

Loading…
Cancel
Save