#include "ByteBuffer.h" #define BOOST_TEST_DYN_LINK //Define our Module name (prints at testing) #define BOOST_TEST_MODULE "ByteBuffer" #include #include "Bluetooth.h" #define checkBuffer(buffer, size, capacity, cursor) \ D << "size=" << buffer.getSize() << ", capacity=" << buffer.getCapacity() << ", cursor=" << buffer.getCursor(); \ BOOST_REQUIRE_EQUAL(buffer.getSize(), size); \ BOOST_REQUIRE_EQUAL(buffer.getCapacity(), capacity); \ BOOST_REQUIRE_EQUAL(buffer.getCursor(), cursor) class Bytes { public: Bytes(size_t size) : capacity(size) { _bytes = new uint8_t[size + 0x100]; bytes = (uint8_t *) (((uint64_t) &_bytes[0x100]) & 0xffffffffffffff00); for (int i = 0; i < size; i++) { bytes[i] = (uint8_t) i; } } ~Bytes() { delete _bytes; } uint8_t *bytes; uint8_t *_bytes; size_t capacity; }; BOOST_AUTO_TEST_CASE(empty_buffer) { Bytes b(1000); ByteBuffer buffer(b.bytes, b.capacity, 0, 0); checkBuffer(buffer, 0, 1000, 0); try { buffer.get8(); BOOST_FAIL("Expected exception"); } catch (ByteBufferException e) { } } BOOST_AUTO_TEST_CASE(basic) { Bytes b(1000); ByteBuffer buffer(b.bytes, 1000, 0, 10); checkBuffer(buffer, 10, 1000, 0); buffer.get8(); checkBuffer(buffer, 10, 1000, 1); } BOOST_AUTO_TEST_CASE(view) { Bytes b(1000); ByteBuffer buffer(b.bytes, b.capacity, 0, 10); BOOST_REQUIRE_EQUAL(buffer.get8(), 0); // checkBuffer(buffer, 10, 1000, 1); ByteBuffer view1 = buffer.view(); checkBuffer(view1, 9, 9, 0); BOOST_REQUIRE_EQUAL(view1.get8(), 1); BOOST_REQUIRE_EQUAL(view1.get8(), 2); ByteBuffer view2 = view1.view(); checkBuffer(view2, 7, 7, 0); BOOST_REQUIRE_EQUAL(view1.get8(), 3); }