Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_bytes / array_test.cc
1 // Copyright 2020 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_bytes/array.h"
16
17 #include <array>
18 #include <cstddef>
19
20 #include "gtest/gtest.h"
21
22 namespace pw::bytes {
23 namespace {
24
25 using std::byte;
26
27 template <typename T, typename U>
28 constexpr bool Equal(const T& lhs, const U& rhs) {
29   if (sizeof(lhs) != sizeof(rhs) || std::size(lhs) != std::size(rhs)) {
30     return false;
31   }
32
33   for (size_t i = 0; i < std::size(lhs); ++i) {
34     if (lhs[i] != rhs[i]) {
35       return false;
36     }
37   }
38
39   return true;
40 }
41
42 constexpr std::array<byte, 5> kHello{
43     byte{'H'}, byte{'e'}, byte{'l'}, byte{'l'}, byte{'o'}};
44
45 constexpr uint32_t kEllo =
46     static_cast<uint32_t>('e') << 0 | static_cast<uint32_t>('l') << 8 |
47     static_cast<uint32_t>('l') << 16 | static_cast<uint32_t>('o') << 24;
48
49 static_assert(Equal(String("Hello"), kHello));
50 static_assert(Equal(String(""), std::array<byte, 0>{}));
51 static_assert(Equal(MakeArray('H', 'e', 'l', 'l', 'o'), kHello));
52 static_assert(Equal(Concat('H', kEllo), kHello));
53
54 constexpr std::array<byte, 3> kInit{byte{'?'}, byte{'?'}, byte{'?'}};
55 static_assert(Equal(Initialized<3>('?'), kInit));
56
57 constexpr std::array<byte, 3> kCounting = MakeArray(0, 1, 2);
58 static_assert(Equal(Initialized<3>([](size_t i) { return i; }), kCounting));
59
60 constexpr std::array<byte, 3> kCounting2 = MakeArray(256, 1, 2);
61 static_assert(Equal(Initialized<3>([](size_t i) { return i; }), kCounting2));
62
63 constexpr auto kArray = Array<1, 2, 3, 255>();
64 static_assert(Equal(MakeArray(1, 2, 3, 255), kArray));
65
66 constexpr std::array<uint8_t, 4> kUintArray = Array<uint8_t, 1, 2, 3, 255>();
67 static_assert(Equal(MakeArray<uint8_t>(1, 2, 3, 255), kUintArray));
68
69 // Create a byte array with bytes::Concat and bytes::String and check that its
70 // contents are correct.
71 constexpr std::array<char, 2> kTestArray = {'a', 'b'};
72
73 constexpr auto kConcatTest = bytes::Concat('a',
74                                            uint16_t(1),
75                                            uint8_t(23),
76                                            kTestArray,
77                                            bytes::String("c"),
78                                            uint64_t(-1));
79
80 static_assert(kConcatTest.size() == 15);
81 static_assert(kConcatTest[0] == byte{'a'});
82 static_assert(kConcatTest[1] == byte{1});
83 static_assert(kConcatTest[2] == byte{0});
84 static_assert(kConcatTest[3] == byte{23});
85 static_assert(kConcatTest[4] == byte{'a'});
86 static_assert(kConcatTest[5] == byte{'b'});
87 static_assert(kConcatTest[6] == byte{'c'});
88 static_assert(kConcatTest[7] == byte{0xff});
89 static_assert(kConcatTest[8] == byte{0xff});
90 static_assert(kConcatTest[9] == byte{0xff});
91 static_assert(kConcatTest[10] == byte{0xff});
92 static_assert(kConcatTest[11] == byte{0xff});
93 static_assert(kConcatTest[12] == byte{0xff});
94 static_assert(kConcatTest[13] == byte{0xff});
95 static_assert(kConcatTest[14] == byte{0xff});
96
97 }  // namespace
98 }  // namespace pw::bytes