class mrpt::hwdrivers::testing::MockStream

Overview

A CStream standing in for a serial port or a socket, so that sensor drivers can be exercised without any hardware attached.

Two ways to feed the driver:

  • pushRx() : queue bytes to be returned by the next Read() calls.

  • replyTo() : register a canned answer that is queued as soon as the driver writes that exact command, which is what request/response protocols need.

Everything the driver writes is kept in tx() for assertions.

#include </home/jlblanco/mrpt/modules/mrpt_hwdrivers/tests/mock_stream.h>

class MockStream: public mrpt::io::CStream
{
public:
    // construction

    MockStream();

    // methods

    void pushRx(const std::string& data);
    void replyTo(const std::string& cmd, const std::string& reply);
    const std::string& tx() const;
    void clearTx();
    size_t pendingRx() const;
    void setReadsReturnNothing(bool b);
    void setWritesThrow(bool b);
    virtual size_t Read(void* Buffer, size_t Count);
    virtual size_t Write(const void* Buffer, size_t Count);

    uint64_t Seek(
        int64_t,
        CStream::TSeekOrigin = sFromBeginning
        );

    virtual uint64_t getTotalBytesCount() const;
    virtual uint64_t getPosition() const;
};

Inherited Members

public:
    // methods

    virtual size_t Read(void* Buffer, size_t Count) = 0;
    virtual size_t Write(const void* Buffer, size_t Count) = 0;
    virtual uint64_t getTotalBytesCount() const = 0;
    virtual uint64_t getPosition() const = 0;

Methods

void pushRx(const std::string& data)

Queue bytes for the driver to read.

void replyTo(const std::string& cmd, const std::string& reply)

Queue reply as soon as the driver writes exactly cmd.

const std::string& tx() const

All the bytes written by the driver so far.

size_t pendingRx() const

Pending bytes not yet read by the driver.

void setReadsReturnNothing(bool b)

Make the next Read() return 0 bytes, to exercise timeout paths.

void setWritesThrow(bool b)

Make Write() throw, to exercise I/O error paths.

virtual size_t Read(void* Buffer, size_t Count)

Introduces a pure virtual method responsible for reading from the stream.

virtual size_t Write(const void* Buffer, size_t Count)

Introduces a pure virtual method responsible for writing to the stream.

Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.

virtual uint64_t getTotalBytesCount() const

Returns the total amount of bytes in the stream.

virtual uint64_t getPosition() const

Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.