blob: c1d295fb5856bdc33803b10a0620ad8a375191b1 [file] [log] [blame]
Wyatt Heplerc84393f2020-03-20 11:23:24 -07001// 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"
David Rogersd64cc012020-05-26 12:37:37 -070018#include "pw_kvs/fake_flash_memory.h"
Wyatt Heplerc84393f2020-03-20 11:23:24 -070019
20namespace pw::kvs::internal {
21namespace {
22
23class SectorsTest : public ::testing::Test {
24 protected:
25 SectorsTest()
26 : partition_(&flash_),
David Rogersf490a082020-07-14 15:28:11 -070027 sectors_(sector_descriptors_, partition_, nullptr) {
28 EXPECT_EQ(0u, sectors_.size());
29 sectors_.Reset();
30 }
Wyatt Heplerc84393f2020-03-20 11:23:24 -070031
David Rogersd64cc012020-05-26 12:37:37 -070032 FakeFlashMemoryBuffer<128, 16> flash_;
Wyatt Heplerc84393f2020-03-20 11:23:24 -070033 FlashPartition partition_;
34 Vector<SectorDescriptor, 32> sector_descriptors_;
35 Sectors sectors_;
36};
37
38TEST_F(SectorsTest, Reset) {
David Rogersf490a082020-07-14 15:28:11 -070039 // Reset is done by the fixture.
Wyatt Heplerc84393f2020-03-20 11:23:24 -070040 EXPECT_EQ(partition_.sector_count(), sectors_.size());
41 EXPECT_EQ(32u, sectors_.max_size());
42 EXPECT_EQ(sectors_.begin(), sectors_.last_new());
43}
44
45TEST_F(SectorsTest, LastNew) {
46 sectors_.set_last_new_sector(130);
47 EXPECT_EQ(128u, sectors_.BaseAddress(*sectors_.last_new()));
48}
49
50TEST_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
61TEST_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
73TEST_F(SectorsTest, NextWritableAddress_EmptySector) {
74 EXPECT_EQ(0u, sectors_.NextWritableAddress(*sectors_.begin()));
75}
76
77TEST_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