Skip to content

Commit 0657740

Browse files
Add Stream tests
1 parent ab1b3ae commit 0657740

File tree

2 files changed

+289
-0
lines changed

2 files changed

+289
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 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_FILESTREAM_H
17+
#define MBED_FILESTREAM_H
18+
19+
#include "platform/Stream.h"
20+
#include "platform/SingletonPtr.h"
21+
#include "platform/PlatformMutex.h"
22+
23+
24+
#define POS_IS_VALID(pos) (pos >= 0 && pos < _end)
25+
#define NEW_POS_IS_VALID(new_pos) (new_pos >= 0 && new_pos < FILE_SIZE)
26+
27+
template<uint32_t FILE_SIZE>
28+
class FileStream : public Stream {
29+
public:
30+
FileStream(const char *name = NULL) : Stream(name), _pos(-1), _end(0) {}
31+
32+
protected:
33+
int _getc();
34+
int _putc(int c);
35+
36+
off_t seek(off_t offset, int whence);
37+
38+
void lock();
39+
void unlock();
40+
41+
private:
42+
uint8_t _data[FILE_SIZE];
43+
int32_t _pos, _end;
44+
45+
static SingletonPtr<PlatformMutex> _mutex;
46+
47+
};
48+
49+
template<uint32_t FILE_SIZE> SingletonPtr<PlatformMutex> FileStream<FILE_SIZE>::_mutex;
50+
51+
template<uint32_t FILE_SIZE>
52+
int FileStream<FILE_SIZE>::_getc()
53+
{
54+
if(POS_IS_VALID(_pos)) {
55+
return _data[_pos++];
56+
} else {
57+
return EOF;
58+
}
59+
}
60+
61+
template<uint32_t FILE_SIZE>
62+
int FileStream<FILE_SIZE>::_putc(int c)
63+
{
64+
if(NEW_POS_IS_VALID(_pos + 1)) {
65+
_data[++_pos] = (uint8_t)(0XFF & c);
66+
if(_pos == _end) {
67+
_end++;
68+
}
69+
return c;
70+
}
71+
return EOF;
72+
}
73+
74+
template<uint32_t FILE_SIZE>
75+
off_t FileStream<FILE_SIZE>::seek(off_t offset, int whence)
76+
{
77+
switch(whence)
78+
{
79+
case SEEK_SET:
80+
if(POS_IS_VALID(offset)) {
81+
_pos = offset;
82+
} else {
83+
return -1;
84+
}
85+
break;
86+
case SEEK_CUR:
87+
if(POS_IS_VALID(_pos + offset)) {
88+
_pos += offset;
89+
} else {
90+
return -1;
91+
}
92+
break;
93+
case SEEK_END:
94+
if(POS_IS_VALID(_end - offset)) {
95+
_pos = _end - offset;
96+
} else {
97+
return -1;
98+
}
99+
break;
100+
default:
101+
return -2;
102+
}
103+
return _pos;
104+
}
105+
106+
107+
template<uint32_t FILE_SIZE>
108+
void FileStream<FILE_SIZE>::lock() {
109+
_mutex->lock();
110+
}
111+
112+
template<uint32_t FILE_SIZE>
113+
void FileStream<FILE_SIZE>::unlock() {
114+
_mutex->unlock();
115+
}
116+
117+
#endif // MBED_FILESTREAM_H

TESTS/mbed_platform/stream/main.cpp

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 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+
#include "mbed.h"
17+
#include "greentea-client/test_env.h"
18+
#include "utest/utest.h"
19+
#include "unity/unity.h"
20+
#include "FileStream.h"
21+
22+
23+
using utest::v1::Case;
24+
25+
#define STRINGS_SIZE 5
26+
const char *test_strings[STRINGS_SIZE] = { "one", "two", "three", "four", "five" };
27+
28+
29+
/** Test Stream interface: printf/scanf
30+
31+
Given a FileStream object created
32+
When put some strings to file stream via printf
33+
Then it can be read from file stream via scanf
34+
*/
35+
void test_printf_scanf()
36+
{
37+
char str[16];
38+
const uint32_t file_max_size = 128;
39+
FileStream<file_max_size> file("printf_scanf_test_file");
40+
41+
for(int i = 0; i < STRINGS_SIZE; i++) {
42+
int ret = file.printf("%s ", test_strings[i]);
43+
}
44+
45+
fseek((std::FILE*)file, SEEK_SET, 0);
46+
47+
for(int i = 0; i < STRINGS_SIZE; i++) {
48+
file.scanf("%s", str);
49+
TEST_ASSERT_EQUAL_STRING(test_strings[i], str);
50+
}
51+
}
52+
53+
/** Test Stream interface: puts/gets
54+
55+
Given a FileStream object created
56+
When put some strings to file stream via puts
57+
Then it can be read from file stream via gets
58+
*/
59+
void test_puts_gets()
60+
{
61+
char str[16];
62+
const uint32_t file_max_size = 128;
63+
FileStream<file_max_size> file("_puts_gets_test_file");
64+
65+
for(int i = 0; i < STRINGS_SIZE; i++) {
66+
file.puts(test_strings[i]);
67+
}
68+
69+
fseek((std::FILE*)file, 0, SEEK_SET);
70+
71+
for(int i = 0; i < STRINGS_SIZE; i++) {
72+
TEST_ASSERT_EQUAL_STRING(test_strings[i], file.gets(str, strlen(test_strings[i]) + 1));
73+
}
74+
}
75+
76+
/** Test Stream interface: putc/getc
77+
78+
Given a FileStream object created
79+
When put some characters to file stream via putc
80+
Then it can be read from file stream via getc
81+
*/
82+
void test_putc_getc()
83+
{
84+
const uint32_t file_max_size = 32;
85+
char chars[] = { 'a', 'b', 'c' };
86+
const uint32_t count = sizeof(chars);
87+
FileStream<file_max_size> file("test_file");
88+
89+
for(int i = 0; i < count; i++) {
90+
file.putc(chars[i]);
91+
}
92+
93+
fseek((std::FILE*)file, 0, SEEK_SET);
94+
95+
for(int i = 0; i < count; i++) {
96+
TEST_ASSERT_EQUAL_INT((int)chars[i], file.getc());
97+
}
98+
}
99+
100+
/** Test FileBase interface: file list
101+
102+
Given a FileStream class inherited from FileBase
103+
When before any FileStream object creation
104+
Then the underlying file list is empty
105+
When the FileStream object is created
106+
Then the file is on the underlying file list and is accessible via get and lookup
107+
When the FileStream object is destroyed
108+
Then it is not present on the underlying file list
109+
*/
110+
void test_FileBase_list()
111+
{
112+
const char *name = "test_file";
113+
const uint32_t file_max_size = 1;
114+
115+
FileBase *file = FileStream<file_max_size>::get(0);
116+
TEST_ASSERT_EQUAL_PTR(NULL, file);
117+
118+
{
119+
FileStream<file_max_size> stream(name);
120+
121+
FileBase *file1 = FileStream<file_max_size>::get(0);
122+
TEST_ASSERT_NOT_NULL(file1);
123+
124+
FileBase *file2 = FileStream<file_max_size>::lookup(name, strlen(name));
125+
TEST_ASSERT_EQUAL_PTR(file1, file2);
126+
}
127+
128+
file = FileStream<file_max_size>::lookup(name, strlen(name));
129+
TEST_ASSERT_EQUAL_PTR(NULL, file);
130+
}
131+
132+
133+
/** Test FileBase interface: file name and file type
134+
135+
Given a FileStream object inherited from FileBase
136+
When check its name
137+
Then the name is same as set during construction
138+
When check its path type
139+
Then the path type is FilePathType
140+
*/
141+
void test_FileBase_name_and_type()
142+
{
143+
const char *name = "test_file";
144+
145+
FileStream<1> stream(name);
146+
147+
TEST_ASSERT_EQUAL_STRING(name, stream.getName());
148+
149+
TEST_ASSERT_EQUAL_MESSAGE(FilePathType, stream.getPathType(), "Wrong file type!!!");
150+
}
151+
152+
153+
utest::v1::status_t test_setup(const size_t number_of_cases)
154+
{
155+
GREENTEA_SETUP(10, "default_auto");
156+
return utest::v1::verbose_test_setup_handler(number_of_cases);
157+
}
158+
159+
Case cases[] = {
160+
Case("Test files list empty", test_FileBase_list),
161+
Case("Test file name and type", test_FileBase_name_and_type),
162+
Case("Test putc/getc", test_putc_getc),
163+
Case("Test puts/gets", test_puts_gets),
164+
Case("Test printf/scanf", test_printf_scanf),
165+
};
166+
167+
utest::v1::Specification specification(test_setup, cases);
168+
169+
int main()
170+
{
171+
return !utest::v1::Harness::run(specification);
172+
}

0 commit comments

Comments
 (0)