Skip to content

Commit 885ca50

Browse files
author
Seppo Takalo
committed
Add unittests for BufferedBlockDevice
1 parent 8f7d644 commit 885ca50

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 "features/storage/blockdevice/BufferedBlockDevice.h"
20+
21+
#define BLOCK_SIZE (512)
22+
#define DEVICE_SIZE (BLOCK_SIZE*10)
23+
24+
class BufferedBlockModuleTest : public testing::Test {
25+
protected:
26+
HeapBlockDevice bd_heap{DEVICE_SIZE};
27+
BufferedBlockDevice bd{&bd_heap};
28+
uint8_t *magic;
29+
uint8_t *buf;
30+
virtual void SetUp()
31+
{
32+
ASSERT_EQ(bd.init(), 0);
33+
magic = new uint8_t[BLOCK_SIZE];
34+
buf = new uint8_t[BLOCK_SIZE];
35+
// Generate simple pattern to verify against
36+
for (int i = 0; i < BLOCK_SIZE; i++) {
37+
magic[i] = 0xaa + i;
38+
}
39+
}
40+
41+
virtual void TearDown()
42+
{
43+
ASSERT_EQ(bd.deinit(), 0);
44+
delete[] magic;
45+
delete[] buf;
46+
}
47+
};
48+
49+
TEST_F(BufferedBlockModuleTest, init)
50+
{
51+
BufferedBlockDevice b(&bd_heap);
52+
EXPECT_EQ(b.get_erase_size(), 0);
53+
EXPECT_EQ(b.get_erase_size(0), 0);
54+
EXPECT_EQ(b.get_erase_value(), BD_ERROR_DEVICE_ERROR);
55+
EXPECT_EQ(b.size(), 0);
56+
57+
EXPECT_EQ(b.init(), 0);
58+
59+
EXPECT_EQ(b.get_erase_size(), bd_heap.get_erase_size());
60+
EXPECT_EQ(b.get_erase_size(0), bd_heap.get_erase_size(0));
61+
EXPECT_EQ(b.get_erase_value(), bd_heap.get_erase_value());
62+
EXPECT_EQ(b.get_program_size(), 1);
63+
EXPECT_EQ(b.get_read_size(), 1);
64+
EXPECT_EQ(b.size(), bd_heap.size());
65+
EXPECT_EQ(b.get_type(), bd_heap.get_type());
66+
}
67+
68+
TEST_F(BufferedBlockModuleTest, read_full_block)
69+
{
70+
bd_heap.program(magic, 0, BLOCK_SIZE);
71+
EXPECT_EQ(0, bd.read(buf, 0, BLOCK_SIZE));
72+
EXPECT_EQ(0, memcmp(magic, buf, BLOCK_SIZE));
73+
}
74+
75+
TEST_F(BufferedBlockModuleTest, over_read)
76+
{
77+
bd_heap.program(magic, DEVICE_SIZE - BLOCK_SIZE, BLOCK_SIZE);
78+
EXPECT_EQ(bd.read(buf, DEVICE_SIZE - BLOCK_SIZE, BLOCK_SIZE), 0);
79+
EXPECT_EQ(bd.read(buf, DEVICE_SIZE, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
80+
}
81+
82+
TEST_F(BufferedBlockModuleTest, unalign_read)
83+
{
84+
bd_heap.program(magic, 0, BLOCK_SIZE);
85+
bd_heap.program(magic, BLOCK_SIZE, BLOCK_SIZE);
86+
EXPECT_EQ(bd.read(buf, BLOCK_SIZE/2, BLOCK_SIZE), 0);
87+
EXPECT_EQ(0, memcmp(buf, magic+(BLOCK_SIZE/2), BLOCK_SIZE/2));
88+
EXPECT_EQ(0, memcmp(magic, buf+(BLOCK_SIZE/2), BLOCK_SIZE/2));
89+
}
90+
91+
TEST_F(BufferedBlockModuleTest, align_big_read)
92+
{
93+
uint8_t *buffer = new uint8_t[BLOCK_SIZE*2];
94+
bd_heap.program(magic, 0, BLOCK_SIZE);
95+
bd_heap.program(magic, BLOCK_SIZE, BLOCK_SIZE);
96+
// Should cause 1 full block to be read unaligned from the device
97+
// second block would require buffering, because it is not a full (-1)
98+
EXPECT_EQ(bd.read(buffer, 0, (BLOCK_SIZE*2)-1), 0);
99+
EXPECT_EQ(0, memcmp(magic, buffer, BLOCK_SIZE));
100+
EXPECT_EQ(0, memcmp(magic, buffer+BLOCK_SIZE, BLOCK_SIZE-1));
101+
delete[] buffer;
102+
}
103+
104+
TEST_F(BufferedBlockModuleTest, program_small_chunks)
105+
{
106+
for (int i=0; i < BLOCK_SIZE - 1; ++i) {
107+
EXPECT_EQ(bd.program(magic+i, i, 1), 0);
108+
}
109+
EXPECT_EQ(bd.read(buf, 0, BLOCK_SIZE), 0);
110+
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE - 1));
111+
}
112+
113+
TEST_F(BufferedBlockModuleTest, program_and_read_from_cache)
114+
{
115+
bd_heap.program(magic, 0, BLOCK_SIZE);
116+
EXPECT_EQ(bd.program("a", 0, 1), 0);
117+
EXPECT_EQ(bd.read(buf, 0, BLOCK_SIZE), 0);
118+
EXPECT_EQ('a', buf[0]);
119+
EXPECT_EQ(0, memcmp(buf+1, magic+1, BLOCK_SIZE-1));
120+
}
121+
122+
TEST_F(BufferedBlockModuleTest, program_and_verify_from_storage)
123+
{
124+
EXPECT_EQ(bd.program(magic, 0, BLOCK_SIZE/2), 0);
125+
EXPECT_EQ(bd.program(magic+BLOCK_SIZE/2, BLOCK_SIZE/2, BLOCK_SIZE/2), 0); // This should actually write to device
126+
EXPECT_EQ(bd.read(buf, 0, BLOCK_SIZE), 0);
127+
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE));
128+
// Verify that data actually is in the underlying block device
129+
bd_heap.read(buf, 0, BLOCK_SIZE);
130+
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE));
131+
}
132+
133+
TEST_F(BufferedBlockModuleTest, flush_automatically)
134+
{
135+
EXPECT_EQ(bd.program(magic, 0, BLOCK_SIZE/2), 0); // Don't write full block
136+
EXPECT_EQ(bd.program(magic, BLOCK_SIZE, BLOCK_SIZE/2), 0); // This should cause flush() for previous data in cache
137+
// Verify that data actually is in the underlying block device
138+
bd_heap.read(buf, 0, BLOCK_SIZE);
139+
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE/2));
140+
}
141+
142+
TEST_F(BufferedBlockModuleTest, flush_at_exit)
143+
{
144+
bd_heap.init(); // Extra init, to prevent deinit() when BufferedBlockDevice de-inits.
145+
EXPECT_EQ(bd.program(magic, 0, BLOCK_SIZE/2), 0); // Don't write full block
146+
EXPECT_EQ(bd.deinit(), 0);
147+
// Verify that data actually is in the underlying block device
148+
EXPECT_EQ(bd_heap.read(buf, 0, BLOCK_SIZE), 0);
149+
EXPECT_EQ(0, memcmp(buf, magic, BLOCK_SIZE/2));
150+
}
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+
6+
set(unittest-includes ${unittest-includes}
7+
.
8+
..
9+
)
10+
11+
set(unittest-sources
12+
../features/storage/blockdevice/BufferedBlockDevice.cpp
13+
../features/storage/blockdevice/HeapBlockDevice.cpp
14+
stubs/mbed_atomic_stub.c
15+
stubs/mbed_assert_stub.cpp
16+
)
17+
18+
set(unittest-test-sources
19+
moduletests/storage/blockdevice/BufferedBlockDevice/moduletest.cpp
20+
)

0 commit comments

Comments
 (0)