Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_kvs / sectors_test.cc
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_kvs/internal/sectors.h"
16
17 #include "gtest/gtest.h"
18 #include "pw_kvs/fake_flash_memory.h"
19
20 namespace pw::kvs::internal {
21 namespace {
22
23 class SectorsTest : public ::testing::Test {
24  protected:
25   SectorsTest()
26       : partition_(&flash_),
27         sectors_(sector_descriptors_, partition_, nullptr) {
28     EXPECT_EQ(0u, sectors_.size());
29     sectors_.Reset();
30   }
31
32   FakeFlashMemoryBuffer<128, 16> flash_;
33   FlashPartition partition_;
34   Vector<SectorDescriptor, 32> sector_descriptors_;
35   Sectors sectors_;
36 };
37
38 TEST_F(SectorsTest, Reset) {
39   // Reset is done by the fixture.
40   EXPECT_EQ(partition_.sector_count(), sectors_.size());
41   EXPECT_EQ(32u, sectors_.max_size());
42   EXPECT_EQ(sectors_.begin(), sectors_.last_new());
43 }
44
45 TEST_F(SectorsTest, LastNew) {
46   sectors_.set_last_new_sector(130);
47   EXPECT_EQ(128u, sectors_.BaseAddress(*sectors_.last_new()));
48 }
49
50 TEST_F(SectorsTest, AddressInSector) {
51   SectorDescriptor& sector = sectors_.FromAddress(128);
52
53   EXPECT_FALSE(sectors_.AddressInSector(sector, 127));
54   for (size_t address = 128; address < 256; ++address) {
55     EXPECT_TRUE(sectors_.AddressInSector(sector, address));
56   }
57   EXPECT_FALSE(sectors_.AddressInSector(sector, 256));
58   EXPECT_FALSE(sectors_.AddressInSector(sector, 1025));
59 }
60
61 TEST_F(SectorsTest, BaseAddressAndFromAddress) {
62   for (size_t address = 0; address < 128; ++address) {
63     EXPECT_EQ(0u, sectors_.BaseAddress(sectors_.FromAddress(address)));
64   }
65   for (size_t address = 128; address < 256; ++address) {
66     EXPECT_EQ(128u, sectors_.BaseAddress(sectors_.FromAddress(address)));
67   }
68   for (size_t address = 256; address < 384; ++address) {
69     EXPECT_EQ(256u, sectors_.BaseAddress(sectors_.FromAddress(address)));
70   }
71 }
72
73 TEST_F(SectorsTest, NextWritableAddress_EmptySector) {
74   EXPECT_EQ(0u, sectors_.NextWritableAddress(*sectors_.begin()));
75 }
76
77 TEST_F(SectorsTest, NextWritableAddress_PartiallyWrittenSector) {
78   sectors_.begin()->RemoveWritableBytes(123);
79   EXPECT_EQ(123u, sectors_.NextWritableAddress(*sectors_.begin()));
80 }
81
82 // TODO: Add tests for FindSpace, FindSpaceDuringGarbageCollection, and
83 // FindSectorToGarbageCollect.
84
85 }  // namespace
86 }  // namespace pw::kvs::internal