aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/ByteBufferTest.cpp59
1 files changed, 32 insertions, 27 deletions
diff --git a/test/ByteBufferTest.cpp b/test/ByteBufferTest.cpp
index 4a612d2..ca7a999 100644
--- a/test/ByteBufferTest.cpp
+++ b/test/ByteBufferTest.cpp
@@ -6,36 +6,41 @@
#include <boost/test/unit_test.hpp>
-#define checkBuffer(buffer, capacity, size, cursor) \
- if(false) {cout << "capacity=" << buffer.getCapacity() << ", size=" << buffer.getSize() << ", cursor=" << buffer.getCursor();} \
- BOOST_CHECK_EQUAL(buffer.getSize(), size); \
- BOOST_CHECK_EQUAL(buffer.getCapacity(), capacity); \
- BOOST_CHECK_EQUAL(buffer.getCursor(), cursor)
-
using namespace std;
+static void checkBuffer(const ByteBuffer& buffer, size_t size, size_t cursor) {
+ if (false) {
+ cout << "size=" << buffer.getSize() << ", cursor=" << buffer.getCursor();
+ }
+ BOOST_CHECK_EQUAL(buffer.getSize(), size);
+ BOOST_CHECK_EQUAL(buffer.getCursor(), cursor);
+}
+
class Bytes {
public:
- Bytes(size_t size) : capacity(size) {
- uint8_t *secret = new uint8_t[size + 0x100];
+ explicit Bytes(size_t size) : capacity(size), secret(new uint8_t[size + 0x100]) {
auto tmp = (uint8_t *) (((uint64_t) &secret[0x100]) & 0xffffffffffffff00);
for (int i = 0; i < size; i++) {
tmp[i] = (uint8_t) i;
}
+ bytes = tmp;
+ }
- bytes = shared_ptr<uint8_t>(tmp, [secret](uint8_t *) {
- delete[]secret;
- });
+ ~Bytes() {
+ delete secret;
}
- shared_ptr<uint8_t> bytes;
+
+ uint8_t* bytes;
size_t capacity;
+
+ uint8_t* secret;
};
BOOST_AUTO_TEST_CASE(empty_buffer) {
Bytes b(0);
ByteBuffer buffer(b.bytes, b.capacity);
- checkBuffer(buffer, 0, 0, 0);
+ checkBuffer(buffer, 0, 0);
try {
buffer.read8();
@@ -46,48 +51,48 @@ BOOST_AUTO_TEST_CASE(empty_buffer) {
BOOST_AUTO_TEST_CASE(basic) {
Bytes b(1000);
- ByteBuffer buffer(b.bytes, 1000, 1000);
- checkBuffer(buffer, 1000, 1000, 0);
+ ByteBuffer buffer(b.bytes, 1000);
+ checkBuffer(buffer, 1000, 0);
BOOST_CHECK_EQUAL(buffer.read8(), 0);
- checkBuffer(buffer, 1000, 1000, 1);
+ checkBuffer(buffer, 1000, 1);
for (int i = 1; i < b.capacity; i++) {
- BOOST_CHECK_EQUAL(buffer.read8(), b.bytes.get()[i]);
+ BOOST_CHECK_EQUAL(buffer.read8(), b.bytes[i]);
}
}
BOOST_AUTO_TEST_CASE(setCursor) {
Bytes b(1000);
- ByteBuffer buffer(b.bytes, 1000, 10, 0);
- checkBuffer(buffer, 1000, 10, 0);
+ ByteBuffer buffer(b.bytes, 1000);
+ checkBuffer(buffer, 1000, 0);
BOOST_CHECK_EQUAL(buffer.read8(), 0);
- checkBuffer(buffer, 1000, 10, 1);
+ checkBuffer(buffer, 1000, 1);
buffer.setCursor(0);
- checkBuffer(buffer, 1000, 10, 0);
+ checkBuffer(buffer, 1000, 0);
BOOST_CHECK_EQUAL(buffer.read8(), 0);
- checkBuffer(buffer, 1000, 10, 1);
+ checkBuffer(buffer, 1000, 1);
buffer.setCursor(9);
- checkBuffer(buffer, 1000, 10, 9);
+ checkBuffer(buffer, 1000, 9);
}
BOOST_AUTO_TEST_CASE(view) {
Bytes b(1000);
- ByteBuffer buffer(b.bytes, b.capacity, 10, 0);
+ ByteBuffer buffer(b.bytes, b.capacity);
BOOST_CHECK_EQUAL(buffer.read8(), 0);
ByteBuffer view1 = buffer.view();
- checkBuffer(view1, 9, 9, 0);
+ checkBuffer(view1, 999, 0);
BOOST_CHECK_EQUAL(view1.read8(), 1);
BOOST_CHECK_EQUAL(view1.read8(), 2);
ByteBuffer view2 = view1.view();
- checkBuffer(view2, 7, 7, 0);
+ checkBuffer(view2, 997, 0);
BOOST_CHECK_EQUAL(view1.read8(), 3);
}
@@ -119,7 +124,7 @@ void c(double input, uint32_t hx, double sigma = 0.00001) {
double output = buffer.readFLOAT();
stringstream str;
- auto actual_hex = ((uint32_t *) b.bytes.get())[0];
+ auto actual_hex = ((uint32_t *) b.bytes)[0];
str << "input=" << setw(20) << setprecision(10) << input <<
", output=" << setw(20) << setprecision(10) << output <<
", diff=" << setw(20) << setprecision(10) << fabs(input - output) <<