Skip to content

Commit b17c230

Browse files
committed
Added unit tests to IntegerEasyTransfer library
1 parent f43d118 commit b17c230

File tree

3 files changed

+316
-0
lines changed

3 files changed

+316
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Unit test for IntegerEasyTransfer library.
3+
* Run this test after adding changes to the library. To run this sketch start
4+
* by downloading and installing the ArduinoUnit library into your sketchbook
5+
* and then open the test example sketches of this library.
6+
*
7+
* The MIT License (MIT)
8+
*
9+
* Copyright (c) 2017 Julian Sanin
10+
*
11+
* Permission is hereby granted, free of charge, to any person obtaining a copy
12+
* of this software and associated documentation files (the "Software"), to deal
13+
* in the Software without restriction, including without limitation the rights
14+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
* copies of the Software, and to permit persons to whom the Software is
16+
* furnished to do so, subject to the following conditions:
17+
*
18+
* The above copyright notice and this permission notice shall be included in
19+
* all copies or substantial portions of the Software.
20+
*
21+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27+
* THE SOFTWARE.
28+
*/
29+
30+
#include <ArduinoUnit.h>
31+
#include <IntegerEasyTransfer.h>
32+
#include <TwoWayIntegerEasyTransfer.h>
33+
34+
void setup() {
35+
Serial.begin(9600);
36+
while (!Serial);
37+
Test::out = &Serial;
38+
}
39+
40+
void loop() {
41+
Test::run();
42+
}
43+
44+
const uint8_t DATA_TRASH{ 0x55 };
45+
const uint8_t HEADER1{ 0x06 };
46+
const uint8_t HEADER0{ 0x85 };
47+
const uint8_t DATA_SIZE{ 3 };
48+
const uint8_t DATA_BYTE{ 0x01 };
49+
const uint8_t DATA_INT1{ 0x02 };
50+
const uint8_t DATA_INT0{ 0x03 };
51+
const int16_t DATA_INT{ (DATA_INT1 << 8) | (DATA_INT0) };
52+
const uint8_t CRC{ DATA_SIZE ^ DATA_BYTE ^ DATA_INT1 ^ DATA_INT0 };
53+
54+
const char partialData[]{
55+
DATA_TRASH,
56+
static_cast<char>(HEADER1), static_cast<char>(HEADER0),
57+
0
58+
};
59+
60+
const char fullData[]{
61+
static_cast<char>(HEADER1), static_cast<char>(HEADER0),
62+
DATA_SIZE, DATA_BYTE, DATA_INT1, DATA_INT0, CRC,
63+
0
64+
};
65+
66+
test(IntegerEasyTransfer_receiveData) {
67+
bool hasReceivedData;
68+
FakeStreamBuffer fakeStreamBuffer;
69+
IntegerEasyTransfer integerEasyTransfer;
70+
71+
integerEasyTransfer.begin(&fakeStreamBuffer);
72+
73+
fakeStreamBuffer.nextBytes(partialData);
74+
hasReceivedData = integerEasyTransfer.receiveData();
75+
assertFalse(hasReceivedData);
76+
fakeStreamBuffer.reset();
77+
78+
fakeStreamBuffer.nextBytes(fullData);
79+
hasReceivedData = integerEasyTransfer.receiveData();
80+
assertTrue(hasReceivedData);
81+
fakeStreamBuffer.reset();
82+
83+
assertEqual(integerEasyTransfer.readByte(), DATA_BYTE);
84+
assertEqual(integerEasyTransfer.readInt(), DATA_INT);
85+
}
86+
87+
test(IntegerEasyTransfer_sendData) {
88+
FakeStream fakeStream;
89+
IntegerEasyTransfer integerEasyTransfer;
90+
91+
integerEasyTransfer.begin(&fakeStream);
92+
integerEasyTransfer.writeByte(DATA_BYTE);
93+
integerEasyTransfer.writeInt(DATA_INT);
94+
integerEasyTransfer.sendData();
95+
96+
assertEqual(fakeStream.bytesWritten(), fullData);
97+
}
98+
99+
const uint8_t DATA_BYTE_MIN{ 0 };
100+
const uint8_t DATA_BYTE_MAX{ 255 };
101+
const int16_t DATA_INT_MIN{ -32768 };
102+
const int16_t DATA_INT_MAX{ 32767 };
103+
104+
test(IntegerEasyTransfer_writeByte) {
105+
FakeStream fakeStream;
106+
IntegerEasyTransfer integerEasyTransfer;
107+
108+
integerEasyTransfer.begin(&fakeStream);
109+
integerEasyTransfer.writeByte(DATA_BYTE_MIN);
110+
integerEasyTransfer.writeByte(DATA_BYTE_MAX);
111+
112+
const char * expected{ "" };
113+
assertEqual(fakeStream.bytesWritten(), expected);
114+
}
115+
116+
test(IntegerEasyTransfer_writeInt) {
117+
FakeStream fakeStream;
118+
IntegerEasyTransfer integerEasyTransfer;
119+
120+
integerEasyTransfer.begin(&fakeStream);
121+
integerEasyTransfer.writeInt(DATA_INT_MIN);
122+
integerEasyTransfer.writeInt(DATA_INT_MAX);
123+
124+
const char * expected{ "" };
125+
assertEqual(fakeStream.bytesWritten(), expected);
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Unit Testing IntegerEasyTransfer library
2+
3+
Tests tests are written using the
4+
[ArduinoUnit library version 2.2.0](https://github.com/mmurdoch/arduinounit).
5+
6+
Follow the instructions in the ArduinoUnit readme to install the library.
7+
8+
Compile and upload the test sketch as you would any other sketch. Then open the
9+
Serial Monitor to view the test results.
10+
11+
If you make changes to the IntegerEasyTransfer library, run the tests in /tests/
12+
to ensure that your changes have not produced any unexpected errors.
13+
14+
You should also perform manual tests against actual hardware.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
* Unit test for IntegerEasyTransfer library.
3+
* Run this test after adding changes to the library. To run this sketch start
4+
* by downloading and installing the ArduinoUnit library into your sketchbook
5+
* and then open the test example sketches of this library.
6+
*
7+
* The MIT License (MIT)
8+
*
9+
* Copyright (c) 2017 Julian Sanin
10+
*
11+
* Permission is hereby granted, free of charge, to any person obtaining a copy
12+
* of this software and associated documentation files (the "Software"), to deal
13+
* in the Software without restriction, including without limitation the rights
14+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
* copies of the Software, and to permit persons to whom the Software is
16+
* furnished to do so, subject to the following conditions:
17+
*
18+
* The above copyright notice and this permission notice shall be included in
19+
* all copies or substantial portions of the Software.
20+
*
21+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27+
* THE SOFTWARE.
28+
*/
29+
30+
#include <ArduinoUnit.h>
31+
#include <IntegerEasyTransfer.h>
32+
#include <TwoWayIntegerEasyTransfer.h>
33+
34+
void setup() {
35+
Serial.begin(9600);
36+
while (!Serial);
37+
Test::out = &Serial;
38+
}
39+
40+
void loop() {
41+
Test::run();
42+
}
43+
44+
const uint8_t HEADER1{ 0x06 };
45+
const uint8_t HEADER0{ 0x85 };
46+
const uint8_t DATA_SIZE{ 3 };
47+
const uint8_t DATA_BYTE{ 0x01 };
48+
const uint8_t DATA_INT1{ 0x02 };
49+
const uint8_t DATA_INT0{ 0x03 };
50+
const int16_t DATA_INT{ (DATA_INT1 << 8) | (DATA_INT0) };
51+
const uint8_t CRC{ DATA_SIZE ^ DATA_BYTE ^ DATA_INT1 ^ DATA_INT0 };
52+
53+
const char fullData[]{
54+
static_cast<char>(HEADER1), static_cast<char>(HEADER0),
55+
DATA_SIZE, DATA_BYTE, DATA_INT1, DATA_INT0, CRC,
56+
0
57+
};
58+
59+
test(TwoWayIntegerEasyTransfer_attachDefaultMessageCallback) {
60+
static uint8_t receivedDataCmd;
61+
static int16_t receivedDataArg;
62+
FakeStreamBuffer fakeStreamBuffer;
63+
64+
receivedDataCmd = 0x00;
65+
receivedDataArg = 0x0000;
66+
fakeStreamBuffer.nextBytes(fullData);
67+
68+
TwoWayIntegerEasyTransfer.begin(&fakeStreamBuffer);
69+
TwoWayIntegerEasyTransfer.attach(
70+
[](uint8_t command, IntegerEasyTransfer & request) {
71+
receivedDataCmd = command;
72+
receivedDataArg = request.readInt();
73+
});
74+
75+
assertTrue(TwoWayIntegerEasyTransfer.hasReceivedData());
76+
77+
TwoWayIntegerEasyTransfer.processInput();
78+
fakeStreamBuffer.reset();
79+
80+
assertEqual(receivedDataCmd, DATA_BYTE);
81+
assertEqual(receivedDataArg, DATA_INT);
82+
}
83+
84+
const uint8_t EXT_SIZE{ 2 };
85+
const uint8_t EXT_MSG{ TwoWayIntegerEasyTransfer.MESSAGE_FEATURE };
86+
const uint8_t CRC_EXT{ EXT_SIZE ^ EXT_MSG ^ DATA_BYTE };
87+
88+
const char extendedMessage[]{
89+
static_cast<char>(HEADER1), static_cast<char>(HEADER0), EXT_SIZE,
90+
static_cast<char>(EXT_MSG), DATA_BYTE, static_cast<char>(CRC_EXT),
91+
0
92+
};
93+
94+
test(TwoWayIntegerEasyTransfer_attachExtendedMessageCallback) {
95+
static uint8_t receivedDataArg;
96+
FakeStreamBuffer fakeStreamBuffer;
97+
98+
receivedDataArg = 0x0000;
99+
fakeStreamBuffer.nextBytes(extendedMessage);
100+
101+
TwoWayIntegerEasyTransfer.begin(&fakeStreamBuffer);
102+
TwoWayIntegerEasyTransfer.attach([](IntegerEasyTransfer & request) {
103+
receivedDataArg = request.readByte();
104+
});
105+
106+
assertTrue(TwoWayIntegerEasyTransfer.hasReceivedData());
107+
108+
TwoWayIntegerEasyTransfer.processInput();
109+
fakeStreamBuffer.reset();
110+
111+
assertEqual(receivedDataArg, DATA_BYTE);
112+
}
113+
114+
const uint8_t EXT_SYS_RESET_SIZE{ 1 };
115+
const uint8_t EXT_SYS_RESET_MSG{
116+
TwoWayIntegerEasyTransfer.MESSAGE_SYSTEM_RESET
117+
};
118+
const uint8_t CRC_EXT_SYS_RESET{ EXT_SYS_RESET_SIZE ^ EXT_SYS_RESET_MSG };
119+
120+
const char extendedSysResetMessage[]{
121+
static_cast<char>(HEADER1), static_cast<char>(HEADER0), EXT_SYS_RESET_SIZE,
122+
static_cast<char>(EXT_SYS_RESET_MSG), static_cast<char>(CRC_EXT_SYS_RESET),
123+
0
124+
};
125+
126+
test(TwoWayIntegerEasyTransfer_attachSystemResetCallback) {
127+
static bool hasReceivedSystemReset;
128+
FakeStreamBuffer fakeStreamBuffer;
129+
130+
hasReceivedSystemReset = false;
131+
fakeStreamBuffer.nextBytes(extendedSysResetMessage);
132+
133+
TwoWayIntegerEasyTransfer.begin(&fakeStreamBuffer);
134+
TwoWayIntegerEasyTransfer.attach([]() {
135+
hasReceivedSystemReset = true;
136+
});
137+
138+
assertTrue(TwoWayIntegerEasyTransfer.hasReceivedData());
139+
TwoWayIntegerEasyTransfer.processInput();
140+
assertTrue(hasReceivedSystemReset);
141+
142+
fakeStreamBuffer.reset();
143+
}
144+
145+
test(TwoWayIntegerEasyTransfer_sendDefaultMessage) {
146+
FakeStream fakeStream;
147+
148+
TwoWayIntegerEasyTransfer.begin(&fakeStream);
149+
150+
TwoWayIntegerEasyTransfer.write(DATA_BYTE);
151+
TwoWayIntegerEasyTransfer.write(DATA_INT);
152+
TwoWayIntegerEasyTransfer.sendData();
153+
154+
assertEqual(fakeStream.bytesWritten(), fullData);
155+
}
156+
157+
test(TwoWayIntegerEasyTransfer_sendExtendedMessage) {
158+
FakeStream fakeStream;
159+
160+
TwoWayIntegerEasyTransfer.begin(&fakeStream);
161+
162+
TwoWayIntegerEasyTransfer.write(EXT_MSG);
163+
TwoWayIntegerEasyTransfer.write(DATA_BYTE);
164+
TwoWayIntegerEasyTransfer.sendData();
165+
166+
assertEqual(fakeStream.bytesWritten(), extendedMessage);
167+
}
168+
169+
test(TwoWayIntegerEasyTransfer_sendSystemResetMessage) {
170+
FakeStream fakeStream;
171+
172+
TwoWayIntegerEasyTransfer.begin(&fakeStream);
173+
TwoWayIntegerEasyTransfer.sendSystemReset();
174+
175+
assertEqual(fakeStream.bytesWritten(), extendedSysResetMessage);
176+
}

0 commit comments

Comments
 (0)