aboutsummaryrefslogtreecommitdiff
path: root/test/ByteBufferTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/ByteBufferTest.cpp')
-rw-r--r--test/ByteBufferTest.cpp63
1 files changed, 46 insertions, 17 deletions
diff --git a/test/ByteBufferTest.cpp b/test/ByteBufferTest.cpp
index f98a770..d6e45f7 100644
--- a/test/ByteBufferTest.cpp
+++ b/test/ByteBufferTest.cpp
@@ -6,21 +6,36 @@
#define BOOST_TEST_MODULE "ByteBuffer"
#include <boost/test/unit_test.hpp>
+#include "Bluetooth.h"
-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);
-}
+#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)
-BOOST_AUTO_TEST_CASE(empty_buffer) {
- uint8_t bytes[1000];
+class Bytes {
+public:
+ Bytes(size_t size) : capacity(size) {
+ _bytes = new uint8_t[size + 0x100];
- for (int i = 0; i < sizeof(bytes); i++) {
- bytes[i] = (uint8_t) i;
+ 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;
+};
- ByteBuffer buffer(bytes, sizeof(bytes), 0, 0);
+BOOST_AUTO_TEST_CASE(empty_buffer) {
+ Bytes b(1000);
+ ByteBuffer buffer(b.bytes, b.capacity, 0, 0);
checkBuffer(buffer, 0, 1000, 0);
@@ -32,15 +47,29 @@ BOOST_AUTO_TEST_CASE(empty_buffer) {
}
BOOST_AUTO_TEST_CASE(basic) {
- uint8_t bytes[1000];
-
- for (int i = 0; i < sizeof(bytes); i++) {
- bytes[i] = (uint8_t) i;
- }
-
- ByteBuffer buffer(bytes, sizeof(bytes), 10, 0);
+ 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);
+}