1 // Copyright 2021 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_containers/to_array.h"
16
17 #include <cstring>
18
19 #include "pw_unit_test/framework.h"
20
21 namespace pw {
22 namespace containers {
23 namespace {
24
TEST(Array,ToArray_StringLiteral)25 TEST(Array, ToArray_StringLiteral) {
26 std::array<char, sizeof("literally!")> array = to_array("literally!");
27 EXPECT_EQ(std::strcmp(array.data(), "literally!"), 0);
28 }
29
TEST(Array,ToArray_Inline)30 TEST(Array, ToArray_Inline) {
31 constexpr std::array<int, 3> kArray = to_array({1, 2, 3});
32 static_assert(kArray.size() == 3, "Size should be 3 as initialized");
33 EXPECT_EQ(kArray[0], 1);
34 }
35
TEST(Array,ToArray_Array)36 TEST(Array, ToArray_Array) {
37 char c_array[] = "array!";
38 std::array<char, sizeof("array!")> array = to_array(c_array);
39 EXPECT_EQ(std::strcmp(array.data(), "array!"), 0);
40 }
41
42 struct MoveOnly {
MoveOnlypw::containers::__anondac98afa0111::MoveOnly43 MoveOnly(char ch) : value(ch) {}
44
45 MoveOnly(const MoveOnly&) = delete;
46 MoveOnly& operator=(const MoveOnly&) = delete;
47
48 MoveOnly(MoveOnly&&) = default;
49 MoveOnly& operator=(MoveOnly&&) = default;
50
51 char value;
52 };
53
TEST(Array,ToArray_MoveOnly)54 TEST(Array, ToArray_MoveOnly) {
55 MoveOnly c_array[]{MoveOnly('a'), MoveOnly('b')};
56 std::array<MoveOnly, 2> array = to_array(std::move(c_array));
57 EXPECT_EQ(array[0].value, 'a');
58 EXPECT_EQ(array[1].value, 'b');
59 }
60
61 } // namespace
62 } // namespace containers
63 } // namespace pw
64