Skip to content

Commit 8e1cfe2

Browse files
Add FileHandle tests
1 parent b590051 commit 8e1cfe2

File tree

2 files changed

+649
-0
lines changed

2 files changed

+649
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
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+
#ifndef MBED_TESTFILEHANDLE_H
17+
#define MBED_TESTFILEHANDLE_H
18+
19+
#include "platform/FileHandle.h"
20+
21+
22+
#define POS_IS_VALID(pos) (pos >= 0 && pos < _end)
23+
#define NEW_POS_IS_VALID(pos) (pos >= 0 && pos < (int32_t)FILE_SIZE)
24+
#define SEEK_POS_IS_VALID(pos) (pos >= 0 && pos <= _end)
25+
#define INVALID_POS (-1)
26+
27+
template<uint32_t FILE_SIZE>
28+
class TestFile : public FileHandle {
29+
public:
30+
TestFile(): _pos(0), _end(0) {}
31+
~TestFile() {}
32+
33+
enum FunctionName {
34+
fnNone, fnRead, fnWrite, fnSeek, fnClose, fnIsatty
35+
};
36+
37+
virtual ssize_t read(void *buffer, size_t size)
38+
{
39+
ssize_t read;
40+
_fnCalled = fnRead;
41+
42+
for(read = 0; (size_t)read < size; read++)
43+
{
44+
if(POS_IS_VALID(_pos)) {
45+
((uint8_t*)buffer)[read] = _data[_pos++];
46+
} else {
47+
break;
48+
}
49+
} // for
50+
return read;
51+
}
52+
53+
virtual ssize_t write(const void *buffer, size_t size)
54+
{
55+
ssize_t written;
56+
_fnCalled = fnWrite;
57+
58+
for(written = 0; (size_t)written < size; written++)
59+
{
60+
if(NEW_POS_IS_VALID(_pos)) {
61+
_data[_pos++] = ((uint8_t*)buffer)[written];
62+
} else {
63+
if(0 == written) {
64+
return -ENOSPC;
65+
}
66+
break;
67+
}
68+
if(_end < _pos) {
69+
_end++;
70+
}
71+
} // for
72+
return written;
73+
}
74+
75+
virtual off_t seek(off_t offset, int whence)
76+
{
77+
_fnCalled = fnSeek;
78+
int32_t new_pos = INVALID_POS;
79+
80+
switch(whence)
81+
{
82+
case SEEK_SET:
83+
new_pos = offset;
84+
break;
85+
86+
case SEEK_CUR:
87+
new_pos = _pos + offset;
88+
break;
89+
90+
case SEEK_END:
91+
new_pos = _end - offset;
92+
break;
93+
94+
default:
95+
// nothing todo
96+
break;
97+
}
98+
99+
if(SEEK_POS_IS_VALID(new_pos)) {
100+
_pos = new_pos;
101+
} else {
102+
return -EINVAL;
103+
}
104+
105+
return _pos;
106+
}
107+
108+
virtual int close()
109+
{
110+
_fnCalled = fnClose;
111+
return 0;
112+
}
113+
114+
115+
static void resetFunctionCallHistory()
116+
{
117+
_fnCalled = fnNone;
118+
}
119+
120+
static bool functionCalled(FunctionName name)
121+
{
122+
return (name == _fnCalled);
123+
}
124+
125+
static FunctionName getFunctionCalled()
126+
{
127+
return _fnCalled;
128+
}
129+
130+
private:
131+
132+
// stores last function call name
133+
static FunctionName _fnCalled;
134+
135+
// file storage
136+
uint8_t _data[FILE_SIZE];
137+
138+
int32_t _pos, _end;
139+
};
140+
141+
template<uint32_t FILE_SIZE>
142+
typename TestFile<FILE_SIZE>::FunctionName TestFile<FILE_SIZE>::_fnCalled;
143+
144+
145+
#endif // MBED_TESTFILEHANDLE_H

0 commit comments

Comments
 (0)