summaryrefslogtreecommitdiffstats
path: root/src/ByteBuffer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ByteBuffer.cpp')
-rw-r--r--src/ByteBuffer.cpp42
1 files changed, 33 insertions, 9 deletions
diff --git a/src/ByteBuffer.cpp b/src/ByteBuffer.cpp
index 64c03d0d3..d2d3beb97 100644
--- a/src/ByteBuffer.cpp
+++ b/src/ByteBuffer.cpp
@@ -54,6 +54,7 @@ public:
{
TestRead();
TestWrite();
+ TestWrap();
}
void TestRead(void)
@@ -61,11 +62,11 @@ public:
cByteBuffer buf(50);
buf.Write("\x05\xac\x02\x00", 4);
UInt32 v1;
- ASSERT(buf.ReadVarInt(v1) && (v1 == 5));
+ assert(buf.ReadVarInt(v1) && (v1 == 5));
UInt32 v2;
- ASSERT(buf.ReadVarInt(v2) && (v2 == 300));
+ assert(buf.ReadVarInt(v2) && (v2 == 300));
UInt32 v3;
- ASSERT(buf.ReadVarInt(v3) && (v3 == 0));
+ assert(buf.ReadVarInt(v3) && (v3 == 0));
}
void TestWrite(void)
@@ -76,9 +77,30 @@ public:
buf.WriteVarInt(0);
AString All;
buf.ReadAll(All);
- ASSERT(All.size() == 4);
- ASSERT(memcmp(All.data(), "\x05\xac\x02\x00", All.size()) == 0);
+ assert(All.size() == 4);
+ assert(memcmp(All.data(), "\x05\xac\x02\x00", All.size()) == 0);
}
+
+ void TestWrap(void)
+ {
+ cByteBuffer buf(3);
+ for (int i = 0; i < 1000; i++)
+ {
+ int FreeSpace = buf.GetFreeSpace();
+ assert(buf.GetReadableSpace() == 0);
+ assert(FreeSpace > 0);
+ assert(buf.Write("a", 1));
+ assert(buf.CanReadBytes(1));
+ assert(buf.GetReadableSpace() == 1);
+ unsigned char v = 0;
+ assert(buf.ReadByte(v));
+ assert(v == 'a');
+ assert(buf.GetReadableSpace() == 0);
+ buf.CommitRead();
+ assert(buf.GetFreeSpace() == FreeSpace); // We're back to normal
+ }
+ }
+
} g_ByteBufferTest;
#endif
@@ -159,7 +181,7 @@ bool cByteBuffer::Write(const char * a_Bytes, int a_Count)
int CurReadableSpace = GetReadableSpace();
int WrittenBytes = 0;
- if (GetFreeSpace() < a_Count)
+ if (CurFreeSpace < a_Count)
{
return false;
}
@@ -773,7 +795,7 @@ void cByteBuffer::ReadAll(AString & a_Data)
-bool cByteBuffer::ReadToByteBuffer(cByteBuffer & a_Dst, int a_NumBytes)
+bool cByteBuffer::ReadToByteBuffer(cByteBuffer & a_Dst, size_t a_NumBytes)
{
if (!a_Dst.CanWriteBytes(a_NumBytes) || !CanReadBytes(a_NumBytes))
{
@@ -781,9 +803,10 @@ bool cByteBuffer::ReadToByteBuffer(cByteBuffer & a_Dst, int a_NumBytes)
return false;
}
char buf[1024];
- while (a_NumBytes > 0)
+ // > 0 without generating warnings about unsigned comparisons where size_t is unsigned
+ while (a_NumBytes != 0)
{
- int num = (a_NumBytes > sizeof(buf)) ? sizeof(buf) : a_NumBytes;
+ size_t num = (a_NumBytes > sizeof(buf)) ? sizeof(buf) : a_NumBytes;
VERIFY(ReadBuf(buf, num));
VERIFY(a_Dst.Write(buf, num));
a_NumBytes -= num;
@@ -863,3 +886,4 @@ void cByteBuffer::CheckValid(void) const
+