Skip to content

Commit fb0f968

Browse files
committed
Initial unit test for SFDP
As a starting point, only sfdp_iterate_next_largest_erase_type(), which the pull request is intended to fix, is tested. More test cases shall be added in the future.
1 parent e0bd9a1 commit fb0f968

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* Copyright (c) 2020 ARM Limited
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "gtest/gtest.h"
18+
#include "gmock/gmock.h"
19+
#include "drivers/internal/SFDP.h"
20+
21+
class TestSFDP : public testing::Test {
22+
protected:
23+
struct mbed::sfdp_smptbl_info smptbl;
24+
25+
/**
26+
* Construct Mbed OS SFDP info.
27+
* Normally this is parsed from the flash-chips's
28+
* raw SFDP table bytes, but for unit test we construct
29+
* SFDP info manually
30+
*/
31+
virtual void SetUp()
32+
{
33+
// The mock flash supports 4KB, 32KB and 64KB erase types
34+
smptbl.erase_type_size_arr[0] = 4 * 1024;
35+
smptbl.erase_type_size_arr[1] = 32 * 1024;
36+
smptbl.erase_type_size_arr[2] = 64 * 1024;
37+
38+
// The mock flash has three regions, with address ranges:
39+
// * 0 to 64KB - 1B
40+
// * 64KB to 256KB - 1B
41+
// * 256KB to 1024KB - 1B
42+
smptbl.region_high_boundary[0] = 64 * 1024 - 1;
43+
smptbl.region_high_boundary[1] = 256 * 1024 - 1;
44+
smptbl.region_high_boundary[2] = 1024 * 1024 - 1;
45+
46+
// Bitfields indicating which regions support which erase types
47+
smptbl.region_erase_types_bitfld[0] = 0b0001; // 4KB only
48+
smptbl.region_erase_types_bitfld[1] = 0b0111; // 64KB, 32KB, 4KB
49+
smptbl.region_erase_types_bitfld[2] = 0b0110; // 64KB, 32KB
50+
}
51+
};
52+
53+
/**
54+
* Test if sfdp_iterate_next_largest_erase_type() returns the most
55+
* optimal erase type, whose erase size is as large as possible
56+
* while being compatible with both alignment and size requirements.
57+
*/
58+
TEST_F(TestSFDP, TestEraseTypeAlgorithm)
59+
{
60+
// Erase 104KB starting at 92KB
61+
int address = 92 * 1024;
62+
int size = 104 * 1024;
63+
int region = 1;
64+
int type;
65+
66+
// Expected outcome:
67+
// * The starting position 92KB is 4KB-aligned
68+
// * The next position 96KB (92KB + 4KB) is 32KB-aligned
69+
// * The next position 128KB (96KB + 32KB) is 64KB-aligned
70+
// * At the final position 192KB (128KB + 64KB), we only
71+
// have 4KB (104KB - 4KB - 32KB - 64KB) remaining to erase
72+
int expected_erase_KBs[] = {4, 32, 64, 4};
73+
74+
for (int i = 0; i < sizeof(expected_erase_KBs) / sizeof(int); i++) {
75+
type = sfdp_iterate_next_largest_erase_type(
76+
smptbl.region_erase_types_bitfld[region],
77+
size,
78+
address,
79+
region,
80+
smptbl);
81+
int erase_size = smptbl.erase_type_size_arr[type];
82+
EXPECT_EQ(erase_size, expected_erase_KBs[i] * 1024);
83+
address += erase_size;
84+
size -= erase_size;
85+
}
86+
87+
EXPECT_EQ(size, 0); // All erased
88+
89+
// Test unaligned erase
90+
// Region 2 only allows at least 32KB-aligned erases
91+
address = (512 + 16) * 1024;
92+
size = 64 * 1024;
93+
region = 2;
94+
type = sfdp_iterate_next_largest_erase_type(
95+
smptbl.region_erase_types_bitfld[region],
96+
size,
97+
address,
98+
region,
99+
smptbl);
100+
EXPECT_EQ(type, -1); // Invalid erase
101+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
####################
3+
# UNIT TESTS
4+
####################
5+
set(TEST_SUITE_NAME "SFDP")
6+
7+
# Source files
8+
set(unittest-sources
9+
../drivers/source/SFDP.cpp
10+
)
11+
12+
# Test files
13+
set(unittest-test-sources
14+
../drivers/tests/UNITTESTS/SFDP/test_sfdp.cpp
15+
stubs/mbed_assert_stub.cpp
16+
)
17+
18+
set(unittest-test-flags
19+
-DDEVICE_SPI
20+
)

0 commit comments

Comments
 (0)