[Yunq] Add basic test for repeated fields.

This commit is contained in:
Drew Galbraith 2024-01-17 15:15:55 -08:00
parent 65e9fa1767
commit 27c39d05e8
5 changed files with 80 additions and 1 deletions

View File

@ -13,3 +13,7 @@ message Types {
message Cap {
capability cap;
}
message Repeated {
repeated u64 unsigned_ints;
}

View File

@ -136,6 +136,43 @@ uint64_t Cap::SerializeInternal(yunq::Serializer& serializer) const {
return serializer.size();
}
glcr::Status Repeated::ParseFromBytes(const yunq::MessageView& message) {
RETURN_ERROR(ParseFromBytesInternal(message));
return glcr::Status::Ok();
}
glcr::Status Repeated::ParseFromBytes(const yunq::MessageView& message, const glcr::CapBuffer& caps) {
RETURN_ERROR(ParseFromBytesInternal(message));
return glcr::Status::Ok();
}
glcr::Status Repeated::ParseFromBytesInternal(const yunq::MessageView& message) {
RETURN_ERROR(message.CheckHeader());
// Parse unsigned_ints.
ASSIGN_OR_RETURN(unsigned_ints_, message.ReadRepeated<uint64_t>(0));
return glcr::Status::Ok();
}
uint64_t Repeated::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
yunq::Serializer serializer(bytes, offset, 1);
return SerializeInternal(serializer);
}
uint64_t Repeated::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
yunq::Serializer serializer(bytes, offset, 1, caps);
return SerializeInternal(serializer);
}
uint64_t Repeated::SerializeInternal(yunq::Serializer& serializer) const {
// Write unsigned_ints.
serializer.WriteRepeated<uint64_t>(0, unsigned_ints_);
serializer.WriteHeader();
return serializer.size();
}
} // namepace ex

View File

@ -98,6 +98,32 @@ class Cap {
uint64_t SerializeInternal(yunq::Serializer& serializer) const;
};
class Repeated {
public:
Repeated() {}
// Delete copy and move until implemented.
Repeated(const Repeated&) = delete;
Repeated(Repeated&&) = default;
Repeated& operator=(Repeated&&) = default;
[[nodiscard]] glcr::Status ParseFromBytes(const yunq::MessageView& message);
[[nodiscard]] glcr::Status ParseFromBytes(const yunq::MessageView& message, const glcr::CapBuffer&);
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset) const;
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const;
const glcr::Vector<uint64_t>& unsigned_ints() const { return unsigned_ints_; }
glcr::Vector<uint64_t>& mutable_unsigned_ints() { return unsigned_ints_; }
void add_unsigned_ints(const uint64_t& value) { unsigned_ints_.PushBack(value); }
void add_unsigned_ints(uint64_t&& value) { unsigned_ints_.PushBack(glcr::Move(value)); }
private:
glcr::Vector<uint64_t> unsigned_ints_;
// Parses everything except for caps.
glcr::Status ParseFromBytesInternal(const yunq::MessageView& message);
uint64_t SerializeInternal(yunq::Serializer& serializer) const;
};
} // namepace ex

View File

@ -86,3 +86,15 @@ TEST_CASE("Cap Serialization Sidebuffer", "[yunq]") {
REQUIRE(b.cap() == 1234);
}
TEST_CASE("Repeated Setter/Getter", "[yunq]") {
ex::Repeated r;
r.mutable_unsigned_ints().PushBack(1);
r.add_unsigned_ints(2);
uint64_t c = 3;
r.add_unsigned_ints(glcr::Move(c));
REQUIRE(r.unsigned_ints()[0] == 1);
REQUIRE(r.unsigned_ints()[1] == 2);
REQUIRE(r.unsigned_ints()[2] == 3);
}

View File

@ -107,7 +107,7 @@ uint64_t {{message.name}}::SerializeInternal(yunq::Serializer& serializer) const
{%- if field.type == Type.MESSAGE %}
serializer.WriteRepeatedMessage<{{field.cpp_type()}}>({{field.number}}, {{field.name}}_);
{%- elif field.type != Type.CAPABILITY %}
{%- elif field.type == Type.CAPABILITY %}
serializer.WriteRepeatedCapability({{field.number}}, {{field.name}}_);
{%- else %}
serializer.WriteRepeated<{{field.cpp_type()}}>({{field.number}}, {{field.name}}_);