Commit 79b91c2a authored by Jakub Pawlik's avatar Jakub Pawlik

added the skipWhile linq function

+ created the skipWhile function
+ added tests for skipWhile function
parent 389a1d10
#include <vector>
#include <string>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "CommonTests.h"
#include "boolinq.h"
using namespace boolinq;
//////////////////////////////////////////////////////////////////////////
TEST(SkipWhileRange, ManyToMore)
{
int src[] = {1,2,3,4,5,6};
int ans[] = {1,2,3,4,5,6};
auto rng = from(src);
auto dst = rng.skipWhile([](int it){return it < 0 || it > 10;});
auto vec = dst.toVector();
CheckRangeEqArray(dst, ans);
}
TEST(SkipWhileRange, ManyToMany)
{
int src[] = {1,2,3,4,5,6};
int ans[] = {1,2,3,4,5,6};
auto rng = from(src);
auto dst = rng.skipWhile([](int it){return it < 0 && it > 6;});
CheckRangeEqArray(dst, ans);
}
TEST(SkipWhileRange, ManyToLess)
{
int src[] = {1,2,3,4,5,6};
int ans[] = {3,4,5,6};
auto rng = from(src);
auto dst = rng.skipWhile([](int it){return it < 3 || it > 4;});
CheckRangeEqArray(dst, ans);
}
TEST(SkipWhileRange, ManyToOne)
{
int src[] = {1,2,3,4,5,6};
int ans[] = {6};
auto rng = from(src);
auto dst = rng.skipWhile([](int it){return it != 6;});
CheckRangeEqArray(dst, ans);
}
TEST(SkipWhileRange, ManyToZero)
{
int src[] = {1,2,3,4,5,6};
auto rng = from(src);
auto dst = rng.skipWhile([](int it){return it > 0;});
EXPECT_THROW(dst.nextObject(), EnumeratorEndException);
}
//////////////////////////////////////////////////////////////////////////
TEST(SkipWhileRange, OneToOne)
{
int src[] = {5};
int ans[] = {5};
auto rng = from(src);
auto dst = rng.skipWhile([](int it){return it != 5;});
CheckRangeEqArray(dst, ans);
}
TEST(SkipWhileRange, OneToZero)
{
int src[] = {5};
auto rng = from(src);
auto dst = rng.skipWhile([](int it){return it == 5;});
EXPECT_THROW(dst.nextObject(), EnumeratorEndException);
}
TEST(SkipWhileRange, ZeroToZero)
{
std::vector<int> src;
auto rng = from(src);
auto dst = rng.skipWhile([](int){return false;});
EXPECT_THROW(rng.nextObject(), EnumeratorEndException);
}
//////////////////////////////////////////////////////////////////////////
......@@ -190,6 +190,17 @@ namespace boolinq
return where_i([=](T, int i){return i >= count;});
}
LinqObj<Enumerator<T,std::pair<TE,int> > > skipWhile(std::function<bool(T)> predicate) const
{
return Enumerator<T,std::pair<TE,int> >([=](std::pair<TE,int> & pair)->T{
T object = pair.first.nextObject();;
if( 0 == pair.second++ )
while (predicate(object))
object = pair.first.nextObject();
return object;
}, std::make_pair(_enumerator,0));
}
template<typename TRet>
LinqObj<Enumerator<TRet,std::pair<TE,int> > > select_i(std::function<TRet(T,int)> transform) const
{
......
......@@ -95,6 +95,7 @@
<ClCompile Include="ReverseRangeTest.cpp" />
<ClCompile Include="SelectRangeTest.cpp" />
<ClCompile Include="SkipRangeTest.cpp" />
<ClCompile Include="SkipWhileRangeTest.cpp" />
<ClCompile Include="SpeedTest.cpp" />
<ClCompile Include="SumTest.cpp" />
<ClCompile Include="TakeRangeTest.cpp" />
......
......@@ -131,6 +131,9 @@
<ClCompile Include="TakeWhileRangeTest.cpp">
<Filter>Test Files\Ranges</Filter>
</ClCompile>
<ClCompile Include="SkipWhileRangeTest.cpp">
<Filter>Test Files\Ranges</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<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