1
2 // Copyright 2024 The Pigweed Authors
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 // use this file except in compliance with the License. You may obtain a copy of
6 // the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 // License for the specific language governing permissions and limitations under
14 // the License.
15
16 #include "pw_preprocessor/compiler.h"
17 #include "pw_protobuf/internal/codegen.h"
18 #include "pw_protobuf_test_protos/edition.pwpb.h"
19 #include "pw_span/span.h"
20 #include "pw_status/status.h"
21 #include "pw_status/status_with_size.h"
22 #include "pw_stream/memory_stream.h"
23 #include "pw_unit_test/framework.h"
24
25 namespace pw::protobuf {
26 namespace {
27
28 using namespace ::pw::protobuf::test::pwpb;
29
TEST(EditionsMessage,GeneratesCorrectTypes)30 TEST(EditionsMessage, GeneratesCorrectTypes) {
31 static_assert(std::is_same_v<decltype(EditionsTest::Message::optional_uint),
32 std::optional<uint32_t>>);
33 static_assert(
34 std::is_same_v<decltype(EditionsTest::Message::default_uint), uint32_t>);
35 static_assert(std::is_same_v<decltype(EditionsTest::Message::packed_values),
36 pw::Vector<int32_t, 8>>);
37 }
38
TEST(EditionsMessage,Write)39 TEST(EditionsMessage, Write) {
40 const EditionsTest::Message message{
41 .optional_uint = std::nullopt,
42 .default_uint = 0,
43 .packed_values = {1000, 2000, 3000, 4000},
44 };
45
46 // clang-format off
47 constexpr uint8_t expected_proto[] = {
48 // optional_uint omitted
49 // default_uint omitted
50 // packed_values[], v={1000, 2000, 3000, 4000}
51 0x1a, 0x08, 0xe8, 0x07, 0xd0, 0x0f, 0xb8, 0x17, 0xa0, 0x1f,
52 };
53 // clang-format on
54
55 std::byte encode_buffer[EditionsTest::kMaxEncodedSizeBytes];
56 stream::MemoryWriter writer(encode_buffer);
57 EditionsTest::StreamEncoder encoder(writer, ByteSpan());
58
59 ASSERT_EQ(encoder.Write(message), OkStatus());
60
61 ConstByteSpan result = writer.WrittenData();
62 EXPECT_EQ(result.size(), sizeof(expected_proto));
63 EXPECT_EQ(std::memcmp(result.data(), expected_proto, sizeof(expected_proto)),
64 0);
65 }
66
67 } // namespace
68 } // namespace pw::protobuf
69