Skip to content

Commit 22dcc5f

Browse files
author
Seppo Takalo
committed
Add unittest for HeapBlockDevice and change some MBED_ASSERTS to errors.
1 parent 3d0b7c4 commit 22dcc5f

File tree

3 files changed

+151
-8
lines changed

3 files changed

+151
-8
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/* Copyright (c) 2019 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 "features/storage/blockdevice/HeapBlockDevice.h"
19+
#include "string.h"
20+
#include "mbed_assert.h"
21+
22+
#define BLOCK_SIZE (512)
23+
#define DEVICE_SIZE (BLOCK_SIZE*10)
24+
25+
class HeapBlockDeviceTest : public testing::Test {
26+
protected:
27+
virtual void SetUp()
28+
{
29+
bd.init();
30+
}
31+
32+
virtual void TearDown()
33+
{
34+
bd.deinit();
35+
}
36+
37+
mbed::HeapBlockDevice bd{DEVICE_SIZE};
38+
};
39+
40+
TEST_F(HeapBlockDeviceTest, constructor)
41+
{
42+
// HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase);
43+
mbed::HeapBlockDevice one{3000, 100, 200, 300};
44+
EXPECT_EQ(one.init(), BD_ERROR_OK);
45+
EXPECT_EQ(one.size(), 3000);
46+
EXPECT_EQ(one.get_read_size(), 100);
47+
EXPECT_EQ(one.get_program_size(), 200);
48+
EXPECT_EQ(one.get_erase_size(), 300);
49+
EXPECT_EQ(one.get_erase_size(0), 300);
50+
EXPECT_EQ(one.deinit(), BD_ERROR_OK);
51+
}
52+
53+
TEST_F(HeapBlockDeviceTest, double_init)
54+
{
55+
mbed::HeapBlockDevice one{DEVICE_SIZE};
56+
EXPECT_EQ(one.init(), BD_ERROR_OK);
57+
EXPECT_EQ(one.init(), BD_ERROR_OK);
58+
EXPECT_EQ(one.deinit(), BD_ERROR_OK); // First de-init does only decrement the counter
59+
EXPECT_EQ(one.deinit(), BD_ERROR_OK);
60+
EXPECT_EQ(one.deinit(), BD_ERROR_OK); //Third one does not de-init, but return immediately
61+
}
62+
63+
TEST_F(HeapBlockDeviceTest, get_type)
64+
{
65+
EXPECT_EQ(0, strcmp(bd.get_type(), "HEAP"));
66+
}
67+
68+
TEST_F(HeapBlockDeviceTest, erase_program_read)
69+
{
70+
uint8_t *block = new uint8_t[BLOCK_SIZE]{0xaa,0xbb,0xcc};
71+
uint8_t *buf = new uint8_t[BLOCK_SIZE];
72+
EXPECT_EQ(bd.erase(0, BLOCK_SIZE), BD_ERROR_OK);
73+
EXPECT_EQ(bd.program(block, 0, BLOCK_SIZE), BD_ERROR_OK);
74+
EXPECT_EQ(bd.read(buf, 0, BLOCK_SIZE), BD_ERROR_OK);
75+
EXPECT_EQ(0, memcmp(block, buf, BLOCK_SIZE));
76+
delete[] block;
77+
delete[] buf;
78+
}
79+
80+
TEST_F(HeapBlockDeviceTest, use_uninitalized)
81+
{
82+
mbed::HeapBlockDevice one{DEVICE_SIZE};
83+
uint8_t *buf = new uint8_t[BLOCK_SIZE];
84+
EXPECT_EQ(one.read(buf, 0, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
85+
EXPECT_EQ(one.program(buf, 0, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
86+
delete[] buf;
87+
}
88+
89+
TEST_F(HeapBlockDeviceTest, over_read)
90+
{
91+
uint8_t *buf = new uint8_t[BLOCK_SIZE];
92+
EXPECT_EQ(bd.read(buf, DEVICE_SIZE, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
93+
delete[] buf;
94+
}
95+
96+
TEST_F(HeapBlockDeviceTest, over_write)
97+
{
98+
uint8_t *buf = new uint8_t[BLOCK_SIZE]{0xaa,0xbb,0xcc};
99+
EXPECT_EQ(bd.program(buf, DEVICE_SIZE, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
100+
delete[] buf;
101+
}
102+
103+
TEST_F(HeapBlockDeviceTest, over_erase)
104+
{
105+
EXPECT_EQ(bd.erase(DEVICE_SIZE, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
106+
}
107+
108+
TEST_F(HeapBlockDeviceTest, erase_uninitialized)
109+
{
110+
mbed::HeapBlockDevice one{DEVICE_SIZE};
111+
EXPECT_EQ(one.erase(DEVICE_SIZE, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
112+
}
113+
114+
TEST_F(HeapBlockDeviceTest, read_unprogrammed)
115+
{
116+
uint8_t *buf = new uint8_t[BLOCK_SIZE];
117+
EXPECT_EQ(bd.read(buf, DEVICE_SIZE - BLOCK_SIZE, BLOCK_SIZE), BD_ERROR_OK);
118+
// Ignore the content, it is now zero, but does not need to be.
119+
delete[] buf;
120+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
####################
3+
# UNIT TESTS
4+
####################
5+
6+
set(unittest-includes ${unittest-includes}
7+
.
8+
..
9+
)
10+
11+
set(unittest-sources
12+
../features/storage/blockdevice/HeapBlockDevice.cpp
13+
stubs/mbed_atomic_stub.c
14+
stubs/mbed_assert_stub.c
15+
)
16+
17+
set(unittest-test-sources
18+
features/storage/blockdevice/HeapBlockDevice/test.cpp
19+
)

features/storage/blockdevice/HeapBlockDevice.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,12 @@ bd_size_t HeapBlockDevice::size() const
117117

118118
int HeapBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
119119
{
120-
MBED_ASSERT(_blocks != NULL);
121-
MBED_ASSERT(is_valid_read(addr, size));
122120
if (!_is_initialized) {
123121
return BD_ERROR_DEVICE_ERROR;
124122
}
123+
if (!is_valid_read(addr, size)) {
124+
return BD_ERROR_DEVICE_ERROR;
125+
}
125126

126127
uint8_t *buffer = static_cast<uint8_t *>(b);
127128

@@ -145,11 +146,12 @@ int HeapBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
145146

146147
int HeapBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
147148
{
148-
MBED_ASSERT(_blocks != NULL);
149-
MBED_ASSERT(is_valid_program(addr, size));
150149
if (!_is_initialized) {
151150
return BD_ERROR_DEVICE_ERROR;
152151
}
152+
if (!is_valid_program(addr, size)) {
153+
return BD_ERROR_DEVICE_ERROR;
154+
}
153155

154156
const uint8_t *buffer = static_cast<const uint8_t *>(b);
155157

@@ -176,10 +178,12 @@ int HeapBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
176178

177179
int HeapBlockDevice::erase(bd_addr_t addr, bd_size_t size)
178180
{
179-
MBED_ASSERT(_blocks != NULL);
180-
MBED_ASSERT(is_valid_erase(addr, size));
181-
// TODO assert on programming unerased blocks
182-
181+
if (!_is_initialized) {
182+
return BD_ERROR_DEVICE_ERROR;
183+
}
184+
if (!is_valid_erase(addr, size)) {
185+
return BD_ERROR_DEVICE_ERROR;
186+
}
183187
return 0;
184188
}
185189

0 commit comments

Comments
 (0)