Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test.cpp File Reference
#include <boost/test/unit_test.hpp>
#include <chainbase/chainbase.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
#include <iostream>
Include dependency graph for test.cpp:

Go to the source code of this file.

Classes

struct  book
 

Macros

#define BOOST_TEST_MODULE   chainbase test
 

Typedefs

typedef 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
 

Functions

 BOOST_AUTO_TEST_CASE (open_and_create)
 

Macro Definition Documentation

◆ BOOST_TEST_MODULE

#define BOOST_TEST_MODULE   chainbase test

Definition at line 1 of file test.cpp.

Typedef Documentation

◆ book_index

typedef 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 at line 37 of file test.cpp.

Function Documentation

◆ BOOST_AUTO_TEST_CASE()

BOOST_AUTO_TEST_CASE ( open_and_create )

open an already created db

index does not exist in read only database

cannot add same index twice

index should exist now

< these are mapped to different address ranges

Definition at line 42 of file test.cpp.

42 {
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}
CK_SESSION_HANDLE session
Definition test.cpp:17
int b
Definition test.cpp:26
int a
Definition test.cpp:25
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
Here is the call graph for this function: