os/persistentdata/persistentstorage/store/TSTOR/t_stordict.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 #include <e32test.h>
    16 #include <s32mem.h>
    17 
    18 //
    19 // CTestStreamDictionary
    20 // The only reason this class to be used here is to 
    21 // get an access to CStreamDictionary::iArray (private data member). 
    22 //
    23 class CTestStreamDictionary : public CStreamDictionary
    24 	{
    25 public:
    26 	struct TEntry 
    27 	    {
    28 	    TUid uid;
    29 	    TStreamId id;
    30 	    };
    31 public:
    32 	static CTestStreamDictionary* NewL();
    33 	CTestStreamDictionary();
    34 	TUid Uid(TInt aInt) 
    35 	    { 
    36 	    return (*iCheat)[aInt].uid; 
    37 	    }
    38 	TStreamId StreamId(TInt aInt) 
    39 	    { 
    40 	    return (*iCheat)[aInt].id; 
    41 	    }
    42 	TInt Count() 
    43 	    { 
    44 	    return iCheat->Count(); 
    45 	    }
    46 private:
    47 	CArrayFixSeg<TEntry>* iCheat;
    48 	};
    49 
    50 CTestStreamDictionary* CTestStreamDictionary::NewL()
    51 	{
    52 	CTestStreamDictionary* thing=new(ELeave) CTestStreamDictionary();
    53 	return thing;
    54 	}
    55 
    56 CTestStreamDictionary::CTestStreamDictionary()
    57 	: iCheat((CArrayFixSeg<TEntry>*)&iCheat-1) //Now iCheat points to the base class' private data member: 
    58 	                                           //CStreamDictionary::iArray.
    59                                                //This way it is possible to call iArray's methods (even though it is private).
    60 	{
    61 	}
    62 
    63 
    64 //
    65 // Test code
    66 //
    67 
    68 const TInt KTestExpandSize=0x20;
    69 
    70 static RTest TheTest(_L("t_stordict"));
    71 
    72 // some uid-stream pairs to use for testing
    73 const TUid testUid1={1};
    74 static TStreamId testStreamId1=TStreamId(1);
    75 //
    76 const TUid testUid2={57};
    77 static TStreamId testStreamId2=TStreamId(57);
    78 //
    79 const TUid testUid3={99999};
    80 static TStreamId testStreamId3=TStreamId(425);
    81 //
    82 
    83 //Put test data files to be deleted at the end here!
    84 void DeleteDataFiles()
    85     {
    86     }
    87 
    88 //Tests macros and functions.
    89 //If (!aValue) then the test will be panicked, the test data files will be deleted.
    90 static void Check(TInt aValue, TInt aLine)
    91     {
    92     if(!aValue)
    93         {
    94         DeleteDataFiles();
    95         TheTest(EFalse, aLine);
    96         }
    97     }
    98 //If (aValue != aExpected) then the test will be panicked, the test data files will be deleted.
    99 static void Check(TInt aValue, TInt aExpected, TInt aLine)
   100     {
   101     if(aValue != aExpected)
   102         {
   103         RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
   104         DeleteDataFiles();
   105         TheTest(EFalse, aLine);
   106         }
   107     }
   108 //Use these to test conditions.
   109 #define TEST(arg) ::Check((arg), __LINE__)
   110 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
   111 
   112 /**
   113 @SYMTestCaseID          SYSLIB-STORE-CT-1201
   114 @SYMTestCaseDesc	    Tests for copy operations on dictionary files
   115 @SYMTestPriority 	    High
   116 @SYMTestActions  	    Attempt for copying two classes using memory based streams.
   117 @SYMTestExpectedResults Test must not fail
   118 @SYMREQ                 REQ0000
   119 */
   120 template <class T1,class T2>
   121 void testCopyL(T1& aCopy,const T2& anOriginal)
   122 	{
   123 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1201 "));
   124 	CBufSeg* buf=0;
   125 	TRAPD(r,buf=CBufSeg::NewL(KTestExpandSize));
   126     TEST2(r, KErrNone);
   127 //
   128 // Write anOriginal out to the buffer.
   129 //
   130 	RBufWriteStream out;
   131 	out.Append(*buf);
   132 	TRAP(r,out<<anOriginal);
   133 	TEST2(r, KErrNone);
   134 	TRAP(r,out.CommitL());
   135     TEST2(r, KErrNone);
   136 //
   137 // Read anOriginal in from the buffer.
   138 //
   139 	RBufReadStream in(*buf);
   140 	TRAP(r,in>>aCopy);
   141     TEST2(r, KErrNone);
   142 //
   143 // See if it's consumed the lot.
   144 //
   145 	TUint8 b;
   146 	TEST2(in.Source()->ReadL(&b,1), 0);
   147 //
   148 	delete buf;
   149 	}
   150 
   151 /**
   152 @SYMTestCaseID          SYSLIB-STORE-CT-1202
   153 @SYMTestCaseDesc	    Tests if two dictionary files are equal
   154 @SYMTestPriority 	    High
   155 @SYMTestActions  	    Tests if count of entries,UID and streamID's are equal
   156 @SYMTestExpectedResults Test must not fail
   157 @SYMREQ                 REQ0000
   158 */
   159 void testIsEqual(CTestStreamDictionary* aCopy,CTestStreamDictionary* aOrig)
   160 	{
   161 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1202 "));
   162 	TInt origCount=aOrig->Count();
   163 	TEST(origCount==aCopy->Count());
   164 	//
   165 	for (TInt i=0 ; i<origCount ; i++)
   166 		{
   167 		TEST(aOrig->Uid(i)==aCopy->Uid(i));
   168 		TEST(aOrig->StreamId(i)==aCopy->StreamId(i));
   169 		}
   170 	}
   171 
   172 /**
   173 @SYMTestCaseID          SYSLIB-STORE-CT-1203
   174 @SYMTestCaseDesc	    Tests for simple operations on a dictionary file
   175 @SYMTestPriority 	    High
   176 @SYMTestActions  	    Tests for assign,re-assigning,removing entries from the file
   177 @SYMTestExpectedResults Test must not fail
   178 @SYMREQ                 REQ0000
   179 */
   180 LOCAL_C void simpleTestsL()
   181 	{
   182 	CTestStreamDictionary* dic=CTestStreamDictionary::NewL();
   183 	// attempt finding and removing with an empty dictionary
   184 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1203 Manipulating an empty dictionary "));
   185 	TEST2(dic->Count(), 0);
   186 	TEST(dic->At(testUid1)==KNullStreamId);
   187 	dic->Remove(testUid1);
   188 	TEST2(dic->Count(), 0);
   189 	TEST(dic->IsNull());
   190 	//
   191 	// assign an entry
   192 	TheTest.Next(_L("Assigning entries and manipulating them"));
   193 	TRAPD(ret,dic->AssignL(testUid1,testStreamId1));
   194 	TEST2(ret, KErrNone);
   195 	TEST2(dic->Count(), 1);
   196 	TEST(!dic->IsNull());
   197 	TEST(dic->At(testUid1)==testStreamId1);
   198 	//
   199 	// assign another entry
   200 	TRAP(ret,dic->AssignL(testUid2,testStreamId2));
   201 	TEST2(ret, KErrNone);
   202 	TEST2(dic->Count(), 2);
   203 	TEST(dic->At(testUid2)==testStreamId2);
   204 	//
   205 	// re-assign uid1
   206 	TRAP(ret,dic->AssignL(testUid1,testStreamId3));
   207 	TEST2(ret, KErrNone);
   208 	TEST2(dic->Count(), 2);
   209 	TEST(dic->At(testUid1)==testStreamId3);
   210 	//
   211 	// test finding and removing a non-existant entry from a non-empty dictionary
   212 	TEST(dic->At(testUid3)==KNullStreamId);
   213 	dic->Remove(testUid3);
   214 	TEST2(dic->Count(), 2);
   215 	//
   216 	// test removing an entry
   217 	dic->Remove(testUid1);
   218 	TEST2(dic->Count(), 1);
   219 	TEST(dic->At(testUid1)==KNullStreamId);
   220 	TEST(dic->At(testUid2)==testStreamId2);
   221 	TEST(!dic->IsNull());
   222 	//
   223 	// test removing the other entry
   224 	dic->Remove(testUid2);
   225 	TEST2(dic->Count(), 0);
   226 	TEST(dic->IsNull());
   227 	TEST(dic->At(testUid1)==KNullStreamId);
   228 	TEST(dic->At(testUid2)==KNullStreamId);
   229 	//
   230 	delete dic;
   231 	}
   232 
   233 /**
   234 @SYMTestCaseID          SYSLIB-STORE-CT-1204
   235 @SYMTestCaseDesc	    Streaming dictionary files tests
   236 @SYMTestPriority 	    High
   237 @SYMTestActions  	    Tests for copying an empty dictionary and dictionary containing different sets of entries
   238                         Tests for equality of two dictionary files and test the copied file.
   239 @SYMTestExpectedResults Test must not fail
   240 @SYMREQ                 REQ0000
   241 */
   242 LOCAL_C void streamingTestsL()
   243 	{
   244 	CTestStreamDictionary* orig=CTestStreamDictionary::NewL();
   245 	CTestStreamDictionary* copy=CTestStreamDictionary::NewL();
   246 	//
   247 	// copy an empty dictionary
   248 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1204 Streaming an empty dictionary "));
   249 	TEST(orig->IsNull());
   250 	testCopyL(*copy,*orig);
   251 	TEST(copy->IsNull());
   252 	//
   253 	// copy a dictionary containing a range of entries
   254 	TheTest.Next(_L("Streaming a dictionary containing entries"));
   255 	TRAPD(ret,orig->AssignL(testUid1,testStreamId1));
   256 	TRAP(ret,orig->AssignL(testUid2,testStreamId2));
   257 	TRAP(ret,orig->AssignL(testUid3,testStreamId3));
   258 	testCopyL(*copy,*orig);
   259 	testIsEqual(copy,orig);
   260 	TEST(!copy->IsNull());
   261 	//
   262 	delete orig;
   263 	delete copy;
   264 	}
   265 
   266 void DoTestL()
   267     {
   268     simpleTestsL();
   269     streamingTestsL();
   270     }
   271 
   272 TInt E32Main()
   273 	{
   274     __UHEAP_MARK;
   275 	
   276     TheTest.Title();
   277 	
   278     CTrapCleanup* trapCleanup = CTrapCleanup::New();
   279     TheTest(trapCleanup != NULL);
   280 
   281 	TheTest.Start(_L("Testing CStreamDictionary..."));
   282 	
   283 	TRAPD(err, DoTestL());
   284     TEST2(err, KErrNone);
   285 
   286     DeleteDataFiles();
   287     
   288     TheTest.End();
   289     TheTest.Close();
   290 
   291     delete trapCleanup;
   292     
   293     __UHEAP_MARKEND;
   294     
   295 	return KErrNone;
   296 	}
   297