pw_persistent_ram: Adds initial Persistent object

Adds the pw_persistent_ram module with initial documentation on
persistent RAM usage and life cycle management and the initial
Persistent<t> container with built in integrity checking.

Change-Id: I7f539228ba5fcc76c6d80a72110f2365724ebf23
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/38021
Pigweed-Auto-Submit: Ewout van Bekkum <ewout@google.com>
Reviewed-by: Ewout van Bekkum <ewout@google.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
Reviewed-by: David Rogers <davidrogers@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
diff --git a/pw_persistent_ram/persistent_test.cc b/pw_persistent_ram/persistent_test.cc
new file mode 100644
index 0000000..6f45489
--- /dev/null
+++ b/pw_persistent_ram/persistent_test.cc
@@ -0,0 +1,85 @@
+// Copyright 2021 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_persistent_ram/persistent.h"
+
+#include <type_traits>
+
+#include "gtest/gtest.h"
+
+namespace pw::persistent_ram {
+namespace {
+
+class PersistentTest : public ::testing::Test {
+ protected:
+  PersistentTest() { ZeroPersistentMemory(); }
+
+  // Emulate invalidation of persistent section(s).
+  void ZeroPersistentMemory() { memset(&buffer_, 0, sizeof(buffer_)); }
+
+  // Allocate a chunk of aligned storage that can be independently controlled.
+  std::aligned_storage_t<sizeof(Persistent<uint32_t>),
+                         alignof(Persistent<uint32_t>)>
+      buffer_;
+};
+
+TEST_F(PersistentTest, DefaultConstructionAndDestruction) {
+  {  // Emulate a boot where the persistent sections were invalidated.
+    // Although the fixture always does this, we do this an extra time to be
+    // 100% confident that an integrity check cannot be accidentally selected
+    // which results in reporting there is valid data when zero'd.
+    ZeroPersistentMemory();
+    auto& persistent = *(new (&buffer_) Persistent<uint32_t>());
+    EXPECT_FALSE(persistent.has_value());
+
+    persistent = 42;
+    ASSERT_TRUE(persistent.has_value());
+    EXPECT_EQ(42u, persistent.value());
+
+    persistent.~Persistent();  // Emulate shutdown / global destructors.
+  }
+
+  {  // Emulate a boot where persistent memory was kept as is.
+    auto& persistent = *(new (&buffer_) Persistent<uint32_t>());
+    ASSERT_TRUE(persistent.has_value());
+    EXPECT_EQ(42u, persistent.value());
+  }
+}
+
+TEST_F(PersistentTest, Reset) {
+  {  // Emulate a boot where the persistent sections were invalidated.
+    auto& persistent = *(new (&buffer_) Persistent<uint32_t>());
+    persistent = 42u;
+    EXPECT_TRUE(persistent.has_value());
+    persistent.reset();
+
+    persistent.~Persistent();  // Emulate shutdown / global destructors.
+  }
+
+  {  // Emulate a boot where persistent memory was kept as is.
+    auto& persistent = *(new (&buffer_) Persistent<uint32_t>());
+    EXPECT_FALSE(persistent.has_value());
+  }
+}
+
+TEST_F(PersistentTest, Emplace) {
+  auto& persistent = *(new (&buffer_) Persistent<uint32_t>());
+  EXPECT_FALSE(persistent.has_value());
+
+  persistent.emplace(42u);
+  ASSERT_TRUE(persistent.has_value());
+  EXPECT_EQ(42u, persistent.value());
+}
+
+}  // namespace
+}  // namespace pw::persistent_ram