diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/ByteBufferTest.cpp | 83 |
1 files changed, 62 insertions, 21 deletions
diff --git a/test/ByteBufferTest.cpp b/test/ByteBufferTest.cpp index 70a98ab..2a9bb8c 100644 --- a/test/ByteBufferTest.cpp +++ b/test/ByteBufferTest.cpp @@ -3,29 +3,40 @@ #define BOOST_TEST_MODULE "ByteBuffer" #include <boost/test/unit_test.hpp> +#include "Bluetooth.h" -class Utils { +#define checkBuffer(buffer, size, capacity, cursor) \ + D << "size=" << buffer.getSize() << ", capacity=" << buffer.getCapacity() << ", cursor=" << buffer.getCursor(); \ + BOOST_CHECK_EQUAL(buffer.getSize(), size); \ + BOOST_CHECK_EQUAL(buffer.getCapacity(), capacity); \ + BOOST_CHECK_EQUAL(buffer.getCursor(), cursor) + +using namespace std; + +class Bytes { public: - uint8_t bytes[1000]; + Bytes(size_t size) : capacity(size) { + _bytes = new uint8_t[size + 0x100]; + + bytes = (uint8_t *) (((uint64_t) &_bytes[0x100]) & 0xffffffffffffff00); - Utils() { - for (int i = 0; i < sizeof(bytes); i++) { + for (int i = 0; i < size; i++) { bytes[i] = (uint8_t) i; } } + ~Bytes() { + delete _bytes; + } + uint8_t *bytes; + uint8_t *_bytes; + size_t capacity; }; -void checkBuffer(ByteBuffer& buffer, size_t size, size_t capacity, size_t cursor) { - BOOST_CHECK(buffer.getSize() == size); - BOOST_CHECK(buffer.getCapacity() == capacity); - BOOST_CHECK(buffer.getCursor() == cursor); -} - BOOST_AUTO_TEST_CASE(empty_buffer) { - Utils u; - ByteBuffer buffer(u.bytes, sizeof(u.bytes), 0, 0); + Bytes b(0); + ByteBuffer buffer(b.bytes, b.capacity); - checkBuffer(buffer, 0, 1000, 0); + checkBuffer(buffer, 0, 0, 0); try { buffer.get8(); @@ -35,19 +46,49 @@ BOOST_AUTO_TEST_CASE(empty_buffer) { } BOOST_AUTO_TEST_CASE(basic) { - Utils u; - ByteBuffer buffer(u.bytes, sizeof(u.bytes), 10, 0); + Bytes b(1000); + ByteBuffer buffer(b.bytes, 1000, 0, 1000); + checkBuffer(buffer, 1000, 1000, 0); + + BOOST_CHECK_EQUAL(buffer.get8(), 0); + checkBuffer(buffer, 1000, 1000, 1); + + for (int i = 1; i < b.capacity; i++) { + BOOST_CHECK_EQUAL(buffer.get8(), b.bytes[i]); + } +} + +BOOST_AUTO_TEST_CASE(setCursor) { + Bytes b(1000); + ByteBuffer buffer(b.bytes, 1000, 0, 10); checkBuffer(buffer, 10, 1000, 0); - buffer.get8(); + BOOST_CHECK_EQUAL(buffer.get8(), 0); checkBuffer(buffer, 10, 1000, 1); -} -BOOST_AUTO_TEST_CASE(view) { - Utils u; - ByteBuffer buffer(u.bytes, sizeof(u.bytes), 10, 0); + buffer.setCursor(0); checkBuffer(buffer, 10, 1000, 0); - buffer.get8(); + BOOST_CHECK_EQUAL(buffer.get8(), 0); checkBuffer(buffer, 10, 1000, 1); + + buffer.setCursor(9); + checkBuffer(buffer, 10, 1000, 9); +} + +BOOST_AUTO_TEST_CASE(view) { + Bytes b(1000); + ByteBuffer buffer(b.bytes, b.capacity, 0, 10); + + BOOST_CHECK_EQUAL(buffer.get8(), 0); + ByteBuffer view1 = buffer.view(); + checkBuffer(view1, 9, 9, 0); + + BOOST_CHECK_EQUAL(view1.get8(), 1); + BOOST_CHECK_EQUAL(view1.get8(), 2); + + ByteBuffer view2 = view1.view(); + checkBuffer(view2, 7, 7, 0); + + BOOST_CHECK_EQUAL(view1.get8(), 3); } |