1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_hex_dump/log_bytes.h"
16
17 #include <array>
18 #include <cinttypes>
19 #include <cstdint>
20 #include <cstring>
21 #include <string_view>
22
23 #include "pw_log/log.h"
24 #include "pw_span/span.h"
25 #include "pw_unit_test/framework.h"
26
27 namespace pw::dump {
28 namespace {
29
30 std::array<const std::byte, 15> short_string = {
31 std::byte('m'),
32 std::byte('y'),
33 std::byte(' '),
34 std::byte('t'),
35 std::byte('e'),
36 std::byte('s'),
37 std::byte('t'),
38 std::byte(' '),
39 std::byte('s'),
40 std::byte('t'),
41 std::byte('r'),
42 std::byte('i'),
43 std::byte('n'),
44 std::byte('g'),
45 std::byte('\n'),
46 };
47
48 std::array<const std::byte, 33> long_buffer = {
49 std::byte(0xa4), std::byte(0xcc), std::byte(0x32), std::byte(0x62),
50 std::byte(0x9b), std::byte(0x46), std::byte(0x38), std::byte(0x1a),
51 std::byte(0x23), std::byte(0x1a), std::byte(0x2a), std::byte(0x7a),
52 std::byte(0xbc), std::byte(0xe2), std::byte(0x40), std::byte(0xa0),
53 std::byte(0xff), std::byte(0x33), std::byte(0xe5), std::byte(0x2b),
54 std::byte(0x9e), std::byte(0x9f), std::byte(0x6b), std::byte(0x3c),
55 std::byte(0xbe), std::byte(0x9b), std::byte(0x89), std::byte(0x3c),
56 std::byte(0x7e), std::byte(0x4a), std::byte(0x7a), std::byte(0x48),
57 std::byte(0x18)};
58
TEST(LogBytes,ShortString)59 TEST(LogBytes, ShortString) {
60 LogBytes(PW_LOG_LEVEL_DEBUG, short_string);
61 LogBytes(PW_LOG_LEVEL_INFO, short_string);
62 LogBytes(PW_LOG_LEVEL_WARN, short_string);
63 LogBytes(PW_LOG_LEVEL_ERROR, short_string);
64 LogBytes(PW_LOG_LEVEL_CRITICAL, short_string);
65 }
66
TEST(LogBytes,BytesPerLine)67 TEST(LogBytes, BytesPerLine) {
68 LogBytes<0>(PW_LOG_LEVEL_DEBUG, short_string);
69 LogBytes<1>(PW_LOG_LEVEL_DEBUG, short_string);
70 LogBytes<2>(PW_LOG_LEVEL_DEBUG, short_string);
71 LogBytes<3>(PW_LOG_LEVEL_DEBUG, short_string);
72 LogBytes<4>(PW_LOG_LEVEL_DEBUG, short_string);
73 LogBytes<8>(PW_LOG_LEVEL_DEBUG, short_string);
74 LogBytes<16>(PW_LOG_LEVEL_DEBUG, short_string);
75 LogBytes<16>(PW_LOG_LEVEL_DEBUG, long_buffer);
76 LogBytes<32>(PW_LOG_LEVEL_DEBUG, long_buffer);
77 }
78
TEST(LogBytes,LongBuffer)79 TEST(LogBytes, LongBuffer) {
80 LogBytes(PW_LOG_LEVEL_DEBUG, long_buffer);
81 LogBytes(PW_LOG_LEVEL_INFO, long_buffer);
82 LogBytes(PW_LOG_LEVEL_WARN, long_buffer);
83 LogBytes(PW_LOG_LEVEL_ERROR, long_buffer);
84 LogBytes(PW_LOG_LEVEL_CRITICAL, long_buffer);
85 }
86
87 } // namespace
88 } // namespace pw::dump
89