Finished tests and fixed RAB accordingly.
This commit is contained in:
parent
0e421e68f5
commit
1a63595188
|
@ -46,39 +46,43 @@ void RandomAccessBinary::setByte(uint position, const uint8_t &data)
|
||||||
void RandomAccessBinary::setBit(uint64_t position, const bool &value)
|
void RandomAccessBinary::setBit(uint64_t position, const bool &value)
|
||||||
{
|
{
|
||||||
uint bytePosition = static_cast<uint>(position>>3);
|
uint bytePosition = static_cast<uint>(position>>3);
|
||||||
uint bitPosition = position%8;
|
uint bitPosition = position % 8;
|
||||||
this->setBit(bytePosition, bitPosition, value);
|
this->setBit(bytePosition, bitPosition, 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RandomAccessBinary::setBits(uint64_t position, uint bits, const uint8_t &value)
|
void RandomAccessBinary::setBits(uint64_t position, uint bits, const uint8_t &value)
|
||||||
{
|
{
|
||||||
uint64_t bytePosition = position>>3;
|
uint64_t bytePosition = position >> 3;
|
||||||
uint bitPosition = position%8;
|
uint bitPosition = position % 8;
|
||||||
this->setBits(bytePosition, bitPosition, bits, value);
|
this->setBits(bytePosition, bitPosition, bits, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
@ -155,18 +159,37 @@ bool RandomAccessBinary::getBit(uint byteOffset, uint bitOffset)
|
||||||
bool RandomAccessBinary::getBit(uint64_t position)
|
bool RandomAccessBinary::getBit(uint64_t position)
|
||||||
{
|
{
|
||||||
uint byteOffset = static_cast<uint>(position>>3);
|
uint byteOffset = static_cast<uint>(position>>3);
|
||||||
uint bitOffset = position%8;
|
uint bitOffset = position % 8;
|
||||||
return this->getBit(byteOffset, bitOffset);
|
return this->getBit(byteOffset, bitOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue