Fix handling on non-float values in INCRBYFLOAT. Fixes #22
This commit is contained in:
parent
2c8cb23098
commit
69c8658be4
|
@ -758,11 +758,14 @@ OpResult<double> StringFamily::OpIncrFloat(const OpArgs& op_args, std::string_vi
|
||||||
|
|
||||||
string tmp;
|
string tmp;
|
||||||
string_view slice = it->second.GetSlice(&tmp);
|
string_view slice = it->second.GetSlice(&tmp);
|
||||||
double base;
|
|
||||||
|
|
||||||
if (!absl::SimpleAtod(slice, &base)) {
|
StringToDoubleConverter stod(StringToDoubleConverter::NO_FLAGS, 0, 0, NULL, NULL);
|
||||||
|
int processed_digits = 0;
|
||||||
|
double base = stod.StringToDouble(slice.data(), slice.size(), &processed_digits);
|
||||||
|
if (unsigned(processed_digits) != slice.size()) {
|
||||||
return OpStatus::INVALID_FLOAT;
|
return OpStatus::INVALID_FLOAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
base += val;
|
base += val;
|
||||||
|
|
||||||
if (isnan(base) || isinf(base)) {
|
if (isnan(base) || isinf(base)) {
|
||||||
|
|
|
@ -307,4 +307,18 @@ TEST_F(StringFamilyTest, Range) {
|
||||||
EXPECT_EQ(Run({"getrange","num", "-5000", "10000"}), "1234");
|
EXPECT_EQ(Run({"getrange","num", "-5000", "10000"}), "1234");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(StringFamilyTest, IncrByFloat) {
|
||||||
|
Run({"SET", "nonum", " 11"});
|
||||||
|
auto resp = Run({"INCRBYFLOAT", "nonum", "1.0"});
|
||||||
|
EXPECT_THAT(resp, ErrArg("not a valid float"));
|
||||||
|
|
||||||
|
Run({"SET", "nonum", "11 "});
|
||||||
|
resp = Run({"INCRBYFLOAT", "nonum", "1.0"});
|
||||||
|
EXPECT_THAT(resp, ErrArg("not a valid float"));
|
||||||
|
|
||||||
|
Run({"SET", "num", "2.566"});
|
||||||
|
resp = Run({"INCRBYFLOAT", "num", "1.0"});
|
||||||
|
EXPECT_EQ(resp, "3.566");
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace dfly
|
} // namespace dfly
|
||||||
|
|
Loading…
Reference in New Issue