aboutsummaryrefslogtreecommitdiff
path: root/common/container/basics/test.hxx
blob: 1edb05f9c01550fd7f51ada4f1721f63f2decb81 (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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// file      : common/container/basics/test.hxx
// copyright : Copyright (c) 2009-2019 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef TEST_HXX
#define TEST_HXX

#include <common/config.hxx> // HAVE_CXX11

#include <map>
#include <set>
#include <list>
#include <vector>
#include <deque>
#include <string>

#ifdef HAVE_CXX11
#  include <array>
#  include <forward_list>
#  include <unordered_map>
#  include <unordered_set>
#endif

#include <odb/core.hxx>

#pragma db value
struct comp
{
  comp () {}
  comp (int n, const std::string& s) : num (n), str (s) {}

  #pragma db column("number")
  int num;
  std::string str;
};

inline bool
operator== (const comp& x, const comp& y)
{
  return x.num == y.num && x.str == y.str;
}

inline bool
operator!= (const comp& x, const comp& y)
{
  return !(x == y);
}

inline bool
operator< (const comp& x, const comp& y)
{
  return x.num != y.num ? x.num < y.num : x.str < y.str;
}

typedef std::list<std::string> str_list;
typedef std::deque<int> num_deque;

typedef std::vector<int> num_vector;
typedef std::vector<std::string> str_vector;

typedef std::set<int> num_set;
typedef std::set<std::string> str_set;
typedef std::set<comp> comp_set;

typedef std::map<int, std::string> num_str_map;
typedef std::map<std::string, int> str_num_map;
typedef std::map<int, comp> num_comp_map;
typedef std::map<comp, std::string> comp_str_map;

#ifdef HAVE_CXX11
struct comp_hash
{
  std::size_t
  operator() (const comp& x) const {return nh (x.num) + sh (x.str);}

  std::hash<int> nh;
  std::hash<std::string> sh;
};

typedef std::array<int, 3> num_array;
typedef std::array<std::string, 3> str_array;
typedef std::array<comp, 3> comp_array;

typedef std::forward_list<int> num_flist;
typedef std::forward_list<std::string> str_flist;
typedef std::forward_list<comp> comp_flist;

typedef std::unordered_set<int> num_uset;
typedef std::unordered_set<std::string> str_uset;
typedef std::unordered_set<comp, comp_hash> comp_uset;

typedef std::unordered_map<int, std::string> num_str_umap;
typedef std::unordered_map<std::string, int> str_num_umap;
typedef std::unordered_map<int, comp> num_comp_umap;
typedef std::unordered_map<comp, std::string, comp_hash> comp_str_umap;
#endif

#pragma db value
struct cont_comp1
{
  // This composite value does not have any columns.
  //
  num_vector sv; // Have the name "conflic" with the one in the object.
};

#pragma db value
struct cont_comp2
{
  cont_comp2 (): num (777), str ("ggg") {}

  int num;
  str_list sl;
  std::string str;
};

#pragma db object
struct object
{
  object (): nv (comp1_.sv), sl (comp2_.sl) {}
  object (const std::string& id) : id_ (id), nv (comp1_.sv), sl (comp2_.sl) {}

  #pragma db id
  std::string id_;

  int num;

  cont_comp1 comp1_;
  cont_comp2 comp2_;

  // vector
  //
  #pragma db transient
  num_vector& nv;

  #pragma db table("object_strings") id_column ("obj_id")
  str_vector sv;

  #pragma db value_column("")
  std::vector<comp> cv;

  #pragma db unordered
  num_vector uv;

  // list
  //
  #pragma db transient
  str_list& sl;

  // deque
  //
  num_deque nd;

  // set
  //
  num_set ns;
  str_set ss;
  comp_set cs;

  // map
  //
  num_str_map nsm;
  str_num_map snm;
  num_comp_map ncm;
  comp_str_map csm;

#ifdef HAVE_CXX11
  // array
  //
  num_array na;
  str_array sa;
  comp_array ca;

  // forward_list
  //
  num_flist nfl;
  str_flist sfl;
  comp_flist cfl;

  // unordered_set
  //
  num_uset nus;
  str_uset sus;
  comp_uset cus;

  // unordered_map
  //
  num_str_umap nsum;
  str_num_umap snum;
  num_comp_umap ncum;
  comp_str_umap csum;
#else
  // Dummy containers to get the equivalent DROP TABLE statements.
  //
  num_vector na;
  num_vector sa;
  num_vector ca;

  num_vector nfl;
  num_vector sfl;
  num_vector cfl;

  num_set nus;
  str_set sus;
  comp_set cus;

  num_str_map nsum;
  str_num_map snum;
  num_comp_map ncum;
  comp_str_map csum;
#endif

  std::string str;
};

inline bool
operator== (const object& x, const object& y)
{
  if (x.uv.size () != y.uv.size ())
    return false;

  int xs (0), ys (0);

  for (num_vector::size_type i (0); i < x.uv.size (); ++i)
  {
    xs += x.uv[i];
    ys += y.uv[i];
  }

  return
    x.id_ == y.id_ &&
    x.num == y.num &&

    x.comp2_.num == y.comp2_.num &&
    x.comp2_.str == y.comp2_.str &&

    x.nv == y.nv &&
    x.sv == y.sv &&
    x.cv == y.cv &&
    xs == ys &&

    x.sl == y.sl &&

    x.nd == y.nd &&

    x.ns == y.ns &&
    x.ss == y.ss &&
    x.cs == y.cs &&

    x.nsm == y.nsm &&
    x.snm == y.snm &&
    x.ncm == y.ncm &&
    x.csm == y.csm &&

#ifdef HAVE_CXX11
    x.na == y.na &&
    x.sa == y.sa &&
    x.ca == y.ca &&

    x.nfl == y.nfl &&
    x.sfl == y.sfl &&
    x.cfl == y.cfl &&

    x.nus == y.nus &&
    x.sus == y.sus &&
    x.cus == y.cus &&

    x.nsum == y.nsum &&
    x.snum == y.snum &&
    x.ncum == y.ncum &&
    x.csum == y.csum &&
#endif

    x.str == y.str;
}

#endif // TEST_HXX