FixedSizeData::take/drop()
This commit is contained in:
parent
25db4064b7
commit
248f0b0d61
@ -30,8 +30,12 @@ public:
|
|||||||
const unsigned char *data() const;
|
const unsigned char *data() const;
|
||||||
unsigned char *data();
|
unsigned char *data();
|
||||||
|
|
||||||
|
template<size_t size> FixedSizeData<size> take() const;
|
||||||
|
template<size_t size> FixedSizeData<SIZE-size> drop() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FixedSizeData() {}
|
FixedSizeData() {}
|
||||||
|
template<unsigned int _SIZE> friend class FixedSizeData;
|
||||||
|
|
||||||
unsigned char _data[BINARY_LENGTH];
|
unsigned char _data[BINARY_LENGTH];
|
||||||
};
|
};
|
||||||
@ -97,6 +101,22 @@ FixedSizeData<SIZE> FixedSizeData<SIZE>::FromBinary(const void *source) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<unsigned int SIZE> template<size_t size>
|
||||||
|
FixedSizeData<size> FixedSizeData<SIZE>::take() const {
|
||||||
|
static_assert(size <= SIZE, "Out of bounds");
|
||||||
|
FixedSizeData<size> result;
|
||||||
|
std::memcpy(result._data, _data, size);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<unsigned int SIZE> template<size_t size>
|
||||||
|
FixedSizeData<SIZE-size> FixedSizeData<SIZE>::drop() const {
|
||||||
|
static_assert(size <= SIZE, "Out of bounds");
|
||||||
|
FixedSizeData<SIZE-size> result;
|
||||||
|
std::memcpy(result._data, _data+size, SIZE-size);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
template<unsigned int SIZE>
|
template<unsigned int SIZE>
|
||||||
bool operator==(const FixedSizeData<SIZE> &lhs, const FixedSizeData<SIZE> &rhs) {
|
bool operator==(const FixedSizeData<SIZE> &lhs, const FixedSizeData<SIZE> &rhs) {
|
||||||
return 0 == std::memcmp(lhs.data(), rhs.data(), FixedSizeData<SIZE>::BINARY_LENGTH);
|
return 0 == std::memcmp(lhs.data(), rhs.data(), FixedSizeData<SIZE>::BINARY_LENGTH);
|
||||||
|
@ -116,6 +116,52 @@ TEST_P(FixedSizeDataTestWithParam, CopyConstructor) {
|
|||||||
EXPECT_EQ(GetParam(), copy);
|
EXPECT_EQ(GetParam(), copy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_P(FixedSizeDataTestWithParam, Take_Half) {
|
||||||
|
FixedSizeData<SIZE> source(GetParam());
|
||||||
|
FixedSizeData<SIZE/2> taken = source.take<SIZE/2>();
|
||||||
|
EXPECT_EQ(0, std::memcmp(source.data(), taken.data(), SIZE/2));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_P(FixedSizeDataTestWithParam, Drop_Half) {
|
||||||
|
FixedSizeData<SIZE> source(GetParam());
|
||||||
|
FixedSizeData<SIZE/2> taken = source.drop<SIZE/2>();
|
||||||
|
EXPECT_EQ(0, std::memcmp(source.data() + SIZE/2, taken.data(), SIZE/2));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_P(FixedSizeDataTestWithParam, Take_One) {
|
||||||
|
FixedSizeData<SIZE> source(GetParam());
|
||||||
|
FixedSizeData<1> taken = source.take<1>();
|
||||||
|
EXPECT_EQ(0, std::memcmp(source.data(), taken.data(), 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_P(FixedSizeDataTestWithParam, Drop_One) {
|
||||||
|
FixedSizeData<SIZE> source(GetParam());
|
||||||
|
FixedSizeData<SIZE-1> taken = source.drop<1>();
|
||||||
|
EXPECT_EQ(0, std::memcmp(source.data() + 1, taken.data(), SIZE-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_P(FixedSizeDataTestWithParam, Take_Nothing) {
|
||||||
|
FixedSizeData<SIZE> source(GetParam());
|
||||||
|
FixedSizeData<0> taken = source.take<0>();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_P(FixedSizeDataTestWithParam, Drop_Nothing) {
|
||||||
|
FixedSizeData<SIZE> source(GetParam());
|
||||||
|
FixedSizeData<SIZE> taken = source.drop<0>();
|
||||||
|
EXPECT_EQ(0, std::memcmp(source.data(), taken.data(), SIZE));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_P(FixedSizeDataTestWithParam, Take_All) {
|
||||||
|
FixedSizeData<SIZE> source(GetParam());
|
||||||
|
FixedSizeData<SIZE> taken = source.take<SIZE>();
|
||||||
|
EXPECT_EQ(0, std::memcmp(source.data(), taken.data(), SIZE));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_P(FixedSizeDataTestWithParam, Drop_All) {
|
||||||
|
FixedSizeData<SIZE> source(GetParam());
|
||||||
|
FixedSizeData<0> taken = source.drop<SIZE>();
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(FixedSizeDataTest, CopyConstructorDoesntChangeSource) {
|
TEST_F(FixedSizeDataTest, CopyConstructorDoesntChangeSource) {
|
||||||
FixedSizeData<SIZE> data1 = FixedSizeData<SIZE>::FromString(DATA1_AS_STRING);
|
FixedSizeData<SIZE> data1 = FixedSizeData<SIZE>::FromString(DATA1_AS_STRING);
|
||||||
FixedSizeData<SIZE> data2(data1);
|
FixedSizeData<SIZE> data2(data1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user