Finished tests and fixed RAB accordingly.

This commit is contained in:
Marcel Otte 2018-02-13 17:15:22 +01:00
parent 0e421e68f5
commit 1a63595188
2 changed files with 59 additions and 14 deletions

View File

@ -52,12 +52,14 @@ void RandomAccessBinary::setBit(uint64_t position, const bool &value)
void RandomAccessBinary::setBit(uint bytePosition, uint bitPosition, const bool &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 byte = this->ptr_data->at(bytePosition);
uint8_t newbyte = 1; uint8_t newbyte = 0;
if (value) if (value)
newbyte = byte | (newbyte << (7-bitPosition)); newbyte = byte | (1<< (7-bitPosition));
else { else {
newbyte = byte & ~(newbyte << (7-bitPosition)); newbyte = byte & ~(1 << (7-bitPosition));
} }
ptr_data->insert(ptr_data->begin()+bytePosition, newbyte); ptr_data->insert(ptr_data->begin()+bytePosition, newbyte);
} }
@ -71,14 +73,16 @@ void RandomAccessBinary::setBits(uint64_t position, uint bits, const uint8_t &va
void RandomAccessBinary::setBits(uint bytePosition, uint bitPosition, uint bits, const uint8_t &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 byte = this->ptr_data->at(bytePosition);
uint8_t newbyte = value; uint8_t newbyte = value;
uint8_t mask = 0; uint8_t mask = 0;
for(uint i = bits-1; i >= 0; --i) for(uint i = 0; i < bits; ++i)
mask |= 1 << i; mask |= (1 << i);
mask <<= (7-bits-bitPosition); mask <<= (8-bits-bitPosition);
byte &= ~mask; byte &= ~mask;
newbyte <<= (7-bits-bitPosition); newbyte <<= (8-bits-bitPosition);
byte |= newbyte; byte |= newbyte;
ptr_data->insert(ptr_data->begin()+bytePosition, byte); ptr_data->insert(ptr_data->begin()+bytePosition, byte);
} }
@ -161,12 +165,31 @@ bool RandomAccessBinary::getBit(uint64_t position)
const std::vector<bool> RandomAccessBinary::getBits(uint byteOffset, uint8_t bitOffset, uint64_t length) const std::vector<bool> RandomAccessBinary::getBits(uint byteOffset, uint8_t bitOffset, uint64_t length)
{ {
return std::vector<bool>(); uint64_t byteLength = length >> 3;
int olength = length - (byteLength<<3) - bitOffset;
std::vector<uint8_t> tmp(ptr_data->begin()+byteOffset, ptr_data->begin()+byteOffset+byteLength+1);
std::vector<bool> 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<bool> RandomAccessBinary::getBits(uint64_t position, uint64_t length) const std::vector<bool> RandomAccessBinary::getBits(uint64_t position, uint64_t length)
{ {
return std::vector<bool>(); uint byteOffset = static_cast<uint>(position>>3);
uint8_t bitOffset = position % 8;
return this->getBits(byteOffset,bitOffset,length);
} }
uint16_t RandomAccessBinary::get_uint16(uint byteOffset) uint16_t RandomAccessBinary::get_uint16(uint byteOffset)

View File

@ -88,5 +88,27 @@ TEST_F(RABTest, Strings) {
} }
TEST_F(RABTest, Bits) { 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());
}