Ewout van Bekkum | 32dc5c5 | 2021-03-16 11:35:37 -0700 | [diff] [blame^] | 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 | #include "pw_persistent_ram/persistent.h" |
| 15 | |
| 16 | #include <type_traits> |
| 17 | |
| 18 | #include "gtest/gtest.h" |
| 19 | |
| 20 | namespace pw::persistent_ram { |
| 21 | namespace { |
| 22 | |
| 23 | class 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 | |
| 36 | TEST_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 | |
| 59 | TEST_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 | |
| 75 | TEST_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 | |
| 84 | } // namespace |
| 85 | } // namespace pw::persistent_ram |