summaryrefslogtreecommitdiff
path: root/src/tests/eet_cxx/eet_cxx_test_descriptors.cc
blob: bc214181d617f012a82bb1ddb4ff0891ca095b29 (plain) (blame)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

#include "Eet.hh"

#include <algorithm>

#include <iostream>

#include <check.h>

struct pod_type
{
  int i;
  char c;
};

START_TEST(eet_cxx_descriptors)
{
  efl::eet::eet_init init;

  auto d = efl::eet::make_descriptor
    ("pod_type"
     , efl::eet::type("i", &pod_type::i)
     , efl::eet::type("c", &pod_type::c));
  static_assert(std::is_same<efl::eet::descriptor<pod_type, int, char>, decltype(d)>::value, "");

  Eet_File* file = eet_open("/tmp/eet_file_test.eet", EET_FILE_MODE_READ_WRITE);
  ck_assert(file != 0);

  pod_type pod = {1, 2};

  int s = eet_data_write(file, d.native_handle(), "pod", &pod, true);
  std::cout << "bytes written " << s << std::endl;
  ck_assert(s > 0);
  eet_sync(file);
  auto p = efl::eet::read_by_ptr(file, "pod", d);
  ck_assert(p != 0);
  ck_assert(p->i == 1);
  ck_assert(p->c == 2);

  eet_close(file);
}
END_TEST

int constructors_called = 0
    , destructors_called = 0;

struct non_pod
{
  non_pod() : i(10)
  {
    ++constructors_called;
  }
  non_pod(non_pod const& other)
    : i(other.i)
  {
    ++constructors_called;
  }
  ~non_pod()
  {
    ++destructors_called;
  }

  int i;
};

START_TEST(eet_cxx_descriptors_non_pod)
{
  efl::eet::eet_init init;

  auto d = efl::eet::make_descriptor
    ("pod_type", EET_CXX_MEMBER(non_pod, i));
  static_assert(std::is_same<efl::eet::descriptor<non_pod, int>, decltype(d)>::value, "");

  {
    Eet_File* file = eet_open("/tmp/eet_file_test.eet", EET_FILE_MODE_READ_WRITE);
    ck_assert(file != 0);

    ::non_pod non_pod;

    int s = eet_data_write(file, d.native_handle(), "non_pod", &non_pod, true);
    std::cout << "bytes written " << s << std::endl;
    ck_assert(s > 0);
    eet_sync(file);
    auto p = efl::eet::read_by_ptr(file, "non_pod", d);
    ck_assert(p != 0);
    ck_assert(p->i == 10);

    auto v = efl::eet::read(file, "non_pod", d);
    ck_assert(v.i == 10);

    eet_close(file);
  }

  std::cout << "constructors called for non pod: " << constructors_called
            << " destructors called for non pod: " << destructors_called << std::endl;

  ck_assert(constructors_called == destructors_called);
}
END_TEST

struct pod_composited
{
  pod_type* member;
};

struct pod_composited_with_non_pod
{
  non_pod* member;
};

struct pod_value_composited
{
  pod_type member;
};

START_TEST(eet_cxx_descriptors_composition)
{
  efl::eet::eet_init init;

  auto pod_descriptor = efl::eet::make_descriptor
    ("pod_type"
     , efl::eet::type("i", &pod_type::i)
     , efl::eet::type("c", &pod_type::c));
  static_assert(std::is_same<efl::eet::descriptor<pod_type, int, char>
                , decltype(pod_descriptor)>::value, "");

  auto non_pod_descriptor = efl::eet::make_descriptor
    ("non_pod"
     , efl::eet::type("i", &non_pod::i));
  static_assert(std::is_same<efl::eet::descriptor<non_pod, int>
                , decltype(non_pod_descriptor)>::value, "");

  {
    auto d = efl::eet::make_descriptor
      ("pod_composited", efl::eet::type("pod_composited", &pod_composited::member, pod_descriptor));
    static_assert(std::is_same<efl::eet::descriptor<pod_composited, pod_type*>, decltype(d)>::value, "");

    Eet_File* file = eet_open("/tmp/eet_file_test.eet", EET_FILE_MODE_READ_WRITE);
    ck_assert(file != 0);
    
    ::pod_composited pod_composited {new pod_type{5, 'a'}};

    int s = eet_data_write(file, d.native_handle(), "foo", &pod_composited, false);
    ck_assert(s > 0);
    eet_sync(file);
    auto p = efl::eet::read_by_ptr(file, "foo", d);
    ck_assert(p != 0);
    ck_assert(p->member->i == 5);
    ck_assert(p->member->c == 'a');

    delete p->member;

    auto v = efl::eet::read(file, "foo", d);
    ck_assert(v.member->i == 5);
    ck_assert(v.member->c == 'a');

    delete v.member;

    eet_close(file);
  }

  {
    auto d = efl::eet::make_descriptor
      ("pod_composited_with_non_pod", efl::eet::type("pod_composited_with_non_pod", &pod_composited_with_non_pod::member, non_pod_descriptor));
    static_assert(std::is_same<efl::eet::descriptor<pod_composited_with_non_pod, non_pod*>, decltype(d)>::value, "");

    Eet_File* file = eet_open("/tmp/eet_file_test.eet", EET_FILE_MODE_READ_WRITE);
    ck_assert(file != 0);
    
    ::pod_composited_with_non_pod pod_composited_with_non_pod {new non_pod};

    int s = eet_data_write(file, d.native_handle(), "foo", &pod_composited_with_non_pod, false);
    ck_assert(s > 0);
    eet_sync(file);
    auto p = efl::eet::read_by_ptr(file, "foo", d);
    ck_assert(p != 0);
    ck_assert(p->member->i == 10);

    delete p->member;

    auto v = efl::eet::read(file, "foo", d);
    ck_assert(v.member->i == 10);

    delete v.member;

    eet_close(file);

    delete pod_composited_with_non_pod.member;
  }

  std::cout << "constructors called for non pod: " << constructors_called
            << " destructors called for non pod: " << destructors_called << std::endl;

  ck_assert(constructors_called == destructors_called);

  {
    auto d = efl::eet::make_descriptor
      ("pod_value_composited", efl::eet::type("member"
                                              , &pod_value_composited::member, pod_descriptor));
    static_assert(std::is_same<efl::eet::descriptor<pod_value_composited, pod_type>, decltype(d)>::value, "");

    Eet_File* file = eet_open("/tmp/eet_file_test.eet", EET_FILE_MODE_READ_WRITE);
    ck_assert(file != 0);
    
    ::pod_value_composited pod_value_composited {{5, 'a'}};

    int s = eet_data_write(file, d.native_handle(), "foo", &pod_value_composited, false);
    ck_assert(s > 0);
    eet_sync(file);
    auto p = efl::eet::read_by_ptr(file, "foo", d);
    ck_assert(p != 0);
    ck_assert(p->member.i == 5);
    ck_assert(p->member.c == 'a');

    auto v = efl::eet::read(file, "foo", d);
    ck_assert(v.member.i == 5);
    ck_assert(v.member.c == 'a');

    eet_close(file);
  }

}
END_TEST

void
eet_test_descriptors(TCase* tc)
{
  tcase_add_test(tc, eet_cxx_descriptors);
  tcase_add_test(tc, eet_cxx_descriptors_non_pod);
  tcase_add_test(tc, eet_cxx_descriptors_composition);
}