Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

Fix operator+ for string and float #579

Merged
merged 2 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 4 additions & 6 deletions src/common/datatypes/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1966,9 +1966,8 @@ Value operator+(const Value& lhs, const Value& rhs) {
return lhs.getFloat() + rhs.getFloat();
}
case Value::Type::STRING: {
return folly::stringPrintf("%lf%s",
lhs.getFloat(),
rhs.getStr().c_str());
Aiee marked this conversation as resolved.
Show resolved Hide resolved
return folly::stringPrintf(
"%s", (folly::to<std::string>(lhs.getFloat()) + rhs.getStr()).c_str());
}
case Value::Type::LIST: {
auto ret = rhs.getList();
Expand All @@ -1993,9 +1992,8 @@ Value operator+(const Value& lhs, const Value& rhs) {
rhs.getInt());
}
case Value::Type::FLOAT: {
return folly::stringPrintf("%s%lf",
lhs.getStr().c_str(),
rhs.getFloat());
return folly::stringPrintf(
"%s", (lhs.getStr() + folly::to<std::string>(rhs.getFloat())).c_str());
}
case Value::Type::STRING: {
return lhs.getStr() + rhs.getStr();
Expand Down
18 changes: 16 additions & 2 deletions src/common/datatypes/test/ValueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ TEST(Value, Arithmetics) {

v = vFloat1 + vStr2;
EXPECT_EQ(Value::Type::STRING, v.type());
EXPECT_EQ(std::string("3.140000World"), v.getStr());
EXPECT_EQ(std::string("3.14World"), v.getStr());

v = vFloat2 + vInt2;
EXPECT_EQ(Value::Type::FLOAT, v.type());
Expand All @@ -92,7 +92,7 @@ TEST(Value, Arithmetics) {

v = vStr1 + vFloat2;
EXPECT_EQ(Value::Type::STRING, v.type());
EXPECT_EQ(std::string("Hello 2.670000"), v.getStr());
EXPECT_EQ(std::string("Hello 2.67"), v.getStr());

v = vStr1 + vBool2;
EXPECT_EQ(Value::Type::STRING, v.type());
Expand Down Expand Up @@ -121,6 +121,20 @@ TEST(Value, Arithmetics) {
v = vList2 + vSet;
EXPECT_EQ(Value::Type::LIST, v.type());
EXPECT_EQ(List({6, 4, 5, Set({8, 7})}), v.getList());

// str + float
v = Value("Allen Wilson") + Value(30.142857142857142);
EXPECT_EQ(Value::Type::STRING, v.type());
EXPECT_EQ(std::string("Allen Wilson30.142857142857142"), v.getStr());
v = Value("Allen Wilson") + Value(-30.142857142857142);
EXPECT_EQ(Value::Type::STRING, v.type());
EXPECT_EQ(std::string("Allen Wilson-30.142857142857142"), v.getStr());
v = Value(30.142857142857142) + Value("Allen Wilson");
EXPECT_EQ(Value::Type::STRING, v.type());
EXPECT_EQ(std::string("30.142857142857142Allen Wilson"), v.getStr());
v = Value(-30.142857142857142) + Value("Allen Wilson");
EXPECT_EQ(Value::Type::STRING, v.type());
EXPECT_EQ(std::string("-30.142857142857142Allen Wilson"), v.getStr());
}
// -
{
Expand Down