forked from phambryan/hiberlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.cpp
195 lines (169 loc) · 3.74 KB
/
tests.cpp
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
#include "hiberlite.h"
using namespace hiberlite;
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
class B{
friend class hiberlite::access;
template<class Archive>
void hibernate(Archive & ar)
{
ar & HIBERLITE_NVP(name);
ar & HIBERLITE_NVP(weight);
ar & HIBERLITE_NVP(height);
}
public:
string name;
double weight;
int height;
};
class A{
friend class hiberlite::access;
template<class Archive>
void hibernate(Archive & ar)
{
ar & HIBERLITE_NVP(seti);
ar & HIBERLITE_NVP(i);
ar & HIBERLITE_NVP(s);
ar & HIBERLITE_NVP(vi);
ar & HIBERLITE_NVP(v);
ar & HIBERLITE_NVP(mm);
}
public:
int i;
string s;
vector< pair<int,int> > vi;
set< double > seti;
vector< bean_ptr<B> > v;
map<int,vector<int> > mm;
};
class X{
friend class hiberlite::access;
template<class Archive>
void hibernate(Archive & ar)
{
ar & HIBERLITE_NVP(seti);
ar & HIBERLITE_NVP(vvi);
ar & HIBERLITE_NVP(mm);
}
public:
set< pair<int,double> > seti;
vector< vector<int> > vvi;
map< vector<string>, map<string,string> > mm;
bool operator==(const X& x){
return seti==x.seti && vvi==x.vvi && mm==x.mm;
}
};
HIBERLITE_EXPORT_CLASS(A)
HIBERLITE_EXPORT_CLASS(B)
HIBERLITE_EXPORT_CLASS(X)
struct Tester{
void test1(){
Database db("test.db");
db.registerBeanClass<A>();
db.registerBeanClass<B>();
vector<string> msg=db.checkModel();
for(size_t ci=0;ci<msg.size();ci++)
cout << "model check reported: " << msg[ci] << endl;
db.dropModel();
db.createModel();
for(int i=0;i<5;i++){
A obj;
obj.i=i;
char ss[100];
sprintf(ss,"obj_num_%d",i);
obj.s=ss;
B x;
for(int i=0;i<5;i++){
obj.mm[i%3].push_back(i);
obj.mm[i%3].push_back(2*i);
obj.vi.push_back(make_pair(i,i*i*i));
obj.seti.insert(1.0/i);
string name("01234567890123123123123");
x.name="name " + name.substr(i,3);
x.weight=sqrt(i);
x.height=-10*i;
bean_ptr<B> ptr=db.copyBean(x);
obj.v.push_back(ptr);
}
while(obj.v.size()){
obj.v[0].destroy();
obj.v.erase(obj.v.begin());
}
bean_ptr<A> a=db.copyBean(obj);
if(a.get_id()%10==0)
cout << "save obj with name " << (*a).s << endl;
}
}
void test2(){
Database db("test.db");
// we will not create or drop tables, so model initialization
// is not necessary
// db.registerBeanClass<A>();
// db.registerBeanClass<B>();
Database otherdb("copy.db");
otherdb.registerBeanClass<A>();
otherdb.registerBeanClass<B>();
otherdb.dropModel();
otherdb.createModel();
bean_ptr<A> be=db.loadBean<A>(1);
A obj2=*be;
bean_ptr<A> a2=otherdb.copyBean(obj2);
cout << "load obj with name " << (*a2).s << endl;
}
void test3(){
X x;
x.vvi.resize(5);
map< vector<string>, map<string,string> > mm;
vector<string> randstr;
int NS=30;
for(int i=0;i<NS;i++)
randstr.push_back(hiberlite::Transformer::toSQLiteValue(rand()%100+i));
for(int i=0;i<100;i++){
x.seti.insert( make_pair(i*i,1.0/i+sqrt(i)) );
x.vvi[(i*i)%5].push_back(10003%(i+1));
vector<string> vs;
for(int i=0;i<5;i++)
vs.push_back( randstr[(17+7*i)%NS] );
for(int i=0;i<10;i++)
x.mm[vs][ randstr[(19*i)%NS] ]=randstr[(23*i)%NS];
}
{
Database db("t3.db");
db.registerBeanClass<X>();
db.dropModel();
db.createModel();
db.copyBean(x);
}
{
Database db("t3.db");
db.registerBeanClass<X>();
bean_ptr<X> xptr=db.loadBean<X>(1);
if( !(*xptr==x) )
throw std::runtime_error("load failed");
}
}
};
int main()
{
try{
{
Tester t;
t.test1();
t.test2();
t.test3();
}
cout << "tests passed\n";
}catch(std::exception& e){
cerr << "caught exception\n";
cerr << e.what() << "\n";
return 1;
}
catch(...){
cerr << "caught strange exception\n";
return 1;
}
return 0;
}/**/