• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_bytes/span.h"
17 #include "pw_protobuf_test_protos/edition.pwpb.h"
18 #include "pw_protobuf_test_protos/edition_file_options.pwpb.h"
19 #include "pw_status/status.h"
20 #include "pw_stream/memory_stream.h"
21 #include "pw_unit_test/framework.h"
22 
23 namespace pw::protobuf {
24 namespace {
25 
26 using namespace ::pw::protobuf::test::pwpb;
27 
TEST(EditionsMessage,GeneratesCorrectTypes)28 TEST(EditionsMessage, GeneratesCorrectTypes) {
29   static_assert(std::is_same_v<decltype(EditionsTest::Message::optional_uint),
30                                std::optional<uint32_t>>);
31   static_assert(
32       std::is_same_v<decltype(EditionsTest::Message::default_uint), uint32_t>);
33   static_assert(std::is_same_v<decltype(EditionsTest::Message::packed_values),
34                                pw::Vector<int32_t, 8>>);
35 }
36 
TEST(EditionsFileOptionsMessage,GeneratesCorrectTypes)37 TEST(EditionsFileOptionsMessage, GeneratesCorrectTypes) {
38   static_assert(std::is_same_v<decltype(EditionsFileOptionsTest::Message::name),
39                                InlineBasicString<char, 16>>);
40   static_assert(
41       std::is_same_v<decltype(EditionsFileOptionsTest::Message::value),
42                      uint32_t>);
43   static_assert(
44       std::is_same_v<decltype(EditionsFileOptionsTest::Message::active), bool>);
45   static_assert(
46       std::is_same_v<decltype(EditionsFileOptionsTest::Message::count),
47                      std::optional<int32_t>>);
48 }
49 
TEST(EditionsMessage,Write)50 TEST(EditionsMessage, Write) {
51   const EditionsTest::Message message{
52       .optional_uint = std::nullopt,
53       .default_uint = 0,
54       .packed_values = {1000, 2000, 3000, 4000},
55   };
56 
57   // clang-format off
58   constexpr uint8_t expected_proto[] = {
59     // optional_uint omitted
60     // default_uint omitted
61     // packed_values[], v={1000, 2000, 3000, 4000}
62     0x1a, 0x08, 0xe8, 0x07, 0xd0, 0x0f, 0xb8, 0x17, 0xa0, 0x1f,
63   };
64   // clang-format on
65 
66   std::byte encode_buffer[EditionsTest::kMaxEncodedSizeBytes];
67   stream::MemoryWriter writer(encode_buffer);
68   EditionsTest::StreamEncoder encoder(writer, ByteSpan());
69 
70   ASSERT_EQ(encoder.Write(message), OkStatus());
71 
72   ConstByteSpan result = writer.WrittenData();
73   EXPECT_EQ(result.size(), sizeof(expected_proto));
74   EXPECT_EQ(std::memcmp(result.data(), expected_proto, sizeof(expected_proto)),
75             0);
76 }
77 
78 }  // namespace
79 }  // namespace pw::protobuf
80