|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Arduino. All rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +/************************************************************************************** |
| 6 | + * INCLUDE |
| 7 | + **************************************************************************************/ |
| 8 | + |
| 9 | +#include <catch.hpp> |
| 10 | + |
| 11 | +#include <StreamMock.h> |
| 12 | + |
| 13 | +/************************************************************************************** |
| 14 | + * TEST CODE |
| 15 | + **************************************************************************************/ |
| 16 | + |
| 17 | +TEST_CASE ("Testing readBytesUntil(char terminator, char *buffer, size_t length)", "[Stream-readBytesUntil-01]") |
| 18 | +{ |
| 19 | + StreamMock mock; |
| 20 | + |
| 21 | + WHEN ("the stream is empty") |
| 22 | + { |
| 23 | + char buf[32] = {0}; |
| 24 | + |
| 25 | + REQUIRE(mock.readBytesUntil(' ', buf, sizeof(buf)) == 0); |
| 26 | + } |
| 27 | + |
| 28 | + WHEN ("the stream contains the termination character") |
| 29 | + { |
| 30 | + char buf[32] = {0}; |
| 31 | + char const str[] = "some stream content"; |
| 32 | + char const EXPECTED_STR[] = "some"; |
| 33 | + mock << str; |
| 34 | + |
| 35 | + REQUIRE(mock.readBytesUntil(' ', buf, sizeof(buf)) == strlen("some")); |
| 36 | + REQUIRE(strncmp(buf, EXPECTED_STR, sizeof(buf)) == 0); |
| 37 | + REQUIRE(mock.readString() == arduino::String("stream content")); |
| 38 | + } |
| 39 | + |
| 40 | + WHEN ("the stream does not contain the termination character") |
| 41 | + { |
| 42 | + char buf[32] = {0}; |
| 43 | + char const STR[] = "some stream content"; |
| 44 | + mock << STR; |
| 45 | + |
| 46 | + REQUIRE(mock.readBytesUntil('!', buf, sizeof(buf)) == strlen(STR)); |
| 47 | + REQUIRE(strncmp(buf, STR, sizeof(buf)) == 0); |
| 48 | + REQUIRE(mock.readString() == arduino::String("")); |
| 49 | + } |
| 50 | +} |
0 commit comments