// Copyright 2020 The Pigweed Authors // // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy of // the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations under // the License. #include "pw_bytes/array.h" #include #include #include "gtest/gtest.h" namespace pw::bytes { namespace { template constexpr bool Equal(const T& lhs, const U& rhs) { if (sizeof(lhs) != sizeof(rhs) || std::size(lhs) != std::size(rhs)) { return false; } for (size_t i = 0; i < std::size(lhs); ++i) { if (lhs[i] != rhs[i]) { return false; } } return true; } using std::byte; constexpr std::array kHello{ byte{'H'}, byte{'e'}, byte{'l'}, byte{'l'}, byte{'o'}}; constexpr uint32_t kEllo = static_cast('e') << 0 | static_cast('l') << 8 | static_cast('l') << 16 | static_cast('o') << 24; static_assert(Equal(String("Hello"), kHello)); static_assert(Equal(MakeArray('H', 'e', 'l', 'l', 'o'), kHello)); static_assert(Equal(Concat('H', kEllo), kHello)); constexpr std::array kInit{byte{'?'}, byte{'?'}, byte{'?'}}; static_assert(Equal(Initialized<3>('?'), kInit)); constexpr std::array kCounting = MakeArray(0, 1, 2); static_assert(Equal(Initialized<3>([](size_t i) { return i; }), kCounting)); constexpr std::array kCounting2 = MakeArray(256, 1, 2); static_assert(Equal(Initialized<3>([](size_t i) { return i; }), kCounting2)); constexpr auto kArray = Array<1, 2, 3, 255>(); static_assert(Equal(MakeArray(1, 2, 3, 255), kArray)); constexpr std::array kUintArray = Array(); static_assert(Equal(MakeArray(1, 2, 3, 255), kUintArray)); } // namespace } // namespace pw::bytes