From 1a63595188cd91d5ab4b002bd807ba67d16ce1fd Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Tue, 13 Feb 2018 17:15:22 +0100 Subject: [PATCH] Finished tests and fixed RAB accordingly. --- src/core/RandomAccessBinary.cpp | 49 ++++++++++++++++++++++++--------- test/core/test_rab.cpp | 24 +++++++++++++++- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/src/core/RandomAccessBinary.cpp b/src/core/RandomAccessBinary.cpp index 8a588e5..27a14c8 100644 --- a/src/core/RandomAccessBinary.cpp +++ b/src/core/RandomAccessBinary.cpp @@ -46,39 +46,43 @@ void RandomAccessBinary::setByte(uint position, const uint8_t &data) void RandomAccessBinary::setBit(uint64_t position, const bool &value) { uint bytePosition = static_cast(position>>3); - uint bitPosition = position%8; + uint bitPosition = position % 8; this->setBit(bytePosition, bitPosition, value); } void RandomAccessBinary::setBit(uint bytePosition, uint bitPosition, const bool &value) { + if(ptr_data->size() < bytePosition+1) + ptr_data->insert(ptr_data->begin()+bytePosition, 0x00); uint8_t byte = this->ptr_data->at(bytePosition); - uint8_t newbyte = 1; + uint8_t newbyte = 0; if (value) - newbyte = byte | (newbyte << (7-bitPosition)); + newbyte = byte | (1<< (7-bitPosition)); else { - newbyte = byte & ~(newbyte << (7-bitPosition)); + newbyte = byte & ~(1 << (7-bitPosition)); } ptr_data->insert(ptr_data->begin()+bytePosition, newbyte); } void RandomAccessBinary::setBits(uint64_t position, uint bits, const uint8_t &value) { - uint64_t bytePosition = position>>3; - uint bitPosition = position%8; + uint64_t bytePosition = position >> 3; + uint bitPosition = position % 8; this->setBits(bytePosition, bitPosition, bits, value); } void RandomAccessBinary::setBits(uint bytePosition, uint bitPosition, uint bits, const uint8_t &value) { + if(ptr_data->size() < bytePosition+1) + ptr_data->insert(ptr_data->begin()+bytePosition, 0x00); uint8_t byte = this->ptr_data->at(bytePosition); uint8_t newbyte = value; uint8_t mask = 0; - for(uint i = bits-1; i >= 0; --i) - mask |= 1 << i; - mask <<= (7-bits-bitPosition); + for(uint i = 0; i < bits; ++i) + mask |= (1 << i); + mask <<= (8-bits-bitPosition); byte &= ~mask; - newbyte <<= (7-bits-bitPosition); + newbyte <<= (8-bits-bitPosition); byte |= newbyte; ptr_data->insert(ptr_data->begin()+bytePosition, byte); } @@ -155,18 +159,37 @@ bool RandomAccessBinary::getBit(uint byteOffset, uint bitOffset) bool RandomAccessBinary::getBit(uint64_t position) { uint byteOffset = static_cast(position>>3); - uint bitOffset = position%8; + uint bitOffset = position % 8; return this->getBit(byteOffset, bitOffset); } const std::vector RandomAccessBinary::getBits(uint byteOffset, uint8_t bitOffset, uint64_t length) { - return std::vector(); + uint64_t byteLength = length >> 3; + int olength = length - (byteLength<<3) - bitOffset; + std::vector tmp(ptr_data->begin()+byteOffset, ptr_data->begin()+byteOffset+byteLength+1); + std::vector output; + uint i = 0; + for(auto it = tmp.begin(); it != tmp.end(); ++it, ++i){ + if(i == 0){ + for(int a = 7-bitOffset; a >= 0; --a) + output.push_back((*it & (1 << a)) > 0); + } else if(olength > 0 && i == byteLength) { + for(int a = 7; a >= olength; --a) + output.push_back((*it & (1 << a)) > 0); + } else { + for(int a = 7; a >= 0; --a) + output.push_back((*it & (1 << a)) > 0); + } + } + return output; } const std::vector RandomAccessBinary::getBits(uint64_t position, uint64_t length) { - return std::vector(); + uint byteOffset = static_cast(position>>3); + uint8_t bitOffset = position % 8; + return this->getBits(byteOffset,bitOffset,length); } uint16_t RandomAccessBinary::get_uint16(uint byteOffset) diff --git a/test/core/test_rab.cpp b/test/core/test_rab.cpp index 36a86ba..87ddf37 100644 --- a/test/core/test_rab.cpp +++ b/test/core/test_rab.cpp @@ -88,5 +88,27 @@ TEST_F(RABTest, Strings) { } TEST_F(RABTest, Bits) { - + bool bit = true; + test3.setBit(4, bit); + EXPECT_EQ(0x08, test3.getByte(0)); + uint8_t bits = 0b101; + test3.setBits(3, 3, bits); + EXPECT_EQ(0x14, test3.getByte(0)); } + +TEST_F(RABTest, BitVectors) { + test3.set_uint64(0, 0xdeadbeefcafebabe); + auto vec = test3.getBits(18, 20); + std::stringstream stream; + for( auto it = vec.begin(); it != vec.end(); ++it) + stream << (*it); + EXPECT_EQ("11111011101111110010", stream.str()); + + test4.set_uint64(0, 0xdeadbeefcafebabe); + auto vec2 = test4.getBits(18, 20); + std::stringstream stream2; + for( auto it = vec2.begin(); it != vec2.end(); ++it) + stream2 << (*it); + EXPECT_EQ("11111011001010111011", stream2.str()); +} +