|
| 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