Skip to content

Commit cc14b3a

Browse files
author
Seppo Takalo
committed
Add unittest for HeapBlockDevice and change some MBED_ASSERTS to errors.
1 parent 049532b commit cc14b3a

File tree

3 files changed

+135
-8
lines changed

3 files changed

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