summaryrefslogtreecommitdiffhomepage
path: root/test/entity.cpp
blob: 8e80ba6468e3a9b5f817599c21a7642b7681bbac (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "app.hpp"
#include "compat/assert.hpp"
#include "entity/field.hpp"
#include "entity/types.hpp"
#include <tuple>

using namespace floormat;
using namespace floormat::entities;

namespace {

struct TestAccessors {
    constexpr int bar() const { return _bar; }
    constexpr void set_bar(int value) { _bar = value; }
    int foo;
    int _bar;
    int _baz;

    static constexpr auto accessors() noexcept;
};

constexpr auto TestAccessors::accessors() noexcept
{
    using entity = Entity<TestAccessors>;
    constexpr auto r_baz = [](const TestAccessors& x) { return x._baz; };
    constexpr auto w_baz = [](TestAccessors& x, int v) { x._baz = v; };

    constexpr auto tuple = std::make_tuple(
        entity::type<int>::field{"foo"_s, &TestAccessors::foo, &TestAccessors::foo},
        entity::type<int>::field{"bar"_s, &TestAccessors::bar, &TestAccessors::set_bar},
        entity::type<int>::field("baz"_s, r_baz, w_baz)
    );
    return tuple;
}

using entity = Entity<TestAccessors>;
constexpr auto m_foo = entity::type<int>::field{"foo"_s, &TestAccessors::foo, &TestAccessors::foo};
constexpr auto m_bar = entity::type<int>::field{"bar"_s, &TestAccessors::bar, &TestAccessors::set_bar};
constexpr auto r_baz = [](const TestAccessors& x) { return x._baz; };
constexpr auto w_baz = [](TestAccessors& x, int v) { x._baz = v; };
constexpr auto m_baz = entity::type<int>::field("baz"_s, r_baz, w_baz);

} // namespace

namespace floormat {

namespace {

constexpr bool test_accessors()
{
    auto x = TestAccessors{111, 222, 333};

    {
        auto a = m_foo.read(x), b = m_bar.read(x), c = m_baz.read(x);
        fm_assert(a == 111 && b == 222 && c == 333);
    }

    {
        m_foo.write(x, 1111);
        m_bar.write(x, 2222);
        m_baz.write(x, 3333);
        auto a = m_foo.read(x), b = m_bar.read(x), c = m_baz.read(x);
        fm_assert(a == 1111 && b == 2222 && c == 3333);
    }
    return true;
}

constexpr bool test_visitor()
{
    {
        constexpr auto tuple = std::make_tuple((unsigned char)1, (unsigned short)2, (int)3, (long)4);
        long ret = 0;
        visit_tuple([&](auto x) { ret += (long)x; }, tuple);
        fm_assert(ret == 1 + 2 + 3 + 4);
    }
    {
        int ret = 0;
        visit_tuple([&] { ret++; }, std::tuple<>{});
        fm_assert(ret == 0);
    }
    {
        constexpr auto tuple = std::make_tuple((char)1, (short)2, (long)3);
        static_assert(find_in_tuple([](auto x) { return x == 3; }, tuple));
        static_assert(!find_in_tuple([](auto x) { return x == 5; }, tuple));
    }

    return true;
}

void test_fun2() {
    static constexpr auto read_fn = [](const TestAccessors& x) constexpr { return x.bar(); };
    static constexpr auto write_fn = [](TestAccessors& x, int value) constexpr { x.set_bar(value); };
    constexpr auto read_bar = fu2::function_view<int(const TestAccessors&) const>{read_fn};
    constexpr auto write_bar = fu2::function_view<void(TestAccessors&, int) const>{write_fn};
    constexpr auto m_bar2 = entity::type<int>::field{"bar"_s, read_bar, write_bar};

    auto x = TestAccessors{1, 2, 3};
    fm_assert(m_bar2.read(x) == 2);
    m_bar2.write(x, 22);
    fm_assert(m_bar2.read(x) == 22);
}

void test_erasure() {
    erased_accessor accessors[] = {
        m_foo.erased(),
        m_bar.erased(),
        m_baz.erased(),
    };
    auto obj = TestAccessors{1, 2, 3};
    int value = 0;
    accessors[1].read_fun(&obj, accessors[1].reader, &value);
    fm_assert(value == 2);
    int value2 = 22222, value2_ = 0;
    accessors[1].write_fun(&obj, accessors[1].writer, &value2);
    accessors[1].read_fun(&obj, accessors[1].reader, &value2_);
    fm_assert(value2 == value2_);
}

void test_metadata()
{
    constexpr auto m = entity_metadata<TestAccessors>();
    static_assert(sizeof m == 1);
    fm_assert(m.class_name == name_of<TestAccessors>);
    fm_assert(m.class_name.contains("TestAccessors"_s));
    const auto [foo, bar, baz] = m.accessors;
    const auto [foo2, bar2, baz2] = m.erased_accessors;
    TestAccessors x{0, 0, 0};
    foo.write(x, 1);
    fm_assert(x.foo == 1);
    int bar_ = 2;
    bar2.write_fun(&x, bar2.writer, &bar_);
    fm_assert(x.bar() == 2);
    baz2.write<TestAccessors, int>(x, 3);
    fm_assert(baz2.read<TestAccessors, int>(x) == 3);
}

void test_type_name()
{
    struct foobar;
    constexpr StringView name = name_of<foobar>;
    fm_assert(name.contains("foobar"_s));
    static_assert(name.data() == name_of<foobar>.data());
    static_assert(name_of<int> != name_of<unsigned>);
    static_assert(name_of<foobar*> != name_of<const foobar*>);
    using foobar2 = foobar;
    static_assert(name_of<foobar2>.data() == name_of<foobar>.data());
}

constexpr bool test_null_writer()
{
    constexpr auto m_foo = entity::type<int>::field{"foo"_s, &TestAccessors::foo, nullptr};
    static_assert(m_foo.writer == nullptr);
    static_assert(!m_foo.can_write);
    static_assert(std::get<0>(TestAccessors::accessors()).can_write);
    return true;
}

} // namespace

static constexpr bool test_names()
{
    constexpr auto m = entity_metadata<TestAccessors>();
    auto [foo1, bar1, baz1] = m.accessors;
    auto [foo2, bar2, baz2] = m.erased_accessors;

    fm_assert(foo1.name == "foo"_s);
    fm_assert(bar1.name == "bar"_s);
    fm_assert(baz1.name == "baz"_s);

    fm_assert(foo2.field_name == "foo"_s);
    fm_assert(bar2.field_name == "bar"_s);
    fm_assert(baz2.field_name == "baz"_s);
    return true;
}

void test_app::test_entity()
{
    static_assert(test_accessors());
    static_assert(test_visitor());
    static_assert(test_null_writer());
    static_assert(test_names());
    test_fun2();
    test_erasure();
    test_type_name();
    test_metadata();
}

} // namespace floormat