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_expandingstring_failure.cpp
Views: 1799
#include <AP_gtest.h>1#include <stdlib.h>2#include <AP_Common/ExpandingString.h>3#include <AP_HAL/AP_HAL.h>45/**6* This file test realloc failure on ExpandingString7*/89const AP_HAL::HAL& hal = AP_HAL::get_HAL();1011static uint32_t count = 0;1213void *realloc(void *ptr, size_t new_size) {14count++;15if (count < 3) {16if (new_size == 0) {17free(ptr);18return nullptr;19}20if (ptr == nullptr) {21return malloc(new_size);22}23void *new_mem = malloc(new_size);24if (new_mem != nullptr) {25memcpy(new_mem, ptr, new_size);26free(ptr);27}28return new_mem;29} else {30return nullptr;31}32}333435// THAT IS UGLY HACK BUT IT WORKS ... it is just used to make print_vprintf return negative value.36class BufferPrinter : public AP_HAL::BetterStream {37public:38BufferPrinter(char* str, size_t size) :39_offs(0), _str(str), _size(size) {}4041size_t write(uint8_t c) override { return 1; }42size_t write(const uint8_t *buffer, size_t size) override { return 1; }4344size_t _offs;45char* const _str;46const size_t _size;4748uint32_t available() override { return 0; }49bool read(uint8_t &b) override { return false; }50uint32_t txspace() override { return 0; }51bool discard_input() override { return false; }52};5354void print_vprintf(AP_HAL::BetterStream *s, const char *fmt, va_list ap);55void print_vprintf(AP_HAL::BetterStream *s, const char *fmt, va_list ap) {56BufferPrinter* p = static_cast<BufferPrinter*>(s);57if (count < 2) {58p->_offs = -1;59return;60}61if (count == 2) {62p->_offs = p->_size * 2;63} else {64p->_offs = p->_size;65}66return;67}6869TEST(ExpandingString, Tests)70{71// Test print_vprintf failure.72ExpandingString *test_string = NEW_NOTHROW ExpandingString();73test_string->printf("Test\n");74EXPECT_STREQ("", test_string->get_string());75EXPECT_STREQ("", test_string->get_writeable_string());76EXPECT_EQ(0u, test_string->get_length());77EXPECT_FALSE(test_string->has_failed_allocation());78// test failure on second printf expand()79test_string = NEW_NOTHROW ExpandingString();80test_string->printf("Test\n");81EXPECT_STREQ("", test_string->get_string());82EXPECT_STREQ("", test_string->get_writeable_string());83EXPECT_EQ(0u, test_string->get_length());84EXPECT_TRUE(test_string->has_failed_allocation());85// Test realloc failure86test_string = NEW_NOTHROW ExpandingString();87test_string->printf("Test\n");88EXPECT_STREQ(nullptr, test_string->get_string());89EXPECT_STREQ(nullptr, test_string->get_writeable_string());90EXPECT_EQ(0u, test_string->get_length());91EXPECT_TRUE(test_string->has_failed_allocation());92// test append failure93EXPECT_FALSE(test_string->append("Test2\n", 6));94// test failure on first printf realloc95test_string->printf("Test\n");96EXPECT_STREQ(nullptr, test_string->get_string());97EXPECT_STREQ(nullptr, test_string->get_writeable_string());98EXPECT_EQ(0u, test_string->get_length());99EXPECT_TRUE(test_string->has_failed_allocation());100// test failure on append realloc101test_string = NEW_NOTHROW ExpandingString();102EXPECT_FALSE(test_string->append("Test2\n", 6));103EXPECT_TRUE(test_string->has_failed_allocation());104EXPECT_STREQ(nullptr, test_string->get_string());105EXPECT_EQ(0u, test_string->get_length());106107test_string->~ExpandingString();108EXPECT_STRNE("Test\n", test_string->get_string());109}110111TEST(ExpandingString, TestsFailure)112{113114115}116AP_GTEST_MAIN()117118119