LastTest.cpp 1.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <list>
#include <vector>
#include <string>

#include <gtest/gtest.h>

#include "boolinq.h"

using namespace boolinq;

TEST(Last, ThreeElements)
{
    std::vector<int> src = {1,2,3};

    EXPECT_EQ(3, from(src).last());
    EXPECT_EQ(2, from(src).last([](int a){return a%2 == 0;}));
    EXPECT_EQ(3, from(src).lastOrDefault());
    EXPECT_EQ(2, from(src).lastOrDefault([](int a){return a%2 == 0;}));
}

TEST(Last, OneElement)
{
    std::vector<int> src = {1};

    EXPECT_EQ(1, from(src).last());
    EXPECT_THROW(from(src).last([](int a){return a%2 == 0;}), LinqEndException);
    EXPECT_EQ(1, from(src).lastOrDefault());
    EXPECT_EQ(0, from(src).lastOrDefault([](int a){return a%2 == 0;}));
}

TEST(Last, NoneElements)
{
    std::vector<int> src = {};

    EXPECT_THROW(from(src).last(), LinqEndException);
    EXPECT_THROW(from(src).last([](int a){return a%2 == 0;}), LinqEndException);
    EXPECT_EQ(0, from(src).lastOrDefault());
    EXPECT_EQ(0, from(src).lastOrDefault([](int a){return a%2 == 0;}));
}
40 41 42 43 44 45 46

TEST(Last, NoneElementsWithArgument) {
    std::vector<int> src = {};

    EXPECT_EQ(2, from(src).lastOrDefault(2));
    EXPECT_EQ(3, from(src).lastOrDefault([](int a) { return a % 2 == 0; }, 3));
}