2018-07-19 16:04:20 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Test IO
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Test functions for IoRead that are not covered by testing the IoBufferRead object
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
static bool
|
|
|
|
testIoReadOpen(void *driver)
|
|
|
|
{
|
2018-08-14 14:21:53 -04:00
|
|
|
if (driver == (void *)998)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2018-07-19 16:04:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2018-09-15 21:07:00 -04:00
|
|
|
testIoRead(void *driver, Buffer *buffer)
|
2018-07-19 16:04:20 -04:00
|
|
|
{
|
|
|
|
ASSERT(driver == (void *)999);
|
2018-09-26 18:46:52 +01:00
|
|
|
bufCat(buffer, bufNewZ("Z"));
|
2018-07-19 16:04:20 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool testIoReadCloseCalled = false;
|
|
|
|
|
|
|
|
static void
|
|
|
|
testIoReadClose(void *driver)
|
|
|
|
{
|
|
|
|
ASSERT(driver == (void *)999);
|
|
|
|
testIoReadCloseCalled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Test functions for IoWrite that are not covered by testing the IoBufferWrite object
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
static bool testIoWriteOpenCalled = false;
|
|
|
|
|
|
|
|
static void
|
|
|
|
testIoWriteOpen(void *driver)
|
|
|
|
{
|
|
|
|
ASSERT(driver == (void *)999);
|
|
|
|
testIoWriteOpenCalled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-09-15 21:07:00 -04:00
|
|
|
testIoWrite(void *driver, const Buffer *buffer)
|
2018-07-19 16:04:20 -04:00
|
|
|
{
|
|
|
|
ASSERT(driver == (void *)999);
|
|
|
|
ASSERT(strEq(strNewBuf(buffer), strNew("ABC")));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool testIoWriteCloseCalled = false;
|
|
|
|
|
|
|
|
static void
|
|
|
|
testIoWriteClose(void *driver)
|
|
|
|
{
|
|
|
|
ASSERT(driver == (void *)999);
|
|
|
|
testIoWriteCloseCalled = true;
|
|
|
|
}
|
|
|
|
|
2018-07-24 21:08:27 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Test filter that counts total bytes
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
typedef struct IoTestFilterSize
|
|
|
|
{
|
|
|
|
MemContext *memContext;
|
|
|
|
size_t size;
|
|
|
|
IoFilter *filter;
|
|
|
|
} IoTestFilterSize;
|
|
|
|
|
|
|
|
static void
|
|
|
|
ioTestFilterSizeProcess(IoTestFilterSize *this, const Buffer *buffer)
|
|
|
|
{
|
2018-08-14 14:21:53 -04:00
|
|
|
FUNCTION_DEBUG_BEGIN(logLevelTrace);
|
|
|
|
FUNCTION_DEBUG_PARAM(VOIDP, this);
|
|
|
|
FUNCTION_DEBUG_PARAM(BUFFER, buffer);
|
|
|
|
FUNCTION_DEBUG_PARAM(STRING, ioFilterType(this->filter));
|
|
|
|
FUNCTION_DEBUG_PARAM(SIZE, this->size);
|
|
|
|
|
|
|
|
FUNCTION_TEST_ASSERT(this != NULL);
|
|
|
|
FUNCTION_TEST_ASSERT(buffer != NULL);
|
|
|
|
FUNCTION_DEBUG_END();
|
|
|
|
|
2018-07-24 21:08:27 -04:00
|
|
|
this->size += bufUsed(buffer);
|
2018-08-14 14:21:53 -04:00
|
|
|
|
|
|
|
FUNCTION_DEBUG_RESULT_VOID();
|
2018-07-24 21:08:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const Variant *
|
|
|
|
ioTestFilterSizeResult(IoTestFilterSize *this)
|
|
|
|
{
|
|
|
|
return varNewUInt64(this->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static IoTestFilterSize *
|
|
|
|
ioTestFilterSizeNew(const char *type)
|
|
|
|
{
|
|
|
|
IoTestFilterSize *this = NULL;
|
|
|
|
|
|
|
|
MEM_CONTEXT_NEW_BEGIN("IoTestFilterSize")
|
|
|
|
{
|
|
|
|
this = memNew(sizeof(IoTestFilterSize));
|
|
|
|
this->memContext = MEM_CONTEXT_NEW();
|
|
|
|
|
2018-09-15 21:07:00 -04:00
|
|
|
this->filter = ioFilterNewP(
|
|
|
|
strNew(type), this, .in = (IoFilterInterfaceProcessIn)ioTestFilterSizeProcess,
|
|
|
|
.result = (IoFilterInterfaceResult)ioTestFilterSizeResult);
|
2018-08-14 14:21:53 -04:00
|
|
|
}
|
|
|
|
MEM_CONTEXT_NEW_END();
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
2018-11-28 14:02:14 -05:00
|
|
|
Test filter to multiply input to the output. It can also flush out a variable number of bytes at the end.
|
2018-08-14 14:21:53 -04:00
|
|
|
***********************************************************************************************************************************/
|
2018-11-28 14:02:14 -05:00
|
|
|
typedef struct IoTestFilterMultiply
|
2018-08-14 14:21:53 -04:00
|
|
|
{
|
|
|
|
MemContext *memContext;
|
|
|
|
unsigned int flushTotal;
|
2018-11-28 14:02:14 -05:00
|
|
|
char flushChar;
|
|
|
|
Buffer *multiplyBuffer;
|
|
|
|
unsigned int multiplier;
|
2018-08-14 14:21:53 -04:00
|
|
|
IoFilter *bufferFilter;
|
|
|
|
IoFilter *filter;
|
2018-11-28 14:02:14 -05:00
|
|
|
} IoTestFilterMultiply;
|
2018-08-14 14:21:53 -04:00
|
|
|
|
|
|
|
static void
|
2018-11-28 14:02:14 -05:00
|
|
|
ioTestFilterMultiplyProcess(IoTestFilterMultiply *this, const Buffer *input, Buffer *output)
|
2018-08-14 14:21:53 -04:00
|
|
|
{
|
|
|
|
FUNCTION_DEBUG_BEGIN(logLevelTrace);
|
|
|
|
FUNCTION_DEBUG_PARAM(VOIDP, this);
|
|
|
|
FUNCTION_DEBUG_PARAM(BUFFER, input);
|
|
|
|
FUNCTION_DEBUG_PARAM(BUFFER, output);
|
|
|
|
|
|
|
|
FUNCTION_TEST_ASSERT(this != NULL);
|
|
|
|
FUNCTION_TEST_ASSERT(output != NULL && bufRemains(output) > 0);
|
|
|
|
FUNCTION_DEBUG_END();
|
|
|
|
|
|
|
|
if (input == NULL)
|
|
|
|
{
|
2018-11-28 14:02:14 -05:00
|
|
|
char flushZ[] = {this->flushChar, 0};
|
|
|
|
bufCat(output, bufNewC(1, flushZ));
|
2018-08-14 14:21:53 -04:00
|
|
|
this->flushTotal--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-28 14:02:14 -05:00
|
|
|
if (this->multiplyBuffer == NULL)
|
2018-08-14 14:21:53 -04:00
|
|
|
{
|
2018-11-28 14:02:14 -05:00
|
|
|
this->multiplyBuffer = bufNew(bufUsed(input) * this->multiplier);
|
2018-08-14 14:21:53 -04:00
|
|
|
unsigned char *inputPtr = bufPtr(input);
|
2018-11-28 14:02:14 -05:00
|
|
|
unsigned char *bufferPtr = bufPtr(this->multiplyBuffer);
|
2018-08-14 14:21:53 -04:00
|
|
|
|
|
|
|
for (unsigned int charIdx = 0; charIdx < bufUsed(input); charIdx++)
|
|
|
|
{
|
2018-11-28 14:02:14 -05:00
|
|
|
for (unsigned int multiplierIdx = 0; multiplierIdx < this->multiplier; multiplierIdx++)
|
|
|
|
bufferPtr[charIdx * this->multiplier + multiplierIdx] = inputPtr[charIdx];
|
2018-08-14 14:21:53 -04:00
|
|
|
}
|
|
|
|
|
2018-11-28 14:02:14 -05:00
|
|
|
bufUsedSet(this->multiplyBuffer, bufSize(this->multiplyBuffer));
|
2018-08-14 14:21:53 -04:00
|
|
|
}
|
|
|
|
|
2018-11-28 14:02:14 -05:00
|
|
|
ioFilterProcessInOut(this->bufferFilter, this->multiplyBuffer, output);
|
2018-08-14 14:21:53 -04:00
|
|
|
|
|
|
|
if (!ioFilterInputSame(this->bufferFilter))
|
2018-11-28 14:02:14 -05:00
|
|
|
this->multiplyBuffer = NULL;
|
2018-08-14 14:21:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
FUNCTION_DEBUG_RESULT_VOID();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2018-11-28 14:02:14 -05:00
|
|
|
ioTestFilterMultiplyDone(IoTestFilterMultiply *this)
|
2018-08-14 14:21:53 -04:00
|
|
|
{
|
|
|
|
return this->flushTotal == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2018-11-28 14:02:14 -05:00
|
|
|
ioTestFilterMultiplyInputSame(IoTestFilterMultiply *this)
|
2018-08-14 14:21:53 -04:00
|
|
|
{
|
|
|
|
return ioFilterInputSame(this->bufferFilter);
|
|
|
|
}
|
|
|
|
|
2018-11-28 14:02:14 -05:00
|
|
|
static IoTestFilterMultiply *
|
|
|
|
ioTestFilterMultiplyNew(const char *type, unsigned int multiplier, unsigned int flushTotal, char flushChar)
|
2018-08-14 14:21:53 -04:00
|
|
|
{
|
2018-11-28 14:02:14 -05:00
|
|
|
IoTestFilterMultiply *this = NULL;
|
2018-08-14 14:21:53 -04:00
|
|
|
|
2018-11-28 14:02:14 -05:00
|
|
|
MEM_CONTEXT_NEW_BEGIN("IoTestFilterMultiply")
|
2018-08-14 14:21:53 -04:00
|
|
|
{
|
2018-11-28 14:02:14 -05:00
|
|
|
this = memNew(sizeof(IoTestFilterMultiply));
|
2018-08-14 14:21:53 -04:00
|
|
|
this->memContext = MEM_CONTEXT_NEW();
|
|
|
|
this->bufferFilter = ioBufferFilter(ioBufferNew());
|
2018-11-28 14:02:14 -05:00
|
|
|
this->multiplier = multiplier;
|
2018-08-14 14:21:53 -04:00
|
|
|
this->flushTotal = flushTotal;
|
2018-11-28 14:02:14 -05:00
|
|
|
this->flushChar = flushChar;
|
2018-08-14 14:21:53 -04:00
|
|
|
|
2018-09-15 21:07:00 -04:00
|
|
|
this->filter = ioFilterNewP(
|
2018-11-28 14:02:14 -05:00
|
|
|
strNew(type), this, .done = (IoFilterInterfaceDone)ioTestFilterMultiplyDone,
|
|
|
|
.inOut = (IoFilterInterfaceProcessInOut)ioTestFilterMultiplyProcess,
|
|
|
|
.inputSame = (IoFilterInterfaceInputSame)ioTestFilterMultiplyInputSame);
|
2018-07-24 21:08:27 -04:00
|
|
|
}
|
|
|
|
MEM_CONTEXT_NEW_END();
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:04:20 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Test Run
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
void
|
2018-08-03 19:19:14 -04:00
|
|
|
testRun(void)
|
2018-07-19 16:04:20 -04:00
|
|
|
{
|
|
|
|
FUNCTION_HARNESS_VOID();
|
|
|
|
|
|
|
|
// *****************************************************************************************************************************
|
|
|
|
if (testBegin("ioBufferSize() and ioBufferSizeSet()"))
|
|
|
|
{
|
|
|
|
TEST_RESULT_SIZE(ioBufferSize(), 65536, "check initial buffer size");
|
|
|
|
TEST_RESULT_VOID(ioBufferSizeSet(16384), "set buffer size");
|
|
|
|
TEST_RESULT_SIZE(ioBufferSize(), 16384, "check buffer size");
|
|
|
|
}
|
|
|
|
|
|
|
|
// *****************************************************************************************************************************
|
2018-08-14 14:21:53 -04:00
|
|
|
if (testBegin("IoRead, IoBufferRead, IoBuffer, IoSize, IoFilter, and IoFilterGroup"))
|
2018-07-19 16:04:20 -04:00
|
|
|
{
|
|
|
|
IoRead *read = NULL;
|
|
|
|
Buffer *buffer = bufNew(2);
|
2018-08-14 14:21:53 -04:00
|
|
|
ioBufferSizeSet(2);
|
2018-07-19 16:04:20 -04:00
|
|
|
|
|
|
|
TEST_ASSIGN(
|
2018-09-15 21:07:00 -04:00
|
|
|
read,
|
|
|
|
ioReadNewP((void *)998, .close = (IoReadInterfaceClose)testIoReadClose, .open = (IoReadInterfaceOpen)testIoReadOpen,
|
|
|
|
.read = (IoReadInterfaceRead)testIoRead),
|
|
|
|
"create io read object");
|
2018-07-19 16:04:20 -04:00
|
|
|
|
|
|
|
TEST_RESULT_BOOL(ioReadOpen(read), false, " open io object");
|
2018-08-14 14:21:53 -04:00
|
|
|
|
|
|
|
TEST_ASSIGN(
|
2018-09-15 21:07:00 -04:00
|
|
|
read,
|
|
|
|
ioReadNewP((void *)999, .close = (IoReadInterfaceClose)testIoReadClose, .open = (IoReadInterfaceOpen)testIoReadOpen,
|
|
|
|
.read = (IoReadInterfaceRead)testIoRead),
|
|
|
|
"create io read object");
|
2018-08-14 14:21:53 -04:00
|
|
|
|
|
|
|
TEST_RESULT_BOOL(ioReadOpen(read), true, " open io object");
|
|
|
|
TEST_RESULT_SIZE(ioRead(read, buffer), 2, " read 2 bytes");
|
2018-07-19 16:04:20 -04:00
|
|
|
TEST_RESULT_BOOL(ioReadEof(read), false, " no eof");
|
|
|
|
TEST_RESULT_VOID(ioReadClose(read), " close io object");
|
|
|
|
TEST_RESULT_BOOL(testIoReadCloseCalled, true, " check io object closed");
|
|
|
|
|
2018-11-15 16:25:46 -05:00
|
|
|
TEST_RESULT_VOID(ioReadFree(read), " free read object");
|
|
|
|
TEST_RESULT_VOID(ioReadFree(NULL), " free null read object");
|
|
|
|
|
2018-07-19 16:04:20 -04:00
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
IoBufferRead *bufferRead = NULL;
|
2018-08-14 14:21:53 -04:00
|
|
|
ioBufferSizeSet(2);
|
2018-07-19 16:04:20 -04:00
|
|
|
buffer = bufNew(2);
|
2018-09-26 18:46:52 +01:00
|
|
|
Buffer *bufferOriginal = bufNewZ("123");
|
2018-07-19 16:04:20 -04:00
|
|
|
|
|
|
|
MEM_CONTEXT_TEMP_BEGIN()
|
|
|
|
{
|
|
|
|
TEST_ASSIGN(bufferRead, ioBufferReadNew(bufferOriginal), "create buffer read object");
|
|
|
|
TEST_RESULT_VOID(ioBufferReadMove(bufferRead, MEM_CONTEXT_OLD()), " move object to new context");
|
|
|
|
TEST_RESULT_VOID(ioBufferReadMove(NULL, MEM_CONTEXT_OLD()), " move NULL object to new context");
|
|
|
|
}
|
|
|
|
MEM_CONTEXT_TEMP_END();
|
|
|
|
|
2018-07-24 21:08:27 -04:00
|
|
|
IoFilterGroup *filterGroup = NULL;
|
|
|
|
TEST_ASSIGN(filterGroup, ioFilterGroupNew(), " create new filter group");
|
2018-08-14 14:21:53 -04:00
|
|
|
IoSize *sizeFilter = ioSizeNew();
|
2018-11-27 22:02:08 -05:00
|
|
|
TEST_RESULT_PTR(ioFilterGroupAdd(filterGroup, ioSizeFilter(sizeFilter)), filterGroup, " add filter to filter group");
|
2018-08-14 14:21:53 -04:00
|
|
|
TEST_RESULT_VOID(
|
2018-11-28 14:02:14 -05:00
|
|
|
ioFilterGroupAdd(filterGroup, ioTestFilterMultiplyNew("double", 2, 1, 'X')->filter), " add filter to filter group");
|
2018-08-14 14:21:53 -04:00
|
|
|
TEST_RESULT_VOID(ioFilterGroupAdd(filterGroup, ioSizeFilter(ioSizeNew())), " add filter to filter group");
|
|
|
|
IoBuffer *bufferFilter = ioBufferNew();
|
|
|
|
TEST_RESULT_VOID(ioFilterGroupAdd(filterGroup, ioBufferFilter(bufferFilter)), " add filter to filter group");
|
2018-07-24 21:08:27 -04:00
|
|
|
TEST_RESULT_VOID(ioReadFilterGroupSet(ioBufferReadIo(bufferRead), filterGroup), " add filter group to read io");
|
|
|
|
TEST_RESULT_PTR(ioFilterMove(NULL, memContextTop()), NULL, " move NULL filter to top context");
|
|
|
|
|
2018-08-14 14:21:53 -04:00
|
|
|
TEST_RESULT_BOOL(ioReadOpen(ioBufferReadIo(bufferRead)), true, " open");
|
2018-07-19 16:04:20 -04:00
|
|
|
TEST_RESULT_BOOL(ioReadEof(ioBufferReadIo(bufferRead)), false, " not eof");
|
|
|
|
TEST_RESULT_SIZE(ioRead(ioBufferReadIo(bufferRead), buffer), 2, " read 2 bytes");
|
2018-07-24 21:08:27 -04:00
|
|
|
TEST_RESULT_SIZE(ioRead(ioBufferReadIo(bufferRead), buffer), 0, " read 0 bytes (full buffer)");
|
2018-08-14 14:21:53 -04:00
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "11", " check read");
|
|
|
|
TEST_RESULT_STR(strPtr(ioFilterType(ioSizeFilter(sizeFilter))), "size", "check filter type");
|
2018-07-19 16:04:20 -04:00
|
|
|
TEST_RESULT_BOOL(ioReadEof(ioBufferReadIo(bufferRead)), false, " not eof");
|
|
|
|
|
|
|
|
TEST_RESULT_VOID(bufUsedZero(buffer), " zero buffer");
|
2018-08-14 14:21:53 -04:00
|
|
|
TEST_RESULT_SIZE(ioRead(ioBufferReadIo(bufferRead), buffer), 2, " read 2 bytes");
|
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "22", " check read");
|
|
|
|
|
|
|
|
TEST_ASSIGN(buffer, bufNew(3), "change output buffer size to 3");
|
|
|
|
TEST_RESULT_SIZE(ioRead(ioBufferReadIo(bufferRead), buffer), 3, " read 3 bytes");
|
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "33X", " check read");
|
|
|
|
|
|
|
|
TEST_RESULT_VOID(bufUsedZero(buffer), " zero buffer");
|
2018-07-19 16:04:20 -04:00
|
|
|
TEST_RESULT_BOOL(ioReadEof(ioBufferReadIo(bufferRead)), true, " eof");
|
2018-11-23 12:01:36 -05:00
|
|
|
TEST_RESULT_BOOL(ioBufferRead(bufferRead, buffer, true), 0, " eof from driver");
|
2018-07-19 16:04:20 -04:00
|
|
|
TEST_RESULT_SIZE(ioRead(ioBufferReadIo(bufferRead), buffer), 0, " read 0 bytes");
|
|
|
|
TEST_RESULT_VOID(ioReadClose(ioBufferReadIo(bufferRead)), " close buffer read object");
|
|
|
|
|
2018-07-24 21:08:27 -04:00
|
|
|
TEST_RESULT_PTR(ioReadFilterGroup(ioBufferReadIo(bufferRead)), filterGroup, " check filter group");
|
2018-08-14 14:21:53 -04:00
|
|
|
TEST_RESULT_UINT(
|
|
|
|
varUInt64(varLstGet(varVarLst(ioFilterGroupResult(filterGroup, ioFilterType(ioSizeFilter(sizeFilter)))), 0)), 3,
|
|
|
|
" check filter result");
|
|
|
|
TEST_RESULT_PTR(ioFilterGroupResult(filterGroup, strNew("double")), NULL, " check filter result is NULL");
|
|
|
|
TEST_RESULT_UINT(
|
|
|
|
varUInt64(varLstGet(varVarLst(ioFilterGroupResult(filterGroup, ioFilterType(ioSizeFilter(sizeFilter)))), 1)), 7,
|
|
|
|
" check filter result");
|
2018-07-24 21:08:27 -04:00
|
|
|
|
2018-07-19 16:04:20 -04:00
|
|
|
TEST_RESULT_VOID(ioBufferReadFree(bufferRead), " free buffer read object");
|
|
|
|
TEST_RESULT_VOID(ioBufferReadFree(NULL), " free NULL buffer read object");
|
2018-07-24 21:08:27 -04:00
|
|
|
|
2018-08-14 14:21:53 -04:00
|
|
|
TEST_RESULT_VOID(ioSizeFree(sizeFilter), " free size filter object");
|
|
|
|
TEST_RESULT_VOID(ioSizeFree(NULL), " free null size filter object");
|
|
|
|
|
|
|
|
TEST_RESULT_VOID(ioBufferFree(bufferFilter), " free buffer filter object");
|
|
|
|
TEST_RESULT_VOID(ioBufferFree(NULL), " free null buffer filter object");
|
|
|
|
|
2018-07-24 21:08:27 -04:00
|
|
|
TEST_RESULT_VOID(ioFilterGroupFree(filterGroup), " free filter group object");
|
|
|
|
TEST_RESULT_VOID(ioFilterGroupFree(NULL), " free NULL filter group object");
|
2018-10-07 17:50:10 +01:00
|
|
|
|
|
|
|
// Mixed line and buffer read
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
ioBufferSizeSet(5);
|
|
|
|
read = ioBufferReadIo(ioBufferReadNew(bufNewZ("AAA123\n1234\n\n12\nBDDDEFF")));
|
|
|
|
ioReadOpen(read);
|
|
|
|
buffer = bufNew(3);
|
|
|
|
|
|
|
|
// Start with a buffer read
|
|
|
|
TEST_RESULT_INT(ioRead(read, buffer), 3, "read buffer");
|
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "AAA", " check buffer");
|
|
|
|
|
|
|
|
// Do line reads of various lengths
|
|
|
|
TEST_RESULT_STR(strPtr(ioReadLine(read)), "123", "read line");
|
|
|
|
TEST_RESULT_STR(strPtr(ioReadLine(read)), "1234", "read line");
|
|
|
|
TEST_RESULT_STR(strPtr(ioReadLine(read)), "", "read line");
|
|
|
|
TEST_RESULT_STR(strPtr(ioReadLine(read)), "12", "read line");
|
|
|
|
|
|
|
|
// Read what was left in the line buffer
|
|
|
|
TEST_RESULT_INT(ioRead(read, buffer), 0, "read buffer");
|
|
|
|
bufUsedSet(buffer, 2);
|
|
|
|
TEST_RESULT_INT(ioRead(read, buffer), 1, "read buffer");
|
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "AAB", " check buffer");
|
|
|
|
bufUsedSet(buffer, 0);
|
|
|
|
|
|
|
|
// Now do a full buffer read from the input
|
|
|
|
TEST_RESULT_INT(ioRead(read, buffer), 3, "read buffer");
|
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "DDD", " check buffer");
|
|
|
|
|
|
|
|
// Read line doesn't work without a linefeed
|
2018-11-12 21:18:53 -05:00
|
|
|
TEST_ERROR(ioReadLine(read), FileReadError, "unexpected eof while reading line");
|
2018-10-07 17:50:10 +01:00
|
|
|
|
|
|
|
// But those bytes can be picked up by a buffer read
|
2018-11-12 21:18:53 -05:00
|
|
|
buffer = bufNew(2);
|
|
|
|
TEST_RESULT_INT(ioRead(read, buffer), 2, "read buffer");
|
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "EF", " check buffer");
|
|
|
|
|
|
|
|
buffer = bufNew(1);
|
|
|
|
TEST_RESULT_INT(ioRead(read, buffer), 1, "read buffer");
|
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "F", " check buffer");
|
2018-10-07 17:50:10 +01:00
|
|
|
|
|
|
|
// Nothing left to read
|
2018-11-12 21:18:53 -05:00
|
|
|
TEST_ERROR(ioReadLine(read), FileReadError, "unexpected eof while reading line");
|
2018-10-07 17:50:10 +01:00
|
|
|
TEST_RESULT_INT(ioRead(read, buffer), 0, "read buffer");
|
2018-11-12 21:18:53 -05:00
|
|
|
|
|
|
|
// Error if buffer is full and there is no linefeed
|
|
|
|
ioBufferSizeSet(10);
|
|
|
|
read = ioBufferReadIo(ioBufferReadNew(bufNewZ("0123456789")));
|
|
|
|
ioReadOpen(read);
|
|
|
|
TEST_ERROR(ioReadLine(read), FileReadError, "unable to find line in 10 byte buffer");
|
2018-07-19 16:04:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// *****************************************************************************************************************************
|
2018-08-14 14:21:53 -04:00
|
|
|
if (testBegin("IoWrite, IoBufferWrite, IoBuffer, IoSize, IoFilter, and IoFilterGroup"))
|
2018-07-19 16:04:20 -04:00
|
|
|
{
|
|
|
|
IoWrite *write = NULL;
|
2018-08-14 14:21:53 -04:00
|
|
|
ioBufferSizeSet(3);
|
2018-07-19 16:04:20 -04:00
|
|
|
|
|
|
|
TEST_ASSIGN(
|
2018-09-15 21:07:00 -04:00
|
|
|
write,
|
|
|
|
ioWriteNewP(
|
|
|
|
(void *)999, .close = (IoWriteInterfaceClose)testIoWriteClose, .open = (IoWriteInterfaceOpen)testIoWriteOpen,
|
|
|
|
.write = (IoWriteInterfaceWrite)testIoWrite),
|
|
|
|
"create io write object");
|
2018-07-19 16:04:20 -04:00
|
|
|
|
|
|
|
TEST_RESULT_VOID(ioWriteOpen(write), " open io object");
|
|
|
|
TEST_RESULT_BOOL(testIoWriteOpenCalled, true, " check io object open");
|
2018-09-26 18:46:52 +01:00
|
|
|
TEST_RESULT_VOID(ioWrite(write, bufNewZ("ABC")), " write 3 bytes");
|
2018-07-19 16:04:20 -04:00
|
|
|
TEST_RESULT_VOID(ioWriteClose(write), " close io object");
|
|
|
|
TEST_RESULT_BOOL(testIoWriteCloseCalled, true, " check io object closed");
|
|
|
|
|
2018-11-15 16:25:46 -05:00
|
|
|
TEST_RESULT_VOID(ioWriteFree(write), " free write object");
|
|
|
|
TEST_RESULT_VOID(ioWriteFree(NULL), " free null write object");
|
|
|
|
|
2018-07-19 16:04:20 -04:00
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
2018-08-14 14:21:53 -04:00
|
|
|
ioBufferSizeSet(3);
|
2018-07-19 16:04:20 -04:00
|
|
|
IoBufferWrite *bufferWrite = NULL;
|
|
|
|
Buffer *buffer = bufNew(0);
|
|
|
|
|
|
|
|
MEM_CONTEXT_TEMP_BEGIN()
|
|
|
|
{
|
|
|
|
TEST_ASSIGN(bufferWrite, ioBufferWriteNew(buffer), "create buffer write object");
|
|
|
|
TEST_RESULT_VOID(ioBufferWriteMove(bufferWrite, MEM_CONTEXT_OLD()), " move object to new context");
|
|
|
|
TEST_RESULT_VOID(ioBufferWriteMove(NULL, MEM_CONTEXT_OLD()), " move NULL object to new context");
|
|
|
|
}
|
|
|
|
MEM_CONTEXT_TEMP_END();
|
|
|
|
|
2018-07-24 21:08:27 -04:00
|
|
|
IoFilterGroup *filterGroup = NULL;
|
|
|
|
TEST_ASSIGN(filterGroup, ioFilterGroupNew(), " create new filter group");
|
2018-08-14 14:21:53 -04:00
|
|
|
IoSize *sizeFilter = ioSizeNew();
|
|
|
|
TEST_RESULT_VOID(ioFilterGroupAdd(filterGroup, ioSizeFilter(sizeFilter)), " add filter to filter group");
|
|
|
|
TEST_RESULT_VOID(
|
2018-11-28 14:02:14 -05:00
|
|
|
ioFilterGroupAdd(filterGroup, ioTestFilterMultiplyNew("double", 2, 3, 'X')->filter), " add filter to filter group");
|
2018-08-14 14:21:53 -04:00
|
|
|
TEST_RESULT_VOID(ioFilterGroupAdd(filterGroup, ioTestFilterSizeNew("size2")->filter), " add filter to filter group");
|
2018-07-24 21:08:27 -04:00
|
|
|
TEST_RESULT_VOID(ioWriteFilterGroupSet(ioBufferWriteIo(bufferWrite), filterGroup), " add filter group to write io");
|
|
|
|
|
2018-07-19 16:04:20 -04:00
|
|
|
TEST_RESULT_VOID(ioWriteOpen(ioBufferWriteIo(bufferWrite)), " open buffer write object");
|
2018-10-07 17:50:10 +01:00
|
|
|
TEST_RESULT_VOID(ioWriteLine(ioBufferWriteIo(bufferWrite), strNew("AB")), " write string");
|
2018-09-26 18:46:52 +01:00
|
|
|
TEST_RESULT_VOID(ioWrite(ioBufferWriteIo(bufferWrite), bufNew(0)), " write 0 bytes");
|
2018-07-19 16:04:20 -04:00
|
|
|
TEST_RESULT_VOID(ioWrite(ioBufferWriteIo(bufferWrite), NULL), " write 0 bytes");
|
2018-10-07 17:50:10 +01:00
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "AABB\n\n", " check write");
|
2018-07-19 16:04:20 -04:00
|
|
|
|
2018-11-14 08:53:42 -05:00
|
|
|
TEST_RESULT_VOID(ioWrite(ioBufferWriteIo(bufferWrite), bufNewStr(strNew("Z"))), " write string");
|
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "AABB\n\n", " no change because output buffer is not full");
|
|
|
|
TEST_RESULT_VOID(ioWriteFlush(ioBufferWriteIo(bufferWrite)), " flush output");
|
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "AABB\n\nZZ", " check output is flushed");
|
|
|
|
TEST_RESULT_VOID(ioWriteFlush(ioBufferWriteIo(bufferWrite)), " flush again (nothing to flush)");
|
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "AABB\n\nZZ", " check output is unchanged");
|
|
|
|
|
2018-11-28 14:02:14 -05:00
|
|
|
TEST_RESULT_VOID(ioWrite(ioBufferWriteIo(bufferWrite), bufNewZ("12345")), " write bytes");
|
2018-11-14 08:53:42 -05:00
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "AABB\n\nZZ112233445", " check write");
|
2018-07-19 16:04:20 -04:00
|
|
|
|
|
|
|
TEST_RESULT_VOID(ioWriteClose(ioBufferWriteIo(bufferWrite)), " close buffer write object");
|
2018-11-14 08:53:42 -05:00
|
|
|
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "AABB\n\nZZ1122334455XXX", " check write after close");
|
2018-07-19 16:04:20 -04:00
|
|
|
|
2018-07-24 21:08:27 -04:00
|
|
|
TEST_RESULT_PTR(ioWriteFilterGroup(ioBufferWriteIo(bufferWrite)), filterGroup, " check filter group");
|
2018-08-14 14:21:53 -04:00
|
|
|
TEST_RESULT_UINT(
|
2018-11-14 08:53:42 -05:00
|
|
|
varUInt64(ioFilterGroupResult(filterGroup, ioFilterType(ioSizeFilter(sizeFilter)))), 9, " check filter result");
|
|
|
|
TEST_RESULT_UINT(varUInt64(ioFilterGroupResult(filterGroup, strNew("size2"))), 21, " check filter result");
|
2018-07-24 21:08:27 -04:00
|
|
|
|
2018-07-19 16:04:20 -04:00
|
|
|
TEST_RESULT_VOID(ioBufferWriteFree(bufferWrite), " free buffer write object");
|
|
|
|
TEST_RESULT_VOID(ioBufferWriteFree(NULL), " free NULL buffer write object");
|
|
|
|
}
|
|
|
|
|
|
|
|
// *****************************************************************************************************************************
|
|
|
|
if (testBegin("ioHandleWriteOneStr()"))
|
|
|
|
{
|
|
|
|
TEST_ERROR(
|
|
|
|
ioHandleWriteOneStr(999999, strNew("test")), FileWriteError,
|
|
|
|
"unable to write to 4 byte(s) to handle: [9] Bad file descriptor");
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
String *fileName = strNewFmt("%s/test.txt", testPath());
|
|
|
|
int fileHandle = open(strPtr(fileName), O_CREAT | O_TRUNC | O_WRONLY, 0700);
|
|
|
|
|
|
|
|
TEST_RESULT_VOID(ioHandleWriteOneStr(fileHandle, strNew("test1\ntest2")), "write string to file");
|
|
|
|
}
|
|
|
|
|
|
|
|
FUNCTION_HARNESS_RESULT_VOID();
|
|
|
|
}
|