Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test.cpp
Go to the documentation of this file.
1#define BOOST_TEST_MODULE chainbase test
2
3#include <boost/test/unit_test.hpp>
5
6#include <boost/multi_index_container.hpp>
7#include <boost/multi_index/ordered_index.hpp>
8#include <boost/multi_index/member.hpp>
9
10#include <iostream>
11
12using namespace chainbase;
13using namespace boost::multi_index;
14
15//BOOST_TEST_SUITE( serialization_tests, clean_database_fixture )
16
17struct book : public chainbase::object<0, book> {
18
19 template<typename Constructor, typename Allocator>
20 book( Constructor&& c, Allocator&& a ) {
21 c(*this);
22 }
23
25 int a = 0;
26 int b = 1;
27};
28
29typedef multi_index_container<
30 book,
31 indexed_by<
32 ordered_unique< member<book,book::id_type,&book::id> >,
33 ordered_unique< BOOST_MULTI_INDEX_MEMBER(book,int,a) >,
34 ordered_unique< BOOST_MULTI_INDEX_MEMBER(book,int,b) >
35 >,
38
40
41
42BOOST_AUTO_TEST_CASE( open_and_create ) {
43 boost::filesystem::path temp = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
44 try {
45 std::cerr << temp << " \n";
46
47 chainbase::database db(temp, database::read_write, 1024*1024*8);
48 chainbase::database db2(temp, database::read_only, 0, true);
49 BOOST_CHECK_THROW( db2.add_index< book_index >(), std::runtime_error );
50
51 db.add_index< book_index >();
52 BOOST_CHECK_THROW( db.add_index<book_index>(), std::logic_error );
53
54
55 db2.add_index< book_index >();
56
57
58 BOOST_TEST_MESSAGE( "Creating book" );
59 const auto& new_book = db.create<book>( []( book& b ) {
60 b.a = 3;
61 b.b = 4;
62 } );
63 const auto& copy_new_book = db2.get( book::id_type(0) );
64 BOOST_REQUIRE( &new_book != &copy_new_book );
65
66 BOOST_REQUIRE_EQUAL( new_book.a, copy_new_book.a );
67 BOOST_REQUIRE_EQUAL( new_book.b, copy_new_book.b );
68
69 db.modify( new_book, [&]( book& b ) {
70 b.a = 5;
71 b.b = 6;
72 });
73 BOOST_REQUIRE_EQUAL( new_book.a, 5 );
74 BOOST_REQUIRE_EQUAL( new_book.b, 6 );
75
76 BOOST_REQUIRE_EQUAL( new_book.a, copy_new_book.a );
77 BOOST_REQUIRE_EQUAL( new_book.b, copy_new_book.b );
78
79 {
80 auto session = db.start_undo_session(true);
81 db.modify( new_book, [&]( book& b ) {
82 b.a = 7;
83 b.b = 8;
84 });
85
86 BOOST_REQUIRE_EQUAL( new_book.a, 7 );
87 BOOST_REQUIRE_EQUAL( new_book.b, 8 );
88 }
89 BOOST_REQUIRE_EQUAL( new_book.a, 5 );
90 BOOST_REQUIRE_EQUAL( new_book.b, 6 );
91
92 {
93 auto session = db.start_undo_session(true);
94 const auto& book2 = db.create<book>( [&]( book& b ) {
95 b.a = 9;
96 b.b = 10;
97 });
98
99 BOOST_REQUIRE_EQUAL( new_book.a, 5 );
100 BOOST_REQUIRE_EQUAL( new_book.b, 6 );
101 BOOST_REQUIRE_EQUAL( book2.a, 9 );
102 BOOST_REQUIRE_EQUAL( book2.b, 10 );
103 }
104 BOOST_CHECK_THROW( db2.get( book::id_type(1) ), std::out_of_range );
105 BOOST_REQUIRE_EQUAL( new_book.a, 5 );
106 BOOST_REQUIRE_EQUAL( new_book.b, 6 );
107
108
109 {
110 auto session = db.start_undo_session(true);
111 db.modify( new_book, [&]( book& b ) {
112 b.a = 7;
113 b.b = 8;
114 });
115
116 BOOST_REQUIRE_EQUAL( new_book.a, 7 );
117 BOOST_REQUIRE_EQUAL( new_book.b, 8 );
118 session.push();
119 }
120 BOOST_REQUIRE_EQUAL( new_book.a, 7 );
121 BOOST_REQUIRE_EQUAL( new_book.b, 8 );
122 db.undo();
123 BOOST_REQUIRE_EQUAL( new_book.a, 5 );
124 BOOST_REQUIRE_EQUAL( new_book.b, 6 );
125
126 BOOST_REQUIRE_EQUAL( new_book.a, copy_new_book.a );
127 BOOST_REQUIRE_EQUAL( new_book.b, copy_new_book.b );
128 } catch ( ... ) {
129 bfs::remove_all( temp );
130 throw;
131 }
132 bfs::remove_all( temp );
133}
134
135// BOOST_AUTO_TEST_SUITE_END()
#define CHAINBASE_SET_INDEX_TYPE(OBJECT_TYPE, INDEX_TYPE)
void modify(const ObjectType &obj, Modifier &&m)
session start_undo_session(bool enabled)
Definition chainbase.cpp:73
const ObjectType & create(Constructor &&con)
const ObjectType & get(CompatibleKey &&key) const
Concept for allocating, resizing and freeing memory block.
CK_SESSION_HANDLE session
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
Definition test.cpp:17
int b
Definition test.cpp:26
int a
Definition test.cpp:25
book(Constructor &&c, Allocator &&a)
Definition test.cpp:20
id_type id
Definition test.cpp:24
BOOST_AUTO_TEST_CASE(open_and_create)
Definition test.cpp:42
multi_index_container< book, indexed_by< ordered_unique< member< book, book::id_type,&book::id > >, ordered_unique< BOOST_MULTI_INDEX_MEMBER(book, int, a) >, ordered_unique< BOOST_MULTI_INDEX_MEMBER(book, int, b) > >, chainbase::node_allocator< book > > book_index
Definition test.cpp:37