159 lines
3.1 KiB
C++
159 lines
3.1 KiB
C++
#include "../lib/Buffer.hpp"
|
|
#include <cassert>
|
|
#include <iostream>
|
|
|
|
void test_basic_operations()
|
|
{
|
|
std::cout << "Testing basic buffer operations...\n";
|
|
|
|
reactor::Buffer buf;
|
|
assert(buf.readableBytes() == 0);
|
|
assert(buf.writableBytes() > 0);
|
|
|
|
std::string data = "Hello World";
|
|
buf.append(data);
|
|
assert(buf.readableBytes() == data.size());
|
|
|
|
std::string result = buf.read(5);
|
|
assert(result == "Hello");
|
|
assert(buf.readableBytes() == 6);
|
|
|
|
std::string remaining = buf.readAll();
|
|
assert(remaining == " World");
|
|
assert(buf.readableBytes() == 0);
|
|
|
|
std::cout << "✓ Basic operations passed\n";
|
|
}
|
|
|
|
void test_network_byte_order()
|
|
{
|
|
std::cout << "Testing network byte order operations...\n";
|
|
|
|
reactor::Buffer buf;
|
|
|
|
uint8_t val8 = 0x42;
|
|
uint16_t val16 = 0x1234;
|
|
uint32_t val32 = 0x12345678;
|
|
uint64_t val64 = 0x123456789ABCDEF0;
|
|
|
|
buf.appendInt8(val8);
|
|
buf.appendInt16(val16);
|
|
buf.appendInt32(val32);
|
|
buf.appendInt64(val64);
|
|
|
|
assert(buf.readInt8() == val8);
|
|
assert(buf.readInt16() == val16);
|
|
assert(buf.readInt32() == val32);
|
|
assert(buf.readInt64() == val64);
|
|
|
|
std::cout << "✓ Network byte order passed\n";
|
|
}
|
|
|
|
void test_prepend_operations()
|
|
{
|
|
std::cout << "Testing prepend operations...\n";
|
|
|
|
reactor::Buffer buf;
|
|
buf.append("World");
|
|
|
|
std::string hello = "Hello ";
|
|
buf.prepend(hello.data(), hello.size());
|
|
|
|
std::string result = buf.readAll();
|
|
assert(result == "Hello World");
|
|
|
|
reactor::Buffer buf2;
|
|
buf2.append("Test");
|
|
buf2.prependInt32(42);
|
|
|
|
assert(buf2.readInt32() == 42);
|
|
assert(buf2.read(4) == "Test");
|
|
|
|
std::cout << "✓ Prepend operations passed\n";
|
|
}
|
|
|
|
void test_buffer_growth()
|
|
{
|
|
std::cout << "Testing buffer growth...\n";
|
|
|
|
reactor::Buffer buf(64);
|
|
std::string large_data(2048, 'X');
|
|
|
|
buf.append(large_data);
|
|
assert(buf.readableBytes() == large_data.size());
|
|
|
|
std::string result = buf.readAll();
|
|
assert(result == large_data);
|
|
|
|
std::cout << "✓ Buffer growth passed\n";
|
|
}
|
|
|
|
void test_buffer_compaction()
|
|
{
|
|
std::cout << "Testing buffer compaction...\n";
|
|
|
|
reactor::Buffer buf(128);
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
buf.append("data");
|
|
buf.read(2);
|
|
}
|
|
|
|
assert(buf.readableBytes() == 20);
|
|
|
|
buf.append("more data");
|
|
assert(buf.readableBytes() == 29);
|
|
|
|
std::cout << "✓ Buffer compaction passed\n";
|
|
}
|
|
|
|
void test_peek_operations()
|
|
{
|
|
std::cout << "Testing peek operations...\n";
|
|
|
|
reactor::Buffer buf;
|
|
buf.appendInt32(0x12345678);
|
|
buf.appendInt16(0xABCD);
|
|
|
|
assert(buf.peekInt32() == 0x12345678);
|
|
assert(buf.readableBytes() == 6);
|
|
|
|
buf.readInt32();
|
|
assert(buf.peekInt16() == 0xABCD);
|
|
assert(buf.readableBytes() == 2);
|
|
|
|
std::cout << "✓ Peek operations passed\n";
|
|
}
|
|
|
|
void test_buffer_swap()
|
|
{
|
|
std::cout << "Testing buffer swap...\n";
|
|
|
|
reactor::Buffer buf1, buf2;
|
|
buf1.append("Buffer1");
|
|
buf2.append("Buffer2");
|
|
|
|
buf1.swap(buf2);
|
|
|
|
assert(buf1.readAll() == "Buffer2");
|
|
assert(buf2.readAll() == "Buffer1");
|
|
|
|
std::cout << "✓ Buffer swap passed\n";
|
|
}
|
|
|
|
int main()
|
|
{
|
|
std::cout << "=== Buffer Tests ===\n";
|
|
|
|
test_basic_operations();
|
|
test_network_byte_order();
|
|
test_prepend_operations();
|
|
test_buffer_growth();
|
|
test_buffer_compaction();
|
|
test_peek_operations();
|
|
test_buffer_swap();
|
|
|
|
std::cout << "All buffer tests passed! ✓\n";
|
|
return 0;
|
|
}
|