File: derive.cpp

package info (click to toggle)
lager 0.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,888 kB
  • sloc: cpp: 10,602; javascript: 10,433; makefile: 214; python: 100; sh: 98
file content (160 lines) | stat: -rw-r--r-- 3,483 bytes parent folder | download | duplicates (2)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// lager - library for functional interactive c++ programs
// Copyright (C) 2017 Juan Pedro Bolivar Puente
//
// This file is part of lager.
//
// lager is free software: you can redistribute it and/or modify
// it under the terms of the MIT License, as detailed in the LICENSE
// file located at the root of this source code distribution,
// or here: <https://github.com/arximboldi/lager/blob/master/LICENSE>
//

#include "test/cereal/cerealize.hpp"

#include <catch2/catch.hpp>

#include <lager/extra/derive.hpp>
#include <lager/extra/derive/cereal.hpp>
#include <lager/extra/derive/eq.hpp>
#include <lager/extra/derive/hana.hpp>
#include <lager/extra/derive/hash.hpp>

#include <boost/hana/assert.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/for_each.hpp>
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/size.hpp>

#include <immer/array.hpp>
#include <immer/set.hpp>

namespace {

template <typename T>
void check_eq()
{
    auto x = T{42, 12};
    auto y = x;
    CHECK(x == y);
}

template <typename T>
void check_hana()
{
    auto x       = T{42, 12};
    auto members = immer::array<std::string>{};
    auto acc     = 0;
    boost::hana::for_each(x, [&](auto&& x) {
        members = std::move(members).push_back(boost::hana::first(x).c_str());
        acc += boost::hana::second(x);
    });
    CHECK(members ==
          immer::array<std::string>{{std::string{"a"}, std::string{"b"}}});
    CHECK(acc == 54);
}

template <typename T>
void check_cereal()
{
    auto x = T{42, 12};
    auto y = cerealize(x);
    CHECK(y == x);
}

template <typename T>
void check_hash()
{
    auto m = immer::set<T>{}.insert(T{42, 12});
    CHECK(m.count(T{42, 12}) == 1);
    CHECK(m.count(T{42, 13}) == 0);
}

} // namespace

namespace ns {
struct derived
{
    int a;
    float b;
};
} // namespace ns
LAGER_DERIVE((EQ, HANA, CEREAL, HASH), ns, derived, a, b);

TEST_CASE("basic")
{
    check_eq<ns::derived>();
    check_hana<ns::derived>();
    check_cereal<ns::derived>();
    check_hash<ns::derived>();
}

namespace ns {
struct derived2
{
    derived a;
    float b;
};
} // namespace ns
LAGER_DERIVE((EQ, HANA, CEREAL, HASH), ns, derived2, a, b);

TEST_CASE("basic-2")
{
    check_eq<ns::derived2>();
    // check_hana<ns::derived2>();
    check_cereal<ns::derived2>();
    check_hash<ns::derived2>();
}

namespace ns {
struct empty_t
{};
} // namespace ns
LAGER_DERIVE((EQ, HANA, CEREAL), ns, empty_t);

TEST_CASE("empty")
{
    CHECK(ns::empty_t{} == ns::empty_t{});
    CHECK(cerealize(ns::empty_t{}) == ns::empty_t{});
    BOOST_HANA_CONSTANT_CHECK(boost::hana::size(ns::empty_t{}) ==
                              boost::hana::size_c<0>);
}

namespace ns {
template <typename A, typename B>
struct foo_tpl
{
    A a;
    B b;
};
} // namespace ns
LAGER_DERIVE_TEMPLATE(
    (EQ, HANA, CEREAL, HASH), ns, (class A, class B), (foo_tpl<A, B>), a, b);

TEST_CASE("template")
{
    check_eq<ns::foo_tpl<int, float>>();
    check_hana<ns::foo_tpl<int, float>>();
    check_cereal<ns::foo_tpl<int, float>>();
    check_hash<ns::foo_tpl<int, float>>();
}

namespace ns {
template <typename A, typename B>
struct foo_tpl2
{
    struct nested
    {
        A a;
        B b;
        LAGER_DERIVE_NESTED((EQ, HANA, CEREAL), nested, a, b);
    };
};
} // namespace ns

TEST_CASE("template_nested")
{
    check_eq<ns::foo_tpl2<int, float>::nested>();
    check_hana<ns::foo_tpl2<int, float>::nested>();
    check_cereal<ns::foo_tpl2<int, float>::nested>();
}