126 GLDEF_C TInt Random(TInt aRange) |
126 GLDEF_C TInt Random(TInt aRange) |
127 { |
127 { |
128 return (Math::Random()>>11)%aRange; |
128 return (Math::Random()>>11)%aRange; |
129 } |
129 } |
130 |
130 |
131 #undef test |
131 /////////////////////////////////////////////////////////////////////////////////////// |
132 LOCAL_C void Test(TInt aValue,TInt anExpected,TInt aLine) |
132 /////////////////////////////////////////////////////////////////////////////////////// |
133 { |
133 |
134 if (aValue==anExpected) |
134 TPtrC FileName(const TText* aFile) |
135 return; |
135 { |
136 test.Printf(_L("** Expected %d, was %d\n"),anExpected,aValue); |
136 TPtrC p(aFile); |
137 test(EFalse,aLine); |
137 TInt ix=p.LocateReverse('\\'); |
138 } |
138 if (ix<0) |
139 #define test1(aTest) test(aTest,__LINE__) |
139 ix=p.LocateReverse('/'); |
140 #define test2(aValue,anExpected) Test(aValue,anExpected,__LINE__) |
140 if (ix>=0) |
|
141 p.Set(p.Mid(1+ix)); |
|
142 return p; |
|
143 } |
|
144 |
|
145 //Test macros and functions |
|
146 void Check1(TInt aValue, const TText* aFile, TInt aLine) |
|
147 { |
|
148 if(!aValue) |
|
149 { |
|
150 TPtrC fname(FileName(aFile)); |
|
151 TheTest.Printf(_L("*** Expression evaluated to false. %S-%d\r\n"), &fname, aLine); |
|
152 TheTest(EFalse, aLine); |
|
153 } |
|
154 } |
|
155 void Check2(TInt aValue, TInt aExpected, const TText* aFile, TInt aLine) |
|
156 { |
|
157 if(aValue != aExpected) |
|
158 { |
|
159 TPtrC fname(FileName(aFile)); |
|
160 TheTest.Printf(_L("*** Expected %d, got %d. %S-%d\r\n"), aExpected, aValue, &fname, aLine); |
|
161 TheTest(EFalse, aLine); |
|
162 } |
|
163 } |
|
164 |
|
165 /////////////////////////////////////////////////////////////////////////////////////// |
141 |
166 |
142 LOCAL_C void CreateIndexL(RDbDatabase& aDatabase,const TDesC& aTable,const TDesC& aColumn,TBool aUnique) |
167 LOCAL_C void CreateIndexL(RDbDatabase& aDatabase,const TDesC& aTable,const TDesC& aColumn,TBool aUnique) |
143 { |
168 { |
144 CDbKey* key=CDbKey::NewLC(); |
169 CDbKey* key=CDbKey::NewLC(); |
145 key->AddL(aColumn); |
170 key->AddL(aColumn); |
146 if (aUnique) |
171 if (aUnique) |
147 key->MakeUnique(); |
172 key->MakeUnique(); |
148 test2(aDatabase.CreateIndex(aColumn,aTable,*key),KErrNone); |
173 TEST2(aDatabase.CreateIndex(aColumn,aTable,*key),KErrNone); |
149 CleanupStack::PopAndDestroy(); |
174 CleanupStack::PopAndDestroy(); |
150 } |
175 } |
151 |
176 |
152 // |
177 // |
153 // Create the database |
178 // Create the database |
158 store->SetTypeL(KPermanentFileStoreLayoutUid); |
183 store->SetTypeL(KPermanentFileStoreLayoutUid); |
159 store->SetRootL(TheDatabase.CreateL(store)); |
184 store->SetRootL(TheDatabase.CreateL(store)); |
160 // create the tables |
185 // create the tables |
161 TheDatabase.Begin(); |
186 TheDatabase.Begin(); |
162 CDbColSet* set=Set::CreateL(AccountsDef); |
187 CDbColSet* set=Set::CreateL(AccountsDef); |
163 test2(TheDatabase.CreateTable(KAccounts,*set),KErrNone); |
188 TEST2(TheDatabase.CreateTable(KAccounts,*set),KErrNone); |
164 delete set; |
189 delete set; |
165 CreateIndexL(TheDatabase,KAccounts,KAccountsID,ETrue); |
190 CreateIndexL(TheDatabase,KAccounts,KAccountsID,ETrue); |
166 CreateIndexL(TheDatabase,KAccounts,KAccountsBalance,EFalse); |
191 CreateIndexL(TheDatabase,KAccounts,KAccountsBalance,EFalse); |
167 set=Set::CreateL(TransactionsDef); |
192 set=Set::CreateL(TransactionsDef); |
168 test2(TheDatabase.CreateTable(KTransactions,*set),KErrNone); |
193 TEST2(TheDatabase.CreateTable(KTransactions,*set),KErrNone); |
169 delete set; |
194 delete set; |
170 CreateIndexL(TheDatabase,KTransactions,KTransactionDate,EFalse); |
195 CreateIndexL(TheDatabase,KTransactions,KTransactionDate,EFalse); |
171 test2(TheDatabase.Commit(),KErrNone); |
196 TEST2(TheDatabase.Commit(),KErrNone); |
172 OldCount=NewCount=0; |
197 OldCount=NewCount=0; |
173 // prepare Accs table |
198 // prepare Accs table |
174 TheDatabase.Begin(); |
199 TheDatabase.Begin(); |
175 test2(Accs.Prepare(TheDatabase,_L("select * from accounts"),Accs.EInsertOnly),KErrNone); |
200 TEST2(Accs.Prepare(TheDatabase,_L("select * from accounts"),Accs.EInsertOnly),KErrNone); |
176 Accs.InsertL(); |
201 Accs.InsertL(); |
177 Accs.SetColL(1,TInt32(ECash)); |
202 Accs.SetColL(1,TInt32(ECash)); |
178 Accs.SetColL(2,KInitialCash); |
203 Accs.SetColL(2,KInitialCash); |
179 Accs.SetColL(3,KInitialCash); |
204 Accs.SetColL(3,KInitialCash); |
180 Accs.PutL(); |
205 Accs.PutL(); |
205 CleanupClosePushL(match); |
230 CleanupClosePushL(match); |
206 for (TInt id=ECash;id<=EPenny;++id) |
231 for (TInt id=ECash;id<=EPenny;++id) |
207 { |
232 { |
208 Buf.Format(_L("id=%d"),id); |
233 Buf.Format(_L("id=%d"),id); |
209 Accs.FirstL(); |
234 Accs.FirstL(); |
210 test1(Accs.FindL(Accs.EForwards,Buf)>=0); |
235 TEST(Accs.FindL(Accs.EForwards,Buf)>=0); |
211 Accs.GetL(); |
236 Accs.GetL(); |
212 TInt balance=Accs.ColInt(2); |
237 TInt balance=Accs.ColInt(2); |
213 Buf.Format(_L("\nStatement for account %d: Previous balance %d\n"),id,balance); |
238 Buf.Format(_L("\nStatement for account %d: Previous balance %d\n"),id,balance); |
214 User::LeaveIfError(file.Write(Buf)); |
239 User::LeaveIfError(file.Write(Buf)); |
215 Buf.Format(_L("from_id=%d or to_id=%d"),id,id); |
240 Buf.Format(_L("from_id=%d or to_id=%d"),id,id); |
233 User::LeaveIfError(file.Write(Buf)); |
258 User::LeaveIfError(file.Write(Buf)); |
234 Buf.Format(_L("Account balance %d\n"),Accs.ColInt(3)); |
259 Buf.Format(_L("Account balance %d\n"),Accs.ColInt(3)); |
235 User::LeaveIfError(file.Write(Buf)); |
260 User::LeaveIfError(file.Write(Buf)); |
236 } |
261 } |
237 CleanupStack::PopAndDestroy(2); |
262 CleanupStack::PopAndDestroy(2); |
238 test1(0); |
263 TEST(0); |
239 } |
264 } |
240 #endif |
265 #endif |
241 |
266 |
242 // |
267 // |
243 // Check that the database structure is fully intact |
268 // Check that the database structure is fully intact |
244 // |
269 // |
245 LOCAL_C void VerifyDatabaseL(CPersistentStore& aStore) |
270 LOCAL_C void VerifyDatabaseL(CPersistentStore& aStore) |
246 { |
271 { |
247 TheDatabase.OpenL(&aStore,aStore.Root()); |
272 TheDatabase.OpenL(&aStore,aStore.Root()); |
248 // check any indexes |
273 // check any indexes |
249 test2(TheTable.Open(TheDatabase,KAccounts,TheTable.EReadOnly),KErrNone); |
274 TEST2(TheTable.Open(TheDatabase,KAccounts,TheTable.EReadOnly),KErrNone); |
250 test2(TheTable.CountL(),KAccountIDs); |
275 TEST2(TheTable.CountL(),KAccountIDs); |
251 TInt r=TheTable.SetIndex(KAccountsID); |
276 TInt r=TheTable.SetIndex(KAccountsID); |
252 if (r!=KErrCorrupt) |
277 if (r!=KErrCorrupt) |
253 { |
278 { |
254 test2(r,KErrNone); |
279 TEST2(r,KErrNone); |
255 test2(TheTable.CountL(),KAccountIDs); |
280 TEST2(TheTable.CountL(),KAccountIDs); |
256 for (TInt id=ECash;id<=EPenny;++id) |
281 for (TInt id=ECash;id<=EPenny;++id) |
257 { |
282 { |
258 test1(TheTable.NextL()); |
283 TEST(TheTable.NextL()); |
259 TheTable.GetL(); |
284 TheTable.GetL(); |
260 test2(TheTable.ColInt(1),id); |
285 TEST2(TheTable.ColInt(1),id); |
261 } |
286 } |
262 test1(!TheTable.NextL()); |
287 TEST(!TheTable.NextL()); |
263 } |
288 } |
264 r=TheTable.SetIndex(KAccountsBalance); |
289 r=TheTable.SetIndex(KAccountsBalance); |
265 if (r!=KErrCorrupt) |
290 if (r!=KErrCorrupt) |
266 { |
291 { |
267 test2(r,KErrNone); |
292 TEST2(r,KErrNone); |
268 test2(TheTable.CountL(),KAccountIDs); |
293 TEST2(TheTable.CountL(),KAccountIDs); |
269 test1(TheTable.FirstL()); |
294 TEST(TheTable.FirstL()); |
270 TheTable.GetL(); |
295 TheTable.GetL(); |
271 TInt last=TheTable.ColInt(2); |
296 TInt last=TheTable.ColInt(2); |
272 for (TInt ii=1;ii<KAccountIDs;++ii) |
297 for (TInt ii=1;ii<KAccountIDs;++ii) |
273 { |
298 { |
274 test1(TheTable.NextL()); |
299 TEST(TheTable.NextL()); |
275 TheTable.GetL(); |
300 TheTable.GetL(); |
276 TInt bal=TheTable.ColInt(2); |
301 TInt bal=TheTable.ColInt(2); |
277 test1(bal>=last); |
302 TEST(bal>=last); |
278 last=bal; |
303 last=bal; |
279 } |
304 } |
280 test1(!TheTable.NextL()); |
305 TEST(!TheTable.NextL()); |
281 } |
306 } |
282 TheTable.Close(); |
307 TheTable.Close(); |
283 test2(TheTable.Open(TheDatabase,KTransactions,TheTable.EReadOnly),KErrNone); |
308 TEST2(TheTable.Open(TheDatabase,KTransactions,TheTable.EReadOnly),KErrNone); |
284 TInt count=TheTable.CountL(); |
309 TInt count=TheTable.CountL(); |
285 r=TheTable.SetIndex(KTransactionDate); |
310 r=TheTable.SetIndex(KTransactionDate); |
286 if (r!=KErrCorrupt) |
311 if (r!=KErrCorrupt) |
287 { |
312 { |
288 test2(r,KErrNone); |
313 TEST2(r,KErrNone); |
289 test2(TheTable.CountL(),count); |
314 TEST2(TheTable.CountL(),count); |
290 if (count) |
315 if (count) |
291 { |
316 { |
292 test1(TheTable.FirstL()); |
317 TEST(TheTable.FirstL()); |
293 TheTable.GetL(); |
318 TheTable.GetL(); |
294 TInt last=TheTable.ColInt(1); |
319 TInt last=TheTable.ColInt(1); |
295 while (--count!=0) |
320 while (--count!=0) |
296 { |
321 { |
297 test1(TheTable.NextL()); |
322 TEST(TheTable.NextL()); |
298 TheTable.GetL(); |
323 TheTable.GetL(); |
299 TInt date=TheTable.ColInt(1); |
324 TInt date=TheTable.ColInt(1); |
300 test1(date>last); |
325 TEST(date>last); |
301 last=date; |
326 last=date; |
302 } |
327 } |
303 test1(!TheTable.NextL()); |
328 TEST(!TheTable.NextL()); |
304 } |
329 } |
305 else |
330 else |
306 test1(!TheTable.FirstL()); |
331 TEST(!TheTable.FirstL()); |
307 } |
332 } |
308 TheTable.Close(); |
333 TheTable.Close(); |
309 // verify database integrity |
334 // verify database integrity |
310 TInt balance[KAccountIDs]; |
335 TInt balance[KAccountIDs]; |
311 test2(Accs.Prepare(TheDatabase,_L("select id,statement_balance,balance from accounts"),Accs.EReadOnly),KErrNone); |
336 TEST2(Accs.Prepare(TheDatabase,_L("select id,statement_balance,balance from accounts"),Accs.EReadOnly),KErrNone); |
312 test2(Accs.CountL(),KAccountIDs); |
337 TEST2(Accs.CountL(),KAccountIDs); |
313 while (Accs.NextL()) |
338 while (Accs.NextL()) |
314 { |
339 { |
315 Accs.GetL(); |
340 Accs.GetL(); |
316 TInt id=Accs.ColInt(1); |
341 TInt id=Accs.ColInt(1); |
317 balance[id]=Accs.ColInt(2); |
342 balance[id]=Accs.ColInt(2); |
318 } |
343 } |
319 test2(Trans.Prepare(TheDatabase,_L("select t_date,from_id,to_id,amount from Transactions"),Trans.EReadOnly),KErrNone); |
344 TEST2(Trans.Prepare(TheDatabase,_L("select t_date,from_id,to_id,amount from Transactions"),Trans.EReadOnly),KErrNone); |
320 TInt transact=0; |
345 TInt transact=0; |
321 while (Trans.NextL()) |
346 while (Trans.NextL()) |
322 { |
347 { |
323 ++transact; |
348 ++transact; |
324 Trans.GetL(); |
349 Trans.GetL(); |
418 TTime timeEnd(timeCurrent); |
443 TTime timeEnd(timeCurrent); |
419 timeEnd += timeInterval; |
444 timeEnd += timeInterval; |
420 |
445 |
421 for (TBool condition=ETrue; condition; condition = aTestExecutionTime > 0 ? (timeCurrent < timeEnd) : ETrue) |
446 for (TBool condition=ETrue; condition; condition = aTestExecutionTime > 0 ? (timeCurrent < timeEnd) : ETrue) |
422 { |
447 { |
423 test.Next(_L("Main loop")); |
448 TheTest.Next(_L("Main loop")); |
424 test.Start(_L("Kick off the thread")); |
449 TheTest.Start(_L("Kick off the thread")); |
425 test2 (StartThread(TheThread,TheStatus),KErrNone); |
450 TEST2 (StartThread(TheThread,TheStatus),KErrNone); |
426 // random delay |
451 // random delay |
427 for (;;) |
452 for (;;) |
428 { |
453 { |
429 User::After(95000); |
454 User::After(95000); |
430 if (TheStatus!=KRequestPending) |
455 if (TheStatus!=KRequestPending) |
431 break; |
456 break; |
432 if (Random(1000)<30) |
457 if (Random(1000)<30) |
433 break; |
458 break; |
434 } |
459 } |
435 test.Next(_L("End the thread")); |
460 TheTest.Next(_L("End the thread")); |
436 TInt exit=EndThread(); |
461 TInt exit=EndThread(); |
437 if (exit!=1) |
462 if (exit!=1) |
438 test.Printf(_L(" thread failed with error %d\n"),exit); |
463 TheTest.Printf(_L(" thread failed with error %d\n"),exit); |
439 // |
464 // |
440 ++Shot; |
465 ++Shot; |
441 CFileStore* store=NULL; |
466 CFileStore* store=NULL; |
442 for (TInt ii=0;;++ii) |
467 for (TInt ii=0;;++ii) |
443 { |
468 { |
444 test.Printf(_L("Opening %d\r"),ii); |
469 TheTest.Printf(_L("Opening %d\r"),ii); |
445 TRAPD(r,store=CFileStore::OpenL(TheFs,KTestDatabase,EFileRead|EFileWrite)); |
470 TRAPD(r,store=CFileStore::OpenL(TheFs,KTestDatabase,EFileRead|EFileWrite)); |
446 if (r==KErrNone) |
471 if (r==KErrNone) |
447 break; |
472 break; |
448 test (r==KErrInUse); |
473 TEST2(r, KErrInUse); |
449 User::After(100000); |
474 User::After(100000); |
450 } |
475 } |
451 test.Next(_L("Verify & Recover")); |
476 TheTest.Next(_L("Verify & Recover")); |
452 test2 (Verify(*store),KErrNone); |
477 TEST2 (Verify(*store),KErrNone); |
453 TInt64 tps(TransId); |
478 TInt64 tps(TransId); |
454 tps*=1000u; |
479 tps*=1000u; |
455 tps/=RunningTime; |
480 tps/=RunningTime; |
456 test.Printf(_L(" Iteration %d, TPS %d, during commit %d%%\n"),Shot,I64LOW(tps),(100*ShotDuringCommit)/Shot); |
481 TheTest.Printf(_L(" Iteration %d, TPS %d, during commit %d%%\n"),Shot,I64LOW(tps),(100*ShotDuringCommit)/Shot); |
457 TInt r=Recover(*store); |
482 TInt r=Recover(*store); |
458 if (r==KErrNoMemory || r==KErrDiskFull) |
483 if (r==KErrNoMemory || r==KErrDiskFull) |
459 { // need to compact before completing recovery |
484 { // need to compact before completing recovery |
460 test.Next(_L("No space, compacting")); |
485 TheTest.Next(_L("No space, compacting")); |
461 test2 (Compact(*store),KErrNone); |
486 TEST2 (Compact(*store),KErrNone); |
462 test.Next(_L("Verify & Recover again")); |
487 TheTest.Next(_L("Verify & Recover again")); |
463 test2 (Verify(*store),KErrNone); |
488 TEST2 (Verify(*store),KErrNone); |
464 r=Recover(*store); |
489 r=Recover(*store); |
465 } |
490 } |
466 test2 (r,KErrNone); |
491 TEST2 (r,KErrNone); |
467 test.Next(_L("Verify & Compact")); |
492 TheTest.Next(_L("Verify & Compact")); |
468 // test2 (Verify(*store),KErrNone); |
493 // TEST2 (Verify(*store),KErrNone); |
469 test2 (Compact(*store),KErrNone); |
494 TEST2 (Compact(*store),KErrNone); |
470 test.Next(_L("Verify")); |
495 TheTest.Next(_L("Verify")); |
471 test2 (Verify(*store),KErrNone); |
496 TEST2 (Verify(*store),KErrNone); |
472 // |
497 // |
473 delete store; |
498 delete store; |
474 test.End(); |
499 TheTest.End(); |
475 |
500 |
476 timeCurrent.UniversalTime(); |
501 timeCurrent.UniversalTime(); |
477 } |
502 } |
478 test.End(); |
503 TheTest.End(); |
479 } |
504 } |
480 |
505 |
481 /** |
506 /** |
482 @SYMTestCaseID SYSLIB-DBMS-CT-0636 |
507 @SYMTestCaseID SYSLIB-DBMS-CT-0636 |
483 @SYMTestCaseDesc DBMS stess testing. |
508 @SYMTestCaseDesc DBMS stess testing. |
486 @SYMTestExpectedResults Test must not fail |
511 @SYMTestExpectedResults Test must not fail |
487 @SYMREQ REQ0000 |
512 @SYMREQ REQ0000 |
488 */ |
513 */ |
489 static void RunVerify() |
514 static void RunVerify() |
490 { |
515 { |
491 test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0636 Open store ")); |
516 TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0636 Open store ")); |
492 CFileStore* store=NULL; |
517 CFileStore* store=NULL; |
493 TRAPD(r,store=CFileStore::OpenL(TheFs,KTestDatabase,EFileRead|EFileWrite)); |
518 TRAPD(r,store=CFileStore::OpenL(TheFs,KTestDatabase,EFileRead|EFileWrite)); |
494 test2 (r,KErrNone); |
519 TEST2 (r,KErrNone); |
495 test.Next(_L("Verify")); |
520 TheTest.Next(_L("Verify")); |
496 NewCount=-1; |
521 NewCount=-1; |
497 TotalMonies=KInitialCash + (EPenny-EJohn+1)*KInitialBalance; |
522 TotalMonies=KInitialCash + (EPenny-EJohn+1)*KInitialBalance; |
498 test2 (Verify(*store),KErrNone); |
523 TEST2 (Verify(*store),KErrNone); |
499 test.Next(_L("Recover")); |
524 TheTest.Next(_L("Recover")); |
500 test2 (Recover(*store),KErrNone); |
525 TEST2 (Recover(*store),KErrNone); |
501 test.Next(_L("Verify")); |
526 TheTest.Next(_L("Verify")); |
502 test2 (Verify(*store),KErrNone); |
527 TEST2 (Verify(*store),KErrNone); |
503 delete store; |
528 delete store; |
504 test.End(); |
529 TheTest.End(); |
505 } |
530 } |
506 |
531 |
507 // |
532 // |
508 // Prepare the test directory. |
533 // Prepare the test directory. |
509 // |
534 // |
510 LOCAL_C void setupTestDirectory() |
535 LOCAL_C void setupTestDirectory() |
511 { |
536 { |
512 TInt r=TheFs.Connect(); |
537 TInt r=TheFs.Connect(); |
513 test2(r,KErrNone); |
538 TEST2(r,KErrNone); |
514 // |
539 // |
515 r=TheFs.MkDir(KTestDir); |
540 r=TheFs.MkDir(KTestDir); |
516 test1(r==KErrNone || r==KErrAlreadyExists); |
541 TEST(r==KErrNone || r==KErrAlreadyExists); |
517 r=TheFs.SetSessionPath(KTestDir); |
542 r=TheFs.SetSessionPath(KTestDir); |
518 test2(r,KErrNone); |
543 TEST2(r,KErrNone); |
519 } |
544 } |
520 |
545 |
|
546 // |
|
547 // Initialise the cleanup stack. |
|
548 // |
521 LOCAL_C CTrapCleanup* setupCleanup() |
549 LOCAL_C CTrapCleanup* setupCleanup() |
522 // |
|
523 // Initialise the cleanup stack. |
|
524 // |
|
525 { |
550 { |
526 CTrapCleanup* cleanup=CTrapCleanup::New(); |
551 CTrapCleanup* cleanup=CTrapCleanup::New(); |
527 test1(cleanup!=NULL); |
552 TEST(cleanup!=NULL); |
528 TRAPD(r,\ |
553 TRAPD(r,\ |
529 {\ |
554 {\ |
530 for (TInt i=KTestCleanupStack;i>0;i--)\ |
555 for (TInt i=KTestCleanupStack;i>0;i--)\ |
531 CleanupStack::PushL((TAny*)0);\ |
556 CleanupStack::PushL((TAny*)0);\ |
532 CleanupStack::Pop(KTestCleanupStack);\ |
557 CleanupStack::Pop(KTestCleanupStack);\ |
533 }); |
558 }); |
534 test2(r,KErrNone); |
559 TEST2(r,KErrNone); |
535 return cleanup; |
560 return cleanup; |
536 } |
561 } |
537 |
562 |
538 // |
563 // |
539 // entry point |
564 // entry point |