hjiang / linqxx

LINQ-like syntactic sugar for C++

This URL has Read+Write access

linqxx / linqxx_test.cc
100644 83 lines (67 sloc) 2.096 kb
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <vector>
 
#include <boost/lambda/lambda.hpp>
#include <gtest/gtest.h>
 
#include "linqxx.h"
 
namespace linqxx {
namespace {
 
using namespace std;
using boost::lambda::_1;
 
struct Person {
    string name;
    int age;
    Person(const string& aName, int anAge) : name(aName), age(anAge) {}
};
 
class LinqxxTest : public testing::Test {
  protected:
    virtual void SetUp() {
        guests_.reset(new vector<Person>());
        guests_->push_back(Person("John", 32));
        guests_->push_back(Person("Mike", 28));
        guests_->push_back(Person("Eliz", 27));
    }
 
    shared_ptr<vector<Person> > guests_;
};
 
TEST_F(LinqxxTest, Count) {
    EXPECT_EQ(3, from(guests_).count());
}
 
TEST_F(LinqxxTest, Where) {
    EXPECT_EQ(1, from(guests_).where(&_1 ->* &Person::age > 30).count());
}
 
TEST_F(LinqxxTest, Select) {
    EXPECT_EQ(3, from(guests_).select<int>(&_1 ->* &Person::age).count());
    shared_ptr<vector<int> > results =
            from(guests_)
            .select<int>(&_1 ->* &Person::age)
            .get();
    EXPECT_EQ(3, results->size());
    EXPECT_EQ(32, (*results)[0]);
}
 
TEST_F(LinqxxTest, Map) {
    EXPECT_EQ(3, from(guests_).map<int>(&_1 ->* &Person::age).count());
}
 
TEST_F(LinqxxTest, NonOwningInitialization) {
    vector<Person> people;
    people.push_back(Person("Joe", 20));
    EXPECT_EQ(1, from(&people).map<int>(&_1 ->* &Person::age).count());
}
 
TEST_F(LinqxxTest, Insert) {
    guests_->push_back(Person("joe", 18));
    DataSet<vector<Person> > results =
            insert(
                    from(guests_)
                    .where(&_1 ->* &Person::age > 30)
                   )
            .into(
                    from(guests_).where(&_1 ->* &Person::name == "joe"));
    EXPECT_EQ(2, results.count());
 
    shared_ptr<vector<int> > ages =
            results
            .select<int>(&_1 ->* &Person::age)
            .get();
    EXPECT_EQ(1, count(ages->begin(), ages->end(), 32));
    EXPECT_EQ(1, count(ages->begin(), ages->end(), 18));
}
 
} // anonymous namespace
} // namespace linqxx