aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-02-17 20:14:34 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-02-17 23:30:04 +0100
commit254be95ff2f37df8adec7ce068448ba8abc8d734 (patch)
treef1483c2ff4c2e194cf90c59ce741d96917f7aaf5 /test
parent0d0543e4daeb1c6b01d4799736026b6f3aef9779 (diff)
parent0d0749ff0f842f10fea6929dc466e4e1be458234 (diff)
downloadble-toys-254be95ff2f37df8adec7ce068448ba8abc8d734.tar.gz
ble-toys-254be95ff2f37df8adec7ce068448ba8abc8d734.tar.bz2
ble-toys-254be95ff2f37df8adec7ce068448ba8abc8d734.tar.xz
ble-toys-254be95ff2f37df8adec7ce068448ba8abc8d734.zip
Merge remote-tracking branch 'origin/master'
Conflicts: Bluetooth.cpp Bluetooth.h ByteBuffer.cpp CMakeLists.txt LinuxBluetooth.cpp main.cpp test/ByteBufferTest.cpp
Diffstat (limited to 'test')
-rw-r--r--test/ByteBufferTest.cpp83
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);
}