Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
portable_storages_test.h
Go to the documentation of this file.
1
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2
// All rights reserved.
3
//
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are met:
6
// * Redistributions of source code must retain the above copyright
7
// notice, this list of conditions and the following disclaimer.
8
// * Redistributions in binary form must reproduce the above copyright
9
// notice, this list of conditions and the following disclaimer in the
10
// documentation and/or other materials provided with the distribution.
11
// * Neither the name of the Andrey N. Sabelnikov nor the
12
// names of its contributors may be used to endorse or promote products
13
// derived from this software without specific prior written permission.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
//
26
27
28
29
#pragma once
30
31
#include <list>
32
#include <string>
33
#include "storages/serializeble_struct_helper.h"
34
#include "
serialization/keyvalue_serialization.h
"
35
#include "
storages/portable_storage.h
"
36
#include "
storages/portable_storage_template_helper.h
"
37
38
namespace
epee
39
{
40
namespace
tests
41
{
42
43
struct
port_test_struct_sub
44
{
45
std::string
m_str
;
46
47
BEGIN_KV_SERIALIZE_MAP
()
48
KV_SERIALIZE_VAL(
m_str
)
49
END_KV_SERIALIZE_MAP
()
50
};
51
52
#pragma pack (push, 1)
53
struct
some_pod_struct
54
{
55
uint64_t
a
;
56
int32_t
b
;
57
};
58
#pragma pack(pop)
59
60
struct
port_test_struct
61
{
62
std::string
m_str
;
63
uint64_t
m_uint64
;
64
uint32_t
m_uint32
;
65
uint16_t
m_uint16
;
66
uint8_t
m_uint8
;
67
int64_t
m_int64
;
68
int32_t
m_int32
;
69
int16_t
m_int16
;
70
int8_t
m_int8
;
71
double
m_double
;
72
bool
m_bool
;
73
some_pod_struct
m_pod
;
74
std::list<std::string>
m_list_of_str
;
75
std::list<uint64_t>
m_list_of_uint64_t
;
76
std::list<uint32_t>
m_list_of_uint32_t
;
77
std::list<uint16_t>
m_list_of_uint16_t
;
78
std::list<uint8_t>
m_list_of_uint8_t
;
79
std::list<int64_t>
m_list_of_int64_t
;
80
std::list<int32_t>
m_list_of_int32_t
;
81
std::list<int16_t>
m_list_of_int16_t
;
82
std::list<int8_t>
m_list_of_int8_t
;
83
std::list<double>
m_list_of_double
;
84
std::list<bool>
m_list_of_bool
;
85
port_test_struct_sub
m_subobj
;
86
std::list<port_test_struct>
m_list_of_self
;
87
88
BEGIN_KV_SERIALIZE_MAP
()
89
KV_SERIALIZE_VAL(
m_str
)
90
KV_SERIALIZE_VAL(
m_uint64
)
91
KV_SERIALIZE_VAL(
m_uint32
)
92
KV_SERIALIZE_VAL(
m_uint16
)
93
KV_SERIALIZE_VAL(
m_uint8
)
94
KV_SERIALIZE_VAL(
m_int64
)
95
KV_SERIALIZE_VAL(
m_int32
)
96
KV_SERIALIZE_VAL(
m_int16
)
97
KV_SERIALIZE_VAL(
m_int8
)
98
KV_SERIALIZE_VAL(
m_double
)
99
KV_SERIALIZE_VAL(
m_bool
)
100
KV_SERIALIZE_VAL_POD_AS_BLOB
(
m_pod
)
101
KV_SERIALIZE_OBJ(
m_subobj
)
102
KV_SERIALIZE_CONTAINER_VAL(
m_list_of_str
)
103
KV_SERIALIZE_CONTAINER_VAL(
m_list_of_uint64_t
)
104
KV_SERIALIZE_CONTAINER_VAL(
m_list_of_uint32_t
)
105
KV_SERIALIZE_CONTAINER_VAL(
m_list_of_uint16_t
)
106
KV_SERIALIZE_CONTAINER_VAL(
m_list_of_uint8_t
)
107
KV_SERIALIZE_CONTAINER_VAL(
m_list_of_int64_t
)
108
KV_SERIALIZE_CONTAINER_VAL(
m_list_of_int32_t
)
109
KV_SERIALIZE_CONTAINER_VAL(
m_list_of_int16_t
)
110
KV_SERIALIZE_CONTAINER_VAL(
m_list_of_int8_t
)
111
KV_SERIALIZE_CONTAINER_VAL(
m_list_of_double
)
112
KV_SERIALIZE_CONTAINER_VAL(
m_list_of_bool
)
113
KV_SERIALIZE_CONTAINER_OBJ(
m_list_of_self
)
114
END_KV_SERIALIZE_MAP
()
115
};
116
117
bool
operator != (const
port_test_struct_sub
&
a
, const
port_test_struct_sub
& b)
118
{
119
return
b.m_str !=
a
.m_str;
120
}
121
122
bool
operator ==
(
const
port_test_struct
&
a
,
const
port_test_struct
& b)
123
{
124
if
( b.
m_str
!=
a
.m_str
125
|| b.
m_uint64
!=
a
.m_uint64
126
|| b.
m_uint32
!=
a
.m_uint32
127
|| b.
m_uint16
!=
a
.m_uint16
128
|| b.
m_uint8
!=
a
.m_uint8
129
|| b.
m_int64
!=
a
.m_int64
130
|| b.
m_int32
!=
a
.m_int32
131
|| b.
m_int16
!=
a
.m_int16
132
|| b.
m_int8
!=
a
.m_int8
133
|| b.
m_double
!=
a
.m_double
134
|| b.
m_bool
!=
a
.m_bool
135
|| b.
m_pod
.
a
!=
a
.m_pod.a
136
|| b.
m_pod
.
b
!=
a
.m_pod.b
137
|| b.
m_list_of_str
!=
a
.m_list_of_str
138
|| b.
m_list_of_uint64_t
!=
a
.m_list_of_uint64_t
139
|| b.
m_list_of_uint32_t
!=
a
.m_list_of_uint32_t
140
|| b.
m_list_of_uint16_t
!=
a
.m_list_of_uint16_t
141
|| b.
m_list_of_uint8_t
!=
a
.m_list_of_uint8_t
142
|| b.
m_list_of_int64_t
!=
a
.m_list_of_int64_t
143
|| b.
m_list_of_int32_t
!=
a
.m_list_of_int32_t
144
|| b.
m_list_of_int16_t
!=
a
.m_list_of_int16_t
145
|| b.
m_list_of_int8_t
!=
a
.m_list_of_int8_t
146
|| b.
m_list_of_double
!=
a
.m_list_of_double
147
|| b.
m_list_of_bool
!=
a
.m_list_of_bool
148
|| b.
m_subobj
!=
a
.m_subobj
149
|| b.
m_list_of_self
!=
a
.m_list_of_self
150
)
151
return
false
;
152
return
true
;
153
}
154
155
void
fill_struct_with_test_values
(
port_test_struct
& s)
156
{
157
s.
m_str
=
"zuzuzuzuzuz"
;
158
s.
m_uint64
= 111111111111111;
159
s.
m_uint32
= 2222222;
160
s.
m_uint16
= 2222;
161
s.
m_uint8
= 22;
162
s.
m_int64
= -111111111111111;
163
s.
m_int32
= -2222222;
164
s.
m_int16
= -2222;
165
s.
m_int8
= -24;
166
s.
m_double
= 0.11111;
167
s.
m_bool
=
true
;
168
s.
m_pod
.
a
= 32342342342342;
169
s.
m_pod
.
b
= -342342;
170
s.
m_list_of_str
.push_back(
"1112121"
);
171
s.
m_list_of_uint64_t
.push_back(1111111111);
172
s.
m_list_of_uint64_t
.push_back(2222222222);
173
s.
m_list_of_uint32_t
.push_back(1111111);
174
s.
m_list_of_uint32_t
.push_back(2222222);
175
s.
m_list_of_uint16_t
.push_back(1111);
176
s.
m_list_of_uint16_t
.push_back(2222);
177
s.
m_list_of_uint8_t
.push_back(11);
178
s.
m_list_of_uint8_t
.push_back(22);
179
180
181
s.
m_list_of_int64_t
.push_back(-1111111111);
182
s.
m_list_of_int64_t
.push_back(-222222222);
183
s.
m_list_of_int32_t
.push_back(-1111111);
184
s.
m_list_of_int32_t
.push_back(-2222222);
185
s.
m_list_of_int16_t
.push_back(-1111);
186
s.
m_list_of_int16_t
.push_back(-2222);
187
s.
m_list_of_int8_t
.push_back(-11);
188
s.
m_list_of_int8_t
.push_back(-22);
189
190
s.
m_list_of_double
.push_back(0.11111);
191
s.
m_list_of_double
.push_back(0.22222);
192
s.
m_list_of_bool
.push_back(
true
);
193
s.
m_list_of_bool
.push_back(
false
);
194
195
s.
m_subobj
.
m_str
=
"subszzzzzzzz"
;
196
s.
m_list_of_self
.push_back(s);
197
}
198
199
bool
test_portable_storages
(
const
std::string& tests_folder)
200
{
201
serialization::portable_storage
ps, ps2;
202
port_test_struct
s1, s2;
203
fill_struct_with_test_values
(s1);
204
205
s1.store(ps);
206
std::string binbuf;
207
bool
r = ps.
store_to_binary
(binbuf);
208
209
ps2.
load_from_binary
(binbuf);
210
s2.load(ps2);
211
if
(!(s1 == s2))
212
{
213
LOG_ERROR
(
"Portable storage test failed!"
);
214
return
false
;
215
}
216
217
218
port_test_struct
ss1, ss2;
219
fill_struct_with_test_values
(ss1);
220
std::string json_buff =
epee::serialization::store_t_to_json
(ss1);
221
epee::serialization::load_t_from_json
(ss2, json_buff);
222
if
(!(ss1 == ss2))
223
{
224
LOG_ERROR
(
"Portable storage test failed!"
);
225
return
false
;
226
}
227
228
return
true
;
229
}
230
231
}
232
}
epee::serialization::portable_storage
Definition
portable_storage.h:51
epee::serialization::portable_storage::store_to_binary
bool store_to_binary(binarybuffer &target)
Definition
portable_storage.h:138
epee::serialization::portable_storage::load_from_binary
bool load_from_binary(const epee::span< const uint8_t > target)
Definition
portable_storage.h:153
operator==
bool operator==(expect< T > const &lhs, expect< U > const &rhs) noexcept(noexcept(lhs.equal(rhs)))
Definition
expect.h:401
keyvalue_serialization.h
KV_SERIALIZE_VAL_POD_AS_BLOB
#define KV_SERIALIZE_VAL_POD_AS_BLOB(varialble)
Definition
keyvalue_serialization.h:105
END_KV_SERIALIZE_MAP
#define END_KV_SERIALIZE_MAP()
Definition
keyvalue_serialization.h:102
BEGIN_KV_SERIALIZE_MAP
#define BEGIN_KV_SERIALIZE_MAP()
Definition
keyvalue_serialization.h:43
LOG_ERROR
#define LOG_ERROR(x)
Definition
misc_log_ex.h:98
epee::serialization::store_t_to_json
bool store_t_to_json(t_struct &str_in, std::string &json_buff, size_t indent=0, bool insert_newlines=true)
Definition
portable_storage_template_helper.h:62
epee::serialization::load_t_from_json
bool load_t_from_json(t_struct &out, const std::string &json_buff)
Definition
portable_storage_template_helper.h:41
epee::tests::fill_struct_with_test_values
void fill_struct_with_test_values(port_test_struct &s)
Definition
portable_storages_test.h:155
epee::tests::test_portable_storages
bool test_portable_storages(const std::string &tests_folder)
Definition
portable_storages_test.h:199
epee
Definition
ado_db_helper.h:67
tests
Definition
core_proxy.h:41
a
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition
pointer.h:1124
portable_storage.h
portable_storage_template_helper.h
int16_t
signed short int16_t
Definition
stdint.h:122
uint16_t
unsigned short uint16_t
Definition
stdint.h:125
int64_t
signed __int64 int64_t
Definition
stdint.h:135
uint32_t
unsigned int uint32_t
Definition
stdint.h:126
int32_t
signed int int32_t
Definition
stdint.h:123
uint8_t
unsigned char uint8_t
Definition
stdint.h:124
uint64_t
unsigned __int64 uint64_t
Definition
stdint.h:136
int8_t
signed char int8_t
Definition
stdint.h:121
epee::tests::port_test_struct_sub
Definition
portable_storages_test.h:44
epee::tests::port_test_struct_sub::m_str
std::string m_str
Definition
portable_storages_test.h:45
epee::tests::port_test_struct
Definition
portable_storages_test.h:61
epee::tests::port_test_struct::m_int64
int64_t m_int64
Definition
portable_storages_test.h:67
epee::tests::port_test_struct::m_subobj
port_test_struct_sub m_subobj
Definition
portable_storages_test.h:85
epee::tests::port_test_struct::m_list_of_self
std::list< port_test_struct > m_list_of_self
Definition
portable_storages_test.h:86
epee::tests::port_test_struct::m_int32
int32_t m_int32
Definition
portable_storages_test.h:68
epee::tests::port_test_struct::m_list_of_double
std::list< double > m_list_of_double
Definition
portable_storages_test.h:83
epee::tests::port_test_struct::m_list_of_int64_t
std::list< int64_t > m_list_of_int64_t
Definition
portable_storages_test.h:79
epee::tests::port_test_struct::m_list_of_uint16_t
std::list< uint16_t > m_list_of_uint16_t
Definition
portable_storages_test.h:77
epee::tests::port_test_struct::m_uint32
uint32_t m_uint32
Definition
portable_storages_test.h:64
epee::tests::port_test_struct::m_int16
int16_t m_int16
Definition
portable_storages_test.h:69
epee::tests::port_test_struct::m_int8
int8_t m_int8
Definition
portable_storages_test.h:70
epee::tests::port_test_struct::m_uint64
uint64_t m_uint64
Definition
portable_storages_test.h:63
epee::tests::port_test_struct::m_pod
some_pod_struct m_pod
Definition
portable_storages_test.h:73
epee::tests::port_test_struct::m_list_of_int32_t
std::list< int32_t > m_list_of_int32_t
Definition
portable_storages_test.h:80
epee::tests::port_test_struct::m_list_of_int8_t
std::list< int8_t > m_list_of_int8_t
Definition
portable_storages_test.h:82
epee::tests::port_test_struct::m_list_of_uint64_t
std::list< uint64_t > m_list_of_uint64_t
Definition
portable_storages_test.h:75
epee::tests::port_test_struct::m_list_of_uint8_t
std::list< uint8_t > m_list_of_uint8_t
Definition
portable_storages_test.h:78
epee::tests::port_test_struct::m_uint16
uint16_t m_uint16
Definition
portable_storages_test.h:65
epee::tests::port_test_struct::m_double
double m_double
Definition
portable_storages_test.h:71
epee::tests::port_test_struct::m_str
std::string m_str
Definition
portable_storages_test.h:62
epee::tests::port_test_struct::m_uint8
uint8_t m_uint8
Definition
portable_storages_test.h:66
epee::tests::port_test_struct::m_bool
bool m_bool
Definition
portable_storages_test.h:72
epee::tests::port_test_struct::m_list_of_bool
std::list< bool > m_list_of_bool
Definition
portable_storages_test.h:84
epee::tests::port_test_struct::m_list_of_int16_t
std::list< int16_t > m_list_of_int16_t
Definition
portable_storages_test.h:81
epee::tests::port_test_struct::m_list_of_uint32_t
std::list< uint32_t > m_list_of_uint32_t
Definition
portable_storages_test.h:76
epee::tests::port_test_struct::m_list_of_str
std::list< std::string > m_list_of_str
Definition
portable_storages_test.h:74
epee::tests::some_pod_struct
Definition
portable_storages_test.h:54
epee::tests::some_pod_struct::a
uint64_t a
Definition
portable_storages_test.h:55
epee::tests::some_pod_struct::b
int32_t b
Definition
portable_storages_test.h:56
contrib
epee
tests
src
storages
portable_storages_test.h
Generated on
for Electroneum by
1.17.0