I have a C ++ function that accepts string input. In function, I use stringstreamto extract double.
Later I check that double is not NaN or infinity. However, I cannot come up with a test input that will force the extraction operator to get any of these values.
Here is an important part of my function. Actual use in some factory classes.
double from_text(const std::string& word, bool& failed){
double ret; failed = true;
std::istringstream param_in(word);
if(!(param_in >> ret)) {
return ret; }
if(is_nan_or_infinity(ret)) {
return ret; }
failed = false; return ret;
}
You can tell if you cannot do this, why bother?
I do not want to remove if, because I know that in some implementations like IBM , stream operators can create such values.
, , , 100% - , , , , . . , from_text, , .
Edit
, , std::istringstream(some_magic_string) >> my_double inf nan , g++ Ubuntu.
, :
#include "test_stuff.hpp"
#include "from_text.hpp"
bool failed;
from_text("my_special_nan_producing_input",failed)
test_assert(failed,true,"NAN producing input causes failure");
from_text("my_special_inf_producing_input",failed)
test_assert(failed,true,"Infinity producing input causes failure");
_text , ret, - NaN inf.