2016-03-13 13:59:42 -04:00
|
|
|
|
|
|
|
// ByteBufferTest.cpp
|
|
|
|
|
|
|
|
// Implements the main app entrypoint for the cByteBuffer class test
|
|
|
|
|
|
|
|
#include "Globals.h"
|
2019-08-26 15:38:34 -04:00
|
|
|
#include "../TestHelpers.h"
|
2016-03-13 13:59:42 -04:00
|
|
|
#include "ByteBuffer.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void TestRead(void)
|
|
|
|
{
|
|
|
|
cByteBuffer buf(50);
|
|
|
|
buf.Write("\x05\xac\x02\x00", 4);
|
|
|
|
UInt32 v1;
|
2019-08-26 15:38:34 -04:00
|
|
|
TEST_TRUE(buf.ReadVarInt(v1));
|
|
|
|
TEST_EQUAL(v1, 5);
|
2016-03-13 13:59:42 -04:00
|
|
|
UInt32 v2;
|
2019-08-26 15:38:34 -04:00
|
|
|
TEST_TRUE(buf.ReadVarInt(v2));
|
|
|
|
TEST_EQUAL(v2, 300);
|
2016-03-13 13:59:42 -04:00
|
|
|
UInt32 v3;
|
2019-08-26 15:38:34 -04:00
|
|
|
TEST_TRUE(buf.ReadVarInt(v3));
|
|
|
|
TEST_EQUAL(v3, 0);
|
2016-03-13 13:59:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void TestWrite(void)
|
|
|
|
{
|
|
|
|
cByteBuffer buf(50);
|
|
|
|
buf.WriteVarInt32(5);
|
|
|
|
buf.WriteVarInt32(300);
|
|
|
|
buf.WriteVarInt32(0);
|
2021-01-11 11:39:43 -05:00
|
|
|
ContiguousByteBuffer All;
|
2016-03-13 13:59:42 -04:00
|
|
|
buf.ReadAll(All);
|
2019-08-26 15:38:34 -04:00
|
|
|
TEST_EQUAL(All.size(), 4);
|
|
|
|
TEST_EQUAL(memcmp(All.data(), "\x05\xac\x02\x00", All.size()), 0);
|
2016-03-13 13:59:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void TestWrap(void)
|
|
|
|
{
|
|
|
|
cByteBuffer buf(3);
|
|
|
|
for (int i = 0; i < 1000; i++)
|
|
|
|
{
|
|
|
|
size_t FreeSpace = buf.GetFreeSpace();
|
2019-08-26 15:38:34 -04:00
|
|
|
TEST_EQUAL(buf.GetReadableSpace(), 0);
|
|
|
|
TEST_GREATER_THAN_OR_EQUAL(FreeSpace, 1);
|
|
|
|
TEST_TRUE(buf.Write("a", 1));
|
|
|
|
TEST_TRUE(buf.CanReadBytes(1));
|
|
|
|
TEST_EQUAL(buf.GetReadableSpace(), 1);
|
2016-03-13 13:59:42 -04:00
|
|
|
UInt8 v = 0;
|
2019-08-26 15:38:34 -04:00
|
|
|
TEST_TRUE(buf.ReadBEUInt8(v));
|
|
|
|
TEST_EQUAL(v, 'a');
|
|
|
|
TEST_EQUAL(buf.GetReadableSpace(), 0);
|
2016-03-13 13:59:42 -04:00
|
|
|
buf.CommitRead();
|
2019-08-26 15:38:34 -04:00
|
|
|
TEST_EQUAL(buf.GetFreeSpace(), FreeSpace); // We're back to normal
|
2016-03-13 13:59:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-08-26 15:38:34 -04:00
|
|
|
IMPLEMENT_TEST_MAIN("ByteBuffer",
|
2016-03-13 13:59:42 -04:00
|
|
|
TestRead();
|
|
|
|
TestWrite();
|
|
|
|
TestWrap();
|
2019-08-26 15:38:34 -04:00
|
|
|
)
|