blob: 868ec1dc36bb5f9a7990a84ef85e3855576b32ad [file] [log] [blame]
Ewout van Bekkum32dc5c52021-03-16 11:35:37 -07001// 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#include "pw_persistent_ram/persistent.h"
15
16#include <type_traits>
17
18#include "gtest/gtest.h"
19
20namespace pw::persistent_ram {
21namespace {
22
23class PersistentTest : public ::testing::Test {
24 protected:
25 PersistentTest() { ZeroPersistentMemory(); }
26
27 // Emulate invalidation of persistent section(s).
28 void ZeroPersistentMemory() { memset(&buffer_, 0, sizeof(buffer_)); }
29
30 // Allocate a chunk of aligned storage that can be independently controlled.
31 std::aligned_storage_t<sizeof(Persistent<uint32_t>),
32 alignof(Persistent<uint32_t>)>
33 buffer_;
34};
35
36TEST_F(PersistentTest, DefaultConstructionAndDestruction) {
37 { // Emulate a boot where the persistent sections were invalidated.
38 // Although the fixture always does this, we do this an extra time to be
39 // 100% confident that an integrity check cannot be accidentally selected
40 // which results in reporting there is valid data when zero'd.
41 ZeroPersistentMemory();
42 auto& persistent = *(new (&buffer_) Persistent<uint32_t>());
43 EXPECT_FALSE(persistent.has_value());
44
45 persistent = 42;
46 ASSERT_TRUE(persistent.has_value());
47 EXPECT_EQ(42u, persistent.value());
48
49 persistent.~Persistent(); // Emulate shutdown / global destructors.
50 }
51
52 { // Emulate a boot where persistent memory was kept as is.
53 auto& persistent = *(new (&buffer_) Persistent<uint32_t>());
54 ASSERT_TRUE(persistent.has_value());
55 EXPECT_EQ(42u, persistent.value());
56 }
57}
58
59TEST_F(PersistentTest, Reset) {
60 { // Emulate a boot where the persistent sections were invalidated.
61 auto& persistent = *(new (&buffer_) Persistent<uint32_t>());
62 persistent = 42u;
63 EXPECT_TRUE(persistent.has_value());
64 persistent.reset();
65
66 persistent.~Persistent(); // Emulate shutdown / global destructors.
67 }
68
69 { // Emulate a boot where persistent memory was kept as is.
70 auto& persistent = *(new (&buffer_) Persistent<uint32_t>());
71 EXPECT_FALSE(persistent.has_value());
72 }
73}
74
75TEST_F(PersistentTest, Emplace) {
76 auto& persistent = *(new (&buffer_) Persistent<uint32_t>());
77 EXPECT_FALSE(persistent.has_value());
78
79 persistent.emplace(42u);
80 ASSERT_TRUE(persistent.has_value());
81 EXPECT_EQ(42u, persistent.value());
82}
83
Armando Montanez9b085ce2021-03-19 15:12:25 -070084class MutablePersistentTest : public ::testing::Test {
85 protected:
86 struct Coordinate {
87 int x;
88 int y;
89 int z;
90 };
91 MutablePersistentTest() { ZeroPersistentMemory(); }
92
93 // Emulate invalidation of persistent section(s).
94 void ZeroPersistentMemory() { memset(&buffer_, 0, sizeof(buffer_)); }
95
96 // Allocate a chunk of aligned storage that can be independently controlled.
97 std::aligned_storage_t<sizeof(Persistent<Coordinate>),
98 alignof(Persistent<Coordinate>)>
99 buffer_;
100};
101
102TEST_F(MutablePersistentTest, DefaultConstructionAndDestruction) {
103 {
104 // Emulate a boot where the persistent sections were invalidated.
105 // Although the fixture always does this, we do this an extra time to be
106 // 100% confident that an integrity check cannot be accidentally selected
107 // which results in reporting there is valid data when zero'd.
108 ZeroPersistentMemory();
109 auto& persistent = *(new (&buffer_) Persistent<Coordinate>());
110 EXPECT_FALSE(persistent.has_value());
111
112 // Default construct of a Coordinate.
113 persistent.emplace(Coordinate({.x = 5, .y = 6, .z = 7}));
114 ASSERT_TRUE(persistent.has_value());
115 {
116 auto mutable_persistent = persistent.mutator();
117 mutable_persistent->x = 42;
118 (*mutable_persistent).y = 1337;
119 mutable_persistent->z = -99;
120 ASSERT_FALSE(persistent.has_value());
121 }
122
123 EXPECT_EQ(1337, persistent.value().y);
124 EXPECT_EQ(-99, persistent.value().z);
125
126 persistent.~Persistent(); // Emulate shutdown / global destructors.
127 }
128
129 {
130 // Emulate a boot where persistent memory was kept as is.
131 auto& persistent = *(new (&buffer_) Persistent<Coordinate>());
132 ASSERT_TRUE(persistent.has_value());
133 EXPECT_EQ(42, persistent.value().x);
134 }
135}
136
Ewout van Bekkum32dc5c52021-03-16 11:35:37 -0700137} // namespace
138} // namespace pw::persistent_ram