Commit 350ff18d authored by Anton Bukov's avatar Anton Bukov

Fix sum tests

parent 49a90223
......@@ -637,7 +637,7 @@ namespace boolinq {
tuple.index = 0;
}
uint8_t *ptr = reinterpret_cast<uint8_t *>(&tuple.value);
unsigned char *ptr = reinterpret_cast<unsigned char *>(&tuple.value);
int byteIndex = tuple.index;
if (tuple.bytesDirection == BytesLastToFirst) {
......@@ -657,7 +657,7 @@ namespace boolinq {
{*this, direction, BitsHighToLow, T(), 0},
[](LinqBytesBitsValueIndex<S, T> &tuple) {
TRet value;
uint8_t *ptr = reinterpret_cast<uint8_t *>(&value);
unsigned char *ptr = reinterpret_cast<unsigned char *>(&value);
for (int i = 0; i < sizeof(TRet); i++) {
int byteIndex = i;
......@@ -683,45 +683,47 @@ namespace boolinq {
tuple.index = 0;
}
uint8_t *ptr = reinterpret_cast<uint8_t *>(&tuple.value);
unsigned char *ptr = reinterpret_cast<unsigned char *>(&tuple.value);
int byteIndex = tuple.index / 8;
int byteIndex = tuple.index / CHAR_BIT;
if (tuple.bytesDirection == BytesLastToFirst) {
byteIndex = sizeof(T) - 1 - byteIndex;
}
int bitIndex = tuple.index % 8;
int bitIndex = tuple.index % CHAR_BIT;
if (tuple.bitsDirection == BitsHighToLow) {
bitIndex = 7 - bitIndex;
bitIndex = CHAR_BIT - 1 - bitIndex;
}
tuple.index++;
return (ptr[byteIndex] & (1 << bitIndex)) != 0;
return ((ptr[byteIndex] & (1 << bitIndex)) != 0) ? 1 : 0;
}
);
}
template<typename TRet = uint8_t>
template<typename TRet = unsigned char>
Linq<LinqBytesBitsValueIndex<S, T>, TRet> unbits(BitsDirection bitsDir = BitsHighToLow, BytesDirection bytesDir = BytesFirstToLast) const
{
return Linq<LinqBytesBitsValueIndex<S, T>, TRet>(
{*this, bytesDir, bitsDir, T(), 0},
[](LinqBytesBitsValueIndex<S, T> &tuple) {
TRet value;
uint8_t *ptr = reinterpret_cast<uint8_t *>(&value);
unsigned char *ptr = reinterpret_cast<unsigned char *>(&value);
for (int i = 0; i < sizeof(TRet); i++) {
int byteIndex = i / 8;
int byteIndex = i / CHAR_BIT;
if (tuple.bytesDirection == BytesLastToFirst) {
byteIndex = sizeof(TRet) - 1 - byteIndex;
}
int bitIndex = i % 8;
int bitIndex = i % CHAR_BIT;
if (tuple.bitsDirection == BitsHighToLow) {
bitIndex = 7 - bitIndex;
bitIndex = CHAR_BIT - 1 - bitIndex;
}
ptr[byteIndex] |= (tuple.linq.next()?1:0) << bitIndex;
if (tuple.linq.next()) {
ptr[byteIndex] |= 1 << bitIndex;
}
}
return value;
......
......@@ -78,8 +78,8 @@ TEST(DotCall, BitsRangeHL)
auto dstFL1 = from(src).bits();
auto dstFL2 = from(src).bits(BitsHighToLow);
auto dstFL3 = from(src).bits(BitsHighToLow,BytesFirstToLast);
auto dstLF1 = from(src).bits(BitsHighToLow,BytesLastToFirst);
auto dstFL3 = from(src).bits(BitsHighToLow, BytesFirstToLast);
auto dstLF1 = from(src).bits(BitsHighToLow, BytesLastToFirst);
CheckRangeEqArray(dstFL1, ansFL);
CheckRangeEqArray(dstFL2, ansFL);
......@@ -106,8 +106,8 @@ TEST(DotCall, BitsRangeLH)
};
auto dstFL1 = from(src).bits(BitsLowToHigh);
auto dstFL2 = from(src).bits(BitsLowToHigh,BytesFirstToLast);
auto dstLF1 = from(src).bits(BitsLowToHigh,BytesLastToFirst);
auto dstFL2 = from(src).bits(BitsLowToHigh, BytesFirstToLast);
auto dstLF1 = from(src).bits(BitsLowToHigh, BytesLastToFirst);
CheckRangeEqArray(dstFL1, ansFL);
CheckRangeEqArray(dstFL2, ansFL);
......@@ -132,8 +132,8 @@ TEST(DotCall, UnbitsRangeHLFL)
auto dst1_4b = from(src).unbits();
auto dst2_4b = from(src).unbits(BitsHighToLow);
auto dst1_1i = from(src).unbits<unsigned>(BitsHighToLow);
auto dst2_1i = from(src).unbits<unsigned>(BitsHighToLow,BytesFirstToLast);
auto dst3_1i = from(src).unbits<unsigned>(BitsHighToLow,BytesLastToFirst);
auto dst2_1i = from(src).unbits<unsigned>(BitsHighToLow, BytesFirstToLast);
auto dst3_1i = from(src).unbits<unsigned>(BitsHighToLow, BytesLastToFirst);
CheckRangeEqArray(dst1_4b, ans_4b);
CheckRangeEqArray(dst2_4b, ans_4b);
......
......@@ -36,7 +36,7 @@ TEST(Sum, FiveInts)
EXPECT_EQ(9, dst1);
}
TEST(Sum, BoolSum)
TEST(Sum, TransformSum)
{
std::vector<int> src;
src.push_back(1);
......@@ -45,10 +45,10 @@ TEST(Sum, BoolSum)
src.push_back(4);
src.push_back(5);
auto rng1 = from(src).sum([](int a){return a%2 == 0;});
auto rng2 = from(src).sum([](int a){return a%2 == 1;});
auto rng1 = from(src).sum([](int a){return a/2;});
auto rng2 = from(src).sum([](int a){return a%2;});
EXPECT_EQ(2, rng1);
EXPECT_EQ(6, rng1);
EXPECT_EQ(3, rng2);
}
......@@ -81,3 +81,17 @@ TEST(Sum, FiveStringsData)
EXPECT_EQ(ans, rng);
}
TEST(Sum, TransfromStringSum)
{
std::vector<std::string> src;
src.push_back("hello");
src.push_back("apple");
src.push_back("nokia");
src.push_back("oracle");
src.push_back("ponny");
auto sum = from(src).sum([](std::string s) { return s.size(); });
EXPECT_EQ(26, sum);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment