• R/O
  • SSH
  • HTTPS

eirsamples: Commit


Commit MetaInfo

Revision28 (tree)
Time2019-07-14 17:51:16
Authorquiret

Log Message

- added tests for the ArrayDatabase transaction database format of libdatacompact

Change Summary

Incremental Difference

--- bigunittest/src/arrdbtest.cpp (nonexistent)
+++ bigunittest/src/arrdbtest.cpp (revision 28)
@@ -0,0 +1,311 @@
1+#include <libdatacompact/arrdb.h>
2+
3+#include <CFileSystem.h>
4+#include <NativeExecutive/CExecutiveManager.h>
5+
6+#include <assert.h>
7+
8+void ARRAYDB_TESTS( void )
9+{
10+ using namespace FileSystem;
11+ using namespace NativeExecutive;
12+
13+ fileSysInstance fileSys;
14+ natExecInst execMan;
15+
16+ printf( "testing ArrayDatabase creation..." );
17+ {
18+ filePtr memFile = fileSys->CreateMemoryFile();
19+
20+ ArrayDatabase::db_construction_params cparams;
21+ cparams.item_block_size = 1024;
22+ cparams.journal_size = 65536;
23+ cparams.user_ident = 0x12345678;
24+
25+ ArrayDatabase::CreateDatabase( memFile, cparams );
26+
27+ ArrayDatabase db( execMan, std::move( memFile ) );
28+
29+ assert( db.GetNumParallelTransactions() == 0 );
30+ assert( db.GetItemBlockSize() == 1024 );
31+ assert( db.GetJournalSize() == 65536 );
32+ assert( db.GetUserIdent() == 0x12345678 );
33+ }
34+ printf( "ok.\n" );
35+
36+ printf( "testing ArrayDatabase simple transaction..." );
37+ {
38+ filePtr memFile = fileSys->CreateMemoryFile();
39+
40+ ArrayDatabase::db_construction_params cparams;
41+ cparams.item_block_size = 1024;
42+ cparams.journal_size = 512;
43+ cparams.user_ident = 0x12345678;
44+
45+ ArrayDatabase::CreateDatabase( memFile, cparams );
46+
47+ // Exercise writing.
48+ {
49+ ArrayDatabase db( execMan, filePtr( memFile, false ) );
50+
51+ // Write some nice stuff.
52+ std::uint32_t tid = db.BeginTransaction( 0 );
53+
54+ db.WriteStruct( tid, 0, (int)1 );
55+ db.WriteStruct( tid, 4, (int)2 );
56+
57+ db.EndTransaction( tid );
58+
59+ // Verify that stuff has been written.
60+ assert( db.ReadStruct_EX <int> ( 0, 0 ) == 1 );
61+ assert( db.ReadStruct_EX <int> ( 0, 4 ) == 2 );
62+ }
63+
64+ // Make sure we can load it again.
65+ {
66+ ArrayDatabase db( execMan, std::move( memFile ) );
67+
68+ // Verify stuff again.
69+ assert( db.ReadStruct_EX <int> ( 0, 0 ) == 1 );
70+ assert( db.ReadStruct_EX <int> ( 0, 4 ) == 2 );
71+ }
72+ }
73+ printf( "ok.\n" );
74+
75+ printf( "testing ArrayDatabase multiple transactions..." );
76+ {
77+ filePtr memFile = fileSys->CreateMemoryFile();
78+
79+ ArrayDatabase::db_construction_params cparams;
80+ cparams.item_block_size = 1024;
81+ cparams.journal_size = 1024;
82+ cparams.user_ident = 0x87654321;
83+
84+ ArrayDatabase::CreateDatabase( memFile, cparams );
85+
86+ ArrayDatabase db( execMan, std::move( memFile ) );
87+
88+ std::uint32_t tid1 = db.BeginTransaction( 0 );
89+ std::uint32_t tid2 = db.BeginTransaction( 0 );
90+ std::uint32_t tid3 = db.BeginTransaction( 0 );
91+ std::uint32_t tid4 = db.BeginTransaction( 0 );
92+
93+ db.WriteStruct( tid1, 0, (int)100 );
94+ db.WriteStruct( tid2, 4, (int)200 );
95+ db.WriteStruct( tid3, 8, (int)300 );
96+ db.WriteStruct( tid4, 12, (int)400 );
97+
98+ db.EndTransaction( tid1 );
99+ db.EndTransaction( tid2 );
100+ db.EndTransaction( tid3 );
101+ db.EndTransaction( tid4 );
102+
103+ assert( db.ReadStruct_EX <int> ( 0, 0 ) == 100 );
104+ assert( db.ReadStruct_EX <int> ( 0, 4 ) == 200 );
105+ assert( db.ReadStruct_EX <int> ( 0, 8 ) == 300 );
106+ assert( db.ReadStruct_EX <int> ( 0, 12 ) == 400 );
107+ }
108+ printf( "ok.\n" );
109+
110+ printf( "testing ArrayDatabase many multiple transactions..." );
111+ {
112+ filePtr memFile = fileSys->CreateMemoryFile();
113+
114+ ArrayDatabase::db_construction_params cparams;
115+ cparams.item_block_size = 128;
116+ cparams.journal_size = 100;
117+ cparams.user_ident = 0x87654321;
118+
119+ ArrayDatabase::CreateDatabase( memFile, cparams );
120+
121+ ArrayDatabase db( execMan, std::move( memFile ) );
122+
123+ static constexpr size_t NUM_TRANS_CAST = 512;
124+
125+ for ( size_t n = 0; n < NUM_TRANS_CAST; n++ )
126+ {
127+ std::uint32_t tid = db.BeginTransaction( 0 );
128+
129+ db.WriteStruct( tid, 0, n );
130+
131+ db.EndTransaction( tid );
132+ }
133+
134+ assert( db.ReadStruct_EX <size_t> ( 0, 0 ) == ( NUM_TRANS_CAST - 1 ) );
135+ }
136+ printf( "ok.\n" );
137+
138+ printf( "testing ArrayDatabase abort transaction..." );
139+ {
140+ filePtr memFile = fileSys->CreateMemoryFile();
141+
142+ ArrayDatabase::db_construction_params cparams;
143+ cparams.item_block_size = 512;
144+ cparams.journal_size = 256;
145+ cparams.user_ident = 0x87654321;
146+
147+ ArrayDatabase::CreateDatabase( memFile, cparams );
148+
149+ ArrayDatabase db( execMan, std::move( memFile ) );
150+
151+ std::uint32_t tid = db.BeginTransaction( 0 );
152+
153+ db.WriteStruct( tid, 0, (int)42 );
154+
155+ db.AbortTransaction( tid );
156+
157+ assert( db.ReadStruct_EX <int> ( 0, 0 ) == 0 );
158+ }
159+ printf( "ok.\n" );
160+
161+ printf( "testing ArrayDatabase abort transaction (cascading)..." );
162+ {
163+ filePtr memFile = fileSys->CreateMemoryFile();
164+
165+ ArrayDatabase::db_construction_params cparams;
166+ cparams.item_block_size = 512;
167+ cparams.journal_size = 256;
168+ cparams.user_ident = 0x87654321;
169+
170+ ArrayDatabase::CreateDatabase( memFile, cparams );
171+
172+ ArrayDatabase db( execMan, std::move( memFile ) );
173+
174+ std::uint32_t tid1 = db.BeginTransaction( 0 );
175+ db.WriteStruct( tid1, 0, (int)33 );
176+
177+ std::uint32_t tid2 = db.BeginTransaction( 0 );
178+ db.WriteStruct( tid2, 4, (int)66 );
179+
180+ db.AbortTransaction( tid1 );
181+
182+ assert( db.ReadStruct_EX <int> ( 0, 0 ) == 0 );
183+ assert( db.ReadStruct_EX <int> ( 0, 4 ) == 0 );
184+ }
185+ printf( "ok.\n" );
186+
187+ printf( "testing ArrayDatabase recovery after sudden termination (simple)..." );
188+ {
189+ filePtr memFile = fileSys->CreateMemoryFile();
190+
191+ ArrayDatabase::db_construction_params cparams;
192+ cparams.item_block_size = 512;
193+ cparams.journal_size = 256;
194+ cparams.user_ident = 0x87654321;
195+
196+ ArrayDatabase::CreateDatabase( memFile, cparams );
197+
198+ // We first write something using a big transaction into the database.
199+ {
200+ ArrayDatabase write_db( execMan, filePtr( memFile, false ) );
201+
202+ // First we write something good into the db.
203+ std::uint32_t tid = write_db.BeginTransaction( 0 );
204+
205+ write_db.WriteStruct <std::uint32_t> ( tid, 0, 42 );
206+ write_db.WriteStruct <std::uint32_t> ( tid, 4, 84 );
207+
208+ write_db.EndTransaction( tid );
209+
210+ // Then we write something that has to be reverted.
211+ tid = write_db.BeginTransaction( 0 );
212+
213+ write_db.WriteStruct <std::uint32_t> ( tid, 2, 100 );
214+
215+ // Do not terminate the transaction on purpose.
216+ }
217+
218+ // Now try to load the database again.
219+ {
220+ ArrayDatabase read_db( execMan, std::move( memFile ) );
221+
222+ assert( read_db.ReadStruct_EX <std::uint32_t> ( 0, 0 ) == 42 );
223+ assert( read_db.ReadStruct_EX <std::uint32_t> ( 0, 4 ) == 84 );
224+ }
225+ }
226+ printf( "ok.\n" );
227+
228+ printf( "testing ArrayDatabase recovery after sudden termination (multi)..." );
229+ {
230+ filePtr memFile = fileSys->CreateMemoryFile();
231+
232+ ArrayDatabase::db_construction_params cparams;
233+ cparams.item_block_size = 512;
234+ cparams.journal_size = 300;
235+ cparams.user_ident = 0x87654321;
236+
237+ ArrayDatabase::CreateDatabase( memFile, cparams );
238+
239+ // We first create a pretty complicated intertwined serialization situation that the recovery must take care of.
240+ {
241+ ArrayDatabase write_db( execMan, filePtr( memFile, false ) );
242+
243+ std::uint32_t tid1 = write_db.BeginTransaction( 0 );
244+
245+ write_db.WriteStruct <std::uint32_t> ( tid1, 0, 0x12345678 );
246+
247+ std::uint32_t tid2 = write_db.BeginTransaction( 0 );
248+
249+ write_db.WriteStruct <std::uint32_t> ( tid2, 4, 0x87654321 );
250+
251+ write_db.EndTransaction( tid1 );
252+
253+ std::uint32_t tid3 = write_db.BeginTransaction( 0 );
254+
255+ write_db.WriteStruct <std::uint32_t> ( tid2, 0, 0 );
256+ write_db.WriteStruct <std::uint32_t> ( tid3, 0, 4 );
257+
258+ write_db.EndTransaction( tid3 );
259+
260+ // tid2 is left running.
261+ }
262+
263+ // All transactions must have been reverted because they are conflicting with tid2 which never ended.
264+ {
265+ ArrayDatabase read_db( execMan, filePtr( memFile, false ) );
266+
267+ assert( read_db.ReadStruct_EX <std::uint32_t> ( 0, 0 ) == 0 );
268+ assert( read_db.ReadStruct_EX <std::uint32_t> ( 0, 4 ) == 0 );
269+ }
270+ }
271+ printf( "ok.\n" );
272+
273+ printf( "testing ArrayDatabase parsing journal with aborted transaction..." );
274+ {
275+ filePtr memFile = fileSys->CreateMemoryFile();
276+
277+ ArrayDatabase::db_construction_params cparams;
278+ cparams.item_block_size = 512;
279+ cparams.journal_size = 200;
280+ cparams.user_ident = 0x87654321;
281+
282+ ArrayDatabase::CreateDatabase( memFile, cparams );
283+
284+ // Aborted transactions should be ignored by the runtime.
285+ {
286+ ArrayDatabase write_db( execMan, filePtr( memFile, false ) );
287+
288+ std::uint32_t tid = write_db.BeginTransaction( 0 );
289+
290+ write_db.WriteStruct <std::uint32_t> ( tid, 42, 21 );
291+
292+ write_db.AbortTransaction( tid );
293+
294+ std::uint32_t tid2 = write_db.BeginTransaction( 0 );
295+
296+ write_db.WriteStruct <std::uint32_t> ( tid2, 42, 1 );
297+
298+ write_db.EndTransaction( tid2 );
299+ }
300+
301+ // So now it should have stayed OK.
302+ {
303+ ArrayDatabase read_db( execMan, std::move( memFile ) );
304+
305+ assert( read_db.ReadStruct_EX <std::uint32_t> ( 0, 42 ) == 1 );
306+ }
307+ }
308+ printf( "ok.\n" );
309+
310+ // TODO: more tests.
311+}
\ No newline at end of file
--- bigunittest/src/main.cpp (revision 27)
+++ bigunittest/src/main.cpp (revision 28)
@@ -6,6 +6,7 @@
66 #include <libparse/syntaxutil.h>
77
88 extern void HASH_TESTS( void );
9+extern void ARRAYDB_TESTS( void );
910
1011 void SYNTAXUTIL_TEST( void )
1112 {
@@ -154,6 +155,7 @@
154155 {
155156 SYNTAXUTIL_TEST();
156157 HASH_TESTS();
158+ ARRAYDB_TESTS();
157159
158160 return 0;
159161 }
\ No newline at end of file
Show on old repository browser