sl@0: // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: RTest TheTest(_L("t_sqlitedb64 test")); sl@0: sl@0: _LIT(KTestDbName, "\\test\\t_sqlitedb64.db"); sl@0: sl@0: RFs TheFs; sl@0: sl@0: sqlite3* TheDb = 0; sl@0: sqlite3_stmt* TheStmt = 0; sl@0: sl@0: const TInt64 K1Mb = 1024LL * 1024LL; sl@0: const TInt64 K1Gb = 1024LL * K1Mb; sl@0: const TInt64 K4Gb = 4LL * K1Gb; sl@0: sl@0: TInt64 TheLastInsertedRowid = -1LL; sl@0: sl@0: struct TTestDriveInfo sl@0: { sl@0: TInt iSizeMb; sl@0: TBool iWritable; sl@0: }; sl@0: sl@0: TTestDriveInfo TheDriveInfo[KMaxDrives]; sl@0: TInt TheBiggestDriveNo = -1; sl@0: TFileName TheDbName; sl@0: char TheDbName8[KMaxFileName]; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: static void DeleteTestFiles() sl@0: { sl@0: if(TheStmt) sl@0: { sl@0: sqlite3_finalize(TheStmt); sl@0: TheStmt = 0; sl@0: } sl@0: if(TheDb) sl@0: { sl@0: (void)sqlite3_close(TheDb); sl@0: TheDb = 0; sl@0: } sl@0: (void)TheFs.Delete(TheDbName); sl@0: } sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: static void PrintSqliteErrMsg() sl@0: { sl@0: if(TheDb) sl@0: { sl@0: const char* msg = sqlite3_errmsg(TheDb); sl@0: TBuf<200> buf; sl@0: buf.Copy(TPtrC8((const TUint8*)msg)); sl@0: TheTest.Printf(_L("*** SQLite error message: \"%S\"\r\n"), &buf); sl@0: } sl@0: } sl@0: sl@0: //Test macros and functions sl@0: static void Check(TInt aValue, TInt aLine) sl@0: { sl@0: if(!aValue) sl@0: { sl@0: DeleteTestFiles(); sl@0: PrintSqliteErrMsg(); sl@0: TheTest(EFalse, aLine); sl@0: } sl@0: } sl@0: static void Check(TInt aValue, TInt aExpected, TInt aLine) sl@0: { sl@0: if(aValue != aExpected) sl@0: { sl@0: DeleteTestFiles(); sl@0: RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue); sl@0: PrintSqliteErrMsg(); sl@0: TheTest(EFalse, aLine); sl@0: } sl@0: } sl@0: #define TEST(arg) ::Check((arg), __LINE__) sl@0: #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__) sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: void SqlTimerPrint(const TDesC& aText, TUint32 aStartTicks, TUint32 aEndTicks) sl@0: { sl@0: static TInt freq = 0; sl@0: if(freq == 0) sl@0: { sl@0: TEST2(HAL::Get(HAL::EFastCounterFrequency, freq), KErrNone); sl@0: } sl@0: TInt64 diffTicks = (TInt64)aEndTicks - (TInt64)aStartTicks; sl@0: if(diffTicks < 0) sl@0: { sl@0: diffTicks = KMaxTUint32 + diffTicks + 1; sl@0: } sl@0: const TInt KMicroSecIn1Sec = 1000000; sl@0: TInt32 us = (diffTicks * KMicroSecIn1Sec) / freq; sl@0: TheTest.Printf(_L("#### %S. Execution time: %d us\r\n"), &aText, us); sl@0: } sl@0: sl@0: TUint32 SqlTimerTicks() sl@0: { sl@0: return User::FastCounter(); sl@0: } sl@0: sl@0: void CollectDriveInfo() sl@0: { sl@0: TheTest.Printf(_L("==================\r\n")); sl@0: _LIT(KType1, "Not present"); sl@0: _LIT(KType2, "Unknown"); sl@0: _LIT(KType3, "Floppy"); sl@0: _LIT(KType4, "Hard disk"); sl@0: _LIT(KType5, "CD ROM"); sl@0: _LIT(KType6, "RAM disk"); sl@0: _LIT(KType7, "Flash"); sl@0: _LIT(KType8, "ROM drive"); sl@0: _LIT(KType9, "Remote drive"); sl@0: _LIT(KType10,"NAND flash"); sl@0: _LIT(KType11,"Rotating media"); sl@0: sl@0: Mem::FillZ(TheDriveInfo, sizeof(TheDriveInfo)); sl@0: TheBiggestDriveNo = 0; sl@0: sl@0: for(TInt drive=EDriveA;drive<=EDriveZ;++drive) sl@0: { sl@0: TDriveInfo driveInfo; sl@0: TInt err = TheFs.Drive(driveInfo, drive); sl@0: if(err == KErrNone) sl@0: { sl@0: TVolumeInfo vinfo; sl@0: err = TheFs.Volume(vinfo, drive); sl@0: if(err == KErrNone) sl@0: { sl@0: TVolumeIOParamInfo vparam; sl@0: err = TheFs.VolumeIOParam(drive, vparam); sl@0: TEST2(err, KErrNone); sl@0: TBuf8<128> vinfoex8; sl@0: err = TheFs.QueryVolumeInfoExt(drive, EFileSystemSubType, vinfoex8); sl@0: TEST2(err, KErrNone); sl@0: TPtrC vinfoex((const TUint16*)(vinfoex8.Ptr() + 8), vinfoex8[0]); sl@0: TPtrC KMediaTypeNames[] = {KType1(), KType2(), KType3(), KType4(), KType5(), KType6(), KType7(), KType8(), KType9(), KType10(), KType11()}; sl@0: TInt sizeMb = vinfo.iSize / K1Mb; sl@0: TheTest.Printf(_L("Drive: %C:, Type: %16.16S, File System: %8.8S, Size: %d Mb.\r\n"), 'A' + drive, &KMediaTypeNames[driveInfo.iType], &vinfoex, sizeMb); sl@0: TheTest.Printf(_L(" Block size=%d, Cluster size=%d, Read buffer size=%d.\r\n"), vparam.iBlockSize, vparam.iClusterSize, vparam.iRecReadBufSize); sl@0: TheDriveInfo[drive].iSizeMb = sizeMb; sl@0: if(driveInfo.iType == EMediaRam || driveInfo.iType == EMediaHardDisk || driveInfo.iType == EMediaFlash || driveInfo.iType == EMediaNANDFlash) sl@0: { sl@0: TheDriveInfo[drive].iWritable = ETrue; sl@0: if(sizeMb > TheDriveInfo[TheBiggestDriveNo].iSizeMb) sl@0: { sl@0: TheBiggestDriveNo = drive; sl@0: } sl@0: } sl@0: } sl@0: else sl@0: { sl@0: TheTest.Printf(_L("Drive %C. RFs::Volume() has failed with err=%d.\r\n"), 'A' + drive, err); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: TheTest.Printf(_L("Drive %C. RFs::Drive() has failed with err=%d.\r\n"), 'A' + drive, err); sl@0: } sl@0: } sl@0: sl@0: TheTest.Printf(_L("The biggest R/W drive is: %C, Size: %d Mb\r\n"), 'A' + TheBiggestDriveNo, TheDriveInfo[TheBiggestDriveNo].iSizeMb); sl@0: TDriveUnit drvUnit(TheBiggestDriveNo); sl@0: TDriveName drvName = drvUnit.Name(); sl@0: TParse parse; sl@0: parse.Set(KTestDbName, &drvName, NULL); sl@0: TheDbName.Copy(parse.FullName()); sl@0: TPtr8 p((TUint8*)TheDbName8, 0, KMaxFileName); sl@0: p.Copy(TheDbName); sl@0: p.Append(TChar(0)); sl@0: sl@0: TRAPD(err, BaflUtils::EnsurePathExistsL(TheFs, TheDbName)); sl@0: TEST(err == KErrNone || err == KErrAlreadyExists); sl@0: sl@0: TheTest.Printf(_L("==================\r\n")); sl@0: } sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: /** sl@0: @SYMTestCaseID PDS-SQLITE3-CT-4041 sl@0: @SYMTestCaseDesc Creation of a database bigger than 4Gb (KMaxTUint). sl@0: The test creates a test database with a table and inserts records into the table sl@0: until the database size gets bigger than 4Gb (KMaxTUint). The purpose of the test is to verify sl@0: that it is possible to create and manipulate 64-bit SQLite databases. sl@0: @SYMTestActions Creation of a database bigger than 4Gb (KMaxTUint). sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMTestPriority High sl@0: @SYMREQ REQ12107 sl@0: REQ12108 sl@0: */ sl@0: void CreateBigDbTest(TInt64 aDbSize) sl@0: { sl@0: __ASSERT_ALWAYS(aDbSize > 0LL, User::Invariant()); sl@0: (void)TheFs.Delete(TheDbName); sl@0: sl@0: const char* ver = sqlite3_libversion(); sl@0: TBuf<20> buf; sl@0: buf.Copy(TPtrC8((const TUint8*)ver)); sl@0: TheTest.Printf(_L("*** SQLite library version: \"%S\"\r\n"), &buf); sl@0: sl@0: TInt err = sqlite3_open(TheDbName8, &TheDb); sl@0: TEST2(err, SQLITE_OK); sl@0: sl@0: // sl@0: err = sqlite3_exec(TheDb, "CREATE TABLE A(Id INTEGER PRIMARY KEY AUTOINCREMENT, Data BLOB)", 0, 0, 0); sl@0: TEST2(err, SQLITE_OK); sl@0: TInt64 fsize = 0; sl@0: TheTest.Printf(_L("==File size:")); sl@0: while(fsize < aDbSize) sl@0: { sl@0: const TInt KRecCnt = 1000; sl@0: //Insert KRecCnt records in a transaction sl@0: err = sqlite3_exec(TheDb, "BEGIN", 0, 0, 0); sl@0: if(err != SQLITE_OK) sl@0: { sl@0: TheTest.Printf(_L("==='BEGIN' has failed with err %d\r\n"), err); sl@0: } sl@0: TEST2(err, SQLITE_OK); sl@0: err = sqlite3_prepare(TheDb, "INSERT INTO A(Data) VALUES(zeroblob(32768))", -1, &TheStmt, 0);//32Kb big blob sl@0: TEST2(err, SQLITE_OK); sl@0: for(TInt i=0;i 0LL); sl@0: //Check and print the file size sl@0: sqlite3_close(TheDb); sl@0: TheDb = 0; sl@0: RFile64 file; sl@0: err = file.Open(TheFs, TheDbName, EFileRead | EFileWrite); sl@0: TEST2(err, KErrNone); sl@0: err = file.Size(fsize); sl@0: TEST2(err, KErrNone); sl@0: file.Close(); sl@0: TheTest.Printf(_L(" %ldMb"), fsize / K1Mb); sl@0: err = sqlite3_open(TheDbName8, &TheDb); sl@0: TEST2(err, SQLITE_OK); sl@0: } sl@0: TheTest.Printf(_L("\r\n")); sl@0: // sl@0: sqlite3_close(TheDb); sl@0: TheDb = 0; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID PDS-SQLITE3-CT-4042 sl@0: @SYMTestCaseDesc SQLite operations on a 64-bit database. sl@0: The test uses the database created in test case PDS-SQLITE3-UT-4041. sl@0: Simple INSERT, UPDATE, DELETE and SELECT statements are executed on the database. sl@0: The data in the test SQL statements is such that the manipulated records are beyond the 4Gb sl@0: file offset. Some other of the test SQL statements will perform sequential scan of the whole sl@0: database from offset 0 to the end of the database file. sl@0: The purpose of the test is to verify that there are no problem if the database offset is 64-bit. sl@0: @SYMTestActions SQLite operations on a 64-bit database. sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMTestPriority High sl@0: @SYMREQ REQ12107 sl@0: REQ12108 sl@0: */ sl@0: void SimpleDbOperationsTest() sl@0: { sl@0: __ASSERT_ALWAYS(TheLastInsertedRowid > 0LL, User::Invariant()); sl@0: TInt err = sqlite3_open(TheDbName8, &TheDb); sl@0: TEST2(err, SQLITE_OK); sl@0: sl@0: //SELECT-1 sl@0: TUint32 start = SqlTimerTicks(); sl@0: err = sqlite3_prepare(TheDb, "SELECT Id FROM A WHERE ROWID = :Prm", -1, &TheStmt, 0); sl@0: TEST2(err, SQLITE_OK); sl@0: err = sqlite3_bind_int64(TheStmt, 1, TheLastInsertedRowid - 1LL); sl@0: TEST2(err, SQLITE_OK); sl@0: err = sqlite3_step(TheStmt); sl@0: TEST2(err, SQLITE_ROW); sl@0: TInt64 id = sqlite3_column_int64(TheStmt, 0); sl@0: TheTest.Printf(_L("==Id=%ld\r\n"), id); sl@0: sqlite3_finalize(TheStmt); sl@0: TheStmt = 0; sl@0: TUint32 end = SqlTimerTicks(); sl@0: SqlTimerPrint(_L("SELECT-1"), start, end); sl@0: sl@0: //INSERT sl@0: start = SqlTimerTicks(); sl@0: err = sqlite3_exec(TheDb, "INSERT INTO A(Data) VALUES('123456')", 0, 0, 0); sl@0: TEST2(err, SQLITE_OK); sl@0: end = SqlTimerTicks(); sl@0: TInt cnt = sqlite3_changes(TheDb); sl@0: TEST2(cnt, 1); sl@0: SqlTimerPrint(_L("INSERT"), start, end); sl@0: sl@0: //UPDATE sl@0: start = SqlTimerTicks(); sl@0: TBuf<100> sql; sl@0: sql.Format(_L("UPDATE A SET Data='56789' WHERE Id=%ld"), id); sl@0: TBuf8<100> sql8; sl@0: sql8.Copy(sql); sl@0: err = sqlite3_exec(TheDb, (const char*)sql8.PtrZ(), 0, 0, 0); sl@0: TEST2(err, SQLITE_OK); sl@0: end = SqlTimerTicks(); sl@0: cnt = sqlite3_changes(TheDb); sl@0: TEST2(cnt, 1); sl@0: SqlTimerPrint(_L("UPDATE"), start, end); sl@0: sl@0: //SELECT-2 sl@0: start = SqlTimerTicks(); sl@0: sql.Format(_L("SELECT Data FROM A WHERE ID = %ld"), id); sl@0: sql8.Copy(sql); sl@0: err = sqlite3_prepare(TheDb, (const char*)sql8.PtrZ(), -1, &TheStmt, 0); sl@0: TEST2(err, SQLITE_OK); sl@0: err = sqlite3_step(TheStmt); sl@0: TEST2(err, SQLITE_ROW); sl@0: const char* data = (const char*)sqlite3_column_text(TheStmt, 0); sl@0: TEST(data != 0); sl@0: err = strcmp(data, "56789"); sl@0: TEST2(err, 0); sl@0: sqlite3_finalize(TheStmt); sl@0: TheStmt = 0; sl@0: end = SqlTimerTicks(); sl@0: SqlTimerPrint(_L("SELECT-2"), start, end); sl@0: sl@0: //SELECT-3 sl@0: start = SqlTimerTicks(); sl@0: err = sqlite3_prepare(TheDb, "SELECT COUNT(*) FROM A", -1, &TheStmt, 0); sl@0: TEST2(err, SQLITE_OK); sl@0: err = sqlite3_step(TheStmt); sl@0: TEST2(err, SQLITE_ROW); sl@0: TInt recCnt = sqlite3_column_int(TheStmt, 0); sl@0: TheTest.Printf(_L("==Records count: %d\r\n"), recCnt); sl@0: sqlite3_finalize(TheStmt); sl@0: TheStmt = 0; sl@0: end = SqlTimerTicks(); sl@0: SqlTimerPrint(_L("SELECT-3"), start, end); sl@0: TEST(recCnt > 0); sl@0: sl@0: //SELECT-4 sl@0: start = SqlTimerTicks(); sl@0: err = sqlite3_prepare(TheDb, "SELECT MAX(ROWID) FROM A", -1, &TheStmt, 0); sl@0: TEST2(err, SQLITE_OK); sl@0: err = sqlite3_step(TheStmt); sl@0: TEST2(err, SQLITE_ROW); sl@0: TInt rowid = sqlite3_column_int(TheStmt, 0); sl@0: TheTest.Printf(_L("==MAX(ROWID): %d\r\n"), recCnt); sl@0: sqlite3_finalize(TheStmt); sl@0: TheStmt = 0; sl@0: end = SqlTimerTicks(); sl@0: SqlTimerPrint(_L("SELECT-4"), start, end); sl@0: TEST(rowid > 0); sl@0: sl@0: //DELETE sl@0: start = SqlTimerTicks(); sl@0: sql.Format(_L("DELETE FROM A WHERE ID = %ld"), id); sl@0: sql8.Copy(sql); sl@0: err = sqlite3_exec(TheDb, (const char*)sql8.PtrZ(), 0, 0, 0); sl@0: TEST2(err, SQLITE_OK); sl@0: end = SqlTimerTicks(); sl@0: cnt = sqlite3_changes(TheDb); sl@0: TEST2(cnt, 1); sl@0: SqlTimerPrint(_L("DELETE"), start, end); sl@0: sl@0: sqlite3_close(TheDb); sl@0: TheDb = 0; sl@0: } sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: static void DoTests() sl@0: { sl@0: TheTest.Start(_L("Collect drive information")); sl@0: CollectDriveInfo(); sl@0: sl@0: TInt64 maxDrvSize = TheDriveInfo[TheBiggestDriveNo].iSizeMb * K1Mb; sl@0: if(maxDrvSize <= K4Gb) sl@0: { sl@0: TheTest.Printf(_L("There is no drive bigger than 4Gb. The tests won't be executed.\r\n")); sl@0: return; sl@0: } sl@0: sl@0: TheTest.Next(_L(" @SYMTestCaseID:PDS-SQLITE3-CT-4041 Create database, bigger than 4Gb")); sl@0: CreateBigDbTest(K4Gb + 64 * K1Mb); sl@0: sl@0: TheTest.Next (_L(" @SYMTestCaseID:PDS-SQLITE3-CT-4042 64-bit database - simple operations test")); sl@0: SimpleDbOperationsTest(); sl@0: sl@0: (void)TheFs.Delete(TheDbName); sl@0: } sl@0: sl@0: TInt E32Main() sl@0: { sl@0: TheTest.Title(); sl@0: sl@0: CTrapCleanup* tc = CTrapCleanup::New(); sl@0: TheTest(tc != NULL); sl@0: sl@0: __UHEAP_MARK; sl@0: sl@0: TInt err = TheFs.Connect(); sl@0: TheTest(err == KErrNone); sl@0: sl@0: DeleteTestFiles(); sl@0: DoTests(); sl@0: DeleteTestFiles(); sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: TheFs.Close(); sl@0: TheTest.End(); sl@0: TheTest.Close(); sl@0: sl@0: delete tc; sl@0: sl@0: User::Heap().Check(); sl@0: return KErrNone; sl@0: }