Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_tracked_storage.cpp File Reference
#include <boost/test/included/unit_test.hpp>
#include <fc/container/tracked_storage.hpp>
#include <fc/io/persistence_util.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/global_fun.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <sstream>
Include dependency graph for test_tracked_storage.cpp:

Go to the source code of this file.

Classes

struct  test_size
 
struct  test_size2
 

Macros

#define BOOST_TEST_MODULE   tracked_storage
 

Typedefs

typedef 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
 

Functions

 FC_REFLECT (test_size,(key)(s)) struct by_key
 
 BOOST_AUTO_TEST_CASE (track_storage_test)
 
 BOOST_AUTO_TEST_CASE (simple_write_read_file_storage_test)
 
 BOOST_AUTO_TEST_CASE (single_write_read_file_storage_test)
 
 BOOST_AUTO_TEST_CASE (write_read_file_storage_test)
 

Variables

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
 

Macro Definition Documentation

◆ BOOST_TEST_MODULE

#define BOOST_TEST_MODULE   tracked_storage

Definition at line 1 of file test_tracked_storage.cpp.

Typedef Documentation

◆ test_size_container

typedef 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

Definition at line 30 of file test_tracked_storage.cpp.

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/4]

BOOST_AUTO_TEST_CASE ( simple_write_read_file_storage_test )

Definition at line 92 of file test_tracked_storage.cpp.

92 {
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}
const fc::path & path() const
tracks the size of storage allocated to its underlying multi_index
static const Segment ds(Segment::ds)
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)
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/4]

BOOST_AUTO_TEST_CASE ( single_write_read_file_storage_test )

Definition at line 119 of file test_tracked_storage.cpp.

119 { 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 );
std::pair< typename primary_index_type::iterator, bool > insert(typename ContainerType::value_type obj)
#define FC_LOG_AND_RETHROW()
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [3/4]

BOOST_AUTO_TEST_CASE ( track_storage_test )

Definition at line 64 of file test_tracked_storage.cpp.

64 {
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}
void erase(const Key &key)
size_t memory_size() const
void modify(typename primary_index_type::iterator itr, Lam lam)
const ContainerType & index() const
primary_index_type::iterator find(const Key &key)
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [4/4]

BOOST_AUTO_TEST_CASE ( write_read_file_storage_test )

Definition at line 150 of file test_tracked_storage.cpp.

150 {
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}
static time_point now()
Definition time.cpp:14
Here is the call graph for this function:

◆ FC_REFLECT()

FC_REFLECT ( test_size ,
(key)(s)  )

Variable Documentation

◆ test_size2_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

Definition at line 59 of file test_tracked_storage.cpp.