Commit 7d8da6a0 authored by Anton Bukov's avatar Anton Bukov

Add groupBy tests and fixes

parent 68892852
...@@ -257,7 +257,7 @@ namespace boolinq { ...@@ -257,7 +257,7 @@ namespace boolinq {
> >
Linq<LinqCopyUnorderedSet<S, T, _TKey>, std::pair<_TKey, _TValue> > groupBy(F apply) const Linq<LinqCopyUnorderedSet<S, T, _TKey>, std::pair<_TKey, _TValue> > groupBy(F apply) const
{ {
return Linq<LinqCopyUnorderedSet<S, T, _TKey>, std::pair<_TKey, Linq<S, T> > >( return Linq<LinqCopyUnorderedSet<S, T, _TKey>, std::pair<_TKey, _TValue> >(
{*this, *this, std::unordered_set<_TKey>()}, {*this, *this, std::unordered_set<_TKey>()},
[apply](LinqCopyUnorderedSet<S, T, _TKey> &tuple){ [apply](LinqCopyUnorderedSet<S, T, _TKey> &tuple){
T value = tuple.linq.next(); T value = tuple.linq.next();
...@@ -267,6 +267,7 @@ namespace boolinq { ...@@ -267,6 +267,7 @@ namespace boolinq {
return apply(v) == key; return apply(v) == key;
})); }));
} }
throw LinqEndException();
} }
); );
} }
...@@ -710,11 +711,8 @@ namespace boolinq { ...@@ -710,11 +711,8 @@ namespace boolinq {
bitIndex = CHAR_BIT - 1 - bitIndex; bitIndex = CHAR_BIT - 1 - bitIndex;
} }
if (tuple.linq.next()) { ptr[byteIndex] &= ~(1 << bitIndex);
ptr[byteIndex] |= (1 << bitIndex); ptr[byteIndex] |= bool(tuple.linq.next()) << bitIndex;
} else {
ptr[byteIndex] &= (~0 ^ (1 << bitIndex));
}
} }
return value; return value;
......
...@@ -32,7 +32,7 @@ SET ( ...@@ -32,7 +32,7 @@ SET (
${PROJECT_SOURCE_DIR}/test/DotCallTest.cpp ${PROJECT_SOURCE_DIR}/test/DotCallTest.cpp
${PROJECT_SOURCE_DIR}/test/ElementAtTest.cpp ${PROJECT_SOURCE_DIR}/test/ElementAtTest.cpp
${PROJECT_SOURCE_DIR}/test/ForeachTest.cpp ${PROJECT_SOURCE_DIR}/test/ForeachTest.cpp
${PROJECT_SOURCE_DIR}/test/GroupByRangeTest.cpp ${PROJECT_SOURCE_DIR}/test/GroupByTest.cpp
${PROJECT_SOURCE_DIR}/test/IterRangeTest.cpp ${PROJECT_SOURCE_DIR}/test/IterRangeTest.cpp
${PROJECT_SOURCE_DIR}/test/LinqTest.cpp ${PROJECT_SOURCE_DIR}/test/LinqTest.cpp
${PROJECT_SOURCE_DIR}/test/MaxTest.cpp ${PROJECT_SOURCE_DIR}/test/MaxTest.cpp
......
#include <vector>
#include <string>
#include <gtest/gtest.h>
#include "CommonTests.h"
#include "boolinq.h"
using namespace boolinq;
//////////////////////////////////////////////////////////////////////////
/*
TEST(GroupByRange, IntsFront)
{
int arr[] = {0,1,2,3,4,5,6,7,8,9};
int ans_1[] = {1,4,7};
int ans_2[] = {2,5,8};
int ans_0[] = {0,3,6,9};
auto rng = from(arr);
auto dst = rng.groupBy([](int a){return a % 3;});
EXPECT_EQ(1, dst.front().key());
EXPECT_EQ(0, dst.back().key());
CheckRangeEqArray(dst.front(), ans_1);
CheckRangeEqArray(dst.back(), ans_0);
CheckRangeEqArray(dst.popFront(), ans_1);
EXPECT_EQ(2, dst.front().key());
EXPECT_EQ(0, dst.back().key());
CheckRangeEqArray(dst.front(), ans_2);
CheckRangeEqArray(dst.back(), ans_0);
CheckRangeEqArray(dst.popFront(), ans_2);
EXPECT_EQ(0, dst.front().key());
EXPECT_EQ(0, dst.back().key());
CheckRangeEqArray(dst.front(), ans_0);
CheckRangeEqArray(dst.back(), ans_0);
CheckRangeEqArray(dst.popFront(), ans_0);
EXPECT_THROW(dst.next(), LinqEndException);
}
TEST(GroupByRange, IntsBack)
{
int arr[] = {0,1,2,3,4,5,6,7,8,9};
int ans_1[] = {1,4,7};
int ans_2[] = {2,5,8};
int ans_0[] = {0,3,6,9};
auto rng = from(arr);
auto dst = groupBy(rng, [](int a){return a % 3;});
EXPECT_EQ(1, dst.front().key());
EXPECT_EQ(0, dst.back().key());
CheckRangeEqArray(dst.front(), ans_1);
CheckRangeEqArray(dst.back(), ans_0);
CheckRangeEqArray(dst.popBack(), ans_0);
EXPECT_EQ(1, dst.front().key());
EXPECT_EQ(2, dst.back().key());
CheckRangeEqArray(dst.front(), ans_1);
CheckRangeEqArray(dst.back(), ans_2);
CheckRangeEqArray(dst.popBack(), ans_2);
EXPECT_EQ(1, dst.front().key());
EXPECT_EQ(1, dst.back().key());
CheckRangeEqArray(dst.front(), ans_1);
CheckRangeEqArray(dst.back(), ans_1);
CheckRangeEqArray(dst.popBack(), ans_1);
EXPECT_THROW(dst.next(), LinqEndException);
}
//////////////////////////////////////////////////////////////////////////
TEST(GroupByRange, CountChildrenByAge)
{
struct Child
{
std::string name;
int age;
bool operator == (const Child & rhs) const
{
return (name == rhs.name) && (age == rhs.age);
}
};
Child children[] =
{
{"Piter", 12},
{"Bella", 14},
{"Torry", 15},
{"Holly", 12},
{"Zamza", 13},
};
Child ans_false[] =
{
{"Bella", 14},
{"Torry", 15},
};
Child ans_true[] =
{
{"Piter", 12},
{"Holly", 12},
{"Zamza", 13},
};
auto rng = from(children);
auto dst = groupBy(rng, [](const Child & a){return a.age < 14;});
EXPECT_EQ(false, dst.front().key());
CheckRangeEqArray(dst.front(), ans_false);
CheckRangeEqArray(dst.back(), ans_true);
CheckRangeEqArray(dst.popFront(), ans_false);
EXPECT_EQ(true, dst.front().key());
CheckRangeEqArray(dst.front(), ans_true);
CheckRangeEqArray(dst.back(), ans_true);
CheckRangeEqArray(dst.popFront(), ans_true);
EXPECT_THROW(dst.next(), LinqEndException);
}
*/
#include <vector>
#include <string>
#include <gtest/gtest.h>
#include "CommonTests.h"
#include "boolinq.h"
using namespace boolinq;
//////////////////////////////////////////////////////////////////////////
TEST(GroupByRange, Ints)
{
int arr[] = {0,1,2,3,4,5,6,7,8,9};
int ans_0[] = {0,3,6,9};
int ans_1[] = {1,4,7};
int ans_2[] = {2,5,8};
auto dst = from(arr).groupBy([](int a){return a % 3;});
EXPECT_EQ(0, dst.elementAt(0).first);
EXPECT_EQ(1, dst.elementAt(1).first);
EXPECT_EQ(2, dst.elementAt(2).first);
CheckRangeEqArray(dst.elementAt(0).second, ans_0);
CheckRangeEqArray(dst.elementAt(1).second, ans_1);
CheckRangeEqArray(dst.elementAt(2).second, ans_2);
EXPECT_THROW(dst.elementAt(3), LinqEndException);
}
//////////////////////////////////////////////////////////////////////////
TEST(GroupByRange, CountChildrenByAge)
{
struct Child
{
std::string name;
int age;
bool operator == (const Child & rhs) const
{
return (name == rhs.name) && (age == rhs.age);
}
};
Child children[] = {
{"Piter", 12},
{"Bella", 14},
{"Torry", 15},
{"Holly", 12},
{"Zamza", 13},
};
Child ans_false[] = {
{"Bella", 14},
{"Torry", 15},
};
Child ans_true[] = {
{"Piter", 12},
{"Holly", 12},
{"Zamza", 13},
};
auto dst = from(children).groupBy([](const Child & a){return a.age < 14;});
EXPECT_EQ(true, dst.elementAt(0).first);
EXPECT_EQ(false, dst.elementAt(1).first);
CheckRangeEqArray(dst.elementAt(0).second, ans_true);
CheckRangeEqArray(dst.elementAt(1).second, ans_false);
EXPECT_THROW(dst.elementAt(2), LinqEndException);
}
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