1 // Copyright 2022 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 #include "pw_bluetooth/address.h"
15
16 #include <array>
17 #include <cstdint>
18
19 #include "pw_span/span.h"
20 #include "pw_unit_test/framework.h"
21
22 namespace pw::bluetooth {
23 namespace {
24
25 constexpr Address kAddressString{"12:34:56:78:90:ab"};
26 constexpr Address kAddressStringFromArray{pw::span<const uint8_t, 6>{
27 std::array<const uint8_t, 6>{0xab, 0x90, 0x78, 0x56, 0x34, 0x12}}};
28
29 constexpr Address kAddressArray{pw::span<const uint8_t, 6>{
30 std::array<const uint8_t, 6>{0x1a, 0x2b, 0x3c, 0x4d, 0x5e, 0x6f}}};
31
32 // Make sure these are actually constexpr.
33 static_assert(kAddressString != kAddressArray, "constexpr check");
34 static_assert(kAddressString == kAddressStringFromArray, "constexpr check");
35 static_assert(kAddressArray.AsSpan().size() == 6, "constexpr check");
36
TEST(AddressTest,ConstructorTest)37 TEST(AddressTest, ConstructorTest) {
38 static_assert(kAddressString.ToString() == "12:34:56:78:90:ab",
39 "constexpr check");
40 static_assert(kAddressArray.ToString() == "6f:5e:4d:3c:2b:1a",
41 "constexpr check");
42 static_assert(kAddressArray == Address("6f:5e:4d:3c:2b:1a"),
43 "constexpr check");
44
45 static_assert(kAddressString == kAddressStringFromArray, "constexpr check");
46 static_assert(kAddressString != kAddressArray, "constexpr check");
47
48 auto addr_span = kAddressString.AsSpan();
49 EXPECT_EQ(addr_span.size(), 6u);
50 EXPECT_EQ(addr_span.data()[0], 0xabu);
51 EXPECT_EQ(addr_span.data()[1], 0x90u);
52 }
53
54 } // namespace
55 } // namespace pw::bluetooth
56