Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Common/tests/test_nmea_vaprint.cpp
Views: 1799
#include <AP_gtest.h>1#include <AP_HAL/UARTDriver.h>2#include <AP_Common/NMEA.h>34const AP_HAL::HAL& hal = AP_HAL::get_HAL();5static uint32_t count = 0;67void *malloc(size_t size) {8if (count == 1) {9return nullptr;10}11return calloc(size, 1);12}1314// THAT IS UGLY HACK BUT IT WORKS ... it is just used to make print_vprintf return negative value.15class BufferPrinter : public AP_HAL::BetterStream {16public:17BufferPrinter(char* str, size_t size) :18_offs(0), _str(str), _size(size) {}1920size_t write(uint8_t c) override { return 1; }21size_t write(const uint8_t *buffer, size_t size) override { return 1; }2223size_t _offs;24char* const _str;25const size_t _size;2627uint32_t available() override { return 0; }28bool read(uint8_t &c) override { return false; };29uint32_t txspace() override { return 0; }30bool discard_input() override { return false; }31};3233void print_vprintf(AP_HAL::BetterStream *s, const char *fmt, va_list ap);34void print_vprintf(AP_HAL::BetterStream *s, const char *fmt, va_list ap) {35BufferPrinter* p = static_cast<BufferPrinter*>(s);36count++;37if (count < 3) {38p->_offs = 4;39return;40}41p->_offs = -1;42return;43}4445class DummyUart: public AP_HAL::UARTDriver {46public:47bool is_initialized() override { return true; };48bool tx_pending() override { return false; };49uint32_t txspace() override { return _txspace; };5051void set_txspace(uint32_t space) {52_txspace = space;53}54uint32_t _txspace;55protected:56void _begin(uint32_t baud, uint16_t rxSpace, uint16_t txSpace) override { };57ssize_t _read(uint8_t *buf, uint16_t size) override { return 0; };58void _end() override { };59void _flush() override { };60uint32_t _available() override { return 1; };61size_t _write(const uint8_t *buffer, size_t size) override { return 1; };62bool _discard_input() override { return false; }63};6465static DummyUart test_uart;6667TEST(NMEA, VAPrintf)68{69// test Malloc failure70EXPECT_FALSE(nmea_printf(&test_uart, "test"));71// test second vsnprintf failure;72EXPECT_FALSE(nmea_printf(&test_uart, "test"));73// test first vsnprintf failure;74EXPECT_FALSE(nmea_printf(&test_uart, "test"));75}7677AP_GTEST_MAIN()787980