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