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_containers/internal/raw_storage.h"
16
17 #include <array>
18 #include <cstddef>
19
20 #include "pw_containers_private/test_helpers.h"
21 #include "pw_unit_test/framework.h"
22
23 namespace pw::containers {
24 namespace {
25
26 using test::CopyOnly;
27 using test::Counter;
28 using test::MoveOnly;
29
TEST(RawStorage,Construct_ZeroSized)30 TEST(RawStorage, Construct_ZeroSized) {
31 internal::RawStorage<int, 0> array;
32 EXPECT_EQ(array.max_size(), 0u);
33 }
34
TEST(RawStorage,Construct_NonZeroSized)35 TEST(RawStorage, Construct_NonZeroSized) {
36 internal::RawStorage<int, 3> array;
37 EXPECT_EQ(array.max_size(), 3u);
38 }
39
TEST(RawStorage,Construct_Constexpr)40 TEST(RawStorage, Construct_Constexpr) {
41 constexpr internal::RawStorage<int, 2> kArray;
42 EXPECT_EQ(kArray.max_size(), 2u);
43 }
44
TEST(RawStorage,Construct_CopyOnly)45 TEST(RawStorage, Construct_CopyOnly) {
46 internal::RawStorage<CopyOnly, 2> array;
47 EXPECT_EQ(array.max_size(), 2u);
48 }
49
TEST(RawStorage,Construct_MoveOnly)50 TEST(RawStorage, Construct_MoveOnly) {
51 internal::RawStorage<MoveOnly, 2> array;
52 EXPECT_EQ(array.max_size(), 2u);
53 }
54
TEST(RawStorage,Destruct)55 TEST(RawStorage, Destruct) {
56 Counter::Reset();
57
58 {
59 [[maybe_unused]] internal::RawStorage<Counter, 128> destroyed;
60 }
61
62 EXPECT_EQ(Counter::created, 0);
63 EXPECT_EQ(Counter::destroyed, 0);
64 }
65
66 static_assert(sizeof(internal::RawStorage<uint8_t, 42>) ==
67 42 * sizeof(uint8_t));
68 static_assert(sizeof(internal::RawStorage<uint16_t, 42>) ==
69 42 * sizeof(uint16_t));
70 static_assert(sizeof(internal::RawStorage<uint32_t, 42>) ==
71 42 * sizeof(uint32_t));
72
73 } // namespace
74 } // namespace pw::containers
75