Commit a43a3df6 authored by Kenton Varda's avatar Kenton Varda

Fix C++ code generation for Float32s with integral defaults.

Appending 'f' to an integer literal doesn't make it a float. We have to add '.0' first.

Fixes #119.
parent 6915e4ff
......@@ -300,7 +300,15 @@ private:
case schema::Value::UINT16: return kj::strTree(value.getUint16(), "u");
case schema::Value::UINT32: return kj::strTree(value.getUint32(), "u");
case schema::Value::UINT64: return kj::strTree(value.getUint64(), "llu");
case schema::Value::FLOAT32: return kj::strTree(value.getFloat32(), "f");
case schema::Value::FLOAT32: {
auto text = kj::str(value.getFloat32());
if (text.findFirst('.') == nullptr &&
text.findFirst('e') == nullptr &&
text.findFirst('E') == nullptr) {
text = kj::str(text, ".0");
}
return kj::strTree(kj::mv(text), "f");
}
case schema::Value::FLOAT64: return kj::strTree(value.getFloat64());
case schema::Value::ENUM: {
EnumSchema schema = schemaLoader.get(type.getEnum().getTypeId()).asEnum();
......
......@@ -1704,6 +1704,16 @@ TEST(Encoding, DefaultFloatPlusNan) {
EXPECT_TRUE(d != d);
}
TEST(Encoding, WholeFloatDefault) {
MallocMessageBuilder message;
auto root = message.initRoot<test::TestWholeFloatDefault>();
EXPECT_EQ(123.0f, root.getField());
EXPECT_EQ(123e30f, root.getBigField());
EXPECT_EQ(456.0f, test::TestWholeFloatDefault::CONSTANT);
EXPECT_EQ(456e30f, test::TestWholeFloatDefault::BIG_CONSTANT);
}
} // namespace
} // namespace _ (private)
} // namespace capnp
......@@ -494,6 +494,15 @@ struct TestPrintInlineStructs {
}
}
struct TestWholeFloatDefault {
# At one point, these failed to compile in C++ because it would produce literals like "123f",
# which is not valid; it needs to be "123.0f".
field @0 :Float32 = 123;
bigField @1 :Float32 = 123e30;
const constant :Float32 = 456;
const bigConstant :Float32 = 456e30;
}
struct TestEmptyStruct {}
struct TestConstants {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment