Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support simple arithmetic expressions in computed fields #88

Merged
merged 8 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 50 additions & 0 deletions cpp/test/computed_fields_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,54 @@ TEST(ComputedFieldsTest, SwitchExpression) {
ASSERT_EQ(gr.TypeIndex(), 1);
}

TEST(ComputedFieldsTest, Arithmetic) {
RecordWithComputedFields r;
ASSERT_EQ(r.Arithmetic1(), 3);
static_assert(std::is_same_v<decltype(r.Arithmetic1()), int32_t>);
ASSERT_EQ(r.Arithmetic2(), 11);
ASSERT_EQ(r.Arithmetic3(), 13);

r.array_field = {{1, 2, 3}, {4, 5, 6}};
r.int_field = 1;
ASSERT_EQ(r.Arithmetic4(), 5);
ASSERT_EQ(r.Arithmetic5(), 3);

ASSERT_EQ(r.Arithmetic6(), 3);

ASSERT_EQ(r.Arithmetic7(), 49.0);
static_assert(std::is_same_v<decltype(r.Arithmetic7()), double>);

r.complexfloat32_field = {2.0f, 3.0f};
ASSERT_EQ(r.Arithmetic8(), std::complex<float>(6.0f, 9.0f));

ASSERT_EQ(r.Arithmetic9(), 2.2);
static_assert(std::is_same_v<decltype(r.Arithmetic9()), double>);

ASSERT_EQ(r.Arithmetic10(), 1e10 + 9e9);

ASSERT_EQ(r.Arithmetic11(), -(5.3));
}

TEST(ComputedFieldsTest, Casting) {
RecordWithComputedFields r;
r.int_field = 42;
ASSERT_EQ(r.CastIntToFloat(), 42.0f);
static_assert(std::is_same_v<decltype(r.CastIntToFloat()), float>);

r.float32_field = 42.9f;
ASSERT_EQ(r.CastFloatToInt(), 42);
static_assert(std::is_same_v<decltype(r.CastFloatToInt()), int32_t>);

ASSERT_EQ(r.CastPower(), 49);
static_assert(std::is_same_v<decltype(r.CastPower()), int32_t>);

r.complexfloat32_field = {2.0f, 3.0f};
r.complexfloat64_field = {2.0, 3.0};
ASSERT_EQ(r.CastComplex32ToComplex64(), std::complex<double>(2.0, 3.0));
ASSERT_EQ(r.CastComplex64ToComplex32(), std::complex<float>(2.0f, 3.0f));

ASSERT_EQ(r.CastFloatToComplex(), std::complex<float>(66.6f, 0.0f));
static_assert(std::is_same_v<decltype(r.CastFloatToComplex()), std::complex<float>>);
}

} // namespace
40 changes: 38 additions & 2 deletions cpp/test/generated/binary/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,18 @@ struct IsTriviallySerializable<test_model::RecordWithComputedFields> {
IsTriviallySerializable<decltype(__T__::dynamic_array_field)>::value &&
IsTriviallySerializable<decltype(__T__::fixed_array_field)>::value &&
IsTriviallySerializable<decltype(__T__::int_field)>::value &&
IsTriviallySerializable<decltype(__T__::int8_field)>::value &&
IsTriviallySerializable<decltype(__T__::uint8_field)>::value &&
IsTriviallySerializable<decltype(__T__::int16_field)>::value &&
IsTriviallySerializable<decltype(__T__::uint16_field)>::value &&
IsTriviallySerializable<decltype(__T__::uint32_field)>::value &&
IsTriviallySerializable<decltype(__T__::int64_field)>::value &&
IsTriviallySerializable<decltype(__T__::uint64_field)>::value &&
IsTriviallySerializable<decltype(__T__::size_field)>::value &&
IsTriviallySerializable<decltype(__T__::float32_field)>::value &&
IsTriviallySerializable<decltype(__T__::float64_field)>::value &&
IsTriviallySerializable<decltype(__T__::complexfloat32_field)>::value &&
IsTriviallySerializable<decltype(__T__::complexfloat64_field)>::value &&
IsTriviallySerializable<decltype(__T__::string_field)>::value &&
IsTriviallySerializable<decltype(__T__::tuple_field)>::value &&
IsTriviallySerializable<decltype(__T__::vector_field)>::value &&
Expand All @@ -389,8 +401,8 @@ struct IsTriviallySerializable<test_model::RecordWithComputedFields> {
IsTriviallySerializable<decltype(__T__::nullable_int_float_union)>::value &&
IsTriviallySerializable<decltype(__T__::union_with_nested_generic_union)>::value &&
IsTriviallySerializable<decltype(__T__::map_field)>::value &&
(sizeof(__T__) == (sizeof(__T__::array_field) + sizeof(__T__::array_field_map_dimensions) + sizeof(__T__::dynamic_array_field) + sizeof(__T__::fixed_array_field) + sizeof(__T__::int_field) + sizeof(__T__::string_field) + sizeof(__T__::tuple_field) + sizeof(__T__::vector_field) + sizeof(__T__::vector_of_vectors_field) + sizeof(__T__::fixed_vector_field) + sizeof(__T__::optional_named_array) + sizeof(__T__::int_float_union) + sizeof(__T__::nullable_int_float_union) + sizeof(__T__::union_with_nested_generic_union) + sizeof(__T__::map_field))) &&
offsetof(__T__, array_field) < offsetof(__T__, array_field_map_dimensions) && offsetof(__T__, array_field_map_dimensions) < offsetof(__T__, dynamic_array_field) && offsetof(__T__, dynamic_array_field) < offsetof(__T__, fixed_array_field) && offsetof(__T__, fixed_array_field) < offsetof(__T__, int_field) && offsetof(__T__, int_field) < offsetof(__T__, string_field) && offsetof(__T__, string_field) < offsetof(__T__, tuple_field) && offsetof(__T__, tuple_field) < offsetof(__T__, vector_field) && offsetof(__T__, vector_field) < offsetof(__T__, vector_of_vectors_field) && offsetof(__T__, vector_of_vectors_field) < offsetof(__T__, fixed_vector_field) && offsetof(__T__, fixed_vector_field) < offsetof(__T__, optional_named_array) && offsetof(__T__, optional_named_array) < offsetof(__T__, int_float_union) && offsetof(__T__, int_float_union) < offsetof(__T__, nullable_int_float_union) && offsetof(__T__, nullable_int_float_union) < offsetof(__T__, union_with_nested_generic_union) && offsetof(__T__, union_with_nested_generic_union) < offsetof(__T__, map_field);
(sizeof(__T__) == (sizeof(__T__::array_field) + sizeof(__T__::array_field_map_dimensions) + sizeof(__T__::dynamic_array_field) + sizeof(__T__::fixed_array_field) + sizeof(__T__::int_field) + sizeof(__T__::int8_field) + sizeof(__T__::uint8_field) + sizeof(__T__::int16_field) + sizeof(__T__::uint16_field) + sizeof(__T__::uint32_field) + sizeof(__T__::int64_field) + sizeof(__T__::uint64_field) + sizeof(__T__::size_field) + sizeof(__T__::float32_field) + sizeof(__T__::float64_field) + sizeof(__T__::complexfloat32_field) + sizeof(__T__::complexfloat64_field) + sizeof(__T__::string_field) + sizeof(__T__::tuple_field) + sizeof(__T__::vector_field) + sizeof(__T__::vector_of_vectors_field) + sizeof(__T__::fixed_vector_field) + sizeof(__T__::optional_named_array) + sizeof(__T__::int_float_union) + sizeof(__T__::nullable_int_float_union) + sizeof(__T__::union_with_nested_generic_union) + sizeof(__T__::map_field))) &&
offsetof(__T__, array_field) < offsetof(__T__, array_field_map_dimensions) && offsetof(__T__, array_field_map_dimensions) < offsetof(__T__, dynamic_array_field) && offsetof(__T__, dynamic_array_field) < offsetof(__T__, fixed_array_field) && offsetof(__T__, fixed_array_field) < offsetof(__T__, int_field) && offsetof(__T__, int_field) < offsetof(__T__, int8_field) && offsetof(__T__, int8_field) < offsetof(__T__, uint8_field) && offsetof(__T__, uint8_field) < offsetof(__T__, int16_field) && offsetof(__T__, int16_field) < offsetof(__T__, uint16_field) && offsetof(__T__, uint16_field) < offsetof(__T__, uint32_field) && offsetof(__T__, uint32_field) < offsetof(__T__, int64_field) && offsetof(__T__, int64_field) < offsetof(__T__, uint64_field) && offsetof(__T__, uint64_field) < offsetof(__T__, size_field) && offsetof(__T__, size_field) < offsetof(__T__, float32_field) && offsetof(__T__, float32_field) < offsetof(__T__, float64_field) && offsetof(__T__, float64_field) < offsetof(__T__, complexfloat32_field) && offsetof(__T__, complexfloat32_field) < offsetof(__T__, complexfloat64_field) && offsetof(__T__, complexfloat64_field) < offsetof(__T__, string_field) && offsetof(__T__, string_field) < offsetof(__T__, tuple_field) && offsetof(__T__, tuple_field) < offsetof(__T__, vector_field) && offsetof(__T__, vector_field) < offsetof(__T__, vector_of_vectors_field) && offsetof(__T__, vector_of_vectors_field) < offsetof(__T__, fixed_vector_field) && offsetof(__T__, fixed_vector_field) < offsetof(__T__, optional_named_array) && offsetof(__T__, optional_named_array) < offsetof(__T__, int_float_union) && offsetof(__T__, int_float_union) < offsetof(__T__, nullable_int_float_union) && offsetof(__T__, nullable_int_float_union) < offsetof(__T__, union_with_nested_generic_union) && offsetof(__T__, union_with_nested_generic_union) < offsetof(__T__, map_field);
};

template <>
Expand Down Expand Up @@ -1515,6 +1527,18 @@ template<typename T0, yardl::binary::Reader<T0> ReadT0, typename T1, yardl::bina
yardl::binary::WriteDynamicNDArray<int32_t, yardl::binary::WriteInteger>(stream, value.dynamic_array_field);
yardl::binary::WriteFixedNDArray<int32_t, yardl::binary::WriteInteger, 3, 4>(stream, value.fixed_array_field);
yardl::binary::WriteInteger(stream, value.int_field);
yardl::binary::WriteInteger(stream, value.int8_field);
yardl::binary::WriteInteger(stream, value.uint8_field);
yardl::binary::WriteInteger(stream, value.int16_field);
yardl::binary::WriteInteger(stream, value.uint16_field);
yardl::binary::WriteInteger(stream, value.uint32_field);
yardl::binary::WriteInteger(stream, value.int64_field);
yardl::binary::WriteInteger(stream, value.uint64_field);
yardl::binary::WriteInteger(stream, value.size_field);
yardl::binary::WriteFloatingPoint(stream, value.float32_field);
yardl::binary::WriteFloatingPoint(stream, value.float64_field);
yardl::binary::WriteFloatingPoint(stream, value.complexfloat32_field);
yardl::binary::WriteFloatingPoint(stream, value.complexfloat64_field);
yardl::binary::WriteString(stream, value.string_field);
test_model::binary::WriteMyTuple<int32_t, yardl::binary::WriteInteger, int32_t, yardl::binary::WriteInteger>(stream, value.tuple_field);
yardl::binary::WriteVector<int32_t, yardl::binary::WriteInteger>(stream, value.vector_field);
Expand All @@ -1538,6 +1562,18 @@ template<typename T0, yardl::binary::Reader<T0> ReadT0, typename T1, yardl::bina
yardl::binary::ReadDynamicNDArray<int32_t, yardl::binary::ReadInteger>(stream, value.dynamic_array_field);
yardl::binary::ReadFixedNDArray<int32_t, yardl::binary::ReadInteger, 3, 4>(stream, value.fixed_array_field);
yardl::binary::ReadInteger(stream, value.int_field);
yardl::binary::ReadInteger(stream, value.int8_field);
yardl::binary::ReadInteger(stream, value.uint8_field);
yardl::binary::ReadInteger(stream, value.int16_field);
yardl::binary::ReadInteger(stream, value.uint16_field);
yardl::binary::ReadInteger(stream, value.uint32_field);
yardl::binary::ReadInteger(stream, value.int64_field);
yardl::binary::ReadInteger(stream, value.uint64_field);
yardl::binary::ReadInteger(stream, value.size_field);
yardl::binary::ReadFloatingPoint(stream, value.float32_field);
yardl::binary::ReadFloatingPoint(stream, value.float64_field);
yardl::binary::ReadFloatingPoint(stream, value.complexfloat32_field);
yardl::binary::ReadFloatingPoint(stream, value.complexfloat64_field);
yardl::binary::ReadString(stream, value.string_field);
test_model::binary::ReadMyTuple<int32_t, yardl::binary::ReadInteger, int32_t, yardl::binary::ReadInteger>(stream, value.tuple_field);
yardl::binary::ReadVector<int32_t, yardl::binary::ReadInteger>(stream, value.vector_field);
Expand Down
48 changes: 48 additions & 0 deletions cpp/test/generated/hdf5/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,18 @@ struct _Inner_RecordWithComputedFields {
dynamic_array_field(o.dynamic_array_field),
fixed_array_field(o.fixed_array_field),
int_field(o.int_field),
int8_field(o.int8_field),
uint8_field(o.uint8_field),
int16_field(o.int16_field),
uint16_field(o.uint16_field),
uint32_field(o.uint32_field),
int64_field(o.int64_field),
uint64_field(o.uint64_field),
size_field(o.size_field),
float32_field(o.float32_field),
float64_field(o.float64_field),
complexfloat32_field(o.complexfloat32_field),
complexfloat64_field(o.complexfloat64_field),
string_field(o.string_field),
tuple_field(o.tuple_field),
vector_field(o.vector_field),
Expand All @@ -710,6 +722,18 @@ struct _Inner_RecordWithComputedFields {
yardl::hdf5::ToOuter(dynamic_array_field, o.dynamic_array_field);
yardl::hdf5::ToOuter(fixed_array_field, o.fixed_array_field);
yardl::hdf5::ToOuter(int_field, o.int_field);
yardl::hdf5::ToOuter(int8_field, o.int8_field);
yardl::hdf5::ToOuter(uint8_field, o.uint8_field);
yardl::hdf5::ToOuter(int16_field, o.int16_field);
yardl::hdf5::ToOuter(uint16_field, o.uint16_field);
yardl::hdf5::ToOuter(uint32_field, o.uint32_field);
yardl::hdf5::ToOuter(int64_field, o.int64_field);
yardl::hdf5::ToOuter(uint64_field, o.uint64_field);
yardl::hdf5::ToOuter(size_field, o.size_field);
yardl::hdf5::ToOuter(float32_field, o.float32_field);
yardl::hdf5::ToOuter(float64_field, o.float64_field);
yardl::hdf5::ToOuter(complexfloat32_field, o.complexfloat32_field);
yardl::hdf5::ToOuter(complexfloat64_field, o.complexfloat64_field);
yardl::hdf5::ToOuter(string_field, o.string_field);
yardl::hdf5::ToOuter(tuple_field, o.tuple_field);
yardl::hdf5::ToOuter(vector_field, o.vector_field);
Expand All @@ -727,6 +751,18 @@ struct _Inner_RecordWithComputedFields {
yardl::hdf5::InnerDynamicNdArray<int32_t, int32_t> dynamic_array_field;
yardl::FixedNDArray<int32_t, 3, 4> fixed_array_field;
int32_t int_field;
int8_t int8_field;
uint8_t uint8_field;
int16_t int16_field;
uint16_t uint16_field;
uint32_t uint32_field;
int64_t int64_field;
uint64_t uint64_field;
yardl::Size size_field;
float float32_field;
double float64_field;
std::complex<float> complexfloat32_field;
std::complex<double> complexfloat64_field;
yardl::hdf5::InnerVlenString string_field;
test_model::MyTuple<int32_t, int32_t> tuple_field;
yardl::hdf5::InnerVlen<int32_t, int32_t> vector_field;
Expand Down Expand Up @@ -1059,6 +1095,18 @@ template <typename _T0_Inner, typename T0, typename _T1_Inner, typename T1>
t.insertMember("dynamicArrayField", HOFFSET(RecordType, dynamic_array_field), yardl::hdf5::DynamicNDArrayDdl<int32_t, int32_t>(H5::PredType::NATIVE_INT32));
t.insertMember("fixedArrayField", HOFFSET(RecordType, fixed_array_field), yardl::hdf5::FixedNDArrayDdl(H5::PredType::NATIVE_INT32, {3, 4}));
t.insertMember("intField", HOFFSET(RecordType, int_field), H5::PredType::NATIVE_INT32);
t.insertMember("int8Field", HOFFSET(RecordType, int8_field), H5::PredType::NATIVE_INT8);
t.insertMember("uint8Field", HOFFSET(RecordType, uint8_field), H5::PredType::NATIVE_UINT8);
t.insertMember("int16Field", HOFFSET(RecordType, int16_field), H5::PredType::NATIVE_INT16);
t.insertMember("uint16Field", HOFFSET(RecordType, uint16_field), H5::PredType::NATIVE_UINT16);
t.insertMember("uint32Field", HOFFSET(RecordType, uint32_field), H5::PredType::NATIVE_UINT32);
t.insertMember("int64Field", HOFFSET(RecordType, int64_field), H5::PredType::NATIVE_INT64);
t.insertMember("uint64Field", HOFFSET(RecordType, uint64_field), H5::PredType::NATIVE_UINT64);
t.insertMember("sizeField", HOFFSET(RecordType, size_field), yardl::hdf5::SizeTypeDdl());
t.insertMember("float32Field", HOFFSET(RecordType, float32_field), H5::PredType::NATIVE_FLOAT);
t.insertMember("float64Field", HOFFSET(RecordType, float64_field), H5::PredType::NATIVE_DOUBLE);
t.insertMember("complexfloat32Field", HOFFSET(RecordType, complexfloat32_field), yardl::hdf5::ComplexTypeDdl<float>());
t.insertMember("complexfloat64Field", HOFFSET(RecordType, complexfloat64_field), yardl::hdf5::ComplexTypeDdl<double>());
t.insertMember("stringField", HOFFSET(RecordType, string_field), yardl::hdf5::InnerVlenStringDdl());
t.insertMember("tupleField", HOFFSET(RecordType, tuple_field), test_model::hdf5::GetMyTupleHdf5Ddl<int32_t, int32_t, int32_t, int32_t>(H5::PredType::NATIVE_INT32, H5::PredType::NATIVE_INT32));
t.insertMember("vectorField", HOFFSET(RecordType, vector_field), yardl::hdf5::InnerVlenDdl(H5::PredType::NATIVE_INT32));
Expand Down
Loading
Loading