Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_tracked_storage.cpp
Go to the documentation of this file.
1#define BOOST_TEST_MODULE tracked_storage
2#include <boost/test/included/unit_test.hpp>
5#include <boost/multi_index_container.hpp>
6#include <boost/multi_index/member.hpp>
7#include <boost/multi_index/ordered_index.hpp>
8#include <boost/multi_index/hashed_index.hpp>
9#include <boost/multi_index/mem_fun.hpp>
10#include <boost/multi_index/global_fun.hpp>
11#include <boost/multi_index/composite_key.hpp>
12#include <sstream>
13
14using boost::multi_index_container;
15using namespace boost::multi_index;
16
17struct test_size {
18 uint64_t key = 0;
20};
21
23
24struct by_key;
25typedef multi_index_container<
27 indexed_by<
28 hashed_unique< tag<by_key>, member<test_size, uint64_t, &test_size::key>, std::hash<uint64_t>>
29 >
31
37
38FC_REFLECT( test_size2, (key)(time)(s) )
39
40namespace fc::tracked {
41 template<>
42 size_t memory_size(const test_size& t) {
43 return t.s;
44 }
45
46 template<>
47 size_t memory_size(const test_size2& t) {
48 return t.s;
49 }
50}
51
52struct by_time;
53typedef multi_index_container<
55 indexed_by<
56 hashed_unique< tag<by_key>, member<test_size2, uint64_t, &test_size2::key>, std::hash<uint64_t>>,
57 ordered_non_unique< tag<by_time>, member<test_size2, fc::time_point, &test_size2::time> >
58 >
60
61
62BOOST_AUTO_TEST_SUITE(tracked_storage_tests)
63
64BOOST_AUTO_TEST_CASE(track_storage_test) {
66 BOOST_CHECK(storage.insert(test_size{ 0, 5 }).second);
67 BOOST_CHECK_EQUAL( storage.memory_size(), 5);
68 BOOST_CHECK_EQUAL( storage.index().size(), 1);
69 BOOST_CHECK(storage.insert(test_size{ 1, 4 }).second);
70 BOOST_CHECK_EQUAL( storage.memory_size(), 9);
71 BOOST_CHECK_EQUAL( storage.index().size(), 2);
72 BOOST_CHECK(storage.insert(test_size{ 2, 15 }).second);
73 BOOST_CHECK_EQUAL( storage.memory_size(), 24);
74 BOOST_CHECK_EQUAL( storage.index().size(), 3);
75 auto to_mod = storage.find(1);
76 storage.modify(to_mod, [](test_size& ts) { ts.s = 14; });
77 BOOST_CHECK_EQUAL( storage.memory_size(), 34);
78 BOOST_CHECK_EQUAL( storage.index().size(), 3);
79 storage.modify(to_mod, [](test_size& ts) { ts.s = 0; });
80 BOOST_CHECK_EQUAL( storage.memory_size(), 20);
81 BOOST_CHECK(!storage.insert(test_size{ 1, 100 }).second);
82 BOOST_CHECK_EQUAL( storage.memory_size(), 20);
83 BOOST_CHECK_EQUAL( storage.index().size(), 3);
84 storage.erase(2);
85 BOOST_CHECK_EQUAL( storage.memory_size(), 5);
86 BOOST_CHECK_NO_THROW(storage.erase(2));
87 BOOST_CHECK_EQUAL( storage.memory_size(), 5);
88 storage.erase( storage.index().find(0) );
89 BOOST_CHECK_EQUAL( storage.memory_size(), 0);
90}
91
92BOOST_AUTO_TEST_CASE(simple_write_read_file_storage_test) {
93 using tracked_storage1 = fc::tracked_storage<test_size_container>;
94 tracked_storage1 storage1_1;
95 BOOST_CHECK_EQUAL( storage1_1.memory_size(), 0);
96 BOOST_CHECK_EQUAL( storage1_1.index().size(), 0);
97
99 auto out = fc::persistence_util::open_cfile_for_write(td.path(), "temp.dat");
101 storage1_1.write(out);
102 out.flush();
103 out.close();
104
105 auto content = fc::persistence_util::open_cfile_for_read(td.path(), "temp.dat");
106 auto version = fc::persistence_util::read_persistence_header(content, 0x12345678, 5, 5);
107 BOOST_CHECK_EQUAL( version, 5 );
108 auto ds = content.create_datastream();
109 tracked_storage1 storage1_2;
110 BOOST_CHECK(storage1_2.read(ds, 500));
111 BOOST_CHECK_EQUAL( storage1_2.index().size(), 0);
112 BOOST_CHECK_EQUAL( storage1_2.memory_size(), 0);
113
114 const auto tellp = content.tellp();
115 content.seek_end(0);
116 BOOST_CHECK_EQUAL( content.tellp(), tellp );
117}
118
119BOOST_AUTO_TEST_CASE(single_write_read_file_storage_test) { try {
120 using tracked_storage1 = fc::tracked_storage<test_size_container>;
121 tracked_storage1 storage1_1;
122 storage1_1.insert(test_size{ 0, 6 });
123 BOOST_CHECK_EQUAL( storage1_1.memory_size(), 6);
124 BOOST_CHECK_EQUAL( storage1_1.index().size(), 1);
126 auto out = fc::persistence_util::open_cfile_for_write(td.path(), "temp.dat");
128 storage1_1.write(out);
129 out.flush();
130 out.close();
131
132 auto content = fc::persistence_util::open_cfile_for_read(td.path(), "temp.dat");
133 auto version = fc::persistence_util::read_persistence_header(content, 0x12345678, 5, 5);
134 BOOST_CHECK_EQUAL( version, 5 );
135 auto ds = content.create_datastream();
136 tracked_storage1 storage1_2;
137 BOOST_CHECK(storage1_2.read(ds, 500));
138 BOOST_CHECK_EQUAL( storage1_2.index().size(), 1);
139 const auto& primary_idx2 = storage1_2.index().get<by_key>();
140 auto itr2 = primary_idx2.cbegin();
141 BOOST_CHECK_EQUAL( itr2->key, 0);
142 BOOST_CHECK_EQUAL( itr2->s, 6);
143 BOOST_CHECK_EQUAL( storage1_2.memory_size(), 6);
144
145 const auto tellp = content.tellp();
146 content.seek_end(0);
147 BOOST_CHECK_EQUAL( content.tellp(), tellp );
149
150BOOST_AUTO_TEST_CASE(write_read_file_storage_test) {
151 using tracked_storage1 = fc::tracked_storage<test_size_container>;
152 tracked_storage1 storage1_1;
153 storage1_1.insert(test_size{ 0, 6 });
154 storage1_1.insert(test_size{ 3, 7 });
155 storage1_1.insert(test_size{ 5, 3 });
156 storage1_1.insert(test_size{ 9, 4 });
157 storage1_1.insert(test_size{ 15, 6 });
158 storage1_1.insert(test_size{ 16, 4 });
159 storage1_1.insert(test_size{ 19, 3 });
160 storage1_1.insert(test_size{ 25, 7 });
161 BOOST_CHECK_EQUAL( storage1_1.memory_size(), 40);
162 BOOST_CHECK_EQUAL( storage1_1.index().size(), 8);
163
165 auto out = fc::persistence_util::open_cfile_for_write(td.path(), "temp.dat");
167 storage1_1.write(out);
168
169 using tracked_storage2 = fc::tracked_storage<test_size2_container>;
170 tracked_storage2 storage2_1;
171 const auto now = fc::time_point::now();
172 storage2_1.insert(test_size2{ 3, now, 7 });
173 BOOST_CHECK_EQUAL( storage2_1.memory_size(), 7);
174 BOOST_CHECK_EQUAL( storage2_1.index().size(), 1);
175
176 storage2_1.write(out);
177
178 out.flush();
179 out.close();
180
181 auto content = fc::persistence_util::open_cfile_for_read(td.path(), "temp.dat");
182 auto version = fc::persistence_util::read_persistence_header(content, 0x12345678, 5, 5);
183 BOOST_CHECK_EQUAL( version, 5 );
184 auto ds = content.create_datastream();
185 tracked_storage1 storage1_2;
186 BOOST_CHECK(storage1_2.read(ds, 500));
187 BOOST_CHECK_EQUAL( storage1_2.index().size(), 8);
188 const auto& primary_idx1_2 = storage1_2.index().get<by_key>();
189 auto itr2 = primary_idx1_2.cbegin();
190 BOOST_CHECK_EQUAL( itr2->key, 0);
191 BOOST_CHECK_EQUAL( itr2->s, 6);
192 BOOST_CHECK_EQUAL( (++itr2)->key, 3);
193 BOOST_CHECK_EQUAL( itr2->s, 7);
194 BOOST_CHECK_EQUAL( (++itr2)->key, 5);
195 BOOST_CHECK_EQUAL( itr2->s, 3);
196 BOOST_CHECK_EQUAL( (++itr2)->key, 9);
197 BOOST_CHECK_EQUAL( itr2->s, 4);
198 BOOST_CHECK_EQUAL( (++itr2)->key, 15);
199 BOOST_CHECK_EQUAL( itr2->s, 6);
200 BOOST_CHECK_EQUAL( (++itr2)->key, 16);
201 BOOST_CHECK_EQUAL( itr2->s, 4);
202 BOOST_CHECK_EQUAL( (++itr2)->key, 19);
203 BOOST_CHECK_EQUAL( itr2->s, 3);
204 BOOST_CHECK_EQUAL( (++itr2)->key, 25);
205 BOOST_CHECK_EQUAL( itr2->s, 7);
206 BOOST_CHECK_EQUAL( storage1_2.memory_size(), 40);
207
208 tracked_storage2 storage2_2;
209 BOOST_CHECK(storage2_2.read(ds, 500));
210 BOOST_CHECK_EQUAL( storage2_2.index().size(), 1);
211 const auto& primary_idx2_2 = storage2_2.index().get<by_key>();
212 auto itr3 = primary_idx2_2.cbegin();
213 BOOST_CHECK_EQUAL( itr3->key, 3);
214 BOOST_CHECK( itr3->time == now);
215 BOOST_CHECK_EQUAL( itr3->s, 7);
216
217 const auto tellp = content.tellp();
218 content.seek_end(0);
219 BOOST_CHECK_EQUAL( content.tellp(), tellp );
220}
221
222BOOST_AUTO_TEST_SUITE_END()
const fc::path & path() const
static time_point now()
Definition time.cpp:14
tracks the size of storage allocated to its underlying multi_index
void erase(const Key &key)
size_t memory_size() const
std::pair< typename primary_index_type::iterator, bool > insert(typename ContainerType::value_type obj)
void modify(typename primary_index_type::iterator itr, Lam lam)
const ContainerType & index() const
primary_index_type::iterator find(const Key &key)
#define FC_LOG_AND_RETHROW()
cfile open_cfile_for_write(const fc::path &dir, const std::string &filename)
void write_persistence_header(cfile &dat_content, const uint32_t magic_number, const uint32_t current_version)
uint32_t read_persistence_header(cfile &dat_content, const uint32_t magic_number, const uint32_t min_supported_version, const uint32_t max_supported_version)
cfile open_cfile_for_read(const fc::path &dir, const std::string &filename)
size_t memory_size(const T &obj)
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition reflect.hpp:311
unsigned __int64 uint64_t
Definition stdint.h:136
fc::time_point time
multi_index_container< test_size, indexed_by< hashed_unique< tag< by_key >, member< test_size, uint64_t, &test_size::key >, std::hash< uint64_t > > > > test_size_container
FC_REFLECT(test_size2,(key)(time)(s)) namespace fc typedef multi_index_container< test_size2, indexed_by< hashed_unique< tag< by_key >, member< test_size2, uint64_t, &test_size2::key >, std::hash< uint64_t > >, ordered_non_unique< tag< by_time >, member< test_size2, fc::time_point, &test_size2::time > > > > test_size2_container
BOOST_AUTO_TEST_CASE(track_storage_test)
char * s