Commit 24603de1 authored by Jakub Pawlik's avatar Jakub Pawlik

added takeWhile and takeWhile_i linq functions

added new LinqObj methods: takeWhile and takeWhile_i with dedicated for
them tests included in TakeWhileRange.cpp
parent 9c4730ea
#include <vector>
#include <string>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "CommonTests.h"
#include "boolinq.h"
using namespace boolinq;
//////////////////////////////////////////////////////////////////////////
TEST(TakeWhileRange, ManyToMore)
{
int src[] = {1,2,3,4,5,6};
int ans[] = {1,2,3,4,5,6};
auto rng = from(src);
auto dst = rng.takeWhile([](int it){return it >= 0 && it <= 10;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, ManyToMoreByIndex)
{
int src[] = {1,3,5,7,9,11};
int ans[] = {1,3,5,7,9,11};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int /*it*/, int idx){return idx >= 0;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, ManyToMoreByItemValue)
{
int src[] = {1,3,5,7,9,11};
int ans[] = {1,3,5,7,9,11};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int it, int /*idx*/){return it%2 != 0;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, ManyToMoreByIndexAndItemValue)
{
int src[] = {1,3,5,7,9,11};
int ans[] = {1,3,5,7,9,11};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int it, int idx){return idx < 3 || it > 5;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange, ManyToMany)
{
int src[] = {1,2,3,4,5,6};
int ans[] = {1,2,3,4,5,6};
auto rng = from(src);
auto dst = rng.takeWhile([](int it){return it >= 1 && it <= 6;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, ManyToManyByIndex)
{
int src[] = {1,3,5,7,9,11};
int ans[] = {1,3,5,7,9,11};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int /*it*/, int idx){return idx >= 0 && idx < 6;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, ManyToManyByItemValue)
{
int src[] = {1,3,5,7,9,11};
int ans[] = {1,3,5,7,9,11};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int it, int /*idx*/){return it > 0 && it < 12;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, ManyToManyByIndexAndItemValue)
{
int src[] = {1,3,5,7,9,11};
int ans[] = {1,3,5,7,9,11};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int it, int idx){return idx < 3 || it > 5 && it < 12;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange, ManyToLess)
{
int src[] = {1,2,3,4,5,6};
int ans[] = {1,2,3};
auto rng = from(src);
auto dst = rng.takeWhile([](int it){return it != 4;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, ManyToLessByIndex)
{
int src[] = {1,2,3,4,5,6,7,8,9,10,11};
int ans[] = {1,2,3,4};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int /*it*/, int idx){return idx < 4;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, ManyToLessByItemValue)
{
int src[] = {1,2,3,4,5,6,7,8,9,10,11};
int ans[] = {1,2,3,4};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int it, int /*idx*/){return it <5;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, ManyToLessByIndexAndItemValue)
{
int src[] = {1,2,3,4,5,6,7,8,9,10,11};
int ans[] = {1,2,3,4};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int it, int idx){return idx < 4 && it > 0;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange, ManyToOne)
{
int src[] = {1,2,3,4,5,6};
int ans[] = {1};
auto rng = from(src);
auto dst = rng.takeWhile([](int it){return it%2 != 0;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, ManyToOneByIndex)
{
int src[] = {1,2,3,4,5,6,7,8,9,10,11};
int ans[] = {1};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int /*it*/, int idx){return idx == 0;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, ManyToOneByItemValue)
{
int src[] = {1,2,3,4,5,6,7,8,9,10,11};
int ans[] = {1};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int it, int /*idx*/){return it == 1;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, ManyToOneByIndexAndItemValue)
{
int src[] = {1,2,3,4,5,6,7,8,9,10,11};
int ans[] = {1};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int it, int idx){return idx < 5 && it < 2;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange, ManyToZero)
{
int src[] = {1,2,3,4,5,6};
auto rng = from(src);
auto dst = rng.takeWhile([](int it){return it < 0;});
EXPECT_THROW(dst.nextObject(), EnumeratorEndException);
}
TEST(TakeWhileRange_i, ManyToZeroByIndex)
{
int src[] = {1,2,3,4,5,6,7,8,9,10,11};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int /*it*/, int idx){return idx > 0;});
EXPECT_THROW(dst.nextObject(), EnumeratorEndException);
}
TEST(TakeWhileRange_i, ManyToZeroByItemValue)
{
int src[] = {1,2,3,4,5,6,7,8,9,10,11};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int it, int /*idx*/){return it > 2;});
EXPECT_THROW(dst.nextObject(), EnumeratorEndException);
}
TEST(TakeWhileRange_i, ManyToZeroByIdexAndItemValue)
{
int src[] = {1,2,3,4,5,6,7,8,9,10,11};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int it, int idx){return it < 0 || idx > 0;});
EXPECT_THROW(dst.nextObject(), EnumeratorEndException);
}
//////////////////////////////////////////////////////////////////////////
TEST(TakeWhileRange, OneToOne)
{
int src[] = {5};
int ans[] = {5};
auto rng = from(src);
auto dst = rng.takeWhile([](int it){return it > 0;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, OneToOneByIndex)
{
int src[] = {5};
int ans[] = {5};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int /*it*/, int idx){return idx < 1;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, OneToOneByItemValue)
{
int src[] = {5};
int ans[] = {5};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int it, int /*idx*/){return it > 2;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange_i, OneToOneByIndexAndItemValue)
{
int src[] = {5};
int ans[] = {5};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int it, int idx){return idx == 0 && it > 2;});
CheckRangeEqArray(dst, ans);
}
TEST(TakeWhileRange, OneToZero)
{
int src[] = {5};
auto rng = from(src);
auto dst = rng.takeWhile([](int){return false;});
EXPECT_THROW(dst.nextObject(), EnumeratorEndException);
}
TEST(TakeWhileRange_i, OneToZeroByIndex)
{
int src[] = {5};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int /*it*/, int idx){return idx > 0;});
EXPECT_THROW(dst.nextObject(), EnumeratorEndException);
}
TEST(TakeWhileRange_i, OneToZeroByItemValue)
{
int src[] = {5};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int it, int /*idx*/){return it < 5;});
EXPECT_THROW(dst.nextObject(), EnumeratorEndException);
}
TEST(TakeWhileRange_i, OneToZeroByIndexAndItemValue)
{
int src[] = {5};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int it, int idx){return idx == 0 && it > 5;});
EXPECT_THROW(dst.nextObject(), EnumeratorEndException);
}
TEST(TakeWhileRange, ZeroToZero)
{
std::vector<int> src;
auto rng = from(src);
auto dst = rng.takeWhile([](int){return false;});
EXPECT_THROW(rng.nextObject(), EnumeratorEndException);
}
TEST(TakeWhileRange_i, ZeroToZero)
{
int src[] = {5};
auto rng = from(src);
auto dst = rng.takeWhile_i([](int /*it*/, int /*idx*/){return false;});
EXPECT_THROW(dst.nextObject(), EnumeratorEndException);
}
//////////////////////////////////////////////////////////////////////////
...@@ -171,6 +171,20 @@ namespace boolinq ...@@ -171,6 +171,20 @@ namespace boolinq
return where_i([=](T, int i){return i < count;}); return where_i([=](T, int i){return i < count;});
} }
LinqObj<Enumerator<T,std::pair<TE,int> > > takeWhile_i(std::function<bool(T,int)> predicate) const
{
return Enumerator<T,std::pair<TE,int> >([=](std::pair<TE,int> & pair)->T{
T object = pair.first.nextObject();
if(!predicate(object,pair.second++)) throw EnumeratorEndException();
return object;
}, std::make_pair(_enumerator,0));
}
LinqObj<Enumerator<T,std::pair<TE,int> > > takeWhile(std::function<bool(T)> predicate) const
{
return takeWhile_i([=](T a,int){return predicate(a);});
}
LinqObj<Enumerator<T,std::pair<TE,int> > > skip(int count) const LinqObj<Enumerator<T,std::pair<TE,int> > > skip(int count) const
{ {
return where_i([=](T, int i){return i >= count;}); return where_i([=](T, int i){return i >= count;});
......
...@@ -100,6 +100,7 @@ ...@@ -100,6 +100,7 @@
<ClCompile Include="TakeRangeTest.cpp" /> <ClCompile Include="TakeRangeTest.cpp" />
<ClCompile Include="BytesRangeTest.cpp" /> <ClCompile Include="BytesRangeTest.cpp" />
<ClCompile Include="BitsRangeTest.cpp" /> <ClCompile Include="BitsRangeTest.cpp" />
<ClCompile Include="TakeWhileRange.cpp" />
<ClCompile Include="ToDequeTest.cpp" /> <ClCompile Include="ToDequeTest.cpp" />
<ClCompile Include="ToListTest.cpp" /> <ClCompile Include="ToListTest.cpp" />
<ClCompile Include="LinqTest.cpp" /> <ClCompile Include="LinqTest.cpp" />
......
...@@ -128,6 +128,9 @@ ...@@ -128,6 +128,9 @@
<ClCompile Include="SpeedTest.cpp"> <ClCompile Include="SpeedTest.cpp">
<Filter>Test Files</Filter> <Filter>Test Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="TakeWhileRange.cpp">
<Filter>Test Files\Ranges</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="boolinq.h"> <ClInclude Include="boolinq.h">
......
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