├── .gitignore ├── .codespellrc ├── test ├── .gitignore ├── src │ ├── PrintMock.cpp │ ├── Stream │ │ ├── test_getTimeout.cpp │ │ ├── test_setTimeout.cpp │ │ ├── test_readString.cpp │ │ ├── test_readStringUntil.cpp │ │ ├── test_readBytes.cpp │ │ ├── test_readBytesUntil.cpp │ │ ├── test_findUntil.cpp │ │ ├── test_find.cpp │ │ ├── test_parseInt.cpp │ │ └── test_parseFloat.cpp │ ├── dtostrf.cpp │ ├── String │ │ ├── test_toLowerCase.cpp │ │ ├── test_toUpperCase.cpp │ │ ├── StringPrinter.h │ │ ├── test_isEmpty.cpp │ │ ├── test_length.cpp │ │ ├── test_substring.cpp │ │ ├── test_toFloat.cpp │ │ ├── test_trim.cpp │ │ ├── test_toInt.cpp │ │ ├── test_toDouble.cpp │ │ ├── test_move.cpp │ │ ├── test_characterAccessFunc.cpp │ │ ├── test_remove.cpp │ │ ├── test_compareTo.cpp │ │ ├── test_indexOf.cpp │ │ ├── test_lastIndexOf.cpp │ │ ├── test_concat.cpp │ │ └── test_replace.cpp │ ├── Print │ │ ├── test_availableForWrite.cpp │ │ ├── test_clearWriteError.cpp │ │ ├── test_getWriteError.cpp │ │ └── test_println.cpp │ ├── IPAddress │ │ ├── test_printTo.cpp │ │ ├── test_operator_parentheses.cpp │ │ ├── test_operator_assignment.cpp │ │ ├── test_toString.cpp │ │ ├── test_IPAddress.cpp │ │ ├── test_operator_parentheses6.cpp │ │ ├── test_fromString.cpp │ │ ├── test_operator_comparison.cpp │ │ ├── test_IPAddress6.cpp │ │ ├── test_operator_comparison6.cpp │ │ └── test_printTo6.cpp │ ├── Ringbuffer │ │ ├── test_store_char.cpp │ │ ├── test_available.cpp │ │ ├── test_availableForStore.cpp │ │ ├── test_clear.cpp │ │ ├── test_peek.cpp │ │ ├── test_read_char.cpp │ │ └── test_isFull.cpp │ ├── Common │ │ ├── test_makeWord.cpp │ │ ├── test_map.cpp │ │ ├── test_max.cpp │ │ └── test_min.cpp │ ├── WCharacter │ │ ├── test_isWhitespace.cpp │ │ ├── test_isLowerCase.cpp │ │ ├── test_isUpperCase.cpp │ │ ├── test_isDigit.cpp │ │ ├── test_isSpace.cpp │ │ ├── test_isPunct.cpp │ │ ├── test_isControl.cpp │ │ ├── test_isHexadecimalDigit.cpp │ │ ├── test_toAscii.cpp │ │ └── test_isAscii.cpp │ ├── CanMsgRingbuffer │ │ └── test_available.cpp │ ├── StreamMock.cpp │ ├── CanMsg │ │ ├── test_isExtendedId.cpp │ │ ├── test_isStandardId.cpp │ │ ├── test_CanMsg_CopyCtor.cpp │ │ ├── test_operator_assignment.cpp │ │ ├── test_printTo.cpp │ │ ├── test_CanStandardId.cpp │ │ ├── test_CanExtendedId.cpp │ │ └── test_CanMsg.cpp │ ├── itoa.cpp │ └── MillisFake.cpp └── include │ ├── MillisFake.h │ ├── PrintMock.h │ ├── PrintableMock.h │ └── StreamMock.h ├── .github ├── ISSUE_TEMPLATE │ ├── new-api-component.md │ ├── config.yml │ ├── other-enhancement.md │ ├── api-improvement.md │ ├── bug_report.md │ └── api-deprecation.md └── workflows │ ├── spell-check.yml │ └── unit-tests.yml └── api ├── deprecated-avr-comp └── avr │ ├── interrupt.h │ ├── dtostrf.h │ └── dtostrf.c.impl ├── Server.h ├── deprecated ├── Print.h ├── Udp.h ├── WString.h ├── Client.h ├── Printable.h ├── Server.h ├── Stream.h ├── IPAddress.h └── HardwareSerial.h ├── Common.cpp ├── Compat.h ├── itoa.h ├── Printable.h ├── Client.h ├── ArduinoAPI.h ├── HardwareI2C.h ├── CanMsg.cpp ├── USBAPI.h ├── Interrupts.h ├── PluggableUSB.h ├── CanMsgRingbuffer.cpp ├── CanMsgRingbuffer.h ├── PluggableUSB.cpp ├── Print.h ├── RingBuffer.h ├── HardwareCAN.h ├── Udp.h ├── IPAddress.h └── HardwareSPI.h /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | .idea/ 3 | -------------------------------------------------------------------------------- /.codespellrc: -------------------------------------------------------------------------------- 1 | # See: https://github.com/codespell-project/codespell#using-a-config-file 2 | [codespell] 3 | # In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here: 4 | ignore-words-list = hel,shiftin 5 | check-filenames = 6 | check-hidden = 7 | skip = ./.git,./test/external 8 | -------------------------------------------------------------------------------- /test/.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | ### CMake ### 3 | CMakeLists.txt.user 4 | CMakeCache.txt 5 | CMakeFiles 6 | CMakeScripts 7 | Testing 8 | Makefile 9 | cmake_install.cmake 10 | install_manifest.txt 11 | compile_commands.json 12 | CTestTestfile.cmake 13 | _deps 14 | 15 | ### CMake Patch ### 16 | CMakeUserPresets.json 17 | 18 | # External projects 19 | *-prefix/ 20 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/new-api-component.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: New API component 3 | about: Suggest the addition of a new API component to ArduinoCore-API. 4 | title: "" 5 | labels: enhancement 6 | assignees: "" 7 | --- 8 | 9 | ### Description 10 | 11 | 12 | 13 | ### Additional information 14 | 15 | 16 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | contact_links: 2 | - name: Learn about the Arduino language 3 | url: https://www.arduino.cc/reference/en 4 | about: User documentation is available at the Arduino language reference. 5 | - name: Support request 6 | url: https://forum.arduino.cc/ 7 | about: We can help you out on the Arduino Forum! 8 | - name: Discuss ArduinoCore-API development 9 | url: https://groups.google.com/a/arduino.cc/g/developers 10 | about: Arduino Developers Mailing List 11 | -------------------------------------------------------------------------------- /.github/workflows/spell-check.yml: -------------------------------------------------------------------------------- 1 | name: Spell Check 2 | 3 | # See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows 4 | on: 5 | push: 6 | pull_request: 7 | schedule: 8 | # Run every Tuesday at 8 AM UTC to catch new misspelling detections resulting from dictionary updates. 9 | - cron: "0 8 * * TUE" 10 | workflow_dispatch: 11 | repository_dispatch: 12 | 13 | jobs: 14 | spellcheck: 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - name: Checkout repository 19 | uses: actions/checkout@v2 20 | 21 | - name: Spell check 22 | uses: codespell-project/actions-codespell@master 23 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/other-enhancement.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Other enhancement 3 | about: 4 | Suggest an improvement for this project that doesn't fit in the specific categories 5 | above. 6 | title: "" 7 | labels: enhancement 8 | assignees: "" 9 | --- 10 | 11 | ### Description 12 | 13 | 14 | 15 | ### Is this a breaking change? 16 | 17 | 18 | 19 | 20 | ### Additional information 21 | 22 | 23 | -------------------------------------------------------------------------------- /test/src/PrintMock.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | /************************************************************************************** 14 | * PUBLIC MEMBER FUNCTIONS 15 | **************************************************************************************/ 16 | 17 | size_t PrintMock::write(uint8_t b) 18 | { 19 | _str.append(1, static_cast(b)); 20 | return 1; 21 | } 22 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/api-improvement.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: API improvement 3 | about: Suggest an improvement to an existing API component. 4 | title: "" 5 | labels: enhancement 6 | assignees: "" 7 | --- 8 | 9 | ### API component 10 | 11 | 12 | 13 | ### Description 14 | 15 | 16 | 17 | ### Is this a breaking change? 18 | 19 | 20 | 21 | 22 | ### Additional information 23 | 24 | 25 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Report problems with the code in this repository. 4 | title: "" 5 | labels: bug 6 | assignees: "" 7 | --- 8 | 9 | ### Description 10 | 11 | 12 | 13 | ### Environment 14 | 15 | - Boards platform name: 16 | - Boards platform version (as shown in Boards Manager): 17 | - ArduinoCore-API version (if you manually installed it): 18 | 19 | ### Current behavior 20 | 21 | 22 | 23 | ### Expected behavior 24 | 25 | 26 | 27 | ### Additional information 28 | 29 | 30 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/api-deprecation.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: API deprecation 3 | about: Suggest the deprecation of an API component defined by ArduinoCore-API. 4 | title: "" 5 | labels: enhancement 6 | assignees: "" 7 | --- 8 | 9 | ### API component 10 | 11 | 12 | 13 | ### Description 14 | 15 | 16 | 17 | 18 | ### Replacement API component 19 | 20 | 21 | 22 | 23 | ### Additional information 24 | 25 | 26 | -------------------------------------------------------------------------------- /test/src/Stream/test_getTimeout.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Verifying if default timeout is returned correctly", "[Stream-getTimeout-01]") 20 | { 21 | StreamMock mock; 22 | REQUIRE(mock.getTimeout() == 1000); 23 | } 24 | -------------------------------------------------------------------------------- /test/src/dtostrf.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * FUNCTION IMPLEMENTATION 17 | **************************************************************************************/ 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | #include 24 | 25 | #ifdef __cplusplus 26 | } // extern "C" 27 | #endif 28 | -------------------------------------------------------------------------------- /test/src/Stream/test_setTimeout.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Verifying if calling 'setTimeout' is indeed modifying the timeout", "[Stream-setTimeout-01]") 20 | { 21 | StreamMock mock; 22 | 23 | mock.setTimeout(100); 24 | 25 | REQUIRE(mock.getTimeout() == 100); 26 | } 27 | -------------------------------------------------------------------------------- /test/src/String/test_toLowerCase.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::toLowerCase", "[String-toLowerCase-01]") 22 | { 23 | arduino::String str("HELLO ARDUINO"); 24 | str.toLowerCase(); 25 | REQUIRE(str == "hello arduino"); 26 | } 27 | -------------------------------------------------------------------------------- /test/src/String/test_toUpperCase.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::toUpperCase", "[String-toUpperCase-01]") 22 | { 23 | arduino::String str("hello arduino"); 24 | str.toUpperCase(); 25 | REQUIRE(str == "HELLO ARDUINO"); 26 | } 27 | -------------------------------------------------------------------------------- /test/include/MillisFake.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Arduino. All rights reserved. 3 | * 4 | * SPDX-License-Identifier: LGPL-2.1-or-later 5 | */ 6 | 7 | #ifndef MILLIS_FAKE_H_ 8 | #define MILLIS_FAKE_H_ 9 | 10 | /************************************************************************************** 11 | * INCLUDE 12 | **************************************************************************************/ 13 | 14 | #include 15 | 16 | /************************************************************************************** 17 | * FUNCTION DECLARATION 18 | **************************************************************************************/ 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | void millis_autoOn(); 25 | void millis_autoOff(); 26 | void set_millis(unsigned long const val); 27 | 28 | #ifdef __cplusplus 29 | } 30 | #endif 31 | 32 | #endif /* MILLIS_FAKE_H_ */ -------------------------------------------------------------------------------- /test/src/Print/test_availableForWrite.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Print::availableForWrite() should return 0 if not overwritten by derived class", "[Print-availableForWrite-01]") 22 | { 23 | PrintMock mock; 24 | REQUIRE(mock.availableForWrite() == 0); 25 | } 26 | -------------------------------------------------------------------------------- /test/src/IPAddress/test_printTo.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | #include 15 | 16 | /************************************************************************************** 17 | * TEST CODE 18 | **************************************************************************************/ 19 | 20 | TEST_CASE ("Print IPAddress via print method", "[IPAddress-printTo-01]") 21 | { 22 | PrintMock mock; 23 | arduino::IPAddress const ip(192,168,1,2); 24 | 25 | mock.print(ip); 26 | 27 | REQUIRE(mock._str == "192.168.1.2"); 28 | } 29 | -------------------------------------------------------------------------------- /test/src/Print/test_clearWriteError.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Clear write error should set the error code back to 0", "[Print-clearWriteError-01]") 22 | { 23 | PrintMock mock; 24 | mock.mock_setWriteError(5); 25 | mock.clearWriteError(); 26 | REQUIRE(mock.getWriteError() == 0); 27 | } 28 | -------------------------------------------------------------------------------- /test/src/String/StringPrinter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Arduino. All rights reserved. 3 | * 4 | * SPDX-License-Identifier: LGPL-2.1-or-later 5 | */ 6 | 7 | #pragma once 8 | 9 | #include 10 | #include 11 | 12 | namespace Catch { 13 | /** 14 | * Template specialization that makes sure Catch can properly print 15 | * Arduino Strings when used in comparisons directly. 16 | * 17 | * Note that without this, String objects are printed as 0 and 1, 18 | * because they are implicitly convertible to StringIfHelperType, 19 | * which is a dummy pointer. 20 | */ 21 | template<> 22 | struct StringMaker { 23 | static std::string convert(const arduino::String& str) { 24 | if (str) 25 | return ::Catch::Detail::stringify(std::string(str.c_str(), str.length())); 26 | else 27 | return "{invalid String}"; 28 | } 29 | }; 30 | } // namespace Catch 31 | -------------------------------------------------------------------------------- /test/include/PrintMock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Arduino. All rights reserved. 3 | * 4 | * SPDX-License-Identifier: LGPL-2.1-or-later 5 | */ 6 | 7 | #ifndef PRINT_MOCK_H_ 8 | #define PRINT_MOCK_H_ 9 | 10 | /************************************************************************************** 11 | * INCLUDE 12 | **************************************************************************************/ 13 | 14 | #include 15 | 16 | #include 17 | 18 | /************************************************************************************** 19 | * CLASS DECLARATION 20 | **************************************************************************************/ 21 | 22 | class PrintMock : public Print 23 | { 24 | public: 25 | std::string _str; 26 | virtual size_t write(uint8_t b) override; 27 | void mock_setWriteError() { setWriteError(); } 28 | void mock_setWriteError(int err) { setWriteError(err); } 29 | }; 30 | 31 | #endif /* PRINT_MOCK_H_ */ 32 | -------------------------------------------------------------------------------- /test/src/Ringbuffer/test_store_char.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Data is put into the ring buffer via 'store_char'", "[Ringbuffer-store_char-01]") 20 | { 21 | arduino::RingBufferN<2> ringbuffer; 22 | ringbuffer.store_char('A'); 23 | REQUIRE(ringbuffer._aucBuffer[0] == 'A'); 24 | ringbuffer.store_char('B'); 25 | REQUIRE(ringbuffer._aucBuffer[1] == 'B'); 26 | } 27 | -------------------------------------------------------------------------------- /test/src/Stream/test_readString.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | #include 15 | 16 | /************************************************************************************** 17 | * TEST CODE 18 | **************************************************************************************/ 19 | 20 | TEST_CASE ("Testing 'readString' with data available within the stream", "[Stream-readString-01]") 21 | { 22 | StreamMock mock; 23 | mock.setTimeout(10); 24 | millis_autoOn(); 25 | mock << "This is test stream content"; 26 | 27 | REQUIRE(mock.readString() == arduino::String("This is test stream content")); 28 | } 29 | -------------------------------------------------------------------------------- /api/deprecated-avr-comp/avr/interrupt.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2015 Arduino LCC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | /* 20 | Empty file. 21 | This file is here to allow compatibility with sketches (made for AVR) 22 | that include 23 | */ 24 | -------------------------------------------------------------------------------- /test/src/IPAddress/test_operator_parentheses.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Testing IPAddress::operator uint32_t() const", "[IPAddress-Operator-()-01]") 20 | { 21 | arduino::IPAddress ip(129,168,1,2); 22 | uint32_t const val_expected = ip; 23 | uint32_t const val_actual = (129 | (168 << 8) | (1 << 16) | (2 << 24)); 24 | // NOTE: Only correct on little-endian systems 25 | REQUIRE(val_expected == val_actual); 26 | } 27 | -------------------------------------------------------------------------------- /test/src/Print/test_getWriteError.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("No write error has been set", "[Print-getWriteError-01]") 22 | { 23 | PrintMock mock; 24 | REQUIRE(mock.getWriteError() == 0); 25 | } 26 | 27 | TEST_CASE ("A write error has been set", "[Print-getWriteError-02]") 28 | { 29 | PrintMock mock; 30 | mock.mock_setWriteError(5); 31 | REQUIRE(mock.getWriteError() == 5); 32 | } 33 | -------------------------------------------------------------------------------- /test/src/String/test_isEmpty.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023 Arduino. All rights reserved. 3 | * 4 | * SPDX-License-Identifier: LGPL-2.1-or-later 5 | */ 6 | 7 | /************************************************************************************** 8 | * INCLUDE 9 | **************************************************************************************/ 10 | 11 | #include 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::isEmpty when string is empty", "[String-isEmpty-01]") 22 | { 23 | arduino::String str; 24 | REQUIRE(str.isEmpty()); 25 | } 26 | 27 | TEST_CASE ("Testing String::isEmpty when string contains characters", "[String-isEmpty-02]") 28 | { 29 | arduino::String str("Testing String::isEmpty"); 30 | REQUIRE(!str.isEmpty()); 31 | } 32 | -------------------------------------------------------------------------------- /test/include/PrintableMock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Arduino. All rights reserved. 3 | * 4 | * SPDX-License-Identifier: LGPL-2.1-or-later 5 | */ 6 | 7 | #ifndef PRINTABLE_MOCK_H_ 8 | #define PRINTABLE_MOCK_H_ 9 | 10 | /************************************************************************************** 11 | * INCLUDE 12 | **************************************************************************************/ 13 | 14 | #include 15 | 16 | #include 17 | 18 | /************************************************************************************** 19 | * CLASS DECLARATION 20 | **************************************************************************************/ 21 | 22 | class PrintableMock : public arduino::Printable 23 | { 24 | public: 25 | int _i; 26 | virtual size_t printTo(arduino::Print& p) const override 27 | { 28 | size_t written = 0; 29 | written += p.print("PrintableMock i = "); 30 | written += p.print(_i); 31 | return written; 32 | } 33 | }; 34 | 35 | #endif /* PRINTABLE_MOCK_H_ */ 36 | -------------------------------------------------------------------------------- /test/include/StreamMock.h: -------------------------------------------------------------------------------- 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 | * INCLUDE 12 | **************************************************************************************/ 13 | 14 | #include 15 | 16 | #include 17 | 18 | /************************************************************************************** 19 | * CLASS DECLARATION 20 | **************************************************************************************/ 21 | 22 | class StreamMock : public arduino::Stream 23 | { 24 | public: 25 | 26 | void operator << (char const * str); 27 | 28 | virtual size_t write(uint8_t ch) override; 29 | virtual int available() override; 30 | virtual int read() override; 31 | virtual int peek() override; 32 | 33 | private: 34 | std::deque _stream; 35 | 36 | }; 37 | 38 | #endif /* STREAM_MOCK_H_ */ 39 | -------------------------------------------------------------------------------- /test/src/String/test_length.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::length when string is empty", "[String-length-01]") 22 | { 23 | arduino::String str; 24 | REQUIRE(str.length() == 0); 25 | } 26 | 27 | TEST_CASE ("Testing String::length when string contains characters", "[String-length-02]") 28 | { 29 | arduino::String str("Testing String::length"); 30 | REQUIRE(str.length() == strlen("Testing String::length")); 31 | } 32 | -------------------------------------------------------------------------------- /api/Server.h: -------------------------------------------------------------------------------- 1 | /* 2 | Server.h - Base class that provides Server 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #pragma once 21 | 22 | #include "Print.h" 23 | 24 | namespace arduino { 25 | 26 | class Server : public Print { 27 | public: 28 | virtual void begin() = 0; 29 | }; 30 | 31 | } -------------------------------------------------------------------------------- /test/src/Common/test_makeWord.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Calling 'makeWord(w)'", "[makeWord-01]") 20 | { 21 | REQUIRE(makeWord(0xDEAD) == 0xDEAD); 22 | REQUIRE(makeWord(0xDE) == 0x00DE); 23 | } 24 | 25 | TEST_CASE ("Calling 'makeWord(h,l)'", "[makeWord-02]") 26 | { 27 | REQUIRE(makeWord(0xDE, 0xAD) == 0xDEAD); 28 | } 29 | 30 | TEST_CASE ("Calling 'word(...)'", "[makeWord-03]") 31 | { 32 | REQUIRE(word(0xDEAD) == 0xDEAD); 33 | REQUIRE(word(0xDE, 0xAD) == 0xDEAD); 34 | } 35 | -------------------------------------------------------------------------------- /api/deprecated/Print.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including Print.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be removed on the next 22 | // major release of the API 23 | #include "../Print.h" 24 | 25 | -------------------------------------------------------------------------------- /api/deprecated/Udp.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including Udp.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be removed on the next 22 | // major release of the API 23 | #include "../Udp.h" 24 | 25 | 26 | -------------------------------------------------------------------------------- /api/deprecated/WString.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2016, Arduino LLC. All Right Reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including WString.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be removed on the next 22 | // major release of the API 23 | #include "../String.h" 24 | 25 | -------------------------------------------------------------------------------- /api/deprecated/Client.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including Client.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be removed on the next 22 | // major release of the API 23 | #include "../Client.h" 24 | 25 | 26 | -------------------------------------------------------------------------------- /api/deprecated/Printable.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including Printable.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be removed on the next 22 | // major release of the API 23 | #include "../Printable.h" 24 | 25 | -------------------------------------------------------------------------------- /api/deprecated/Server.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including Server.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be removed on the next 22 | // major release of the API 23 | #include "../Server.h" 24 | 25 | 26 | -------------------------------------------------------------------------------- /api/deprecated/Stream.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including Stream.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be removed on the next 22 | // major release of the API 23 | #include "../Stream.h" 24 | 25 | 26 | -------------------------------------------------------------------------------- /api/deprecated/IPAddress.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including IPAddress.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be removed on the next 22 | // major release of the API 23 | #include "../IPAddress.h" 24 | 25 | 26 | -------------------------------------------------------------------------------- /api/deprecated/HardwareSerial.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2016 Arduino LLC. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | // including HardwareSerial.h is deprecated, for all future projects use Arduino.h instead 20 | 21 | // This include is added for compatibility, it will be removed on the next 22 | // major release of the API 23 | #include "../HardwareSerial.h" 24 | 25 | 26 | -------------------------------------------------------------------------------- /test/src/WCharacter/test_isWhitespace.cpp: -------------------------------------------------------------------------------- 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 12 | #include 13 | 14 | #include 15 | 16 | /************************************************************************************** 17 | * TEST CODE 18 | **************************************************************************************/ 19 | 20 | TEST_CASE ("isWhitespace(...) is called with ' '", "[isWhitespace-01]") 21 | { 22 | REQUIRE(arduino::isWhitespace(' ') == true); 23 | } 24 | 25 | TEST_CASE ("isWhitespace(...) is called with '\t'", "[isWhitespace-02]") 26 | { 27 | REQUIRE(arduino::isWhitespace('\t') == true); 28 | } 29 | 30 | TEST_CASE ("isWhitespace(...) is called with a non whitespace char", "[isWhitespace-03]") 31 | { 32 | REQUIRE(arduino::isWhitespace('\r') == false); 33 | } 34 | -------------------------------------------------------------------------------- /test/src/WCharacter/test_isLowerCase.cpp: -------------------------------------------------------------------------------- 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 12 | #include 13 | 14 | #include 15 | 16 | /************************************************************************************** 17 | * TEST CODE 18 | **************************************************************************************/ 19 | 20 | TEST_CASE ("isLowerCase(...) is called with a upper case number", "[isLowerCase-01]") 21 | { 22 | REQUIRE(arduino::isLowerCase('A') == false); 23 | } 24 | 25 | TEST_CASE ("isLowerCase(...) is called with a lower case number", "[isLowerCase-02]") 26 | { 27 | REQUIRE(arduino::isLowerCase('a') == true); 28 | } 29 | 30 | TEST_CASE ("isLowerCase(...) is called with a non-alphabetic number", "[isLowerCase-03]") 31 | { 32 | REQUIRE(arduino::isLowerCase('0') == false); 33 | } 34 | -------------------------------------------------------------------------------- /test/src/WCharacter/test_isUpperCase.cpp: -------------------------------------------------------------------------------- 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 12 | #include 13 | 14 | #include 15 | 16 | /************************************************************************************** 17 | * TEST CODE 18 | **************************************************************************************/ 19 | 20 | TEST_CASE ("isUpperCase(...) is called with a upper case number", "[isUpperCase-01]") 21 | { 22 | REQUIRE(arduino::isUpperCase('A') == true); 23 | } 24 | 25 | TEST_CASE ("isUpperCase(...) is called with a lower case number", "[isUpperCase-02]") 26 | { 27 | REQUIRE(arduino::isUpperCase('a') == false); 28 | } 29 | 30 | TEST_CASE ("isUpperCase(...) is called with a non-alphabetic number", "[isUpperCase-03]") 31 | { 32 | REQUIRE(arduino::isUpperCase('0') == false); 33 | } 34 | -------------------------------------------------------------------------------- /test/src/Ringbuffer/test_available.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("'available' should return 0 for empty ring buffer", "[Ringbuffer-available-01]") 20 | { 21 | arduino::RingBufferN<2> ringbuffer; 22 | REQUIRE(ringbuffer.available() == 0); 23 | } 24 | 25 | TEST_CASE ("'available' should return number of elements in ringbuffer", "[Ringbuffer-available-02]") 26 | { 27 | arduino::RingBufferN<2> ringbuffer; 28 | ringbuffer.store_char('A'); 29 | REQUIRE(ringbuffer.available() == 1); 30 | ringbuffer.store_char('B'); 31 | REQUIRE(ringbuffer.available() == 2); 32 | } 33 | -------------------------------------------------------------------------------- /api/deprecated-avr-comp/avr/dtostrf.h: -------------------------------------------------------------------------------- 1 | /* 2 | dtostrf - Emulation for dtostrf function from avr-libc 3 | Copyright (c) 2015 Arduino LLC. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #pragma once 21 | 22 | #if !defined(ARDUINO_ARCH_AVR) 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | char *dtostrf(double val, signed char width, unsigned char prec, char *sout); 29 | 30 | #ifdef __cplusplus 31 | } 32 | #endif 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /api/Common.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Common.cpp - Common function implementations 3 | Copyright (c) 2017 Arduino LLC. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "Common.h" 21 | 22 | /* C++ prototypes */ 23 | long map(long x, long in_min, long in_max, long out_min, long out_max) 24 | { 25 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; 26 | } 27 | 28 | uint16_t makeWord(uint16_t w) { return w; } 29 | uint16_t makeWord(uint8_t h, uint8_t l) { return (h << 8) | l; } -------------------------------------------------------------------------------- /api/Compat.h: -------------------------------------------------------------------------------- 1 | /* 2 | Compat.h - Compatibility layer for Arduino API 3 | Copyright (c) 2018 Arduino LLC. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef __COMPAT_H__ 21 | #define __COMPAT_H__ 22 | 23 | namespace arduino { 24 | 25 | inline void pinMode(pin_size_t pinNumber, int mode) { 26 | pinMode(pinNumber, (PinMode)mode); 27 | }; 28 | 29 | inline void digitalWrite(pin_size_t pinNumber, int status) { 30 | digitalWrite(pinNumber, (PinStatus)status); 31 | }; 32 | 33 | } 34 | 35 | #endif -------------------------------------------------------------------------------- /test/src/Ringbuffer/test_availableForStore.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("'availableForStore' should return ring buffer size for empty ring buffer", "[Ringbuffer-availableForStore-01]") 20 | { 21 | arduino::RingBufferN<2> ringbuffer; 22 | REQUIRE(ringbuffer.availableForStore() == 2); 23 | } 24 | 25 | TEST_CASE ("'availableForStore' should return number of free elements in ringbuffer", "[Ringbuffer-availableForStore-02]") 26 | { 27 | arduino::RingBufferN<2> ringbuffer; 28 | ringbuffer.store_char('A'); 29 | REQUIRE(ringbuffer.availableForStore() == 1); 30 | ringbuffer.store_char('B'); 31 | REQUIRE(ringbuffer.availableForStore() == 0); 32 | } 33 | -------------------------------------------------------------------------------- /test/src/Ringbuffer/test_clear.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Calling 'clear' on a empty ring buffer should have no effect", "[Ringbuffer-clear-01]") 20 | { 21 | arduino::RingBufferN<2> ringbuffer; 22 | REQUIRE(ringbuffer.available() == 0); 23 | ringbuffer.clear(); 24 | REQUIRE(ringbuffer.available() == 0); 25 | } 26 | 27 | TEST_CASE ("Calling 'clear' on a partially filled ring buffer should \"remove\" all elements", "[Ringbuffer-clear-02]") 28 | { 29 | arduino::RingBufferN<2> ringbuffer; 30 | ringbuffer.store_char('A'); 31 | REQUIRE(ringbuffer.available() == 1); 32 | ringbuffer.clear(); 33 | REQUIRE(ringbuffer.available() == 0); 34 | } 35 | -------------------------------------------------------------------------------- /test/src/IPAddress/test_operator_assignment.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Testing IPAddress::operator = (const uint8_t * a)", "[IPAddress-Operator-=-01]") 20 | { 21 | arduino::IPAddress ip1; 22 | uint8_t const ip2[] = {192,168,1,2}; 23 | 24 | ip1 = ip2; 25 | REQUIRE(ip1 == arduino::IPAddress(192,168,1,2)); 26 | } 27 | 28 | TEST_CASE ("Testing IPAddress::operator = (uint32_t a)", "[IPAddress-Operator-=-02]") 29 | { 30 | arduino::IPAddress ip1; 31 | uint32_t const ip2 = 192 | (168 << 8) | (1 << 16) | (2 << 24); 32 | 33 | ip1 = ip2; 34 | // NOTE: Only correct on little-endian systems 35 | REQUIRE(ip1 == arduino::IPAddress(192,168,1,2)); 36 | } 37 | -------------------------------------------------------------------------------- /test/src/Ringbuffer/test_peek.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Data is accessed but not removed from the ring buffer via 'peek'", "[Ringbuffer-peek-01]") 20 | { 21 | arduino::RingBufferN<2> ringbuffer; 22 | 23 | WHEN("The ringbuffer is empty") 24 | THEN("'peek' should return -1") 25 | REQUIRE(ringbuffer.peek() == -1); 26 | 27 | WHEN("The ringbuffer contains data") 28 | { 29 | ringbuffer.store_char('A'); 30 | ringbuffer.store_char('B'); 31 | THEN("'peek' should return first inserted element first (FIFO) and not remove it") 32 | { 33 | REQUIRE(ringbuffer.peek() == 'A'); 34 | REQUIRE(ringbuffer.peek() == 'A'); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /test/src/Ringbuffer/test_read_char.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Data is removed from the ring buffer via 'read_char'", "[Ringbuffer-read_char-01]") 20 | { 21 | arduino::RingBufferN<2> ringbuffer; 22 | 23 | WHEN("The ringbuffer is empty") 24 | THEN("'read_char' should return -1") 25 | REQUIRE(ringbuffer.read_char() == -1); 26 | 27 | WHEN("The ringbuffer contains data") 28 | { 29 | ringbuffer.store_char('A'); 30 | ringbuffer.store_char('B'); 31 | THEN("'read_char' should return first inserted element first (FIFO)") 32 | { 33 | REQUIRE(ringbuffer.read_char() == 'A'); 34 | REQUIRE(ringbuffer.read_char() == 'B'); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /test/src/String/test_substring.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::substring(unsigned int, unsigned int)", "[String-substring-01]") 22 | { 23 | WHEN ("left higher than len") 24 | { 25 | arduino::String str("Hello"); 26 | str.substring(7,9); 27 | } 28 | 29 | WHEN ("right higher than len") 30 | { 31 | arduino::String str1("Hello"); 32 | arduino::String str2("ello"); 33 | REQUIRE(str2 == str1.substring(1,9)); 34 | } 35 | 36 | WHEN ("left higher than right") 37 | { 38 | arduino::String str1("Hello"); 39 | arduino::String str2("ello"); 40 | REQUIRE(str2 == str1.substring(9,1)); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /test/src/CanMsgRingbuffer/test_available.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("'available' should return 0 for empty CanMsg ring buffer", "[CanMsgRingbuffer-available-01]") 20 | { 21 | arduino::CanMsgRingbuffer ringbuffer; 22 | REQUIRE(ringbuffer.available() == 0); 23 | } 24 | 25 | TEST_CASE ("'available' should return number of elements in CanMsg ringbuffer", "[CanMsgRingbuffer-available-02]") 26 | { 27 | arduino::CanMsgRingbuffer ringbuffer; 28 | arduino::CanMsg msg; 29 | ringbuffer.enqueue(msg); 30 | REQUIRE(ringbuffer.available() == 1); 31 | ringbuffer.enqueue(msg); 32 | REQUIRE(ringbuffer.available() == 2); 33 | ringbuffer.dequeue(); 34 | REQUIRE(ringbuffer.available() == 1); 35 | } 36 | -------------------------------------------------------------------------------- /test/src/String/test_toFloat.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::toFloat when string is empty", "[String-toFloat-01]") 22 | { 23 | arduino::String str; 24 | float const val = str.toFloat(); 25 | REQUIRE(val == 0.0f); 26 | } 27 | 28 | TEST_CASE ("Testing String::toFloat when string contains no number", "[String-toFloat-02]") 29 | { 30 | arduino::String str("abc"); 31 | float const val = str.toFloat(); 32 | REQUIRE(val == 0.0f); 33 | } 34 | 35 | TEST_CASE ("Testing String::toFloat when string contains a number", "[String-toFloat-03]") 36 | { 37 | arduino::String str("-1.2345"); 38 | float const val = str.toFloat(); 39 | REQUIRE(val == -1.2345f); 40 | } 41 | -------------------------------------------------------------------------------- /test/src/StreamMock.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | /************************************************************************************** 14 | * PUBLIC MEMBER FUNCTIONS 15 | **************************************************************************************/ 16 | 17 | void StreamMock::operator << (char const * str) 18 | { 19 | for (size_t c = 0; c < strlen(str); c++) 20 | _stream.push_back(str[c]); 21 | } 22 | 23 | size_t StreamMock::write(uint8_t ch) 24 | { 25 | _stream.push_back(static_cast(ch)); 26 | return 1; 27 | } 28 | 29 | int StreamMock::available() 30 | { 31 | return _stream.size(); 32 | } 33 | 34 | int StreamMock::read() 35 | { 36 | if (available() == 0) 37 | return -1; 38 | 39 | /* Extract first/oldest element. */ 40 | char const c = _stream.at(0); 41 | /* Erase first/oldest element. */ 42 | _stream.pop_front(); 43 | 44 | return c; 45 | } 46 | 47 | int StreamMock::peek() 48 | { 49 | if (available() == 0) 50 | return -1; 51 | 52 | return _stream.at(0); 53 | } 54 | -------------------------------------------------------------------------------- /test/src/CanMsg/test_isExtendedId.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * NAMESPACE 17 | **************************************************************************************/ 18 | 19 | using namespace arduino; 20 | 21 | /************************************************************************************** 22 | * TEST CODE 23 | **************************************************************************************/ 24 | 25 | TEST_CASE ("\"isExtendedId\" should return true for extended CAN ID", "[CanMsg-isExtendedId-01]") 26 | { 27 | CanMsg const msg_ext_id(CanExtendedId(0x020), 0, nullptr); 28 | REQUIRE(msg_ext_id.isExtendedId() == true); 29 | } 30 | 31 | TEST_CASE ("\"isExtendedId\" should return false for standard CAN ID", "[CanMsg-isExtendedId-02]") 32 | { 33 | CanMsg const msg_std_id(CanStandardId(0x020), 0, nullptr); 34 | REQUIRE(msg_std_id.isExtendedId() == false); 35 | } 36 | -------------------------------------------------------------------------------- /test/src/CanMsg/test_isStandardId.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * NAMESPACE 17 | **************************************************************************************/ 18 | 19 | using namespace arduino; 20 | 21 | /************************************************************************************** 22 | * TEST CODE 23 | **************************************************************************************/ 24 | 25 | TEST_CASE ("\"isStandardId\" should return true for standard CAN ID", "[CanMsg-isStandardId-01]") 26 | { 27 | CanMsg const msg_std_id(CanStandardId(0x020), 0, nullptr); 28 | REQUIRE(msg_std_id.isStandardId() == true); 29 | } 30 | 31 | TEST_CASE ("\"isStandardId\" should return false for extended CAN ID", "[CanMsg-isStandardId-02]") 32 | { 33 | CanMsg const msg_ext_id(CanExtendedId(0x020), 0, nullptr); 34 | REQUIRE(msg_ext_id.isStandardId() == false); 35 | } 36 | -------------------------------------------------------------------------------- /test/src/Ringbuffer/test_isFull.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("'isFull' should return false for empty ring buffer", "[Ringbuffer-isFull-01]") 20 | { 21 | arduino::RingBufferN<2> ringbuffer; 22 | REQUIRE(ringbuffer.isFull() == false); 23 | } 24 | 25 | TEST_CASE ("'isFull' should return false for a partial full ring buffer", "[Ringbuffer-isFull-02]") 26 | { 27 | arduino::RingBufferN<2> ringbuffer; 28 | ringbuffer.store_char('A'); 29 | REQUIRE(ringbuffer.isFull() == false); 30 | } 31 | 32 | TEST_CASE ("'isFull' should return true for full ring buffer", "[Ringbuffer-isFull-03]") 33 | { 34 | arduino::RingBufferN<2> ringbuffer; 35 | ringbuffer.store_char('A'); 36 | ringbuffer.store_char('B'); 37 | REQUIRE(ringbuffer.isFull() == true); 38 | } 39 | -------------------------------------------------------------------------------- /test/src/CanMsg/test_CanMsg_CopyCtor.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * NAMESPACE 17 | **************************************************************************************/ 18 | 19 | using namespace arduino; 20 | 21 | /************************************************************************************** 22 | * TEST CODE 23 | **************************************************************************************/ 24 | 25 | TEST_CASE ("Test copy constructor", "[CanMsg-CopyCtor-01]") 26 | { 27 | uint8_t const msg_data[4] = {0xDE, 0xAD, 0xC0, 0xDE}; 28 | 29 | CanMsg const msg_1(CanStandardId(0x20), sizeof(msg_data), msg_data); 30 | CanMsg const msg_2(msg_1); 31 | 32 | REQUIRE(msg_1.data_length == msg_2.data_length); 33 | 34 | for (size_t i = 0; i < msg_1.data_length; i++) 35 | { 36 | REQUIRE(msg_1.data[i] == msg_data[i]); 37 | REQUIRE(msg_2.data[i] == msg_data[i]); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /test/src/IPAddress/test_toString.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | #include 15 | 16 | /************************************************************************************** 17 | * TEST CODE 18 | **************************************************************************************/ 19 | 20 | TEST_CASE ("Extract valid string from IPv4address", "[IPAddress-toString-01]") 21 | { 22 | arduino::IPAddress ip(129,168,1,2); 23 | 24 | REQUIRE(ip.toString().equals("129.168.1.2") == true); 25 | } 26 | 27 | TEST_CASE ("Extract valid ipv6 string from IPv6address", "[IPAddress-toString-02]") 28 | { 29 | arduino::IPAddress ip(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc); 30 | 31 | REQUIRE(ip.toString().equals("2001:0db8:0102:0304:0506:0708:090a:0b0c") == true); 32 | } 33 | 34 | TEST_CASE ("Extract 0.0.0.0 string from uninitialized IP address", "[IPAddress-toString-03]") 35 | { 36 | arduino::IPAddress ip; 37 | 38 | REQUIRE(ip.toString().equals("0.0.0.0") == true); 39 | } 40 | -------------------------------------------------------------------------------- /.github/workflows/unit-tests.yml: -------------------------------------------------------------------------------- 1 | name: Unit Tests 2 | 3 | on: 4 | pull_request: 5 | # Only run workflow if a file in these paths is modified 6 | paths: 7 | - ".github/workflows/unit-tests.yml" 8 | - "test/**" 9 | - "api/**" 10 | 11 | push: 12 | paths: 13 | - ".github/workflows/unit-tests.yml" 14 | - "test/**" 15 | - "api/**" 16 | 17 | jobs: 18 | test: 19 | name: Run unit tests 20 | runs-on: ubuntu-latest 21 | 22 | env: 23 | COVERAGE_DATA_PATH: extras/coverage-data/coverage.info 24 | 25 | steps: 26 | - name: Checkout repository 27 | uses: actions/checkout@v2 28 | 29 | # See: https://github.com/arduino/cpp-test-action/blob/main/README.md 30 | - uses: arduino/cpp-test-action@main 31 | with: 32 | source-path: test 33 | build-path: test/build 34 | runtime-path: test/build/bin/test-ArduinoCore-API 35 | coverage-exclude-paths: | 36 | - '*/test/*' 37 | - '/usr/*' 38 | coverage-data-path: ${{ env.COVERAGE_DATA_PATH }} 39 | 40 | # See: https://github.com/codecov/codecov-action/blob/master/README.md 41 | - name: Code coverage 42 | uses: codecov/codecov-action@v3 43 | with: 44 | token: ${{ secrets.CODECOV_TOKEN }} 45 | files: ${{ env.COVERAGE_DATA_PATH }} 46 | fail_ci_if_error: true 47 | -------------------------------------------------------------------------------- /test/src/Stream/test_readStringUntil.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | #include 15 | #include 16 | /************************************************************************************** 17 | * TEST CODE 18 | **************************************************************************************/ 19 | 20 | TEST_CASE ("Testing 'readStringUntil' with separator available within the stream", "[Stream-readStringUntil-01]") 21 | { 22 | StreamMock mock; 23 | mock.setTimeout(10); 24 | millis_autoOn(); 25 | mock << "This is test! lorem ipsum lalala"; 26 | 27 | REQUIRE(mock.readStringUntil('!') == arduino::String("This is test")); 28 | } 29 | 30 | TEST_CASE ("Testing 'readStringUntil' with separator not available within the stream", "[Stream-readStringUntil-02]") 31 | { 32 | StreamMock mock; 33 | mock.setTimeout(10); 34 | millis_autoOn(); 35 | mock << "This is test ... lorem ipsum lalala"; 36 | 37 | REQUIRE(mock.readStringUntil('!') == arduino::String("This is test ... lorem ipsum lalala")); 38 | } 39 | -------------------------------------------------------------------------------- /test/src/CanMsg/test_operator_assignment.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * NAMESPACE 17 | **************************************************************************************/ 18 | 19 | using namespace arduino; 20 | 21 | /************************************************************************************** 22 | * TEST CODE 23 | **************************************************************************************/ 24 | 25 | TEST_CASE ("Testing CanMsg::operator = (CanMsg const &)", "[CanMsg-Operator-=-1]") 26 | { 27 | uint8_t const msg_data[4] = {0xDE, 0xAD, 0xC0, 0xDE}; 28 | 29 | CanMsg const msg_1(CanStandardId(0x20), sizeof(msg_data), msg_data); 30 | CanMsg msg_2(CanStandardId(0x21), 0, nullptr); 31 | 32 | msg_2 = msg_1; 33 | 34 | REQUIRE(msg_1.data_length == msg_2.data_length); 35 | 36 | for (size_t i = 0; i < msg_1.data_length; i++) 37 | { 38 | REQUIRE(msg_1.data[i] == msg_data[i]); 39 | REQUIRE(msg_2.data[i] == msg_data[i]); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /test/src/String/test_trim.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::trim with space at the beginning", "[String-trim-01]") 22 | { 23 | arduino::String str(" hello"); 24 | str.trim(); 25 | REQUIRE(str == "hello"); 26 | } 27 | 28 | TEST_CASE ("Testing String::trim with space at the end", "[String-trim-02]") 29 | { 30 | arduino::String str("hello "); 31 | str.trim(); 32 | REQUIRE(str == "hello"); 33 | } 34 | 35 | TEST_CASE ("Testing String::trim with space at both beginning and end", "[String-trim-03]") 36 | { 37 | arduino::String str(" hello "); 38 | str.trim(); 39 | REQUIRE(str == "hello"); 40 | } 41 | 42 | TEST_CASE ("Testing String::trim with space in the middle", "[String-trim-04]") 43 | { 44 | arduino::String str("Hello Arduino!"); 45 | str.trim(); 46 | REQUIRE(str == "Hello Arduino!"); 47 | } 48 | -------------------------------------------------------------------------------- /api/itoa.h: -------------------------------------------------------------------------------- 1 | /* 2 | itoa.h - Integer to ASCII conversion 3 | Copyright (c) 2016 Arduino LLC. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #pragma once 21 | 22 | // Standard C functions required in Arduino API 23 | // If these functions are not provided by the standard library, the 24 | // core should supply an implementation of them. 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | extern char* itoa(int value, char *string, int radix); 31 | extern char* ltoa(long value, char *string, int radix); 32 | extern char* utoa(unsigned value, char *string, int radix); 33 | extern char* ultoa(unsigned long value, char *string, int radix); 34 | 35 | #ifdef __cplusplus 36 | } // extern "C" 37 | #endif 38 | 39 | -------------------------------------------------------------------------------- /api/Printable.h: -------------------------------------------------------------------------------- 1 | /* 2 | Printable.h - Interface for classes that can be printed via Print 3 | Copyright (c) 2016 Arduino LLC. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #pragma once 21 | 22 | #include 23 | 24 | namespace arduino { 25 | 26 | class Print; 27 | 28 | /** The Printable class provides a way for new classes to allow themselves to be printed. 29 | By deriving from Printable and implementing the printTo method, it will then be possible 30 | for users to print out instances of this class by passing them into the usual 31 | Print::print and Print::println methods. 32 | */ 33 | 34 | class Printable 35 | { 36 | public: 37 | virtual size_t printTo(Print& p) const = 0; 38 | }; 39 | 40 | } -------------------------------------------------------------------------------- /test/src/WCharacter/test_isDigit.cpp: -------------------------------------------------------------------------------- 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 12 | #include 13 | 14 | #include 15 | 16 | #include 17 | 18 | /************************************************************************************** 19 | * CONSTANTS 20 | **************************************************************************************/ 21 | 22 | std::vector const VALID_DIGIT_VECT = {'0','1','2','3','4','5','6','7','8','9'}; 23 | 24 | /************************************************************************************** 25 | * TEST CODE 26 | **************************************************************************************/ 27 | 28 | TEST_CASE ("isDigit(...) is called with valid digits", "[isDigit-01]") 29 | { 30 | std::for_each(std::begin(VALID_DIGIT_VECT), 31 | std::end (VALID_DIGIT_VECT), 32 | [](char const c) 33 | { 34 | REQUIRE(arduino::isDigit(c) == true); 35 | }); 36 | } 37 | 38 | TEST_CASE ("isDigit(...) is called with non digit", "[isDigit-02]") 39 | { 40 | REQUIRE(arduino::isDigit('z') == false); 41 | } 42 | -------------------------------------------------------------------------------- /test/src/String/test_toInt.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::toInt when string is empty", "[String-toInt-01]") 22 | { 23 | arduino::String str; 24 | int const val = str.toInt(); 25 | REQUIRE(val == 0); 26 | } 27 | 28 | TEST_CASE ("Testing String::toInt when string contains no number", "[String-toInt-02]") 29 | { 30 | arduino::String str("abc"); 31 | int const val = str.toInt(); 32 | REQUIRE(val == 0); 33 | } 34 | 35 | TEST_CASE ("Testing String::toInt when string contains a number", "[String-toInt-03]") 36 | { 37 | arduino::String str("-1"); 38 | int const val = str.toInt(); 39 | REQUIRE(val == -1); 40 | } 41 | 42 | TEST_CASE ("Testing String::toInt when string does not have a buffer", "[String-toInt-04]") 43 | { 44 | char *buffer = NULL; 45 | arduino::String str(buffer); 46 | REQUIRE(str.toInt() == 0); 47 | } 48 | -------------------------------------------------------------------------------- /test/src/WCharacter/test_isSpace.cpp: -------------------------------------------------------------------------------- 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 12 | #include 13 | 14 | #include 15 | 16 | #include 17 | 18 | /************************************************************************************** 19 | * CONSTANTS 20 | **************************************************************************************/ 21 | 22 | std::vector const VALID_SPACE_VECT = {' ', '\t', '\n', '\v', '\f', '\r'}; 23 | 24 | /************************************************************************************** 25 | * TEST CODE 26 | **************************************************************************************/ 27 | 28 | TEST_CASE ("isSpace(...) is called with a valid white space character", "[isSpace-01]") 29 | { 30 | std::for_each(std::begin(VALID_SPACE_VECT), 31 | std::end (VALID_SPACE_VECT), 32 | [](char const c) 33 | { 34 | REQUIRE(arduino::isSpace(c) == true); 35 | }); 36 | } 37 | 38 | TEST_CASE ("isSpace(...) is called with a invalid white space character", "[isSpace-02]") 39 | { 40 | REQUIRE(arduino::isSpace('{') == false); 41 | } 42 | -------------------------------------------------------------------------------- /test/src/String/test_toDouble.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::toDouble when string is empty", "[String-toDouble-01]") 22 | { 23 | arduino::String str; 24 | double const val = str.toDouble(); 25 | REQUIRE(val == 0.0); 26 | } 27 | 28 | TEST_CASE ("Testing String::toDouble when string contains no number", "[String-toDouble-02]") 29 | { 30 | arduino::String str("abc"); 31 | double const val = str.toDouble(); 32 | REQUIRE(val == 0.0); 33 | } 34 | 35 | TEST_CASE ("Testing String::toDouble when string contains a number", "[String-toDouble-03]") 36 | { 37 | arduino::String str("-1.2345"); 38 | double const val = str.toDouble(); 39 | REQUIRE(val == -1.2345); 40 | } 41 | 42 | TEST_CASE ("Testing String::toDouble when string does not have a buffer", "[String-toDouble-04]") 43 | { 44 | char *buffer = NULL; 45 | arduino::String str(buffer); 46 | REQUIRE(str.toDouble() == 0); 47 | } 48 | -------------------------------------------------------------------------------- /test/src/WCharacter/test_isPunct.cpp: -------------------------------------------------------------------------------- 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 12 | #include 13 | 14 | #include 15 | 16 | #include 17 | 18 | /************************************************************************************** 19 | * CONSTANTS 20 | **************************************************************************************/ 21 | 22 | std::vector const VALID_PUNCT_VECT = {'!','"','#','$','%','&','\'','(',')','*','+',',','-','.','/',':',';','<','=','>','?','@','[','\\',']','^','_','`','{','|','}','~'}; 23 | 24 | /************************************************************************************** 25 | * TEST CODE 26 | **************************************************************************************/ 27 | 28 | TEST_CASE ("isPunct('.') is called with a valid punct character", "[isPunct-01]") 29 | { 30 | std::for_each(std::begin(VALID_PUNCT_VECT), 31 | std::end (VALID_PUNCT_VECT), 32 | [](char const c) 33 | { 34 | REQUIRE(arduino::isPunct(c) == true); 35 | }); 36 | } 37 | 38 | TEST_CASE ("isPunct(...) is called with a invalid punct character", "[isPunct-02]") 39 | { 40 | REQUIRE(arduino::isPunct('a') == false); 41 | } 42 | -------------------------------------------------------------------------------- /test/src/CanMsg/test_printTo.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | #include 15 | 16 | /************************************************************************************** 17 | * NAMESPACE 18 | **************************************************************************************/ 19 | 20 | using namespace arduino; 21 | 22 | /************************************************************************************** 23 | * TEST CODE 24 | **************************************************************************************/ 25 | 26 | TEST_CASE ("Print CAN frame with standard ID", "[CanMsg-printTo-1]") 27 | { 28 | uint8_t const std_msg_data[] = {0xBE, 0xEF}; 29 | CanMsg const std_msg(CanStandardId(0x20), sizeof(std_msg_data), std_msg_data); 30 | 31 | PrintMock mock; 32 | mock.print(std_msg); 33 | 34 | REQUIRE(mock._str == "[020] (2) : BEEF"); 35 | } 36 | 37 | TEST_CASE ("Print CAN frame with extended ID", "[CanMsg-printTo-2]") 38 | { 39 | uint8_t const ext_msg_data[] = {0xDE, 0xAD, 0xC0, 0xDE}; 40 | CanMsg const ext_msg(CanExtendedId(0x20), sizeof(ext_msg_data), ext_msg_data); 41 | 42 | PrintMock mock; 43 | mock.print(ext_msg); 44 | 45 | REQUIRE(mock._str == "[00000020] (4) : DEADC0DE"); 46 | } 47 | -------------------------------------------------------------------------------- /test/src/WCharacter/test_isControl.cpp: -------------------------------------------------------------------------------- 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 12 | #include 13 | 14 | #include 15 | 16 | #include 17 | 18 | /************************************************************************************** 19 | * CONSTANTS 20 | **************************************************************************************/ 21 | 22 | std::vector const VALID_CONTROL_VECT = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,'\t','\f','\v','\n','\r',0x0E,0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x7F}; 23 | 24 | /************************************************************************************** 25 | * TEST CODE 26 | **************************************************************************************/ 27 | 28 | TEST_CASE ("isControl(...) is called with control char", "[isControl-01]") 29 | { 30 | std::for_each(std::begin(VALID_CONTROL_VECT), 31 | std::end (VALID_CONTROL_VECT), 32 | [](char const c) 33 | { 34 | REQUIRE(arduino::isControl(c) == true); 35 | }); 36 | } 37 | 38 | TEST_CASE ("isControl(...) is called with non control char", "[isControl-02]") 39 | { 40 | REQUIRE(arduino::isControl('z') == false); 41 | } 42 | -------------------------------------------------------------------------------- /test/src/CanMsg/test_CanStandardId.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * NAMESPACE 17 | **************************************************************************************/ 18 | 19 | using namespace arduino; 20 | 21 | /************************************************************************************** 22 | * TEST CODE 23 | **************************************************************************************/ 24 | 25 | TEST_CASE ("Verify correct conversion to 11-Bit CAN ID for lowest valid 11-Bit CAN ID", "[CanMsg-CanStandardId-01]") 26 | { 27 | REQUIRE(CanStandardId(0) == 0U); 28 | } 29 | 30 | TEST_CASE ("Verify correct conversion to 11-Bit CAN ID for highest valid 11-Bit CAN ID", "[CanMsg-CanStandardId-02]") 31 | { 32 | REQUIRE(CanStandardId(0x7FF) == 0x7FF); 33 | } 34 | 35 | TEST_CASE ("Verify capping of CAN IDs exceeding 11-Bit CAN ID range", "[CanMsg-CanStandardId-03]") 36 | { 37 | REQUIRE(CanStandardId(0x800U) == 0x000U); 38 | REQUIRE(CanStandardId(0x801U) == 0x001U); 39 | REQUIRE(CanStandardId(0x8FFU) == 0x0FFU); 40 | REQUIRE(CanStandardId(0x1FFFFFFFU) == 0x7FFU); 41 | REQUIRE(CanStandardId(0xFFFFFFFFU) == 0x7FFU); 42 | } 43 | -------------------------------------------------------------------------------- /api/deprecated-avr-comp/avr/dtostrf.c.impl: -------------------------------------------------------------------------------- 1 | /* 2 | dtostrf - Emulation for dtostrf function from avr-libc 3 | Copyright (c) 2016 Arduino LLC. All rights reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | // This is a default implementation for dtostrf function. 21 | // This file should be used if the standard lib doesn't provide an 22 | // implementation of dtostrf. 23 | 24 | // Create a file called "dtostrf.c" with the following include: 25 | // #include "api/deprecated-avr-comp/avr/dtostrf.c.impl" 26 | 27 | #include 28 | 29 | char *dtostrf (double val, signed char width, unsigned char prec, char *sout) { 30 | asm(".global _printf_float"); 31 | 32 | #pragma GCC diagnostic push 33 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 34 | char fmt[20]; 35 | sprintf(fmt, "%%%d.%df", width, prec); 36 | sprintf(sout, fmt, val); 37 | return sout; 38 | #pragma GCC diagnostic pop 39 | } 40 | 41 | -------------------------------------------------------------------------------- /test/src/WCharacter/test_isHexadecimalDigit.cpp: -------------------------------------------------------------------------------- 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 12 | #include 13 | 14 | #include 15 | 16 | #include 17 | 18 | /************************************************************************************** 19 | * CONSTANTS 20 | **************************************************************************************/ 21 | 22 | std::vector const VALID_HEXADECIMAL_NUMBER_VECT = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','A','B','C','D','E','F'}; 23 | 24 | /************************************************************************************** 25 | * TEST CODE 26 | **************************************************************************************/ 27 | 28 | TEST_CASE ("isHexadecimalDigit(...) is called with valid hexadecimal numbers", "[isHexadecimalDigit-01]") 29 | { 30 | std::for_each(std::begin(VALID_HEXADECIMAL_NUMBER_VECT), 31 | std::end (VALID_HEXADECIMAL_NUMBER_VECT), 32 | [](char const c) 33 | { 34 | REQUIRE(arduino::isHexadecimalDigit(c) == true); 35 | }); 36 | } 37 | 38 | TEST_CASE ("isHexadecimalDigit(...) is called with non hexadecimal number", "[isHexadecimalDigit-02]") 39 | { 40 | REQUIRE(arduino::isHexadecimalDigit('z') == false); 41 | } 42 | -------------------------------------------------------------------------------- /api/Client.h: -------------------------------------------------------------------------------- 1 | /* 2 | Client.h - Base class that provides Client 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #pragma once 21 | 22 | #include "Stream.h" 23 | #include "IPAddress.h" 24 | 25 | namespace arduino { 26 | 27 | class Client : public Stream { 28 | 29 | public: 30 | virtual int connect(IPAddress ip, uint16_t port) =0; 31 | virtual int connect(const char *host, uint16_t port) =0; 32 | virtual size_t write(uint8_t) =0; 33 | virtual size_t write(const uint8_t *buf, size_t size) =0; 34 | virtual int available() = 0; 35 | virtual int read() = 0; 36 | virtual int read(uint8_t *buf, size_t size) = 0; 37 | virtual int peek() = 0; 38 | virtual void flush() = 0; 39 | virtual void stop() = 0; 40 | virtual uint8_t connected() = 0; 41 | virtual operator bool() = 0; 42 | protected: 43 | uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; 44 | }; 45 | 46 | } -------------------------------------------------------------------------------- /test/src/Stream/test_readBytes.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Testing readBytes(char *buffer, size_t length)", "[Stream-readBytes-01]") 20 | { 21 | StreamMock mock; 22 | 23 | WHEN ("the stream is empty") 24 | { 25 | char buf[32] = {0}; 26 | 27 | REQUIRE(mock.readBytes(buf, sizeof(buf)) == 0); 28 | } 29 | 30 | WHEN ("the stream contains less data than we want to read") 31 | { 32 | char buf[32] = {0}; 33 | char const str[] = "some stream content"; 34 | mock << str; 35 | 36 | REQUIRE(mock.readBytes(buf, sizeof(buf)) == strlen(str)); 37 | REQUIRE(strncmp(buf, str, sizeof(buf)) == 0); 38 | REQUIRE(mock.readString() == arduino::String("")); 39 | } 40 | 41 | WHEN ("the stream contains more data than we want to read") 42 | { 43 | char buf[5] = {0}; 44 | mock << "some stream content"; 45 | char const EXPECTED_STR[] = "some "; 46 | 47 | REQUIRE(mock.readBytes(buf, sizeof(buf)) == 5); 48 | REQUIRE(strncmp(buf, EXPECTED_STR, sizeof(buf)) == 0); 49 | REQUIRE(mock.readString() == arduino::String("stream content")); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /test/src/String/test_move.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Arduino. All rights reserved. 3 | * 4 | * SPDX-License-Identifier: LGPL-2.1-or-later 5 | */ 6 | 7 | #include 8 | 9 | #include 10 | 11 | #include "StringPrinter.h" 12 | 13 | #include 14 | 15 | TEST_CASE("Testing String move constructor", "[String-move-01]") 16 | { 17 | arduino::String a("src"); 18 | char const* const a_str = a.c_str(); 19 | arduino::String b(std::move(a)); 20 | REQUIRE(a.length() == 0); 21 | REQUIRE(a.c_str() == nullptr); 22 | REQUIRE(b.c_str() == a_str); 23 | REQUIRE(b.length() == 3); 24 | } 25 | 26 | TEST_CASE("Testing String move assignment", "[String-move-02]") 27 | { 28 | arduino::String a("src"); 29 | char const* const a_str = a.c_str(); 30 | arduino::String b; 31 | b = std::move(a); 32 | REQUIRE(a.length() == 0); 33 | REQUIRE(a.c_str() == nullptr); 34 | REQUIRE(b == arduino::String("src")); 35 | REQUIRE(b.c_str() == a_str); 36 | } 37 | 38 | TEST_CASE("Testing String move self assignment", "[String-move-03]") 39 | { 40 | #if (defined(GCC_VERSION) && GCC_VERSION >= 13) || (defined(__clang_major__) && __clang_major__ >= 14) \ 41 | || (defined(__GNUC__) && __GNUC__ >= 13) 42 | #pragma GCC diagnostic push 43 | #pragma GCC diagnostic ignored "-Wself-move" 44 | #endif 45 | arduino::String a("src"); 46 | a = std::move(a); 47 | REQUIRE(a == "src"); 48 | #if defined(GCC_VERSION) && GCC_VERSION >= 13 || (defined(__clang_major__) && __clang_major__ >= 14) \ 49 | || (defined(__GNUC__) && __GNUC__ >= 13) 50 | #pragma GCC diagnostic pop 51 | #endif 52 | } 53 | -------------------------------------------------------------------------------- /api/ArduinoAPI.h: -------------------------------------------------------------------------------- 1 | /* 2 | Arduino API main include 3 | Copyright (c) 2016 Arduino LLC. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef ARDUINO_API_H 21 | #define ARDUINO_API_H 22 | 23 | // version 1.5.2 24 | #define ARDUINO_API_VERSION 10502 25 | 26 | #include "Binary.h" 27 | 28 | #ifdef __cplusplus 29 | #include "Interrupts.h" 30 | #include "IPAddress.h" 31 | #include "Print.h" 32 | #include "Printable.h" 33 | #include "PluggableUSB.h" 34 | #include "Server.h" 35 | #include "String.h" 36 | #include "Stream.h" 37 | #include "Udp.h" 38 | #include "USBAPI.h" 39 | #include "WCharacter.h" 40 | #endif 41 | 42 | /* Standard C library includes */ 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | 49 | // Misc Arduino core functions 50 | #include "Common.h" 51 | 52 | #ifdef __cplusplus 53 | // Compatibility layer for older code 54 | #include "Compat.h" 55 | #endif 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /api/HardwareI2C.h: -------------------------------------------------------------------------------- 1 | /* 2 | HardwareI2C.h - Hardware I2C interface for Arduino 3 | Copyright (c) 2016 Arduino LLC. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #pragma once 21 | 22 | #include 23 | #include "Stream.h" 24 | 25 | namespace arduino { 26 | 27 | class HardwareI2C : public Stream 28 | { 29 | public: 30 | virtual void begin() = 0; 31 | virtual void begin(uint8_t address) = 0; 32 | virtual void end() = 0; 33 | 34 | virtual void setClock(uint32_t freq) = 0; 35 | 36 | virtual void beginTransmission(uint8_t address) = 0; 37 | virtual uint8_t endTransmission(bool stopBit) = 0; 38 | virtual uint8_t endTransmission(void) = 0; 39 | 40 | virtual size_t requestFrom(uint8_t address, size_t len, bool stopBit) = 0; 41 | virtual size_t requestFrom(uint8_t address, size_t len) = 0; 42 | 43 | virtual void onReceive(void(*)(int)) = 0; 44 | virtual void onRequest(void(*)(void)) = 0; 45 | }; 46 | 47 | } 48 | 49 | -------------------------------------------------------------------------------- /test/src/IPAddress/test_IPAddress.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Testing IPAddress() default constructor()", "[IPAddress-Ctor-01]") 20 | { 21 | arduino::IPAddress ip; 22 | REQUIRE(ip[0] == 0); 23 | REQUIRE(ip[1] == 0); 24 | REQUIRE(ip[2] == 0); 25 | REQUIRE(ip[3] == 0); 26 | } 27 | 28 | TEST_CASE ("Testing IPAddress(o,o,o,o) constructor", "[IPAddress-Ctor-02]") 29 | { 30 | arduino::IPAddress ip(129,168,1,2); 31 | REQUIRE(ip[0] == 129); 32 | REQUIRE(ip[1] == 168); 33 | REQUIRE(ip[2] == 1); 34 | REQUIRE(ip[3] == 2); 35 | } 36 | 37 | TEST_CASE ("Testing IPAddress(a) constructor", "[IPAddress-Ctor-03]") 38 | { 39 | arduino::IPAddress ip(129 | (168 << 8) | (1 << 16) | (2 << 24)); 40 | REQUIRE(ip[0] == 129); 41 | REQUIRE(ip[1] == 168); 42 | REQUIRE(ip[2] == 1); 43 | REQUIRE(ip[3] == 2); 44 | } 45 | 46 | TEST_CASE ("Testing IPAddress(a *) constructor", "[IPAddress-Ctor-04]") 47 | { 48 | uint8_t const ip_addr_array[] = {129,168,1,2}; 49 | 50 | arduino::IPAddress ip(ip_addr_array); 51 | REQUIRE(ip[0] == 129); 52 | REQUIRE(ip[1] == 168); 53 | REQUIRE(ip[2] == 1); 54 | REQUIRE(ip[3] == 2); 55 | } 56 | -------------------------------------------------------------------------------- /test/src/Common/test_map.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Testing 'map(val, fromLow, fromHigh, toLow, toHigh)' usage with reduction of output", "[map-01]") 20 | { 21 | REQUIRE(map(50, 0, 100, 0, 10) == 5); 22 | } 23 | 24 | TEST_CASE ("Testing 'map(val, fromLow, fromHigh, toLow, toHigh)' usage with increase of output", "[map-02]") 25 | { 26 | REQUIRE(map(5, 0, 10, 0, 100) == 50); 27 | } 28 | 29 | TEST_CASE ("Testing 'map(val, fromLow, fromHigh, toLow, toHigh)' where output is reduced to a value < 0", "[map-03]") 30 | { 31 | REQUIRE(map(1, 0, 100, 0, 10) == 0); /* Would be 0.1 if we'd be using floating point. */ 32 | } 33 | 34 | TEST_CASE ("Testing 'map(val, fromLow, fromHigh, toLow, toHigh)' default usage with negative values", "[map-04]") 35 | { 36 | WHEN ("Negative 'from' values") 37 | { 38 | REQUIRE(map(0, 0, -256, 0, 1024) == 0); 39 | REQUIRE(map(-256, 0, -256, 0, 1024) == 1024); 40 | REQUIRE(map(-128, 0, -256, 0, 1024) == 512); 41 | } 42 | WHEN ("Negative 'to' values") 43 | { 44 | REQUIRE(map(0, 0, 256, 0, -1024) == 0); 45 | REQUIRE(map(256, 0, 256, 0, -1024) == -1024); 46 | REQUIRE(map(128, 0, 256, 0, -1024) == -512); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /test/src/Stream/test_readBytesUntil.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Testing readBytesUntil(char terminator, char *buffer, size_t length)", "[Stream-readBytesUntil-01]") 20 | { 21 | StreamMock mock; 22 | 23 | WHEN ("the stream is empty") 24 | { 25 | char buf[32] = {0}; 26 | 27 | REQUIRE(mock.readBytesUntil(' ', buf, sizeof(buf)) == 0); 28 | } 29 | 30 | WHEN ("the stream contains the termination character") 31 | { 32 | char buf[32] = {0}; 33 | char const str[] = "some stream content"; 34 | char const EXPECTED_STR[] = "some"; 35 | mock << str; 36 | 37 | REQUIRE(mock.readBytesUntil(' ', buf, sizeof(buf)) == strlen("some")); 38 | REQUIRE(strncmp(buf, EXPECTED_STR, sizeof(buf)) == 0); 39 | REQUIRE(mock.readString() == arduino::String("stream content")); 40 | } 41 | 42 | WHEN ("the stream does not contain the termination character") 43 | { 44 | char buf[32] = {0}; 45 | char const STR[] = "some stream content"; 46 | mock << STR; 47 | 48 | REQUIRE(mock.readBytesUntil('!', buf, sizeof(buf)) == strlen(STR)); 49 | REQUIRE(strncmp(buf, STR, sizeof(buf)) == 0); 50 | REQUIRE(mock.readString() == arduino::String("")); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /test/src/WCharacter/test_toAscii.cpp: -------------------------------------------------------------------------------- 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 12 | #include 13 | 14 | #include 15 | 16 | #include 17 | 18 | /************************************************************************************** 19 | * CONSTANTS 20 | **************************************************************************************/ 21 | 22 | std::vector const VALID_ASCII_VECT = {' ', 'a', 'b', 'q', '\n', '\r'}; 23 | 24 | /************************************************************************************** 25 | * TEST CODE 26 | **************************************************************************************/ 27 | 28 | TEST_CASE ("toAscii(...) is called with a valid ascii character", "[toAscii-01]") 29 | { 30 | std::for_each(std::begin(VALID_ASCII_VECT), 31 | std::end (VALID_ASCII_VECT), 32 | [](char const c) 33 | { 34 | REQUIRE(arduino::toAscii(c) == c); 35 | }); 36 | } 37 | 38 | TEST_CASE ("toAscii(...) is called with a invalid ascii character", "[toAscii-02]") 39 | { 40 | REQUIRE(arduino::toAscii(0xf7) == 0x77); 41 | } 42 | 43 | TEST_CASE ("toAscii(...) is called with a invalid casted ascii character", "[toAscii-03]") 44 | { 45 | REQUIRE(arduino::toAscii((unsigned char)0xf7) == 0x77); 46 | } 47 | 48 | TEST_CASE ("toAscii(...) is called with a character larger than 1 byte", "[toAscii-04]") 49 | { 50 | REQUIRE(arduino::toAscii(0x3030) == 0x30); 51 | } 52 | -------------------------------------------------------------------------------- /test/src/Stream/test_findUntil.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Testing findUntil(const char *target, const char *terminator)", "[Stream-findUntil-01]") 20 | { 21 | StreamMock mock; 22 | 23 | WHEN ("'target' is contained in stream") 24 | { 25 | WHEN ("'terminator' appears before 'target'") 26 | { 27 | mock << "This is a : test string"; 28 | REQUIRE(mock.findUntil("test", ": ") == false); 29 | REQUIRE(mock.readString() == arduino::String("test string")); 30 | } 31 | WHEN ("'terminator' appears after 'target'") 32 | { 33 | mock << "This is a test : string"; 34 | REQUIRE(mock.findUntil("test", ": ") == true); 35 | REQUIRE(mock.readString() == arduino::String(" : string")); 36 | } 37 | WHEN ("'terminator' is not included in the string at all") 38 | { 39 | mock << "This is a test string"; 40 | REQUIRE(mock.findUntil("test", ": ") == true); 41 | REQUIRE(mock.readString() == arduino::String(" string")); 42 | } 43 | } 44 | 45 | WHEN ("'target' is not contained in stream") 46 | { 47 | mock << "This is a test string"; 48 | REQUIRE(mock.findUntil("abc", "def") == false); 49 | REQUIRE(mock.readString() == arduino::String("")); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /test/src/WCharacter/test_isAscii.cpp: -------------------------------------------------------------------------------- 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 12 | #include 13 | 14 | #include 15 | 16 | #include 17 | 18 | /************************************************************************************** 19 | * CONSTANTS 20 | **************************************************************************************/ 21 | 22 | std::vector const VALID_ASCII_VECT = {' ', 'a', 'b', 'q', '\n', '\r'}; 23 | 24 | /************************************************************************************** 25 | * TEST CODE 26 | **************************************************************************************/ 27 | 28 | TEST_CASE ("isAscii(...) is called with a valid ascii character", "[isAscii-01]") 29 | { 30 | std::for_each(std::begin(VALID_ASCII_VECT), 31 | std::end (VALID_ASCII_VECT), 32 | [](char const c) 33 | { 34 | REQUIRE(arduino::isAscii(c) == true); 35 | }); 36 | } 37 | 38 | TEST_CASE ("isAscii(...) is called with a invalid ascii character", "[isAscii-02]") 39 | { 40 | REQUIRE(arduino::isAscii(0xf7) == false); 41 | } 42 | 43 | TEST_CASE ("isAscii(...) is called with a invalid casted ascii character", "[isAscii-03]") 44 | { 45 | REQUIRE(arduino::isAscii((unsigned char)0xf7) == false); 46 | } 47 | 48 | TEST_CASE ("isAscii(...) is called with a character latger than 1 byte", "[isAscii-04]") 49 | { 50 | REQUIRE(arduino::isAscii(0x3030) == false); 51 | } 52 | 53 | -------------------------------------------------------------------------------- /test/src/CanMsg/test_CanExtendedId.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * NAMESPACE 17 | **************************************************************************************/ 18 | 19 | using namespace arduino; 20 | 21 | /************************************************************************************** 22 | * TEST CODE 23 | **************************************************************************************/ 24 | 25 | TEST_CASE ("Verify correct conversion to 29-Bit CAN ID for lowest valid 29-Bit CAN ID", "[CanMsg-CanExtendedId-01]") 26 | { 27 | REQUIRE(CanExtendedId(0) == (CanMsg::CAN_EFF_FLAG | 0U)); 28 | } 29 | 30 | TEST_CASE ("Verify correct conversion to 29-Bit CAN ID for highest valid 29-Bit CAN ID", "[CanMsg-CanExtendedId-02]") 31 | { 32 | REQUIRE(CanExtendedId(0x1FFFFFFFU) == (CanMsg::CAN_EFF_FLAG | 0x1FFFFFFFU)); 33 | } 34 | 35 | TEST_CASE ("Verify capping of CAN IDs exceeding 29-Bit CAN ID range", "[CanMsg-CanExtendedId-03]") 36 | { 37 | REQUIRE(CanExtendedId(0x2FFFFFFFU) == (CanMsg::CAN_EFF_FLAG | 0x0FFFFFFFU)); 38 | REQUIRE(CanExtendedId(0x3FFFFFFFU) == (CanMsg::CAN_EFF_FLAG | 0x1FFFFFFFU)); 39 | REQUIRE(CanExtendedId(0x4FFFFFFFU) == (CanMsg::CAN_EFF_FLAG | 0x0FFFFFFFU)); 40 | REQUIRE(CanExtendedId(0x5FFFFFFFU) == (CanMsg::CAN_EFF_FLAG | 0x1FFFFFFFU)); 41 | /* ... */ 42 | REQUIRE(CanExtendedId(0xFFFFFFFFU) == (CanMsg::CAN_EFF_FLAG | 0x1FFFFFFFU)); 43 | } 44 | -------------------------------------------------------------------------------- /api/CanMsg.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | CanMsg.cpp - Library for CAN message handling 3 | Copyright (c) 2023 Arduino. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | /************************************************************************************** 21 | * INCLUDE 22 | **************************************************************************************/ 23 | 24 | #include "CanMsg.h" 25 | 26 | /************************************************************************************** 27 | * NAMESPACE 28 | **************************************************************************************/ 29 | 30 | namespace arduino 31 | { 32 | 33 | /************************************************************************************** 34 | * STATIC CONST DEFINITION 35 | **************************************************************************************/ 36 | 37 | uint8_t const CanMsg::MAX_DATA_LENGTH; 38 | uint32_t const CanMsg::CAN_EFF_FLAG; 39 | uint32_t const CanMsg::CAN_SFF_MASK; 40 | uint32_t const CanMsg::CAN_EFF_MASK; 41 | 42 | /************************************************************************************** 43 | * NAMESPACE 44 | **************************************************************************************/ 45 | 46 | } /* arduino */ 47 | -------------------------------------------------------------------------------- /test/src/itoa.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | #include 15 | 16 | #include 17 | 18 | /************************************************************************************** 19 | * FUNCTION IMPLEMENTATION 20 | **************************************************************************************/ 21 | 22 | std::string radixToFmtString(int const radix) 23 | { 24 | if (radix == 8) return std::string("%o"); 25 | else if (radix == 10) return std::string("%d"); 26 | else if (radix == 16) return std::string("%X"); 27 | else throw std::runtime_error("Invalid radix."); 28 | } 29 | 30 | char * itoa(int value, char * str, int radix) 31 | { 32 | #pragma GCC diagnostic push 33 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 34 | sprintf(str, radixToFmtString(radix).c_str(), value); 35 | #pragma GCC diagnostic pop 36 | return str; 37 | } 38 | 39 | char * ltoa(long value, char * str, int radix) 40 | { 41 | #pragma GCC diagnostic push 42 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 43 | sprintf(str, radixToFmtString(radix).c_str(), value); 44 | #pragma GCC diagnostic pop 45 | return str; 46 | } 47 | 48 | char * utoa(unsigned value, char *str, int radix) 49 | { 50 | #pragma GCC diagnostic push 51 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 52 | sprintf(str, radixToFmtString(radix).c_str(), value); 53 | #pragma GCC diagnostic pop 54 | return str; 55 | } 56 | 57 | char * ultoa(unsigned long value, char * str, int radix) 58 | { 59 | #pragma GCC diagnostic push 60 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 61 | sprintf(str, radixToFmtString(radix).c_str(), value); 62 | #pragma GCC diagnostic pop 63 | return str; 64 | } 65 | -------------------------------------------------------------------------------- /test/src/MillisFake.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TYPEDEF 17 | **************************************************************************************/ 18 | 19 | enum class MillisFakeMode 20 | { 21 | Auto, Manual 22 | }; 23 | 24 | /************************************************************************************** 25 | * GLOBAL VARIABLES 26 | **************************************************************************************/ 27 | 28 | static unsigned long millis_val = 0; 29 | static MillisFakeMode millis_fake_mode = MillisFakeMode::Auto; 30 | static std::chrono::steady_clock::time_point const millis_begin = std::chrono::steady_clock::now(); 31 | 32 | /************************************************************************************** 33 | * FUNCTION DEFINITION 34 | **************************************************************************************/ 35 | 36 | void millis_autoOn() 37 | { 38 | millis_fake_mode = MillisFakeMode::Auto; 39 | } 40 | 41 | void millis_autoOff() 42 | { 43 | millis_fake_mode = MillisFakeMode::Manual; 44 | } 45 | 46 | void set_millis(unsigned long const val) 47 | { 48 | millis_autoOff(); 49 | millis_val = val; 50 | } 51 | 52 | unsigned long millis() 53 | { 54 | if (millis_fake_mode == MillisFakeMode::Manual) 55 | return millis_val; 56 | 57 | if (millis_fake_mode == MillisFakeMode::Auto) 58 | { 59 | std::chrono::steady_clock::time_point millis_now = std::chrono::steady_clock::now(); 60 | auto millis = std::chrono::duration_cast(millis_now - millis_begin).count(); 61 | return static_cast(millis); 62 | } 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /test/src/IPAddress/test_operator_parentheses6.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | // These comparisons should always return false, as you can't compare an IPv6 to an int32_t 20 | 21 | TEST_CASE ("Testing implicit cast of IPv6 compatible (little endian) to uint32_t always false", "[IPAddress6-Operator-()-01]") 22 | { 23 | // On little endian systems, considering only last four octets (ignoring the rest) 24 | arduino::IPAddress ip(0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 129,168, 1,2); 25 | uint32_t const val_expected = ip; 26 | uint32_t const val_actual = (129 | (168 << 8) | (1 << 16) | (2 << 24)); 27 | REQUIRE((val_expected == val_actual) == false); 28 | } 29 | 30 | TEST_CASE ("Testing implicit cast of IPv6 full little endian to uint32_t always false", "[IPAddress6-Operator-()-01]") 31 | { 32 | // On little endian systems (full value) 33 | arduino::IPAddress ip(129,168, 1,2, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0); 34 | uint32_t const val_expected = ip; 35 | uint32_t const val_actual = (129 | (168 << 8) | (1 << 16) | (2 << 24)); 36 | REQUIRE((val_expected == val_actual) == false); 37 | } 38 | 39 | TEST_CASE ("Testing implicit cast of IPv6 to uint32_t always false", "[IPAddress6-Operator-()-01]") 40 | { 41 | // Actual value of the 128-bit IPv6 address, which is network byte order 42 | arduino::IPAddress ip(0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 129,168, 1,2); 43 | uint32_t const val_expected = ip; 44 | uint32_t const val_actual = ((129 << 24) | (168 << 16) | (1 << 8) | 2); 45 | REQUIRE((val_expected == val_actual) == false); 46 | } 47 | -------------------------------------------------------------------------------- /test/src/String/test_characterAccessFunc.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::charAt(unsigned int)", "[String-charAt-01]") 22 | { 23 | arduino::String str1("Hello"); 24 | REQUIRE(str1.charAt(2) == 'l'); 25 | } 26 | 27 | TEST_CASE ("Testing String::setCharAt(unsigned int, char )", "[String-setCharAt-02]") 28 | { 29 | arduino::String str1("Hello"); 30 | str1.setCharAt(1, 'a'); 31 | REQUIRE(str1 == "Hallo"); 32 | } 33 | 34 | TEST_CASE ("Testing String::getBytes(unsigned char, unsigned int, unsigned int)", "[String-getBytes-02]") 35 | { 36 | WHEN("No bufsize") { 37 | arduino::String str("Hello"); 38 | unsigned char buf[2]; 39 | str.getBytes(buf, 0, 0); 40 | } 41 | 42 | WHEN("Index >= len") { 43 | arduino::String str("Hello"); 44 | unsigned char buf[2]; 45 | str.getBytes(buf, 5, 6); 46 | } 47 | 48 | WHEN("Valid operation") { 49 | arduino::String str("Hello"); 50 | unsigned char buf[3]; 51 | str.getBytes(buf, 5, 3); 52 | REQUIRE(buf[0] == 'l'); 53 | REQUIRE(buf[1] == 'o'); 54 | REQUIRE(buf[2] == '\0'); 55 | } 56 | } 57 | 58 | TEST_CASE ("Testing & String::operator[]", "[String-&operator subscript-03]") 59 | { 60 | arduino::String str("Hello"); 61 | str[0] = 'M'; 62 | REQUIRE(str == "Mello"); 63 | } 64 | 65 | TEST_CASE ("Testing & String::operator[] with invalid buffer", "[String-&operator subscript-04]") 66 | { 67 | char *buffer = NULL; 68 | arduino::String str(buffer); 69 | str[0] = 'M'; 70 | REQUIRE(str[0] == 0); 71 | } 72 | -------------------------------------------------------------------------------- /test/src/IPAddress/test_fromString.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | #include 15 | 16 | /************************************************************************************** 17 | * TEST CODE 18 | **************************************************************************************/ 19 | 20 | TEST_CASE ("Extract valid IP address 'fromString(const char *)'", "[IPAddress-fromString-01]") 21 | { 22 | arduino::IPAddress ip; 23 | 24 | REQUIRE(ip.fromString("129.168.1.2") == true); 25 | 26 | REQUIRE(ip[0] == 129); 27 | REQUIRE(ip[1] == 168); 28 | REQUIRE(ip[2] == 1); 29 | REQUIRE(ip[3] == 2); 30 | } 31 | 32 | TEST_CASE ("Extract valid IP address 'fromString(const String &)'", "[IPAddress-fromString-02]") 33 | { 34 | arduino::IPAddress ip; 35 | 36 | arduino::String const ip_addr_str("129.168.1.2"); 37 | 38 | REQUIRE(ip.fromString(ip_addr_str) == true); 39 | 40 | REQUIRE(ip[0] == 129); 41 | REQUIRE(ip[1] == 168); 42 | REQUIRE(ip[2] == 1); 43 | REQUIRE(ip[3] == 2); 44 | } 45 | 46 | TEST_CASE ("Extract invalid IP address 'fromString(const char *)'", "[IPAddress-fromString-03]") 47 | { 48 | arduino::IPAddress ip; 49 | 50 | REQUIRE(ip.fromString("") == false); 51 | REQUIRE(ip.fromString("1") == false); 52 | REQUIRE(ip.fromString("1.") == false); 53 | REQUIRE(ip.fromString("1.1") == false); 54 | REQUIRE(ip.fromString("1.1.") == false); 55 | REQUIRE(ip.fromString("1.1.1") == false); 56 | REQUIRE(ip.fromString("1.1.1.") == false); 57 | REQUIRE(ip.fromString("...") == false); 58 | REQUIRE(ip.fromString("256.1.1.1") == false); 59 | REQUIRE(ip.fromString("a.1.1.1") == false); 60 | REQUIRE(ip.fromString("-.1.1.1") == false); 61 | REQUIRE(ip.fromString("-1.1.1.1") == false); 62 | } 63 | -------------------------------------------------------------------------------- /test/src/IPAddress/test_operator_comparison.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Testing IPAddress::operator == (IPAddress ip1, IPAddress ip2) with ip1 == ip2", "[IPAddress-Operator-==-01]") 20 | { 21 | arduino::IPAddress ip1(129,168,1,2), ip2(129,168,1,2); 22 | REQUIRE((ip1 == ip2) == true); 23 | } 24 | 25 | TEST_CASE ("Testing IPAddress::operator == (IPAddress ip1, IPAddress ip2) with ip1 != ip2", "[IPAddress-Operator-==-02]") 26 | { 27 | arduino::IPAddress ip1(129,168,1,2), ip2(10,0,0,1); 28 | REQUIRE((ip1 == ip2) == false); 29 | } 30 | 31 | TEST_CASE ("Testing IPAddress::operator == (IPAddress ip1, uint8_t const * ip2) with ip1 == ip2", "[IPAddress-Operator-==-03]") 32 | { 33 | arduino::IPAddress ip1(129,168,1,2); 34 | uint8_t const ip2[] = {129,168,1,2}; 35 | REQUIRE((ip1 == ip2) == true); 36 | } 37 | 38 | TEST_CASE ("Testing IPAddress::operator == (IPAddress ip1, uint8_t const * ip2) with ip1 != ip2", "[IPAddress-Operator-==-04]") 39 | { 40 | arduino::IPAddress ip1(129,168,1,2); 41 | uint8_t const ip2[] = {10,0,0,1}; 42 | REQUIRE((ip1 == ip2) == false); 43 | } 44 | 45 | TEST_CASE ("Testing IPAddress::operator != (IPAddress ip1, uint8_t const * ip2) with ip1 != ip2", "[IPAddress-Operator-==-05]") 46 | { 47 | arduino::IPAddress ip1(129,168,1,2), ip2(10,0,0,1); 48 | REQUIRE((ip1 != ip2) == true); 49 | } 50 | 51 | TEST_CASE ("Testing IPAddress::operator != (IPAddress ip1, uint8_t const * ip2) with ip1 == ip2", "[IPAddress-Operator-==-05]") 52 | { 53 | arduino::IPAddress ip1(129,168,1,2), ip2(129,168,1,2); 54 | REQUIRE((ip1 != ip2) == false); 55 | } 56 | -------------------------------------------------------------------------------- /api/USBAPI.h: -------------------------------------------------------------------------------- 1 | /* 2 | USBAPI.h 3 | Copyright (c) 2005-2014 Arduino. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef __USBAPI__ 21 | #define __USBAPI__ 22 | 23 | #include 24 | 25 | namespace arduino { 26 | //================================================================================ 27 | //================================================================================ 28 | // Low level API 29 | 30 | typedef struct __attribute__((packed)) 31 | { 32 | union { 33 | uint8_t bmRequestType; 34 | struct { 35 | uint8_t direction : 5; 36 | uint8_t type : 2; 37 | uint8_t transferDirection : 1; 38 | }; 39 | }; 40 | uint8_t bRequest; 41 | uint8_t wValueL; 42 | uint8_t wValueH; 43 | uint16_t wIndex; 44 | uint16_t wLength; 45 | } USBSetup; 46 | 47 | } 48 | 49 | //================================================================================ 50 | // USB APIs (C scope) 51 | //================================================================================ 52 | 53 | int USB_SendControl(uint8_t flags, const void* d, int len); 54 | int USB_RecvControl(void* d, int len); 55 | int USB_RecvControlLong(void* d, int len); 56 | 57 | uint8_t USB_Available(uint8_t ep); 58 | uint8_t USB_SendSpace(uint8_t ep); 59 | int USB_Send(uint8_t ep, const void* data, int len); // blocking 60 | int USB_Recv(uint8_t ep, void* data, int len); // non-blocking 61 | int USB_Recv(uint8_t ep); // non-blocking 62 | void USB_Flush(uint8_t ep); 63 | 64 | #endif -------------------------------------------------------------------------------- /test/src/String/test_remove.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::remove(index) when string is empty", "[String-remove-01]") 22 | { 23 | arduino::String str; 24 | str.remove(0); 25 | REQUIRE(str.length() == 0); 26 | } 27 | 28 | TEST_CASE ("Testing String::remove(index) when index is > string length", "[String-remove-02]") 29 | { 30 | arduino::String str("Hello Arduino!"); 31 | str.remove(100); 32 | REQUIRE(str == "Hello Arduino!"); 33 | } 34 | 35 | TEST_CASE ("Testing String::remove(index) when index is < string length", "[String-remove-03]") 36 | { 37 | arduino::String str("Hello Arduino!"); 38 | str.remove(5); 39 | REQUIRE(str == "Hello"); 40 | } 41 | 42 | TEST_CASE ("Testing String::remove(index,count) when string is empty", "[String-remove-04]") 43 | { 44 | arduino::String str; 45 | str.remove(0, 10); 46 | REQUIRE(str.length() == 0); 47 | } 48 | 49 | TEST_CASE ("Testing String::remove(index,count) when index is > string length", "[String-remove-05]") 50 | { 51 | arduino::String str("Hello Arduino!"); 52 | str.remove(100, 10); 53 | REQUIRE(str == "Hello Arduino!"); 54 | } 55 | 56 | TEST_CASE ("Testing String::remove(index,count) when index is < string length && count is > remaining length", "[String-remove-06]") 57 | { 58 | arduino::String str("Hello Arduino!"); 59 | str.remove(5, 100); 60 | REQUIRE(str == "Hello"); 61 | } 62 | 63 | TEST_CASE ("Testing String::remove(index,count) when index is < string length && count is < remaining length", "[String-remove-07]") 64 | { 65 | arduino::String str("Hello Arduino!"); 66 | str.remove(5, 1); 67 | REQUIRE(str == "HelloArduino!"); 68 | } 69 | -------------------------------------------------------------------------------- /api/Interrupts.h: -------------------------------------------------------------------------------- 1 | /* 2 | Interrupts.h - Arduino interrupt management functions 3 | Copyright (c) 2018 Arduino LLC. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef W_INTERRUPTS_CPP 21 | #define W_INTERRUPTS_CPP 22 | #ifdef __cplusplus 23 | 24 | #include 25 | #include 26 | #include 27 | #include "Common.h" 28 | 29 | namespace arduino { 30 | 31 | template 32 | using voidTemplateFuncPtrParam = void (*)(T param); 33 | 34 | template struct __container__ { 35 | void* param; 36 | voidTemplateFuncPtrParam function; 37 | }; 38 | 39 | // C++ only overloaded version of attachInterrupt function 40 | template void attachInterrupt(pin_size_t interruptNum, voidTemplateFuncPtrParam userFunc, PinStatus mode, T& param) { 41 | 42 | struct __container__ *cont = new __container__(); 43 | cont->param = ¶m; 44 | cont->function = userFunc; 45 | 46 | // TODO: check lambda scope 47 | // TODO: add structure to delete(__container__) when detachInterrupt() is called 48 | auto f = [](void* a) -> void 49 | { 50 | T param = *(T*)((struct __container__*)a)->param; 51 | (((struct __container__*)a)->function)(param); 52 | }; 53 | 54 | attachInterruptParam(interruptNum, f, mode, cont); 55 | } 56 | 57 | template void attachInterrupt(pin_size_t interruptNum, voidTemplateFuncPtrParam userFunc, PinStatus mode, T* param) { 58 | attachInterruptParam(interruptNum, (voidFuncPtrParam)userFunc, mode, (void*)param); 59 | } 60 | 61 | } 62 | #endif 63 | #endif 64 | -------------------------------------------------------------------------------- /test/src/CanMsg/test_CanMsg.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * NAMESPACE 17 | **************************************************************************************/ 18 | 19 | using namespace arduino; 20 | 21 | /************************************************************************************** 22 | * TEST CODE 23 | **************************************************************************************/ 24 | 25 | TEST_CASE ("Test constructor with no data (data length = 0)", "[CanMsg-CanMsg-01]") 26 | { 27 | CanMsg const msg(CanStandardId(0x20), 0, nullptr); 28 | 29 | REQUIRE(msg.data_length == 0); 30 | for (size_t i = 0; i < CanMsg::MAX_DATA_LENGTH; i++) 31 | REQUIRE(msg.data[i] == 0); 32 | } 33 | 34 | TEST_CASE ("Test constructor with data (data length < CanMsg::MAX_DATA_LENGTH)", "[CanMsg-CanMsg-02]") 35 | { 36 | uint8_t const msg_data[4] = {0xDE, 0xAD, 0xC0, 0xDE}; 37 | 38 | CanMsg const msg(CanStandardId(0x20), sizeof(msg_data), msg_data); 39 | 40 | REQUIRE(msg.data_length == 4); 41 | for (size_t i = 0; i < msg.data_length; i++) 42 | REQUIRE(msg.data[i] == msg_data[i]); 43 | } 44 | 45 | TEST_CASE ("Test constructor with data (data length > CanMsg::MAX_DATA_LENGTH)", "[CanMsg-CanMsg-03]") 46 | { 47 | uint8_t const msg_data[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; 48 | 49 | CanMsg const msg(CanStandardId(0x20), sizeof(msg_data), msg_data); 50 | 51 | REQUIRE(msg.data_length == 8); 52 | for (size_t i = 0; i < msg.data_length; i++) 53 | REQUIRE(msg.data[i] == msg_data[i]); 54 | } 55 | 56 | TEST_CASE ("Test constructor constructing a CAN frame with standard ID", "[CanMsg-CanMsg-04]") 57 | { 58 | CanMsg const msg(CanStandardId(0x20), 0, nullptr); 59 | 60 | REQUIRE(msg.id == 0x20); 61 | } 62 | 63 | TEST_CASE ("Test constructor constructing a CAN frame with extended ID", "[CanMsg-CanMsg-05]") 64 | { 65 | CanMsg const msg(CanExtendedId(0x20), 0, nullptr); 66 | 67 | REQUIRE(msg.id == (CanMsg::CAN_EFF_FLAG | 0x20)); 68 | } 69 | -------------------------------------------------------------------------------- /test/src/Stream/test_find.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Testing find(const char *target)", "[Stream-find-01]") 20 | { 21 | StreamMock mock; 22 | 23 | WHEN ("'target' is contained in stream") 24 | { 25 | mock << "This is a test string"; 26 | 27 | REQUIRE(mock.find("test") == true); 28 | REQUIRE(mock.readString() == arduino::String(" string")); 29 | } 30 | WHEN ("'target' is not contained in stream") 31 | { 32 | mock << "This is a string"; 33 | 34 | REQUIRE(mock.find("test") == false); 35 | REQUIRE(mock.readString() == arduino::String("")); 36 | } 37 | } 38 | 39 | TEST_CASE ("Testing find(const char *target, size_t length)", "[Stream-find-02]") 40 | { 41 | StreamMock mock; 42 | 43 | WHEN ("'target' is contained in stream") 44 | { 45 | mock << "This is a test string"; 46 | 47 | /* 'length' should actually be '4' or strlen("test"). I'd rather 48 | * think this API should not be exposed at all. 49 | */ 50 | REQUIRE(mock.find("test", 3) == true); 51 | REQUIRE(mock.readString() == arduino::String("t string")); 52 | } 53 | WHEN ("'target' is not contained in stream") 54 | { 55 | mock << "This is a string"; 56 | 57 | REQUIRE(mock.find("test", 3) == false); 58 | REQUIRE(mock.readString() == arduino::String("")); 59 | } 60 | } 61 | 62 | TEST_CASE ("Testing find(char target)", "[Stream-find-03]") 63 | { 64 | StreamMock mock; 65 | 66 | WHEN ("'target' is contained in stream") 67 | { 68 | mock << "This is a test string"; 69 | 70 | REQUIRE(mock.find('t') == true); 71 | REQUIRE(mock.readString() == arduino::String("est string")); 72 | } 73 | WHEN ("'target' is not contained in stream") 74 | { 75 | mock << "This is a string"; 76 | 77 | REQUIRE(mock.find('!') == false); 78 | REQUIRE(mock.readString() == arduino::String("")); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /test/src/Common/test_max.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Calling 'max(a,b)' with a < b", "[max-01]") 20 | { 21 | WHEN("a > 0 and b > 0") REQUIRE(max(1,5) == 5); 22 | WHEN("a < 0 and b > 0") REQUIRE(max(-1,5) == 5); 23 | WHEN("a < 0 and b < 0") REQUIRE(max(-5,-1) == -1); 24 | } 25 | 26 | TEST_CASE ("Calling 'max(a,b)' with a > b", "[max-02]") 27 | { 28 | WHEN("a > 0 and b > 0") REQUIRE(max(5,1) == 5); 29 | WHEN("a > 0 and b < 0") REQUIRE(max(5,-1) == 5); 30 | WHEN("a < 0 and b < 0") REQUIRE(max(-1,-5) == -1); 31 | } 32 | 33 | TEST_CASE ("Calling 'max(a,b)' with a == b", "[max-03]") 34 | { 35 | WHEN("a = b > 0") REQUIRE(max(5,5) == 5); 36 | WHEN("a = b < 0") REQUIRE(max(-5,-5) == -5); 37 | WHEN("a = b = 0") REQUIRE(max(0,0) == 0); 38 | } 39 | 40 | TEST_CASE ("Calling 'max(a,b)' with type(a) != type(b)", "[max-04]") 41 | { 42 | WHEN("type(A) = uint8_t, type(b) = uint16_t") 43 | { 44 | uint8_t const a = 32; 45 | uint16_t const b = 10; 46 | REQUIRE(typeid(max(a,b)) == typeid(int)); 47 | } 48 | WHEN("type(A) = uint16_t, type(b) = uint32_t") 49 | { 50 | uint16_t const a = 32; 51 | uint32_t const b = 10; 52 | REQUIRE(typeid(max(a,b)) == typeid(unsigned int)); 53 | } 54 | WHEN("type(A) = uint32_t, type(b) = uint64_t") 55 | { 56 | uint32_t const a = 32; 57 | uint64_t const b = 10; 58 | REQUIRE(typeid(max(a,b)) == typeid(uint64_t)); 59 | } 60 | WHEN("type(A) = int8_t, type(b) = int16_t") 61 | { 62 | int8_t const a = -32; 63 | int16_t const b = -10; 64 | REQUIRE(typeid(max(a,b)) == typeid(int)); 65 | } 66 | WHEN("type(A) = int16_t, type(b) = int32_t") 67 | { 68 | int16_t const a = -32; 69 | int32_t const b = -10; 70 | REQUIRE(typeid(max(a,b)) == typeid(int)); 71 | } 72 | WHEN("type(A) = int32_t, type(b) = int64_t") 73 | { 74 | int32_t const a = -32; 75 | int64_t const b = -10; 76 | REQUIRE(typeid(max(a,b)) == typeid(int64_t)); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /test/src/Common/test_min.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Calling 'min(a,b)' with a < b", "[min-01]") 20 | { 21 | WHEN("a > 0 and b > 0") REQUIRE(min(1,5) == 1); 22 | WHEN("a < 0 and b > 0") REQUIRE(min(-1,5) == -1); 23 | WHEN("a < 0 and b < 0") REQUIRE(min(-5,-1) == -5); 24 | } 25 | 26 | TEST_CASE ("Calling 'min(a,b)' with a > b", "[min-02]") 27 | { 28 | WHEN("a > 0 and b > 0") REQUIRE(min(5,1) == 1); 29 | WHEN("a > 0 and b < 0") REQUIRE(min(5,-1) == -1); 30 | WHEN("a < 0 and b < 0") REQUIRE(min(-1,-5) == -5); 31 | } 32 | 33 | TEST_CASE ("Calling 'min(a,b)' with a == b", "[min-03]") 34 | { 35 | WHEN("a = b > 0") REQUIRE(min(5,5) == 5); 36 | WHEN("a = b < 0") REQUIRE(min(-5,-5) == -5); 37 | WHEN("a = b = 0") REQUIRE(min(0,0) == 0); 38 | } 39 | 40 | TEST_CASE ("Calling 'min(a,b)' with type(a) != type(b)", "[min-04]") 41 | { 42 | WHEN("type(A) = uint8_t, type(b) = uint16_t") 43 | { 44 | uint8_t const a = 32; 45 | uint16_t const b = 10; 46 | REQUIRE(typeid(min(a,b)) == typeid(int)); 47 | } 48 | WHEN("type(A) = uint16_t, type(b) = uint32_t") 49 | { 50 | uint16_t const a = 32; 51 | uint32_t const b = 10; 52 | REQUIRE(typeid(min(a,b)) == typeid(unsigned int)); 53 | } 54 | WHEN("type(A) = uint32_t, type(b) = uint64_t") 55 | { 56 | uint32_t const a = 32; 57 | uint64_t const b = 10; 58 | REQUIRE(typeid(min(a,b)) == typeid(uint64_t)); 59 | } 60 | WHEN("type(A) = int8_t, type(b) = int16_t") 61 | { 62 | int8_t const a = -32; 63 | int16_t const b = -10; 64 | REQUIRE(typeid(min(a,b)) == typeid(int)); 65 | } 66 | WHEN("type(A) = int16_t, type(b) = int32_t") 67 | { 68 | int16_t const a = -32; 69 | int32_t const b = -10; 70 | REQUIRE(typeid(min(a,b)) == typeid(int)); 71 | } 72 | WHEN("type(A) = int32_t, type(b) = int64_t") 73 | { 74 | int32_t const a = -32; 75 | int64_t const b = -10; 76 | REQUIRE(typeid(min(a,b)) == typeid(int64_t)); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /api/PluggableUSB.h: -------------------------------------------------------------------------------- 1 | /* 2 | PluggableUSB.h 3 | Copyright (c) 2015 Arduino LLC 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef PUSB_h 21 | #define PUSB_h 22 | 23 | #include "USBAPI.h" 24 | #include 25 | #include 26 | 27 | namespace arduino { 28 | 29 | class PluggableUSBModule { 30 | public: 31 | PluggableUSBModule(uint8_t numEps, uint8_t numIfs, unsigned int *epType) : 32 | numEndpoints(numEps), numInterfaces(numIfs), endpointType(epType) 33 | { } 34 | 35 | protected: 36 | virtual bool setup(USBSetup& setup) = 0; 37 | virtual int getInterface(uint8_t* interfaceCount) = 0; 38 | virtual int getDescriptor(USBSetup& setup) = 0; 39 | virtual uint8_t getShortName(char *name) { name[0] = 'A'+pluggedInterface; return 1; } 40 | 41 | uint8_t pluggedInterface; 42 | uint8_t pluggedEndpoint; 43 | 44 | const uint8_t numEndpoints; 45 | const uint8_t numInterfaces; 46 | const unsigned int *endpointType; 47 | 48 | PluggableUSBModule *next = NULL; 49 | 50 | friend class PluggableUSB_; 51 | }; 52 | 53 | class PluggableUSB_ { 54 | public: 55 | PluggableUSB_(); 56 | bool plug(PluggableUSBModule *node); 57 | int getInterface(uint8_t* interfaceCount); 58 | int getDescriptor(USBSetup& setup); 59 | bool setup(USBSetup& setup); 60 | void getShortName(char *iSerialNum); 61 | 62 | private: 63 | uint8_t lastIf; 64 | uint8_t lastEp; 65 | PluggableUSBModule* rootNode; 66 | uint8_t totalEP; 67 | }; 68 | } 69 | 70 | // core need to define 71 | void* epBuffer(unsigned int n); // -> returns a pointer to the Nth element of the EP buffer structure 72 | 73 | // Replacement for global singleton. 74 | // This function prevents static-initialization-order-fiasco 75 | // https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use 76 | arduino::PluggableUSB_& PluggableUSB(); 77 | 78 | #endif -------------------------------------------------------------------------------- /api/CanMsgRingbuffer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | CanMsgRingbuffer.cpp - Library for CAN message handling 3 | Copyright (c) 2023 Arduino. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | /************************************************************************************** 21 | * INCLUDE 22 | **************************************************************************************/ 23 | 24 | #include "CanMsgRingbuffer.h" 25 | 26 | /************************************************************************************** 27 | * NAMESPACE 28 | **************************************************************************************/ 29 | 30 | namespace arduino 31 | { 32 | 33 | /************************************************************************************** 34 | * CTOR/DTOR 35 | **************************************************************************************/ 36 | 37 | CanMsgRingbuffer::CanMsgRingbuffer() 38 | : _head{0} 39 | , _tail{0} 40 | , _num_elems{0} 41 | { 42 | } 43 | 44 | /************************************************************************************** 45 | * PUBLIC MEMBER FUNCTIONS 46 | **************************************************************************************/ 47 | 48 | void CanMsgRingbuffer::enqueue(CanMsg const & msg) 49 | { 50 | if (isFull()) 51 | return; 52 | 53 | _buf[_head] = msg; 54 | _head = next(_head); 55 | _num_elems = _num_elems + 1; 56 | } 57 | 58 | CanMsg CanMsgRingbuffer::dequeue() 59 | { 60 | if (isEmpty()) 61 | return CanMsg(); 62 | 63 | CanMsg const msg = _buf[_tail]; 64 | _tail = next(_tail); 65 | _num_elems = _num_elems - 1; 66 | 67 | return msg; 68 | } 69 | 70 | /************************************************************************************** 71 | * NAMESPACE 72 | **************************************************************************************/ 73 | 74 | } /* arduino */ 75 | -------------------------------------------------------------------------------- /api/CanMsgRingbuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | CanMsgRingbuffer.h - Library for CAN message handling 3 | Copyright (c) 2023 Arduino. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef ARDUINOCORE_API_CAN_MSG_RING_BUFFER_H_ 21 | #define ARDUINOCORE_API_CAN_MSG_RING_BUFFER_H_ 22 | 23 | /************************************************************************************** 24 | * INCLUDE 25 | **************************************************************************************/ 26 | 27 | #include 28 | 29 | #include "CanMsg.h" 30 | 31 | /************************************************************************************** 32 | * NAMESPACE 33 | **************************************************************************************/ 34 | 35 | namespace arduino 36 | { 37 | 38 | /************************************************************************************** 39 | * CLASS DECLARATION 40 | **************************************************************************************/ 41 | 42 | class CanMsgRingbuffer 43 | { 44 | public: 45 | static size_t constexpr RING_BUFFER_SIZE = 32U; 46 | 47 | CanMsgRingbuffer(); 48 | 49 | inline bool isFull() const { return (_num_elems == RING_BUFFER_SIZE); } 50 | void enqueue(CanMsg const & msg); 51 | 52 | inline bool isEmpty() const { return (_num_elems == 0); } 53 | CanMsg dequeue(); 54 | 55 | inline size_t available() const { return _num_elems; } 56 | 57 | private: 58 | CanMsg _buf[RING_BUFFER_SIZE]; 59 | volatile size_t _head; 60 | volatile size_t _tail; 61 | volatile size_t _num_elems; 62 | 63 | inline size_t next(size_t const idx) const { return ((idx + 1) % RING_BUFFER_SIZE); } 64 | }; 65 | 66 | /************************************************************************************** 67 | * NAMESPACE 68 | **************************************************************************************/ 69 | 70 | } /* arduino */ 71 | 72 | #endif /* ARDUINOCORE_API_CAN_MSG_RING_BUFFER_H_ */ 73 | -------------------------------------------------------------------------------- /test/src/IPAddress/test_IPAddress6.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Testing IPAddress(type) constructor()", "[IPAddress6-Ctor-01]") 20 | { 21 | arduino::IPAddress ip (arduino::IPType::IPv6); 22 | 23 | REQUIRE(ip.type() == arduino::IPType::IPv6); 24 | REQUIRE(ip[0] == 0); 25 | REQUIRE(ip[1] == 0); 26 | REQUIRE(ip[2] == 0); 27 | REQUIRE(ip[3] == 0); 28 | REQUIRE(ip[4] == 0); 29 | REQUIRE(ip[5] == 0); 30 | REQUIRE(ip[6] == 0); 31 | REQUIRE(ip[7] == 0); 32 | REQUIRE(ip[8] == 0); 33 | REQUIRE(ip[9] == 0); 34 | REQUIRE(ip[10] == 0); 35 | REQUIRE(ip[11] == 0); 36 | REQUIRE(ip[12] == 0); 37 | REQUIRE(ip[13] == 0); 38 | REQUIRE(ip[14] == 0); 39 | REQUIRE(ip[15] == 0); 40 | } 41 | 42 | TEST_CASE ("Testing IPAddress(o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o) constructor", "[IPAddress-Ctor6-02]") 43 | { 44 | arduino::IPAddress ip(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc); 45 | 46 | REQUIRE(ip.type() == arduino::IPType::IPv6); 47 | REQUIRE(ip[0] == 0x20); 48 | REQUIRE(ip[1] == 0x01); 49 | REQUIRE(ip[2] == 0xd); 50 | REQUIRE(ip[3] == 0xb8); 51 | REQUIRE(ip[4] == 1); 52 | REQUIRE(ip[5] == 2); 53 | REQUIRE(ip[6] == 3); 54 | REQUIRE(ip[7] == 4); 55 | REQUIRE(ip[8] == 5); 56 | REQUIRE(ip[9] == 6); 57 | REQUIRE(ip[10] == 7); 58 | REQUIRE(ip[11] == 8); 59 | REQUIRE(ip[12] == 9); 60 | REQUIRE(ip[13] == 0xa); 61 | REQUIRE(ip[14] == 0xb); 62 | REQUIRE(ip[15] == 0xc); 63 | } 64 | 65 | TEST_CASE ("Testing IPAddress(type, a *) constructor", "[IPAddress6-Ctor-03]") 66 | { 67 | uint8_t const ip_addr_array[] = {0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc}; 68 | arduino::IPAddress ip(arduino::IPType::IPv6, ip_addr_array); 69 | 70 | REQUIRE(ip.type() == arduino::IPType::IPv6); 71 | REQUIRE(ip[0] == 0x20); 72 | REQUIRE(ip[1] == 0x01); 73 | REQUIRE(ip[2] == 0xd); 74 | REQUIRE(ip[3] == 0xb8); 75 | REQUIRE(ip[4] == 1); 76 | REQUIRE(ip[5] == 2); 77 | REQUIRE(ip[6] == 3); 78 | REQUIRE(ip[7] == 4); 79 | REQUIRE(ip[8] == 5); 80 | REQUIRE(ip[9] == 6); 81 | REQUIRE(ip[10] == 7); 82 | REQUIRE(ip[11] == 8); 83 | REQUIRE(ip[12] == 9); 84 | REQUIRE(ip[13] == 0xa); 85 | REQUIRE(ip[14] == 0xb); 86 | REQUIRE(ip[15] == 0xc); 87 | } 88 | -------------------------------------------------------------------------------- /api/PluggableUSB.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | PluggableUSB.cpp 3 | Copyright (c) 2015 Arduino LLC 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include "USBAPI.h" 21 | #include "PluggableUSB.h" 22 | 23 | using namespace arduino; 24 | 25 | int PluggableUSB_::getInterface(uint8_t* interfaceCount) 26 | { 27 | int sent = 0; 28 | PluggableUSBModule* node; 29 | for (node = rootNode; node; node = node->next) { 30 | int res = node->getInterface(interfaceCount); 31 | if (res < 0) 32 | return -1; 33 | sent += res; 34 | } 35 | return sent; 36 | } 37 | 38 | int PluggableUSB_::getDescriptor(USBSetup& setup) 39 | { 40 | PluggableUSBModule* node; 41 | for (node = rootNode; node; node = node->next) { 42 | int ret = node->getDescriptor(setup); 43 | // ret!=0 -> request has been processed 44 | if (ret) 45 | return ret; 46 | } 47 | return 0; 48 | } 49 | 50 | void PluggableUSB_::getShortName(char *iSerialNum) 51 | { 52 | PluggableUSBModule* node; 53 | for (node = rootNode; node; node = node->next) { 54 | iSerialNum += node->getShortName(iSerialNum); 55 | } 56 | *iSerialNum = 0; 57 | } 58 | 59 | bool PluggableUSB_::setup(USBSetup& setup) 60 | { 61 | PluggableUSBModule* node; 62 | for (node = rootNode; node; node = node->next) { 63 | if (node->setup(setup)) { 64 | return true; 65 | } 66 | } 67 | return false; 68 | } 69 | 70 | bool PluggableUSB_::plug(PluggableUSBModule *node) 71 | { 72 | if ((lastEp + node->numEndpoints) > totalEP) { 73 | return false; 74 | } 75 | 76 | if (!rootNode) { 77 | rootNode = node; 78 | } else { 79 | PluggableUSBModule *current = rootNode; 80 | while (current->next) { 81 | current = current->next; 82 | } 83 | current->next = node; 84 | } 85 | 86 | node->pluggedInterface = lastIf; 87 | node->pluggedEndpoint = lastEp; 88 | lastIf += node->numInterfaces; 89 | for (uint8_t i = 0; i < node->numEndpoints; i++) { 90 | *(unsigned int*)(epBuffer(lastEp)) = node->endpointType[i]; 91 | lastEp++; 92 | } 93 | return true; 94 | // restart USB layer??? 95 | } 96 | 97 | PluggableUSB_& PluggableUSB() 98 | { 99 | static PluggableUSB_ obj; 100 | return obj; 101 | } -------------------------------------------------------------------------------- /test/src/String/test_compareTo.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::compareTo(const String &)", "[String-compareTo-01]") 22 | { 23 | WHEN ("Strings are equal") 24 | { 25 | arduino::String str1("Hello"), str2("Hello"); 26 | REQUIRE(str1.compareTo(str2) == 0); 27 | } 28 | 29 | WHEN ("str2 is empty") 30 | { 31 | arduino::String str1("Hello"), str2; 32 | REQUIRE(str1.compareTo(str2) > 0); 33 | } 34 | 35 | WHEN ("str1 is empty") 36 | { 37 | arduino::String str1, str2("Hello"); 38 | REQUIRE(str1.compareTo(str2) < 0); 39 | } 40 | } 41 | 42 | TEST_CASE ("Testing String::compareTo(const char *)", "[String-compareTo-02]") 43 | { 44 | WHEN ("Strings are equal") 45 | { 46 | arduino::String str("Hello"); 47 | REQUIRE(str.compareTo("Hello") == 0); 48 | } 49 | 50 | WHEN ("Passed string is empty") 51 | { 52 | arduino::String str("Hello"); 53 | REQUIRE(str.compareTo("") > 0); 54 | } 55 | 56 | WHEN ("Passed string is compared with empty string") 57 | { 58 | arduino::String str; 59 | REQUIRE(str.compareTo("") == 0); 60 | } 61 | } 62 | 63 | TEST_CASE ("Testing String::compareTo(const char *) with empty buffer", "[String-compareTo-03]") 64 | { 65 | WHEN ("First string has a valid buffer") 66 | { 67 | char *buffer = NULL; 68 | 69 | arduino::String str("Hello"); 70 | REQUIRE(str.compareTo(buffer) != 0); 71 | } 72 | 73 | WHEN ("First string does NOT have a valid buffer") 74 | { 75 | char *buffer1 = NULL; 76 | char *buffer2 = NULL; 77 | 78 | arduino::String str(buffer1); 79 | REQUIRE(str.compareTo(buffer2) == 0); 80 | } 81 | } 82 | 83 | 84 | TEST_CASE ("Testing String::compareTo(const String &) with empty buffer", "[String-compareTo-04]") 85 | { 86 | WHEN ("First string has a valid buffer") 87 | { 88 | char *buffer = NULL; 89 | 90 | arduino::String str1("Hello"); 91 | arduino::String str2(buffer); 92 | REQUIRE(str1.compareTo(str2) != 0); 93 | } 94 | 95 | WHEN ("First string does NOT have a valid buffer") 96 | { 97 | char *buffer1 = NULL; 98 | char *buffer2 = NULL; 99 | 100 | arduino::String str1(buffer1); 101 | arduino::String str2(buffer2); 102 | REQUIRE(str1.compareTo(str2) == 0); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /test/src/IPAddress/test_operator_comparison6.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | TEST_CASE ("Testing two basic constructs the same", "[IPAddress6-Operator-==-01]") 20 | { 21 | arduino::IPAddress ip1(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc), ip2(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc); 22 | REQUIRE((ip1 == ip2) == true); 23 | } 24 | 25 | TEST_CASE ("Testing two addresses different", "[IPAddress-Operator-==-02]") 26 | { 27 | arduino::IPAddress ip1(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc), ip2(0xfd,0x12, 0x34,0x56, 0x78,0x9a, 0,1, 0,0, 0,0, 0,0, 0,1); 28 | REQUIRE((ip1 == ip2) == false); 29 | } 30 | 31 | TEST_CASE ("Testing not equals different address is true", "[IPAddress-Operator-==-03]") 32 | { 33 | arduino::IPAddress ip1(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc), ip2(0xfd,0x12, 0x34,0x56, 0x78,0x9a, 0,1, 0,0, 0,0, 0,0, 0,1); 34 | REQUIRE((ip1 != ip2) == true); 35 | } 36 | 37 | TEST_CASE ("Testing not equals same address is false", "[IPAddress-Operator-==-04]") 38 | { 39 | arduino::IPAddress ip1(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc), ip2(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc); 40 | REQUIRE((ip1 != ip2) == false); 41 | } 42 | 43 | // IPv4 and IPv6 differ based on type (irrespective of bytes) 44 | 45 | TEST_CASE ("Testing IPv4 vs IPv6", "[IPAddress6-Operator-==-05]") 46 | { 47 | arduino::IPAddress ip1(10, 0, 0, 1), ip2(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc); 48 | REQUIRE((ip1 == ip2) == false); 49 | } 50 | 51 | TEST_CASE ("Testing IPv4 vs IPv6 equivalent IPv4-compatible address (deprecated)", "[IPAddress6-Operator-==-05]") 52 | { 53 | arduino::IPAddress ip1(10, 0, 0, 1), ip2(0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 10,0, 0,1); 54 | REQUIRE((ip1 == ip2) == false); 55 | } 56 | 57 | TEST_CASE ("Testing IPv4 vs IPv6 localhost", "[IPAddress6-Operator-==-05]") 58 | { 59 | arduino::IPAddress ip1(127, 0, 0, 1), ip2(0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 10,0, 0,1); 60 | REQUIRE((ip1 == ip2) == false); 61 | } 62 | 63 | TEST_CASE ("Testing IPv4 equivalent compatible address vs IPv6 localhost", "[IPAddress6-Operator-==-05]") 64 | { 65 | arduino::IPAddress ip1(0, 0, 0, 1), ip2(0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,1); 66 | REQUIRE((ip1 == ip2) == false); 67 | } 68 | 69 | TEST_CASE ("Testing IPv6 never matches as raw byte sequence assumed to be length 4", "[IPAddress6-Operator-==-06]") 70 | { 71 | arduino::IPAddress ip1(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc); 72 | uint8_t const ip2[] = {0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc}; 73 | REQUIRE((ip1 == ip2) == false); 74 | } 75 | -------------------------------------------------------------------------------- /test/src/String/test_indexOf.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::indexOf(char ch)", "[String-indexOf-01]") 22 | { 23 | WHEN ("str is empty") 24 | { 25 | arduino::String str; 26 | REQUIRE(str.indexOf('a') == -1); 27 | } 28 | WHEN ("str does not contained searched element") 29 | { 30 | arduino::String str("Hello"); 31 | REQUIRE(str.indexOf('a') == -1); 32 | } 33 | WHEN ("str does contain searched element") 34 | { 35 | arduino::String str("Hello"); 36 | REQUIRE(str.indexOf('l') == 2); 37 | } 38 | } 39 | 40 | TEST_CASE ("Testing String::indexOf(char ch, unsigned int fromIndex)", "[String-indexOf-02]") 41 | { 42 | WHEN ("str is empty") 43 | { 44 | arduino::String str; 45 | REQUIRE(str.indexOf('a', 5) == -1); 46 | } 47 | WHEN ("str does not contained searched element") 48 | { 49 | arduino::String str("Hallo"); 50 | REQUIRE(str.indexOf('a', 3) == -1); 51 | } 52 | WHEN ("str does contain searched element") 53 | { 54 | arduino::String str("Hello"); 55 | REQUIRE(str.indexOf('l', 3) == 3); 56 | } 57 | } 58 | 59 | TEST_CASE ("Testing String::indexOf(const String &)", "[String-indexOf-03]") 60 | { 61 | arduino::String const search_str("Arduino"); 62 | 63 | WHEN ("str is empty") 64 | { 65 | arduino::String str; 66 | REQUIRE(str.indexOf(search_str) == -1); 67 | } 68 | WHEN ("str does not contained searched element") 69 | { 70 | arduino::String str("Hallo"); 71 | REQUIRE(str.indexOf(search_str) == -1); 72 | } 73 | WHEN ("str does contain searched element") 74 | { 75 | arduino::String str("Hello Arduino!"); 76 | REQUIRE(str.indexOf(search_str) == 6); 77 | } 78 | } 79 | 80 | TEST_CASE ("Testing String::indexOf(const String &, unsigned int fromIndex)", "[String-indexOf-04]") 81 | { 82 | arduino::String const search_str("Arduino"); 83 | 84 | WHEN ("str is empty") 85 | { 86 | arduino::String str; 87 | REQUIRE(str.indexOf(search_str, 3) == -1); 88 | } 89 | WHEN ("str does not contained searched element") 90 | { 91 | arduino::String str("Hallo"); 92 | REQUIRE(str.indexOf(search_str, 3) == -1); 93 | } 94 | WHEN ("str does contain searched element and fromIndex is < start of searched element") 95 | { 96 | arduino::String str("Hello Arduino!"); 97 | REQUIRE(str.indexOf(search_str, 3) == 6); 98 | } 99 | WHEN ("str does contain searched element and fromIndex is > start of searched element") 100 | { 101 | arduino::String str("Hello Arduino!"); 102 | REQUIRE(str.indexOf(search_str, 8) == -1); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /test/src/String/test_lastIndexOf.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::lastIndexOf(char ch)", "[String-lastIndexOf-01]") 22 | { 23 | WHEN ("str is empty") 24 | { 25 | arduino::String str; 26 | REQUIRE(str.lastIndexOf('a') == -1); 27 | } 28 | WHEN ("str does not contained searched element") 29 | { 30 | arduino::String str("Hello"); 31 | REQUIRE(str.lastIndexOf('a') == -1); 32 | } 33 | WHEN ("str does contain searched element") 34 | { 35 | arduino::String str("Hellolol"); 36 | REQUIRE(str.lastIndexOf('l') == 7); 37 | } 38 | } 39 | 40 | TEST_CASE ("Testing String::lastIndexOf(char ch, unsigned int fromIndex)", "[String-lastIndexOf-02]") 41 | { 42 | WHEN ("str is empty") 43 | { 44 | arduino::String str; 45 | REQUIRE(str.lastIndexOf('a', 5) == -1); 46 | } 47 | WHEN ("str does not contained searched element") 48 | { 49 | arduino::String str("Hallo"); 50 | REQUIRE(str.lastIndexOf('o', 3) == -1); 51 | } 52 | WHEN ("str does contain searched element") 53 | { 54 | arduino::String str("Hellolol"); 55 | REQUIRE(str.lastIndexOf('l', 3) == 3); 56 | } 57 | } 58 | 59 | TEST_CASE ("Testing String::lastIndexOf(const String &)", "[String-lastIndexOf-03]") 60 | { 61 | arduino::String const search_str("Arduino"); 62 | 63 | WHEN ("str is empty") 64 | { 65 | arduino::String str; 66 | REQUIRE(str.lastIndexOf(search_str) == -1); 67 | } 68 | WHEN ("str does not contained searched element") 69 | { 70 | arduino::String str("Hallo"); 71 | REQUIRE(str.lastIndexOf(search_str) == -1); 72 | } 73 | WHEN ("str does contain searched element") 74 | { 75 | arduino::String str("Hello Arduino, Arduino!"); 76 | REQUIRE(str.lastIndexOf(search_str) == 15); 77 | } 78 | } 79 | 80 | TEST_CASE ("Testing String::lastIndexOf(const String &, unsigned int fromIndex)", "[String-lastIndexOf-04]") 81 | { 82 | arduino::String const search_str("Arduino"); 83 | 84 | WHEN ("str is empty") 85 | { 86 | arduino::String str; 87 | REQUIRE(str.lastIndexOf(search_str, 3) == -1); 88 | } 89 | WHEN ("str does not contained searched element") 90 | { 91 | arduino::String str("Hallo"); 92 | REQUIRE(str.lastIndexOf(search_str, 3) == -1); 93 | } 94 | WHEN ("str does contain searched element and fromIndex is > start of searched element") 95 | { 96 | arduino::String str("Hello Arduino, Arduino!"); 97 | REQUIRE(str.lastIndexOf(search_str, 17) == 15); 98 | } 99 | WHEN ("str does contain searched element and fromIndex is < start of searched element") 100 | { 101 | arduino::String str("Hello Arduino, Arduino!"); 102 | REQUIRE(str.lastIndexOf(search_str, 3) == -1); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /test/src/String/test_concat.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::concat(const String &)", "[String-concat-01]") 22 | { 23 | arduino::String str1("Hello "), str2("Arduino!"); 24 | REQUIRE(str1.concat(str2) == 1); 25 | REQUIRE(str1 == "Hello Arduino!"); 26 | } 27 | 28 | TEST_CASE ("Testing String::concat(const char *)", "[String-concat-02]") 29 | { 30 | arduino::String str("Hello "); 31 | REQUIRE(str.concat("Arduino!") == 1); 32 | REQUIRE(str == "Hello Arduino!"); 33 | } 34 | 35 | TEST_CASE ("Testing String::concat(char)", "[String-concat-03]") 36 | { 37 | arduino::String str("Hello "); 38 | char const c = 'A'; 39 | REQUIRE(str.concat(c) == 1); 40 | REQUIRE(str == "Hello A"); 41 | } 42 | 43 | TEST_CASE ("Testing String::concat(unsigned char)", "[String-concat-04]") 44 | { 45 | arduino::String str("Hello "); 46 | unsigned char const c = 'A'; 47 | REQUIRE(str.concat(c) == 1); 48 | REQUIRE(str == "Hello 65"); /* ASCII['A'] = 65 */ 49 | } 50 | 51 | TEST_CASE ("Testing String::concat(int)", "[String-concat-05]") 52 | { 53 | arduino::String str("Hello "); 54 | int const num = -1; 55 | REQUIRE(str.concat(num) == 1); 56 | REQUIRE(str == "Hello -1"); 57 | } 58 | 59 | TEST_CASE ("Testing String::concat(unsigned int)", "[String-concat-06]") 60 | { 61 | arduino::String str("Hello "); 62 | unsigned int const num = 1; 63 | REQUIRE(str.concat(num) == 1); 64 | REQUIRE(str == "Hello 1"); 65 | } 66 | 67 | TEST_CASE ("Testing String::concat(long)", "[String-concat-07]") 68 | { 69 | arduino::String str("Hello "); 70 | long const num = -1; 71 | REQUIRE(str.concat(num) == 1); 72 | REQUIRE(str == "Hello -1"); 73 | } 74 | 75 | TEST_CASE ("Testing String::concat(unsigned long)", "[String-concat-08]") 76 | { 77 | arduino::String str("Hello "); 78 | unsigned long const num = 1; 79 | REQUIRE(str.concat(num) == 1); 80 | REQUIRE(str == "Hello 1"); 81 | } 82 | 83 | TEST_CASE ("Testing String::concat(float)", "[String-concat-09]") 84 | { 85 | arduino::String str("Hello "); 86 | float const num = 1.234f; 87 | REQUIRE(str.concat(num) == 1); 88 | REQUIRE(str == "Hello 1.23"); 89 | } 90 | 91 | TEST_CASE ("Testing String::concat(double)", "[String-concat-10]") 92 | { 93 | arduino::String str("Hello "); 94 | double const num = 5.678; 95 | REQUIRE(str.concat(num) == 1); 96 | REQUIRE(str == "Hello 5.68"); 97 | } 98 | 99 | TEST_CASE ("Testing String::concat(const __FlashStringHelper *)", "[String-concat-11]") 100 | { 101 | #undef F 102 | #define F(string_literal) (reinterpret_cast(PSTR(string_literal))) 103 | arduino::String str1("Hello"); 104 | REQUIRE(str1.concat(F(" Arduino")) == 1); 105 | REQUIRE(str1 == "Hello Arduino"); 106 | } 107 | -------------------------------------------------------------------------------- /api/Print.h: -------------------------------------------------------------------------------- 1 | /* 2 | Print.h - Base class that provides print() and println() 3 | Copyright (c) 2016 Arduino LLC. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #pragma once 21 | 22 | #include 23 | #include // for size_t 24 | 25 | #include "String.h" 26 | #include "Printable.h" 27 | 28 | #define DEC 10 29 | #define HEX 16 30 | #define OCT 8 31 | #define BIN 2 32 | 33 | namespace arduino { 34 | 35 | class Print 36 | { 37 | private: 38 | int write_error; 39 | size_t printNumber(unsigned long, uint8_t); 40 | size_t printULLNumber(unsigned long long, uint8_t); 41 | size_t printFloat(double, int); 42 | protected: 43 | void setWriteError(int err = 1) { write_error = err; } 44 | public: 45 | Print() : write_error(0) {} 46 | 47 | int getWriteError() { return write_error; } 48 | void clearWriteError() { setWriteError(0); } 49 | 50 | virtual size_t write(uint8_t) = 0; 51 | size_t write(const char *str) { 52 | if (str == NULL) return 0; 53 | return write((const uint8_t *)str, strlen(str)); 54 | } 55 | virtual size_t write(const uint8_t *buffer, size_t size); 56 | size_t write(const char *buffer, size_t size) { 57 | return write((const uint8_t *)buffer, size); 58 | } 59 | 60 | // default to zero, meaning "a single write may block" 61 | // should be overridden by subclasses with buffering 62 | virtual int availableForWrite() { return 0; } 63 | 64 | size_t print(const __FlashStringHelper *); 65 | size_t print(const String &); 66 | size_t print(const char[]); 67 | size_t print(char); 68 | size_t print(unsigned char, int = DEC); 69 | size_t print(int, int = DEC); 70 | size_t print(unsigned int, int = DEC); 71 | size_t print(long, int = DEC); 72 | size_t print(unsigned long, int = DEC); 73 | size_t print(long long, int = DEC); 74 | size_t print(unsigned long long, int = DEC); 75 | size_t print(double, int = 2); 76 | size_t print(const Printable&); 77 | 78 | size_t println(const __FlashStringHelper *); 79 | size_t println(const String &s); 80 | size_t println(const char[]); 81 | size_t println(char); 82 | size_t println(unsigned char, int = DEC); 83 | size_t println(int, int = DEC); 84 | size_t println(unsigned int, int = DEC); 85 | size_t println(long, int = DEC); 86 | size_t println(unsigned long, int = DEC); 87 | size_t println(long long, int = DEC); 88 | size_t println(unsigned long long, int = DEC); 89 | size_t println(double, int = 2); 90 | size_t println(const Printable&); 91 | size_t println(void); 92 | 93 | virtual void flush() { /* Empty implementation for backward compatibility */ } 94 | }; 95 | 96 | } 97 | using arduino::Print; -------------------------------------------------------------------------------- /test/src/Stream/test_parseInt.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | /************************************************************************************** 16 | * TEST CODE 17 | **************************************************************************************/ 18 | 19 | using namespace arduino; 20 | 21 | TEST_CASE ("Testing parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR)", "[Stream-parseInt-01]") 22 | { 23 | StreamMock mock; 24 | 25 | WHEN ("A positive integer is contained in stream") 26 | { 27 | mock << "1234"; 28 | REQUIRE(mock.parseInt() == 1234); 29 | } 30 | WHEN ("A negative integer is contained in stream") 31 | { 32 | mock << "-1234"; 33 | REQUIRE(mock.parseInt() == -1234); 34 | } 35 | WHEN ("A integer is prepended by digits") 36 | { 37 | mock << "abcdef1234"; 38 | REQUIRE(mock.parseInt() == 1234); 39 | } 40 | WHEN ("A integer is prepended by whitespace chars") 41 | { 42 | mock << "\r\n\t 1234"; 43 | REQUIRE(mock.parseInt() == 1234); 44 | } 45 | } 46 | 47 | TEST_CASE ("Testing parseInt(LookaheadMode lookahead = SKIP_NONE, char ignore = NO_IGNORE_CHAR)", "[Stream-parseInt-02]") 48 | { 49 | StreamMock mock; 50 | 51 | WHEN ("A positive integer is contained in stream") 52 | { 53 | mock << "1234"; 54 | REQUIRE(mock.parseInt(SKIP_NONE) == 1234); 55 | REQUIRE(mock.readString() == arduino::String("")); 56 | } 57 | WHEN ("A integer is prepended by digits") 58 | { 59 | mock << "abcdef1234"; 60 | REQUIRE(mock.parseInt(SKIP_NONE) == 0); 61 | REQUIRE(mock.readString() == arduino::String("abcdef1234")); 62 | } 63 | WHEN ("A integer is prepended by whitespace chars") 64 | { 65 | mock << "\r\n\t 1234"; 66 | REQUIRE(mock.parseInt(SKIP_NONE) == 0); 67 | REQUIRE(mock.readString() == arduino::String("\r\n\t 1234")); 68 | } 69 | } 70 | 71 | TEST_CASE ("Testing parseInt(LookaheadMode lookahead = SKIP_WHITESPACE, char ignore = NO_IGNORE_CHAR)", "[Stream-parseInt-03]") 72 | { 73 | StreamMock mock; 74 | 75 | WHEN ("A integer is prepended by whitespace chars") 76 | { 77 | mock << "\r\n\t 1234"; 78 | REQUIRE(mock.parseInt(SKIP_WHITESPACE) == 1234); 79 | REQUIRE(mock.readString() == arduino::String("")); 80 | } 81 | } 82 | 83 | TEST_CASE ("Testing parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = 'a')", "[Stream-parseInt-04]") 84 | { 85 | StreamMock mock; 86 | 87 | WHEN ("A positive integer is contained in stream") 88 | { 89 | mock << "1234"; 90 | REQUIRE(mock.parseInt(SKIP_ALL, 'a') == 1234); 91 | REQUIRE(mock.readString() == arduino::String("")); 92 | } 93 | WHEN ("A integer contains only ignore char values") 94 | { 95 | mock << "12a3a4a"; 96 | REQUIRE(mock.parseInt(SKIP_ALL, 'a') == 1234); 97 | REQUIRE(mock.readString() == arduino::String("")); 98 | } 99 | WHEN ("A integer contains other than ignore chars") 100 | { 101 | mock << "1bed234"; 102 | REQUIRE(mock.parseInt(SKIP_ALL, 'a') == 1); 103 | REQUIRE(mock.readString() == arduino::String("bed234")); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /test/src/String/test_replace.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include "StringPrinter.h" 16 | 17 | /************************************************************************************** 18 | * TEST CODE 19 | **************************************************************************************/ 20 | 21 | TEST_CASE ("Testing String::replace(char, char) when string is empty", "[String-replace-01]") 22 | { 23 | arduino::String str; 24 | str.replace('a', 'b'); 25 | REQUIRE(str.length() == 0); 26 | } 27 | 28 | TEST_CASE ("Testing String::replace(char, char) when string contains elements != 'find'", "[String-replace-02]") 29 | { 30 | arduino::String str("Hello Arduino!"); 31 | str.replace('Z', '0'); 32 | REQUIRE(str == "Hello Arduino!"); 33 | } 34 | 35 | TEST_CASE ("Testing String::replace(char, char) when string contains elements = 'find'", "[String-replace-03]") 36 | { 37 | arduino::String str("Hello Arduino!"); 38 | str.replace('o', '0'); 39 | str.replace('e', '3'); 40 | str.replace('i', '1'); 41 | REQUIRE(str == "H3ll0 Ardu1n0!"); 42 | } 43 | 44 | TEST_CASE ("Testing String::replace(String, String) when string does not contain substr 'find'", "[String-replace-04]") 45 | { 46 | arduino::String str("Hello Arduino!"); 47 | str.replace(arduino::String("Zulu"), arduino::String("11")); 48 | REQUIRE(str == "Hello Arduino!"); 49 | } 50 | 51 | TEST_CASE ("Testing String::replace(String, String) when string contains substr 'find'", "[String-replace-05]") 52 | { 53 | arduino::String str("Hello Arduino!"); 54 | str.replace(arduino::String("ll"), arduino::String("11")); 55 | REQUIRE(str == "He11o Arduino!"); 56 | } 57 | 58 | TEST_CASE ("Testing String::replace(String, String) substr 'find' larger than 'replace'", "[String-replace-06]") 59 | { 60 | arduino::String str("Hello Arduino!"); 61 | str.replace(arduino::String("llo"), arduino::String("11")); 62 | REQUIRE(str == "He11 Arduino!"); 63 | } 64 | 65 | TEST_CASE ("Testing String::replace(String, String) substr 'find' smaller than 'replace'", "[String-replace-07]") 66 | { 67 | arduino::String str("Hello Arduino!"); 68 | str.replace(arduino::String("ll"), arduino::String("111")); 69 | REQUIRE(str == "He111o Arduino!"); 70 | } 71 | 72 | TEST_CASE ("Testing String::replace(String, String) substr 'find' smaller than 'replace' multiple occurencies", "[String-replace-08]") 73 | { 74 | arduino::String str("Hello Arduino! Hello, Hello, Hello"); 75 | str.replace(arduino::String("ll"), arduino::String("lll")); 76 | REQUIRE(str == "Helllo Arduino! Helllo, Helllo, Helllo"); 77 | } 78 | 79 | TEST_CASE ("Testing String::replace(String, String) substr 'find' same length as 'replace' multiple occurencies", "[String-replace-09]") 80 | { 81 | arduino::String str("Hello Arduino! Hello, Hello, Hello"); 82 | str.replace(arduino::String("ll"), arduino::String("11")); 83 | REQUIRE(str == "He11o Arduino! He11o, He11o, He11o"); 84 | } 85 | 86 | TEST_CASE ("Testing String::replace(String, String) substr 'find' larger than 'replace' multiple occurencies", "[String-replace-10]") 87 | { 88 | arduino::String str("Helllo Arduino! Helllo, Helllo, Helllo"); 89 | str.replace(arduino::String("lll"), arduino::String("ll")); 90 | REQUIRE(str == "Hello Arduino! Hello, Hello, Hello"); 91 | } 92 | -------------------------------------------------------------------------------- /api/RingBuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | RingBuffer.h - Ring buffer implementation 3 | Copyright (c) 2014 Arduino. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifdef __cplusplus 21 | 22 | #ifndef _RING_BUFFER_ 23 | #define _RING_BUFFER_ 24 | 25 | #include 26 | #include 27 | 28 | namespace arduino { 29 | 30 | // Define constants and variables for buffering incoming serial data. We're 31 | // using a ring buffer (I think), in which head is the index of the location 32 | // to which to write the next incoming character and tail is the index of the 33 | // location from which to read. 34 | #define SERIAL_BUFFER_SIZE 64 35 | 36 | template 37 | class RingBufferN 38 | { 39 | public: 40 | uint8_t _aucBuffer[N] ; 41 | volatile int _iHead ; 42 | volatile int _iTail ; 43 | volatile int _numElems; 44 | 45 | public: 46 | RingBufferN( void ) ; 47 | void store_char( uint8_t c ) ; 48 | void clear(); 49 | int read_char(); 50 | int available(); 51 | int availableForStore(); 52 | int peek(); 53 | bool isFull(); 54 | 55 | private: 56 | int nextIndex(int index); 57 | inline bool isEmpty() const { return (_numElems == 0); } 58 | }; 59 | 60 | typedef RingBufferN RingBuffer; 61 | 62 | 63 | template 64 | RingBufferN::RingBufferN( void ) 65 | { 66 | memset( _aucBuffer, 0, N ) ; 67 | clear(); 68 | } 69 | 70 | template 71 | void RingBufferN::store_char( uint8_t c ) 72 | { 73 | // if we should be storing the received character into the location 74 | // just before the tail (meaning that the head would advance to the 75 | // current location of the tail), we're about to overflow the buffer 76 | // and so we don't write the character or advance the head. 77 | if (!isFull()) 78 | { 79 | _aucBuffer[_iHead] = c ; 80 | _iHead = nextIndex(_iHead); 81 | _numElems = _numElems + 1; 82 | } 83 | } 84 | 85 | template 86 | void RingBufferN::clear() 87 | { 88 | _iHead = 0; 89 | _iTail = 0; 90 | _numElems = 0; 91 | } 92 | 93 | template 94 | int RingBufferN::read_char() 95 | { 96 | if (isEmpty()) 97 | return -1; 98 | 99 | uint8_t value = _aucBuffer[_iTail]; 100 | _iTail = nextIndex(_iTail); 101 | _numElems = _numElems - 1; 102 | 103 | return value; 104 | } 105 | 106 | template 107 | int RingBufferN::available() 108 | { 109 | return _numElems; 110 | } 111 | 112 | template 113 | int RingBufferN::availableForStore() 114 | { 115 | return (N - _numElems); 116 | } 117 | 118 | template 119 | int RingBufferN::peek() 120 | { 121 | if (isEmpty()) 122 | return -1; 123 | 124 | return _aucBuffer[_iTail]; 125 | } 126 | 127 | template 128 | int RingBufferN::nextIndex(int index) 129 | { 130 | return (uint32_t)(index + 1) % N; 131 | } 132 | 133 | template 134 | bool RingBufferN::isFull() 135 | { 136 | return (_numElems == N); 137 | } 138 | 139 | } 140 | 141 | #endif /* _RING_BUFFER_ */ 142 | #endif /* __cplusplus */ 143 | -------------------------------------------------------------------------------- /test/src/Print/test_println.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | 18 | /************************************************************************************** 19 | * TEST CODE 20 | **************************************************************************************/ 21 | 22 | TEST_CASE ("Print::println(char)", "[Print-println-01]") 23 | { 24 | PrintMock mock; 25 | 26 | mock.println('A'); 27 | 28 | REQUIRE(mock._str == "A\r\n"); 29 | } 30 | 31 | TEST_CASE ("Print::println(const String &)", "[Print-println-02]") 32 | { 33 | PrintMock mock; 34 | arduino::String const str("Test String"); 35 | 36 | mock.println(str); 37 | 38 | REQUIRE(mock._str == "Test String\r\n"); 39 | } 40 | 41 | TEST_CASE ("Print::println(const char str[])", "[Print-println-03]") 42 | { 43 | PrintMock mock; 44 | const char str[] = "Test String"; 45 | 46 | mock.println(str); 47 | 48 | REQUIRE(mock._str == "Test String\r\n"); 49 | } 50 | 51 | TEST_CASE ("Print::println(int, int = DEC (default))", "[Print-println-04]") 52 | { 53 | PrintMock mock; 54 | int const val = -1; 55 | 56 | mock.println(val); 57 | 58 | REQUIRE(mock._str == "-1\r\n"); 59 | } 60 | 61 | TEST_CASE ("Print::println(unsigned int, int = DEC (default))", "[Print-println-05]") 62 | { 63 | PrintMock mock; 64 | unsigned int const val = 17; 65 | 66 | mock.println(val); 67 | 68 | REQUIRE(mock._str == "17\r\n"); 69 | } 70 | 71 | TEST_CASE ("Print::println(long, int = DEC (default))", "[Print-println-06]") 72 | { 73 | PrintMock mock; 74 | long const val = -1; 75 | 76 | mock.println(val); 77 | 78 | REQUIRE(mock._str == "-1\r\n"); 79 | } 80 | 81 | TEST_CASE ("Print::println(unsigned long, int = DEC (default))", "[Print-println-07]") 82 | { 83 | PrintMock mock; 84 | unsigned long const val = 17; 85 | 86 | mock.println(val); 87 | 88 | REQUIRE(mock._str == "17\r\n"); 89 | } 90 | 91 | TEST_CASE ("Print::println(long long, int = DEC (default))", "[Print-println-08]") 92 | { 93 | PrintMock mock; 94 | long long const val = -1; 95 | 96 | mock.println(val); 97 | 98 | REQUIRE(mock._str == "-1\r\n"); 99 | } 100 | 101 | TEST_CASE ("Print::println(unsigned long long, int = DEC|HEX|OCT|BIN)", "[Print-println-09]") 102 | { 103 | PrintMock mock; 104 | unsigned long long const val = 17; 105 | 106 | mock.println(val); 107 | 108 | REQUIRE(mock._str == "17\r\n"); 109 | } 110 | 111 | TEST_CASE ("Print::println(double, int = 2)", "[Print-println-10]") 112 | { 113 | PrintMock mock; 114 | double const val = 3.1459; 115 | 116 | mock.println(val); 117 | 118 | REQUIRE(mock._str == "3.15\r\n"); 119 | } 120 | 121 | TEST_CASE ("Print::println(Printable)", "[Print-println-11]") 122 | { 123 | PrintMock mock; 124 | PrintableMock printable; 125 | printable._i = 1; 126 | 127 | mock.println(printable); 128 | 129 | REQUIRE(mock._str == "PrintableMock i = 1\r\n"); 130 | } 131 | 132 | TEST_CASE ("Print::println(unsigned char, int base = DEC (default))", "[Print-println-12]") 133 | { 134 | PrintMock mock; 135 | 136 | mock.println('A', DEC); 137 | 138 | REQUIRE(mock._str == "65\r\n"); 139 | } 140 | 141 | TEST_CASE ("Testing Print::println(const __FlashStringHelper *)", "[Print-println-13]") 142 | { 143 | #undef F 144 | #define F(string_literal) (reinterpret_cast(PSTR(string_literal))) 145 | PrintMock mock; 146 | 147 | mock.println(F("Hello flash string")); 148 | 149 | REQUIRE(mock._str == "Hello flash string\r\n"); 150 | } 151 | -------------------------------------------------------------------------------- /test/src/IPAddress/test_printTo6.cpp: -------------------------------------------------------------------------------- 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 12 | 13 | #include 14 | #include 15 | 16 | /************************************************************************************** 17 | * TEST CODE 18 | **************************************************************************************/ 19 | 20 | TEST_CASE ("Print IPv6", "[IPAddress-printTo6-01]") 21 | { 22 | PrintMock mock; 23 | arduino::IPAddress ip(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc); 24 | 25 | mock.print(ip); 26 | 27 | REQUIRE(mock._str == "2001:db8:102:304:506:708:90a:b0c"); 28 | } 29 | 30 | TEST_CASE ("Print IPv6 any", "[IPAddress-printTo6-02]") 31 | { 32 | PrintMock mock; 33 | arduino::IPAddress const ip(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); 34 | 35 | mock.print(ip); 36 | 37 | REQUIRE(mock._str == "::"); 38 | } 39 | 40 | TEST_CASE ("Print IPv6 localhost", "[IPAddress-printTo6-03]") 41 | { 42 | PrintMock mock; 43 | arduino::IPAddress const ip(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1); 44 | 45 | mock.print(ip); 46 | 47 | REQUIRE(mock._str == "::1"); 48 | } 49 | 50 | TEST_CASE ("Print IPv6 different length segments", "[IPAddress-printTo6-04]") 51 | { 52 | PrintMock mock; 53 | arduino::IPAddress const ip(0xab,0xcd, 0x0e,0xf1, 0x00,0x23, 0,0, 0x00,0x04, 0,0, 0,0, 0,0); 54 | 55 | mock.print(ip); 56 | 57 | REQUIRE(mock._str == "abcd:ef1:23:0:4::"); 58 | } 59 | 60 | TEST_CASE ("Print IPv6 zero longest run end", "[IPAddress-printTo6-05]") 61 | { 62 | PrintMock mock; 63 | arduino::IPAddress const ip(0,0, 0,1, 0,0, 0,0, 0,2, 0,0, 0,0, 0,0); 64 | 65 | mock.print(ip); 66 | 67 | REQUIRE(mock._str == "0:1:0:0:2::"); 68 | } 69 | 70 | TEST_CASE ("Print IPv6 zero longest run mid", "[IPAddress-printTo6-06]") 71 | { 72 | PrintMock mock; 73 | arduino::IPAddress const ip(0,0, 0,1, 0,0, 0,0, 0,0, 0,2, 0,0, 0,0); 74 | 75 | mock.print(ip); 76 | 77 | REQUIRE(mock._str == "0:1::2:0:0"); 78 | } 79 | 80 | TEST_CASE ("Print IPv6 start zero", "[IPAddress-printTo6-07]") 81 | { 82 | PrintMock mock; 83 | arduino::IPAddress const ip(0,0, 0,2, 0,3, 0,4, 0,5, 0,6, 0,7, 0,8); 84 | 85 | mock.print(ip); 86 | 87 | REQUIRE(mock._str == "0:2:3:4:5:6:7:8"); 88 | } 89 | 90 | TEST_CASE ("Print IPv6 ending zero", "[IPAddress-printTo6-08]") 91 | { 92 | PrintMock mock; 93 | arduino::IPAddress const ip(0,1, 0,2, 0,3, 0,4, 0,5, 0,6, 0,7, 0,0); 94 | 95 | mock.print(ip); 96 | 97 | REQUIRE(mock._str == "1:2:3:4:5:6:7:0"); 98 | } 99 | 100 | TEST_CASE ("Print IPv6 start two zero", "[IPAddress-printTo6-09]") 101 | { 102 | PrintMock mock; 103 | arduino::IPAddress const ip(0,0, 0,0, 0,3, 0,4, 0,5, 0,6, 0,7, 0,0); 104 | 105 | mock.print(ip); 106 | 107 | REQUIRE(mock._str == "::3:4:5:6:7:0"); 108 | } 109 | 110 | TEST_CASE ("Print IPv6 ending two zero", "[IPAddress-printTo6-10]") 111 | { 112 | PrintMock mock; 113 | arduino::IPAddress const ip(0,0, 0,2, 0,3, 0,4, 0,5, 0,6, 0,0, 0,0); 114 | 115 | mock.print(ip); 116 | 117 | REQUIRE(mock._str == "0:2:3:4:5:6::"); 118 | } 119 | 120 | TEST_CASE ("Print IPv6 first out of same length", "[IPAddress-printTo6-11]") 121 | { 122 | PrintMock mock; 123 | arduino::IPAddress const ip(0,1, 0,0, 0,0, 0,4, 0,5, 0,0, 0,0, 0,8); 124 | 125 | mock.print(ip); 126 | 127 | REQUIRE(mock._str == "1::4:5:0:0:8"); 128 | } 129 | 130 | 131 | TEST_CASE ("Print IPv6 single zeros not compressed", "[IPAddress-printTo6-12]") 132 | { 133 | PrintMock mock; 134 | arduino::IPAddress const ip(0,1, 0,0, 0,3, 0,0, 0,5, 0,0, 0,7, 0,8); 135 | 136 | mock.print(ip); 137 | 138 | REQUIRE(mock._str == "1:0:3:0:5:0:7:8"); 139 | } 140 | -------------------------------------------------------------------------------- /test/src/Stream/test_parseFloat.cpp: -------------------------------------------------------------------------------- 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 12 | #include 13 | 14 | using namespace Catch; 15 | 16 | #include 17 | 18 | #include 19 | 20 | using namespace arduino; 21 | 22 | /************************************************************************************** 23 | * TEST CODE 24 | **************************************************************************************/ 25 | 26 | TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR)", "[Stream-parseFloat-01]") 27 | { 28 | StreamMock mock; 29 | 30 | WHEN ("Only a integer (no comma) is contained in stream") 31 | { 32 | mock << "12"; 33 | REQUIRE(mock.parseFloat() == 12.0f); 34 | } 35 | WHEN ("A positive float is contained in stream") 36 | { 37 | mock << "12.34"; 38 | REQUIRE(mock.parseFloat() == 12.34f); 39 | } 40 | WHEN ("A negative float is contained in stream") 41 | { 42 | mock << "-12.34"; 43 | REQUIRE(mock.parseFloat() == -12.34f); 44 | } 45 | WHEN ("A float is prepended by digits") 46 | { 47 | mock << "abcdef12.34"; 48 | REQUIRE(mock.parseFloat() == 12.34f); 49 | } 50 | WHEN ("The integer is prepended by whitespace chars") 51 | { 52 | mock << "\r\n\t 12.34"; 53 | REQUIRE(mock.parseFloat() == 12.34f); 54 | } 55 | WHEN ("A float is provided with too many digits after the decimal point") 56 | { 57 | mock << "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870064"; 58 | REQUIRE(mock.parseFloat() == Approx(3.141592654f)); 59 | } 60 | WHEN ("A float is larger than LONG_MAX") 61 | { 62 | mock << "602200000000000000000000.00"; 63 | REQUIRE(mock.parseFloat() == Approx(6.022e23f)); 64 | } 65 | } 66 | 67 | TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_NONE, char ignore = NO_IGNORE_CHAR)", "[Stream-parseFloat-02]") 68 | { 69 | StreamMock mock; 70 | 71 | WHEN ("Only a integer is contained in stream") 72 | { 73 | mock << "12.34"; 74 | REQUIRE(mock.parseFloat(SKIP_NONE) == 12.34f); 75 | REQUIRE(mock.readString() == arduino::String("")); 76 | } 77 | WHEN ("The integer is prepended by digits") 78 | { 79 | mock << "abcdef12.34"; 80 | REQUIRE(mock.parseFloat(SKIP_NONE) == 0); 81 | REQUIRE(mock.readString() == arduino::String("abcdef12.34")); 82 | } 83 | WHEN ("The integer is prepended by whitespace chars") 84 | { 85 | mock << "\r\n\t 12.34"; 86 | REQUIRE(mock.parseFloat(SKIP_NONE) == 0); 87 | REQUIRE(mock.readString() == arduino::String("\r\n\t 12.34")); 88 | } 89 | } 90 | 91 | TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_WHITESPACE, char ignore = NO_IGNORE_CHAR)", "[Stream-parseFloat-03]") 92 | { 93 | StreamMock mock; 94 | 95 | WHEN ("The integer is prepended by whitespace chars") 96 | { 97 | mock << "\r\n\t 12.34"; 98 | REQUIRE(mock.parseFloat(SKIP_WHITESPACE) == 12.34f); 99 | REQUIRE(mock.readString() == arduino::String("")); 100 | } 101 | } 102 | 103 | 104 | TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = 'a')", "[Stream-parseFloat-04]") 105 | { 106 | StreamMock mock; 107 | 108 | WHEN ("A float is contained in stream") 109 | { 110 | mock << "12.34"; 111 | REQUIRE(mock.parseFloat(SKIP_ALL, 'a') == 12.34f); 112 | REQUIRE(mock.readString() == arduino::String("")); 113 | } 114 | WHEN ("The float contains only ignore char values") 115 | { 116 | mock << "12a.3a4a"; 117 | REQUIRE(mock.parseFloat(SKIP_ALL, 'a') == 12.34f); 118 | REQUIRE(mock.readString() == arduino::String("")); 119 | } 120 | WHEN ("The integer contains other than ignore chars") 121 | { 122 | mock << "1bed234"; 123 | REQUIRE(mock.parseFloat(SKIP_ALL, 'a') == 1.0f); 124 | REQUIRE(mock.readString() == arduino::String("bed234")); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /api/HardwareCAN.h: -------------------------------------------------------------------------------- 1 | /* 2 | HardwareCAN.h - CAN bus interface for Arduino core 3 | Copyright (c) 2023 Arduino. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef ARDUINOCORE_API_HARDWARECAN_H 21 | #define ARDUINOCORE_API_HARDWARECAN_H 22 | 23 | /************************************************************************************** 24 | * INCLUDE 25 | **************************************************************************************/ 26 | 27 | #include "CanMsg.h" 28 | #include "CanMsgRingbuffer.h" 29 | 30 | /************************************************************************************** 31 | * TYPEDEF 32 | **************************************************************************************/ 33 | 34 | enum class CanBitRate : int 35 | { 36 | BR_125k = 125000, 37 | BR_250k = 250000, 38 | BR_500k = 500000, 39 | BR_1000k = 1000000, 40 | }; 41 | 42 | /************************************************************************************** 43 | * NAMESPACE 44 | **************************************************************************************/ 45 | 46 | namespace arduino 47 | { 48 | 49 | /************************************************************************************** 50 | * CLASS DECLARATION 51 | **************************************************************************************/ 52 | 53 | class HardwareCAN 54 | { 55 | public: 56 | virtual ~HardwareCAN() {} 57 | 58 | 59 | /** 60 | * Initialize the CAN controller. 61 | * 62 | * @param can_bitrate the bus bit rate 63 | * @return true if initialization succeeded and the controller is operational 64 | */ 65 | virtual bool begin(CanBitRate const can_bitrate) = 0; 66 | 67 | /** 68 | * Disable the CAN controller. 69 | * 70 | * Whether any messages that are buffered will be sent is _implementation defined_. 71 | */ 72 | virtual void end() = 0; 73 | 74 | /** 75 | * Enqueue a message for transmission to the CAN bus. 76 | * 77 | * This call returns when the message has been enqueued for transmission. 78 | * Due to bus arbitration and error recovery there may be a substantial delay 79 | * before the message is actually sent. 80 | * 81 | * An implementation must ensure that all messages with the same CAN priority 82 | * are sent in the order in which they are enqueued. 83 | * 84 | * It is _implementation defined_ whether multiple messages can be enqueued 85 | * for transmission, and if messages with higher CAN priority can preempt the 86 | * transmission of previously enqueued messages. The default configuration for 87 | * and implementation should not allow multiple messages to be enqueued. 88 | * 89 | * @param msg the message to send 90 | * @return 1 if the message was enqueued, an _implementation defined_ error code < 0 if there was an error 91 | * @todo define specific error codes, especially "message already pending" 92 | */ 93 | virtual int write(CanMsg const &msg) = 0; 94 | 95 | /** 96 | * Determine if any messages have been received and buffered. 97 | * 98 | * @return the number of unread messages that have been received 99 | */ 100 | virtual size_t available() = 0; 101 | 102 | /** 103 | * Returns the first message received, or an empty message if none are available. 104 | * 105 | * Messages must be returned in the order received. 106 | * 107 | * @return the first message in the receive buffer 108 | */ 109 | virtual CanMsg read() = 0; 110 | }; 111 | 112 | /************************************************************************************** 113 | * NAMESPACE 114 | **************************************************************************************/ 115 | 116 | } /* arduino */ 117 | 118 | #endif /* ARDUINOCORE_API_HARDWARECAN_H */ 119 | -------------------------------------------------------------------------------- /api/Udp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Udp.cpp: Library to send/receive UDP packets. 3 | * 4 | * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these) 5 | * 1) UDP does not guarantee the order in which assembled UDP packets are received. This 6 | * might not happen often in practice, but in larger network topologies, a UDP 7 | * packet can be received out of sequence. 8 | * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being 9 | * aware of it. Again, this may not be a concern in practice on small local networks. 10 | * For more information, see http://www.cafeaulait.org/course/week12/35.html 11 | * 12 | * MIT License: 13 | * Copyright (c) 2008 Bjoern Hartmann 14 | * Permission is hereby granted, free of charge, to any person obtaining a copy 15 | * of this software and associated documentation files (the "Software"), to deal 16 | * in the Software without restriction, including without limitation the rights 17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 | * copies of the Software, and to permit persons to whom the Software is 19 | * furnished to do so, subject to the following conditions: 20 | * 21 | * The above copyright notice and this permission notice shall be included in 22 | * all copies or substantial portions of the Software. 23 | * 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 | * THE SOFTWARE. 31 | * 32 | * bjoern@cs.stanford.edu 12/30/2008 33 | */ 34 | 35 | #pragma once 36 | 37 | #include "Stream.h" 38 | #include "IPAddress.h" 39 | 40 | namespace arduino { 41 | 42 | class UDP : public Stream { 43 | 44 | public: 45 | virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use 46 | virtual uint8_t beginMulticast(IPAddress, uint16_t) { return 0; } // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure 47 | virtual void stop() =0; // Finish with the UDP socket 48 | 49 | // Sending UDP packets 50 | 51 | // Start building up a packet to send to the remote host specified in ip and port 52 | // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port 53 | virtual int beginPacket(IPAddress ip, uint16_t port) =0; 54 | // Start building up a packet to send to the remote host specified in host and port 55 | // Returns 1 if successful, 0 if there was a problem resolving the hostname or port 56 | virtual int beginPacket(const char *host, uint16_t port) =0; 57 | // Finish off this packet and send it 58 | // Returns 1 if the packet was sent successfully, 0 if there was an error 59 | virtual int endPacket() =0; 60 | // Write a single byte into the packet 61 | virtual size_t write(uint8_t) =0; 62 | // Write size bytes from buffer into the packet 63 | virtual size_t write(const uint8_t *buffer, size_t size) =0; 64 | 65 | // Start processing the next available incoming packet 66 | // Returns the size of the packet in bytes, or 0 if no packets are available 67 | virtual int parsePacket() =0; 68 | // Number of bytes remaining in the current packet 69 | virtual int available() =0; 70 | // Read a single byte from the current packet 71 | virtual int read() =0; 72 | // Read up to len bytes from the current packet and place them into buffer 73 | // Returns the number of bytes read, or 0 if none are available 74 | virtual int read(unsigned char* buffer, size_t len) =0; 75 | // Read up to len characters from the current packet and place them into buffer 76 | // Returns the number of characters read, or 0 if none are available 77 | virtual int read(char* buffer, size_t len) =0; 78 | // Return the next byte from the current packet without moving on to the next byte 79 | virtual int peek() =0; 80 | virtual void flush() =0; // Finish reading the current packet 81 | 82 | // Return the IP address of the host who sent the current incoming packet 83 | virtual IPAddress remoteIP() =0; 84 | // Return the port of the host who sent the current incoming packet 85 | virtual uint16_t remotePort() =0; 86 | protected: 87 | uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; 88 | }; 89 | 90 | } 91 | 92 | using arduino::UDP; -------------------------------------------------------------------------------- /api/IPAddress.h: -------------------------------------------------------------------------------- 1 | /* 2 | IPAddress.h - Base class that provides IPAddress 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #pragma once 21 | 22 | #include 23 | #include "Printable.h" 24 | #include "String.h" 25 | 26 | #define IPADDRESS_V4_BYTES_INDEX 12 27 | #define IPADDRESS_V4_DWORD_INDEX 3 28 | 29 | // forward declarations of global name space friend classes 30 | class EthernetClass; 31 | class DhcpClass; 32 | class DNSClient; 33 | 34 | namespace arduino { 35 | 36 | // A class to make it easier to handle and pass around IP addresses 37 | 38 | enum IPType { 39 | IPv4, 40 | IPv6 41 | }; 42 | 43 | class IPAddress : public Printable { 44 | private: 45 | union { 46 | uint8_t bytes[16]; 47 | uint32_t dword[4]; 48 | } _address; 49 | IPType _type; 50 | 51 | // Access the raw byte array containing the address. Because this returns a pointer 52 | // to the internal structure rather than a copy of the address this function should only 53 | // be used when you know that the usage of the returned uint8_t* will be transient and not 54 | // stored. 55 | uint8_t* raw_address() { return _type == IPv4 ? &_address.bytes[IPADDRESS_V4_BYTES_INDEX] : _address.bytes; } 56 | 57 | public: 58 | // Constructors 59 | 60 | // Default IPv4 61 | IPAddress(); 62 | IPAddress(IPType ip_type); 63 | IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); 64 | IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4, uint8_t o5, uint8_t o6, uint8_t o7, uint8_t o8, uint8_t o9, uint8_t o10, uint8_t o11, uint8_t o12, uint8_t o13, uint8_t o14, uint8_t o15, uint8_t o16); 65 | // IPv4; see implementation note 66 | IPAddress(uint32_t address); 67 | // Default IPv4 68 | IPAddress(const uint8_t *address); 69 | IPAddress(IPType ip_type, const uint8_t *address); 70 | // If IPv4 fails tries IPv6 see fromString function 71 | IPAddress(const char *address); 72 | 73 | bool fromString(const char *address); 74 | bool fromString(const String &address) { return fromString(address.c_str()); } 75 | 76 | // Overloaded cast operator to allow IPAddress objects to be used where a uint32_t is expected 77 | // NOTE: IPv4 only; see implementation note 78 | operator uint32_t() const { return _type == IPv4 ? _address.dword[IPADDRESS_V4_DWORD_INDEX] : 0; }; 79 | 80 | bool operator==(const IPAddress& addr) const; 81 | bool operator!=(const IPAddress& addr) const { return !(*this == addr); }; 82 | 83 | // NOTE: IPv4 only; we don't know the length of the pointer 84 | bool operator==(const uint8_t* addr) const; 85 | 86 | // Overloaded index operator to allow getting and setting individual octets of the address 87 | uint8_t operator[](int index) const; 88 | uint8_t& operator[](int index); 89 | 90 | // Overloaded copy operators to allow initialisation of IPAddress objects from other types 91 | // NOTE: IPv4 only 92 | IPAddress& operator=(const uint8_t *address); 93 | // NOTE: IPv4 only; see implementation note 94 | IPAddress& operator=(uint32_t address); 95 | // If IPv4 fails tries IPv6 see fromString function 96 | IPAddress& operator=(const char *address); 97 | 98 | virtual size_t printTo(Print& p) const; 99 | String toString() const; 100 | 101 | IPType type() const { return _type; } 102 | 103 | friend class UDP; 104 | friend class Client; 105 | friend class Server; 106 | 107 | friend ::EthernetClass; 108 | friend ::DhcpClass; 109 | friend ::DNSClient; 110 | 111 | protected: 112 | bool fromString4(const char *address); 113 | bool fromString6(const char *address); 114 | String toString4() const; 115 | String toString6() const; 116 | }; 117 | 118 | extern const IPAddress IN6ADDR_ANY; 119 | extern const IPAddress INADDR_NONE; 120 | } 121 | 122 | using arduino::IPAddress; -------------------------------------------------------------------------------- /api/HardwareSPI.h: -------------------------------------------------------------------------------- 1 | /* 2 | HardwareSPI.h - Hardware SPI interface for Arduino 3 | Copyright (c) 2018 Arduino LLC. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #pragma once 21 | 22 | #include "Common.h" 23 | #include 24 | #include "Stream.h" 25 | 26 | #define SPI_HAS_TRANSACTION 27 | 28 | namespace arduino { 29 | 30 | typedef enum { 31 | SPI_MODE0 = 0, 32 | SPI_MODE1 = 1, 33 | SPI_MODE2 = 2, 34 | SPI_MODE3 = 3, 35 | } SPIMode; 36 | 37 | // Platforms should define SPI_HAS_PERIPHERAL_MODE if SPI peripheral 38 | // mode is supported, to allow applications to check whether peripheral 39 | // mode is available or not. 40 | typedef enum { 41 | SPI_CONTROLLER = 0, 42 | SPI_PERIPHERAL = 1, 43 | } SPIBusMode; 44 | 45 | 46 | class SPISettings { 47 | public: 48 | SPISettings(uint32_t clock, BitOrder bitOrder, SPIMode dataMode, SPIBusMode busMode = SPI_CONTROLLER) { 49 | if (__builtin_constant_p(clock)) { 50 | init_AlwaysInline(clock, bitOrder, dataMode, busMode); 51 | } else { 52 | init_MightInline(clock, bitOrder, dataMode, busMode); 53 | } 54 | } 55 | 56 | SPISettings(uint32_t clock, BitOrder bitOrder, int dataMode, SPIBusMode busMode = SPI_CONTROLLER) { 57 | if (__builtin_constant_p(clock)) { 58 | init_AlwaysInline(clock, bitOrder, (SPIMode)dataMode, busMode); 59 | } else { 60 | init_MightInline(clock, bitOrder, (SPIMode)dataMode, busMode); 61 | } 62 | } 63 | 64 | // Default speed set to 4MHz, SPI mode set to MODE 0 and Bit order set to MSB first. 65 | SPISettings() { init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0, SPI_CONTROLLER); } 66 | 67 | bool operator==(const SPISettings& rhs) const 68 | { 69 | if ((this->clockFreq == rhs.clockFreq) && 70 | (this->bitOrder == rhs.bitOrder) && 71 | (this->dataMode == rhs.dataMode) && 72 | (this->busMode == rhs.busMode)) { 73 | return true; 74 | } 75 | return false; 76 | } 77 | 78 | bool operator!=(const SPISettings& rhs) const 79 | { 80 | return !(*this == rhs); 81 | } 82 | 83 | uint32_t getClockFreq() const { 84 | return clockFreq; 85 | } 86 | SPIMode getDataMode() const { 87 | return dataMode; 88 | } 89 | BitOrder getBitOrder() const { 90 | return (bitOrder); 91 | } 92 | SPIBusMode getBusMode() const { 93 | return busMode; 94 | } 95 | 96 | private: 97 | void init_MightInline(uint32_t clock, BitOrder bitOrder, SPIMode dataMode, SPIBusMode busMode) { 98 | init_AlwaysInline(clock, bitOrder, dataMode, busMode); 99 | } 100 | 101 | // Core developer MUST use an helper function in beginTransaction() to use this data 102 | void init_AlwaysInline(uint32_t clock, BitOrder bitOrder, SPIMode dataMode, SPIBusMode busMode) __attribute__((__always_inline__)) { 103 | this->clockFreq = clock; 104 | this->dataMode = dataMode; 105 | this->bitOrder = bitOrder; 106 | this->busMode = busMode; 107 | } 108 | 109 | uint32_t clockFreq; 110 | SPIMode dataMode; 111 | BitOrder bitOrder; 112 | SPIBusMode busMode; 113 | 114 | friend class HardwareSPI; 115 | }; 116 | 117 | const SPISettings DEFAULT_SPI_SETTINGS = SPISettings(); 118 | 119 | class HardwareSPI 120 | { 121 | public: 122 | virtual ~HardwareSPI() { } 123 | 124 | virtual uint8_t transfer(uint8_t data) = 0; 125 | virtual uint16_t transfer16(uint16_t data) = 0; 126 | virtual void transfer(void *buf, size_t count) = 0; 127 | 128 | // Transaction Functions 129 | virtual void usingInterrupt(int interruptNumber) = 0; 130 | virtual void notUsingInterrupt(int interruptNumber) = 0; 131 | virtual void beginTransaction(SPISettings settings) = 0; 132 | virtual void endTransaction(void) = 0; 133 | 134 | // SPI Configuration methods 135 | virtual void attachInterrupt() = 0; 136 | virtual void detachInterrupt() = 0; 137 | 138 | virtual void begin() = 0; 139 | virtual void end() = 0; 140 | }; 141 | 142 | // Alias SPIClass to HardwareSPI since it's already the defacto standard for SPI class name 143 | typedef HardwareSPI SPIClass; 144 | 145 | } 146 | --------------------------------------------------------------------------------