Skip to content

Commit

Permalink
Explicit coverage of optional<vector<byte>>
Browse files Browse the repository at this point in the history
Note: It just worked, unlike variant. Are the tests redundant?
  • Loading branch information
SeanCurtis-TRI committed Dec 20, 2024
1 parent 2efd623 commit 7053485
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
18 changes: 18 additions & 0 deletions bindings/pydrake/common/test/yaml_typed_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ class InnerStruct:
inner_value: float = nan


@dc.dataclass
class OptionalByteStruct:
value: bytes | None = None


@dc.dataclass
class OptionalStruct:
value: float | None = nan
Expand Down Expand Up @@ -532,6 +537,15 @@ def test_read_optional(self, *, options):
schema=schema, data=amended_data, **options)
self.assertEqual(actual, schema(expected))

def test_read_optional_bytes(self):
"""Smoke test for compatibility for the odd scalar: vector<byte>.
It skips the nuance of parsing configuration, assuming that is handled
by the more general test on Optional.
"""
data = "value: !!binary b3RoZXID/3N0dWZm"
actual = yaml_load_typed(schema=OptionalByteStruct, data=data)
self.assertEqual(actual, OptionalByteStruct(b"other\x03\xffstuff"))

@run_with_multiple_values(_all_typed_read_options())
def test_read_variant(self, *, options):
data = "value: foo"
Expand Down Expand Up @@ -1039,6 +1053,10 @@ def test_write_optional(self):
actual_doc = yaml_dump_typed(LegacyOptionalStruct(value=value))
self.assertEqual(actual_doc, expected_doc)

# Smoke test for compatibility for the odd scalar: vector<byte>.
actual_doc = yaml_dump_typed(OptionalByteStruct(b"other\x03\xffstuff"))
self.assertEqual(actual_doc, "value: !!binary |\n b3RoZXID/3N0dWZm\n")

def test_write_variant(self):
cases = [
(
Expand Down
18 changes: 18 additions & 0 deletions common/yaml/test/example_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@ struct UnorderedMapStruct {
string_unordered_map<double> value;
};

struct OptionalBytesStruct {
template <typename Archive>
void Serialize(Archive* a) {
a->Visit(DRAKE_NVP(value));
}

OptionalBytesStruct() {
using b = std::byte;
value = std::vector<std::byte>{b(10), b(20), b(30)};
}
explicit OptionalBytesStruct(
const std::optional<std::vector<std::byte>>& value_in) {
value = value_in;
}

std::optional<std::vector<std::byte>> value;
};

struct OptionalStruct {
template <typename Archive>
void Serialize(Archive* a) {
Expand Down
14 changes: 14 additions & 0 deletions common/yaml/test/yaml_read_archive_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,20 @@ TEST_P(YamlReadArchiveTest, Optional) {
}
}

/* Smoke test for compatibility for the odd scalar: vector<byte>. This skips the
nuance of parsing configuration and default/non-default. We assume that the
Optional test covers that and there is nothing about vector<bytes> that would
invalidate it. */
TEST_P(YamlReadArchiveTest, OptionalBytes) {
const std::string byte_str("other\x03\xffstuff");
const auto* byte_ptr = reinterpret_cast<const std::byte*>(byte_str.data());
const std::vector<std::byte> expected(byte_ptr, byte_ptr + byte_str.size());
const auto& x = AcceptNoThrow<OptionalBytesStruct>(
Load("doc:\n value: !!binary b3RoZXID/3N0dWZm"));
ASSERT_TRUE(x.value.has_value());
EXPECT_EQ(x.value.value(), expected);
}

TEST_P(YamlReadArchiveTest, Variant) {
const auto test = [](const std::string& doc, const Variant4& expected) {
const auto& x = AcceptNoThrow<VariantStruct>(Load(doc));
Expand Down
13 changes: 13 additions & 0 deletions common/yaml/test/yaml_write_archive_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,19 @@ TEST_F(YamlWriteArchiveTest, Optional) {

test(std::nullopt, "doc:\n");
test(1.0, "doc:\n value: 1.0\n");

// Smoke test for compatibility for the odd scalar: vector<byte>.
using ByteString = std::vector<std::byte>;
const auto test_bytes = [](const std::optional<ByteString>& value,
const std::string& expected) {
const OptionalBytesStruct x(value);
EXPECT_EQ(Save(x), expected);
};
test_bytes(std::nullopt, "doc:\n");
const std::string bytes("other\x03\xffstuff");
const auto* byte_ptr = reinterpret_cast<const std::byte*>(bytes.data());
test_bytes(ByteString(byte_ptr, byte_ptr + bytes.size()),
"doc:\n value: !!binary b3RoZXID/3N0dWZm\n");
}

TEST_F(YamlWriteArchiveTest, Variant) {
Expand Down

0 comments on commit 7053485

Please sign in to comment.