Electroneum
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"
37
38namespace epee
39{
40 namespace tests
41 {
42
44 {
45 std::string m_str;
46
48 KV_SERIALIZE_VAL(m_str)
50 };
51
52#pragma pack (push, 1)
58#pragma pack(pop)
59
61 {
62 std::string m_str;
71 double m_double;
72 bool m_bool;
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;
86 std::list<port_test_struct> m_list_of_self;
87
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)
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)
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
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
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 {
202 port_test_struct s1, s2;
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;
220 std::string json_buff = epee::serialization::store_t_to_json(ss1);
222 if(!(ss1 == ss2))
223 {
224 LOG_ERROR("Portable storage test failed!");
225 return false;
226 }
227
228 return true;
229 }
230
231 }
232}
bool store_to_binary(binarybuffer &target)
bool load_from_binary(const epee::span< const uint8_t > target)
bool operator==(expect< T > const &lhs, expect< U > const &rhs) noexcept(noexcept(lhs.equal(rhs)))
Definition expect.h:401
#define KV_SERIALIZE_VAL_POD_AS_BLOB(varialble)
#define END_KV_SERIALIZE_MAP()
#define BEGIN_KV_SERIALIZE_MAP()
#define LOG_ERROR(x)
Definition misc_log_ex.h:98
bool store_t_to_json(t_struct &str_in, std::string &json_buff, size_t indent=0, bool insert_newlines=true)
bool load_t_from_json(t_struct &out, const std::string &json_buff)
void fill_struct_with_test_values(port_test_struct &s)
bool test_portable_storages(const std::string &tests_folder)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1124
signed short int16_t
Definition stdint.h:122
unsigned short uint16_t
Definition stdint.h:125
signed __int64 int64_t
Definition stdint.h:135
unsigned int uint32_t
Definition stdint.h:126
signed int int32_t
Definition stdint.h:123
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
signed char int8_t
Definition stdint.h:121
std::list< port_test_struct > m_list_of_self
std::list< uint16_t > m_list_of_uint16_t
std::list< uint64_t > m_list_of_uint64_t
std::list< uint32_t > m_list_of_uint32_t
std::list< std::string > m_list_of_str