Skip to content

Commit

Permalink
Fix #148 (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
naegelejd authored May 28, 2024
1 parent 3e49f78 commit ed63c58
Show file tree
Hide file tree
Showing 25 changed files with 1,087 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cpp/test/generated/binary/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3570,6 +3570,34 @@ void MultiDArraysReader::CloseImpl() {
stream_.VerifyFinished();
}

void ComplexArraysWriter::WriteFloatsImpl(yardl::DynamicNDArray<std::complex<float>> const& value) {
yardl::binary::WriteDynamicNDArray<std::complex<float>, yardl::binary::WriteFloatingPoint>(stream_, value);
}

void ComplexArraysWriter::WriteDoublesImpl(yardl::NDArray<std::complex<double>, 2> const& value) {
yardl::binary::WriteNDArray<std::complex<double>, yardl::binary::WriteFloatingPoint, 2>(stream_, value);
}

void ComplexArraysWriter::Flush() {
stream_.Flush();
}

void ComplexArraysWriter::CloseImpl() {
stream_.Flush();
}

void ComplexArraysReader::ReadFloatsImpl(yardl::DynamicNDArray<std::complex<float>>& value) {
yardl::binary::ReadDynamicNDArray<std::complex<float>, yardl::binary::ReadFloatingPoint>(stream_, value);
}

void ComplexArraysReader::ReadDoublesImpl(yardl::NDArray<std::complex<double>, 2>& value) {
yardl::binary::ReadNDArray<std::complex<double>, yardl::binary::ReadFloatingPoint, 2>(stream_, value);
}

void ComplexArraysReader::CloseImpl() {
stream_.VerifyFinished();
}

void MapsWriter::WriteStringToIntImpl(std::unordered_map<std::string, int32_t> const& value) {
yardl::binary::WriteMap<std::string, int32_t, yardl::binary::WriteString, yardl::binary::WriteInteger>(stream_, value);
}
Expand Down
38 changes: 38 additions & 0 deletions cpp/test/generated/binary/protocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,44 @@ class MultiDArraysReader : public test_model::MultiDArraysReaderBase, yardl::bin
size_t current_block_remaining_ = 0;
};

// Binary writer for the ComplexArrays protocol.
class ComplexArraysWriter : public test_model::ComplexArraysWriterBase, yardl::binary::BinaryWriter {
public:
ComplexArraysWriter(std::ostream& stream, Version version = Version::Current)
: yardl::binary::BinaryWriter(stream, test_model::ComplexArraysWriterBase::SchemaFromVersion(version)), version_(version) {}

ComplexArraysWriter(std::string file_name, Version version = Version::Current)
: yardl::binary::BinaryWriter(file_name, test_model::ComplexArraysWriterBase::SchemaFromVersion(version)), version_(version) {}

void Flush() override;

protected:
void WriteFloatsImpl(yardl::DynamicNDArray<std::complex<float>> const& value) override;
void WriteDoublesImpl(yardl::NDArray<std::complex<double>, 2> const& value) override;
void CloseImpl() override;

Version version_;
};

// Binary reader for the ComplexArrays protocol.
class ComplexArraysReader : public test_model::ComplexArraysReaderBase, yardl::binary::BinaryReader {
public:
ComplexArraysReader(std::istream& stream)
: yardl::binary::BinaryReader(stream), version_(test_model::ComplexArraysReaderBase::VersionFromSchema(schema_read_)) {}

ComplexArraysReader(std::string file_name)
: yardl::binary::BinaryReader(file_name), version_(test_model::ComplexArraysReaderBase::VersionFromSchema(schema_read_)) {}

Version GetVersion() { return version_; }

protected:
void ReadFloatsImpl(yardl::DynamicNDArray<std::complex<float>>& value) override;
void ReadDoublesImpl(yardl::NDArray<std::complex<double>, 2>& value) override;
void CloseImpl() override;

Version version_;
};

// Binary writer for the Maps protocol.
class MapsWriter : public test_model::MapsWriterBase, yardl::binary::BinaryWriter {
public:
Expand Down
28 changes: 28 additions & 0 deletions cpp/test/generated/factories.cc
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,34 @@ std::unique_ptr<test_model::MultiDArraysReaderBase> CreateReader<test_model::Mul
}
}

template<>
std::unique_ptr<test_model::ComplexArraysWriterBase> CreateWriter<test_model::ComplexArraysWriterBase>(Format format, std::string const& filename) {
switch (format) {
case Format::kHdf5:
return std::make_unique<test_model::hdf5::ComplexArraysWriter>(filename);
case Format::kBinary:
return std::make_unique<test_model::binary::ComplexArraysWriter>(filename);
case Format::kNDJson:
return std::make_unique<test_model::ndjson::ComplexArraysWriter>(filename);
default:
throw std::runtime_error("Unknown format");
}
}

template<>
std::unique_ptr<test_model::ComplexArraysReaderBase> CreateReader<test_model::ComplexArraysReaderBase>(Format format, std::string const& filename) {
switch (format) {
case Format::kHdf5:
return std::make_unique<test_model::hdf5::ComplexArraysReader>(filename);
case Format::kBinary:
return std::make_unique<test_model::binary::ComplexArraysReader>(filename);
case Format::kNDJson:
return std::make_unique<test_model::ndjson::ComplexArraysReader>(filename);
default:
throw std::runtime_error("Unknown format");
}
}

template<>
std::unique_ptr<test_model::MapsWriterBase> CreateWriter<test_model::MapsWriterBase>(Format format, std::string const& filename) {
switch (format) {
Expand Down
24 changes: 24 additions & 0 deletions cpp/test/generated/hdf5/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2699,6 +2699,30 @@ bool MultiDArraysReader::ReadFramesImpl(std::vector<yardl::FixedNDArray<float, 1
return has_more;
}

ComplexArraysWriter::ComplexArraysWriter(std::string path)
: yardl::hdf5::Hdf5Writer::Hdf5Writer(path, "ComplexArrays", schema_) {
}

void ComplexArraysWriter::WriteFloatsImpl(yardl::DynamicNDArray<std::complex<float>> const& value) {
yardl::hdf5::WriteScalarDataset<yardl::hdf5::InnerDynamicNdArray<std::complex<float>, std::complex<float>>, yardl::DynamicNDArray<std::complex<float>>>(group_, "floats", yardl::hdf5::DynamicNDArrayDdl<std::complex<float>, std::complex<float>>(yardl::hdf5::ComplexTypeDdl<float>()), value);
}

void ComplexArraysWriter::WriteDoublesImpl(yardl::NDArray<std::complex<double>, 2> const& value) {
yardl::hdf5::WriteScalarDataset<yardl::hdf5::InnerNdArray<std::complex<double>, std::complex<double>, 2>, yardl::NDArray<std::complex<double>, 2>>(group_, "doubles", yardl::hdf5::NDArrayDdl<std::complex<double>, std::complex<double>, 2>(yardl::hdf5::ComplexTypeDdl<double>()), value);
}

ComplexArraysReader::ComplexArraysReader(std::string path)
: yardl::hdf5::Hdf5Reader::Hdf5Reader(path, "ComplexArrays", schema_) {
}

void ComplexArraysReader::ReadFloatsImpl(yardl::DynamicNDArray<std::complex<float>>& value) {
yardl::hdf5::ReadScalarDataset<yardl::hdf5::InnerDynamicNdArray<std::complex<float>, std::complex<float>>, yardl::DynamicNDArray<std::complex<float>>>(group_, "floats", yardl::hdf5::DynamicNDArrayDdl<std::complex<float>, std::complex<float>>(yardl::hdf5::ComplexTypeDdl<float>()), value);
}

void ComplexArraysReader::ReadDoublesImpl(yardl::NDArray<std::complex<double>, 2>& value) {
yardl::hdf5::ReadScalarDataset<yardl::hdf5::InnerNdArray<std::complex<double>, std::complex<double>, 2>, yardl::NDArray<std::complex<double>, 2>>(group_, "doubles", yardl::hdf5::NDArrayDdl<std::complex<double>, std::complex<double>, 2>(yardl::hdf5::ComplexTypeDdl<double>()), value);
}

MapsWriter::MapsWriter(std::string path)
: yardl::hdf5::Hdf5Writer::Hdf5Writer(path, "Maps", schema_) {
}
Expand Down
25 changes: 25 additions & 0 deletions cpp/test/generated/hdf5/protocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,31 @@ class MultiDArraysReader : public test_model::MultiDArraysReaderBase, public yar
std::unique_ptr<yardl::hdf5::DatasetReader> frames_dataset_state_;
};

// HDF5 writer for the ComplexArrays protocol.
class ComplexArraysWriter : public test_model::ComplexArraysWriterBase, public yardl::hdf5::Hdf5Writer {
public:
ComplexArraysWriter(std::string path);

protected:
void WriteFloatsImpl(yardl::DynamicNDArray<std::complex<float>> const& value) override;

void WriteDoublesImpl(yardl::NDArray<std::complex<double>, 2> const& value) override;

private:
};

// HDF5 reader for the ComplexArrays protocol.
class ComplexArraysReader : public test_model::ComplexArraysReaderBase, public yardl::hdf5::Hdf5Reader {
public:
ComplexArraysReader(std::string path);

void ReadFloatsImpl(yardl::DynamicNDArray<std::complex<float>>& value) override;

void ReadDoublesImpl(yardl::NDArray<std::complex<double>, 2>& value) override;

private:
};

// HDF5 writer for the Maps protocol.
class MapsWriter : public test_model::MapsWriterBase, public yardl::hdf5::Hdf5Writer {
public:
Expand Down
89 changes: 89 additions & 0 deletions cpp/test/generated/mocks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,87 @@ class TestMultiDArraysWriterBase : public MultiDArraysWriterBase {
bool close_called_ = false;
};

class MockComplexArraysWriter : public ComplexArraysWriterBase {
public:
void WriteFloatsImpl (yardl::DynamicNDArray<std::complex<float>> const& value) override {
if (WriteFloatsImpl_expected_values_.empty()) {
throw std::runtime_error("Unexpected call to WriteFloatsImpl");
}
if (WriteFloatsImpl_expected_values_.front() != value) {
throw std::runtime_error("Unexpected argument value for call to WriteFloatsImpl");
}
WriteFloatsImpl_expected_values_.pop();
}

std::queue<yardl::DynamicNDArray<std::complex<float>>> WriteFloatsImpl_expected_values_;

void ExpectWriteFloatsImpl (yardl::DynamicNDArray<std::complex<float>> const& value) {
WriteFloatsImpl_expected_values_.push(value);
}

void WriteDoublesImpl (yardl::NDArray<std::complex<double>, 2> const& value) override {
if (WriteDoublesImpl_expected_values_.empty()) {
throw std::runtime_error("Unexpected call to WriteDoublesImpl");
}
if (WriteDoublesImpl_expected_values_.front() != value) {
throw std::runtime_error("Unexpected argument value for call to WriteDoublesImpl");
}
WriteDoublesImpl_expected_values_.pop();
}

std::queue<yardl::NDArray<std::complex<double>, 2>> WriteDoublesImpl_expected_values_;

void ExpectWriteDoublesImpl (yardl::NDArray<std::complex<double>, 2> const& value) {
WriteDoublesImpl_expected_values_.push(value);
}

void Verify() {
if (!WriteFloatsImpl_expected_values_.empty()) {
throw std::runtime_error("Expected call to WriteFloatsImpl was not received");
}
if (!WriteDoublesImpl_expected_values_.empty()) {
throw std::runtime_error("Expected call to WriteDoublesImpl was not received");
}
}
};

class TestComplexArraysWriterBase : public ComplexArraysWriterBase {
public:
TestComplexArraysWriterBase(std::unique_ptr<test_model::ComplexArraysWriterBase> writer, std::function<std::unique_ptr<ComplexArraysReaderBase>()> create_reader) : writer_(std::move(writer)), create_reader_(create_reader) {
}

~TestComplexArraysWriterBase() {
if (!close_called_ && !std::uncaught_exceptions()) {
ADD_FAILURE() << "Close() needs to be called on 'TestComplexArraysWriterBase' to verify mocks";
}
}

protected:
void WriteFloatsImpl(yardl::DynamicNDArray<std::complex<float>> const& value) override {
writer_->WriteFloats(value);
mock_writer_.ExpectWriteFloatsImpl(value);
}

void WriteDoublesImpl(yardl::NDArray<std::complex<double>, 2> const& value) override {
writer_->WriteDoubles(value);
mock_writer_.ExpectWriteDoublesImpl(value);
}

void CloseImpl() override {
close_called_ = true;
writer_->Close();
std::unique_ptr<ComplexArraysReaderBase> reader = create_reader_();
reader->CopyTo(mock_writer_);
mock_writer_.Verify();
}

private:
std::unique_ptr<test_model::ComplexArraysWriterBase> writer_;
std::function<std::unique_ptr<test_model::ComplexArraysReaderBase>()> create_reader_;
MockComplexArraysWriter mock_writer_;
bool close_called_ = false;
};

class MockMapsWriter : public MapsWriterBase {
public:
void WriteStringToIntImpl (std::unordered_map<std::string, int32_t> const& value) override {
Expand Down Expand Up @@ -4411,6 +4492,14 @@ std::unique_ptr<test_model::MultiDArraysWriterBase> CreateValidatingWriter<test_
);
}

template<>
std::unique_ptr<test_model::ComplexArraysWriterBase> CreateValidatingWriter<test_model::ComplexArraysWriterBase>(Format format, std::string const& filename) {
return std::make_unique<test_model::TestComplexArraysWriterBase>(
CreateWriter<test_model::ComplexArraysWriterBase>(format, filename),
[format, filename](){ return CreateReader<test_model::ComplexArraysReaderBase>(format, filename);}
);
}

template<>
std::unique_ptr<test_model::MapsWriterBase> CreateValidatingWriter<test_model::MapsWriterBase>(Format format, std::string const& filename) {
return std::make_unique<test_model::TestMapsWriterBase>(
Expand Down
22 changes: 22 additions & 0 deletions cpp/test/generated/model.json
Original file line number Diff line number Diff line change
Expand Up @@ -4801,6 +4801,28 @@
}
]
},
{
"name": "ComplexArrays",
"sequence": [
{
"name": "floats",
"type": {
"array": {
"items": "complexfloat32"
}
}
},
{
"name": "doubles",
"type": {
"array": {
"items": "complexfloat64",
"dimensions": 2
}
}
}
]
},
{
"name": "Maps",
"sequence": [
Expand Down
28 changes: 28 additions & 0 deletions cpp/test/generated/ndjson/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3291,6 +3291,34 @@ void MultiDArraysReader::CloseImpl() {
VerifyFinished();
}

void ComplexArraysWriter::WriteFloatsImpl(yardl::DynamicNDArray<std::complex<float>> const& value) {
ordered_json json_value = value;
yardl::ndjson::WriteProtocolValue(stream_, "floats", json_value);}

void ComplexArraysWriter::WriteDoublesImpl(yardl::NDArray<std::complex<double>, 2> const& value) {
ordered_json json_value = value;
yardl::ndjson::WriteProtocolValue(stream_, "doubles", json_value);}

void ComplexArraysWriter::Flush() {
stream_.flush();
}

void ComplexArraysWriter::CloseImpl() {
stream_.flush();
}

void ComplexArraysReader::ReadFloatsImpl(yardl::DynamicNDArray<std::complex<float>>& value) {
yardl::ndjson::ReadProtocolValue(stream_, line_, "floats", true, unused_step_, value);
}

void ComplexArraysReader::ReadDoublesImpl(yardl::NDArray<std::complex<double>, 2>& value) {
yardl::ndjson::ReadProtocolValue(stream_, line_, "doubles", true, unused_step_, value);
}

void ComplexArraysReader::CloseImpl() {
VerifyFinished();
}

void MapsWriter::WriteStringToIntImpl(std::unordered_map<std::string, int32_t> const& value) {
ordered_json json_value = value;
yardl::ndjson::WriteProtocolValue(stream_, "stringToInt", json_value);}
Expand Down
36 changes: 36 additions & 0 deletions cpp/test/generated/ndjson/protocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,42 @@ class MultiDArraysReader : public test_model::MultiDArraysReaderBase, yardl::ndj
void CloseImpl() override;
};

// NDJSON writer for the ComplexArrays protocol.
class ComplexArraysWriter : public test_model::ComplexArraysWriterBase, yardl::ndjson::NDJsonWriter {
public:
ComplexArraysWriter(std::ostream& stream)
: yardl::ndjson::NDJsonWriter(stream, schema_) {
}

ComplexArraysWriter(std::string file_name)
: yardl::ndjson::NDJsonWriter(file_name, schema_) {
}

void Flush() override;

protected:
void WriteFloatsImpl(yardl::DynamicNDArray<std::complex<float>> const& value) override;
void WriteDoublesImpl(yardl::NDArray<std::complex<double>, 2> const& value) override;
void CloseImpl() override;
};

// NDJSON reader for the ComplexArrays protocol.
class ComplexArraysReader : public test_model::ComplexArraysReaderBase, yardl::ndjson::NDJsonReader {
public:
ComplexArraysReader(std::istream& stream)
: yardl::ndjson::NDJsonReader(stream, schema_) {
}

ComplexArraysReader(std::string file_name)
: yardl::ndjson::NDJsonReader(file_name, schema_) {
}

protected:
void ReadFloatsImpl(yardl::DynamicNDArray<std::complex<float>>& value) override;
void ReadDoublesImpl(yardl::NDArray<std::complex<double>, 2>& value) override;
void CloseImpl() override;
};

// NDJSON writer for the Maps protocol.
class MapsWriter : public test_model::MapsWriterBase, yardl::ndjson::NDJsonWriter {
public:
Expand Down
Loading

0 comments on commit ed63c58

Please sign in to comment.