Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dcb58d7

Browse filesBrowse files
committed
[core][utest] add Stream test cases
1 parent d12f9b4 commit dcb58d7
Copy full SHA for dcb58d7
Expand file treeCollapse file tree

13 files changed

+724
-0
lines changed

‎core/utest/Stream/mock/StreamMock.cpp

Copy file name to clipboard
+108Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: LGPL-2.1-or-later
5+
*/
6+
7+
/**************************************************************************************
8+
* INCLUDE
9+
**************************************************************************************/
10+
11+
#include "StreamMock.h"
12+
#include <utest.h>
13+
14+
/**************************************************************************************
15+
* PUBLIC MEMBER FUNCTIONS
16+
**************************************************************************************/
17+
18+
void StreamMock::resize(size_t new_capacity)
19+
{
20+
if (new_capacity == 0) {
21+
return;
22+
}
23+
24+
if (new_capacity == _capacity) {
25+
return;
26+
}
27+
28+
char* new_buffer = new char[new_capacity];
29+
if (!_buffer) { // If buffer is null, nothing to copy
30+
// Directly set the new buffer and return
31+
_buffer = new_buffer;
32+
_capacity = new_capacity;
33+
_head = 0;
34+
_tail = 0;
35+
return;
36+
}
37+
38+
size_t count = _size;
39+
size_t index = 0;
40+
41+
// Copy data from old buffer to new buffer
42+
for (size_t i = 0; i < count; ++i) {
43+
new_buffer[index++] = _buffer[(_head + i) % _capacity];
44+
}
45+
delete[] _buffer; // Now safe to delete old buffer
46+
47+
_buffer = new_buffer;
48+
_capacity = new_capacity;
49+
_head = 0;
50+
_tail = count;
51+
}
52+
53+
void StreamMock::operator<<(char const* str)
54+
{
55+
size_t len = 0;
56+
while (str[len] != '\0') ++len;
57+
58+
size_t new_size = _size + len;
59+
if (new_size > _capacity) {
60+
resize(new_size * 2); // Grow buffer
61+
}
62+
63+
for (size_t i = 0; i < len; ++i) {
64+
_buffer[(_tail + i) % _capacity] = str[i];
65+
}
66+
_tail = (_tail + len) % _capacity;
67+
_size += len;
68+
}
69+
70+
size_t StreamMock::write(uint8_t ch)
71+
{
72+
if (_size == _capacity) {
73+
resize(_capacity == 0 ? 1 : _capacity * 2);
74+
}
75+
76+
_buffer[_tail] = static_cast<char>(ch);
77+
_tail = (_tail + 1) % _capacity;
78+
++_size;
79+
80+
return 1;
81+
}
82+
83+
int StreamMock::available()
84+
{
85+
return static_cast<int>(_size);
86+
}
87+
88+
int StreamMock::read()
89+
{
90+
if (_size == 0) {
91+
return -1;
92+
}
93+
94+
char c = _buffer[_head];
95+
_head = (_head + 1) % _capacity;
96+
--_size;
97+
98+
return static_cast<int>(c);
99+
}
100+
101+
int StreamMock::peek()
102+
{
103+
if (_size == 0) {
104+
return -1;
105+
}
106+
107+
return static_cast<int>(_buffer[_head]);
108+
}

‎core/utest/Stream/mock/StreamMock.h

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: LGPL-2.1-or-later
5+
*/
6+
7+
#ifndef STREAM_MOCK_H_
8+
#define STREAM_MOCK_H_
9+
10+
/**************************************************************************************
11+
* CLASS DECLARATION
12+
**************************************************************************************/
13+
14+
class StreamMock : public Stream
15+
{
16+
public:
17+
StreamMock() : _buffer(nullptr), _size(0), _capacity(0), _head(0), _tail(0) {}
18+
19+
~StreamMock() {
20+
delete[] _buffer;
21+
}
22+
23+
void operator<<(char const* str);
24+
25+
virtual size_t write(uint8_t ch) override;
26+
virtual int available() override;
27+
virtual int read() override;
28+
virtual int peek() override;
29+
30+
private:
31+
void resize(size_t new_capacity);
32+
33+
char* _buffer;
34+
size_t _size;
35+
size_t _capacity;
36+
size_t _head;
37+
size_t _tail;
38+
};
39+
40+
#endif /* STREAM_MOCK_H_ */

‎core/utest/Stream/test_find.cpp

Copy file name to clipboard
+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: LGPL-2.1-or-later
5+
*/
6+
7+
/**************************************************************************************
8+
* TEST CODE
9+
**************************************************************************************/
10+
11+
TEST_CASE ("Testing find(const char *target)", "[Stream-find-01]")
12+
{
13+
StreamMock mock;
14+
15+
WHEN ("'target' is contained in stream")
16+
{
17+
mock << "This is a test string";
18+
19+
REQUIRE(mock.find("test") == true);
20+
REQUIRE(mock.readString() == String(" string"));
21+
}
22+
WHEN ("'target' is not contained in stream")
23+
{
24+
mock << "This is a string";
25+
26+
REQUIRE(mock.find("test") == false);
27+
REQUIRE(mock.readString() == String(""));
28+
}
29+
}
30+
31+
TEST_CASE ("Testing find(const char *target, size_t length)", "[Stream-find-02]")
32+
{
33+
StreamMock mock;
34+
35+
WHEN ("'target' is contained in stream")
36+
{
37+
mock << "This is a test string";
38+
39+
/* 'length' should actually be '4' or strlen("test"). I'd rather
40+
* think this API should not be exposed at all.
41+
*/
42+
REQUIRE(mock.find("test", 3) == true);
43+
REQUIRE(mock.readString() == String("t string"));
44+
}
45+
WHEN ("'target' is not contained in stream")
46+
{
47+
mock << "This is a string";
48+
49+
REQUIRE(mock.find("test", 3) == false);
50+
REQUIRE(mock.readString() == String(""));
51+
}
52+
}
53+
54+
TEST_CASE ("Testing find(char target)", "[Stream-find-03]")
55+
{
56+
StreamMock mock;
57+
58+
WHEN ("'target' is contained in stream")
59+
{
60+
mock << "This is a test string";
61+
62+
REQUIRE(mock.find('t') == true);
63+
REQUIRE(mock.readString() == String("est string"));
64+
}
65+
WHEN ("'target' is not contained in stream")
66+
{
67+
mock << "This is a string";
68+
69+
REQUIRE(mock.find('!') == false);
70+
REQUIRE(mock.readString() == String(""));
71+
}
72+
}

‎core/utest/Stream/test_findUntil.cpp

Copy file name to clipboard
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: LGPL-2.1-or-later
5+
*/
6+
7+
/**************************************************************************************
8+
* TEST CODE
9+
**************************************************************************************/
10+
11+
TEST_CASE ("Testing findUntil(const char *target, const char *terminator)", "[Stream-findUntil-01]")
12+
{
13+
StreamMock mock;
14+
15+
WHEN ("'target' is contained in stream")
16+
{
17+
WHEN ("'terminator' appears before 'target'")
18+
{
19+
mock << "This is a : test string";
20+
REQUIRE(mock.findUntil("test", ": ") == false);
21+
REQUIRE(mock.readString() == String("test string"));
22+
}
23+
WHEN ("'terminator' appears after 'target'")
24+
{
25+
mock << "This is a test : string";
26+
REQUIRE(mock.findUntil("test", ": ") == true);
27+
REQUIRE(mock.readString() == String(" : string"));
28+
}
29+
WHEN ("'terminator' is not included in the string at all")
30+
{
31+
mock << "This is a test string";
32+
REQUIRE(mock.findUntil("test", ": ") == true);
33+
REQUIRE(mock.readString() == String(" string"));
34+
}
35+
}
36+
37+
WHEN ("'target' is not contained in stream")
38+
{
39+
mock << "This is a test string";
40+
REQUIRE(mock.findUntil("abc", "def") == false);
41+
REQUIRE(mock.readString() == String(""));
42+
}
43+
}

‎core/utest/Stream/test_getTimeout.cpp

Copy file name to clipboard
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: LGPL-2.1-or-later
5+
*/
6+
7+
/**************************************************************************************
8+
* TEST CODE
9+
**************************************************************************************/
10+
11+
TEST_CASE ("Verifying if default timeout is returned correctly", "[Stream-getTimeout-01]")
12+
{
13+
StreamMock mock;
14+
REQUIRE(mock.getTimeout() == 1000);
15+
}

‎core/utest/Stream/test_parseFloat.cpp

Copy file name to clipboard
+113Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: LGPL-2.1-or-later
5+
*/
6+
7+
#include <float.h>
8+
9+
/**************************************************************************************
10+
* TEST CODE
11+
**************************************************************************************/
12+
13+
TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR)", "[Stream-parseFloat-01]")
14+
{
15+
StreamMock mock;
16+
17+
WHEN ("Only a integer (no comma) is contained in stream")
18+
{
19+
mock << "12";
20+
REQUIRE(mock.parseFloat() == 12.0f);
21+
}
22+
WHEN ("A positive float is contained in stream")
23+
{
24+
mock << "12.34";
25+
REQUIRE(mock.parseFloat() == 12.34f);
26+
}
27+
WHEN ("A negative float is contained in stream")
28+
{
29+
mock << "-12.34";
30+
REQUIRE(mock.parseFloat() == -12.34f);
31+
}
32+
WHEN ("A float is prepended by digits")
33+
{
34+
mock << "abcdef12.34";
35+
REQUIRE(mock.parseFloat() == 12.34f);
36+
}
37+
WHEN ("The integer is prepended by whitespace chars")
38+
{
39+
mock << "\r\n\t 12.34";
40+
REQUIRE(mock.parseFloat() == 12.34f);
41+
}
42+
WHEN ("A float is provided with too many digits after the decimal point")
43+
{
44+
mock << "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870064";
45+
REQUIRE(mock.parseFloat() == Approx(3.141592654f));
46+
}
47+
WHEN ("A float is larger than LONG_MAX")
48+
{
49+
mock << "602200000000000000000000.00";
50+
REQUIRE(mock.parseFloat() == Approx(6.022e23f));
51+
}
52+
}
53+
54+
TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_NONE, char ignore = NO_IGNORE_CHAR)", "[Stream-parseFloat-02]")
55+
{
56+
StreamMock mock;
57+
58+
WHEN ("Only a integer is contained in stream")
59+
{
60+
mock << "12.34";
61+
REQUIRE(mock.parseFloat(SKIP_NONE) == 12.34f);
62+
REQUIRE(mock.readString() == String(""));
63+
}
64+
WHEN ("The integer is prepended by digits")
65+
{
66+
mock << "abcdef12.34";
67+
REQUIRE(mock.parseFloat(SKIP_NONE) == 0);
68+
REQUIRE(mock.readString() == String("abcdef12.34"));
69+
}
70+
WHEN ("The integer is prepended by whitespace chars")
71+
{
72+
mock << "\r\n\t 12.34";
73+
REQUIRE(mock.parseFloat(SKIP_NONE) == 0);
74+
REQUIRE(mock.readString() == String("\r\n\t 12.34"));
75+
}
76+
}
77+
78+
TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_WHITESPACE, char ignore = NO_IGNORE_CHAR)", "[Stream-parseFloat-03]")
79+
{
80+
StreamMock mock;
81+
82+
WHEN ("The integer is prepended by whitespace chars")
83+
{
84+
mock << "\r\n\t 12.34";
85+
REQUIRE(mock.parseFloat(SKIP_WHITESPACE) == 12.34f);
86+
REQUIRE(mock.readString() == String(""));
87+
}
88+
}
89+
90+
91+
TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = 'a')", "[Stream-parseFloat-04]")
92+
{
93+
StreamMock mock;
94+
95+
WHEN ("A float is contained in stream")
96+
{
97+
mock << "12.34";
98+
REQUIRE(mock.parseFloat(SKIP_ALL, 'a') == 12.34f);
99+
REQUIRE(mock.readString() == String(""));
100+
}
101+
WHEN ("The float contains only ignore char values")
102+
{
103+
mock << "12a.3a4a";
104+
REQUIRE(mock.parseFloat(SKIP_ALL, 'a') == 12.34f);
105+
REQUIRE(mock.readString() == String(""));
106+
}
107+
WHEN ("The integer contains other than ignore chars")
108+
{
109+
mock << "1bed234";
110+
REQUIRE(mock.parseFloat(SKIP_ALL, 'a') == 1.0f);
111+
REQUIRE(mock.readString() == String("bed234"));
112+
}
113+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.