persistentstorage/store/TSTOR/t_stordict.cpp
branchRCL_3
changeset 6 5ffdb8f2067f
parent 0 08ec8eefde2f
equal deleted inserted replaced
2:6862383cf555 6:5ffdb8f2067f
    10 //
    10 //
    11 // Contributors:
    11 // Contributors:
    12 //
    12 //
    13 // Description:
    13 // Description:
    14 //
    14 //
    15 
       
    16 #if !defined(__E32TEST_H__)
       
    17 #include <e32test.h>
    15 #include <e32test.h>
    18 #endif
       
    19 
       
    20 #include <s32mem.h>
    16 #include <s32mem.h>
    21 
    17 
    22 //
    18 //
    23 // CTestStreamDictionary
    19 // CTestStreamDictionary
    24 //
    20 // The only reason this class to be used here is to 
    25 
    21 // get an access to CStreamDictionary::iArray (private data member). 
       
    22 //
    26 class CTestStreamDictionary : public CStreamDictionary
    23 class CTestStreamDictionary : public CStreamDictionary
    27 	{
    24 	{
    28 public:
    25 public:
    29 	struct TEntry {TUid uid;TStreamId id;};
    26 	struct TEntry 
       
    27 	    {
       
    28 	    TUid uid;
       
    29 	    TStreamId id;
       
    30 	    };
    30 public:
    31 public:
    31 	static CTestStreamDictionary* NewL();
    32 	static CTestStreamDictionary* NewL();
    32 	CTestStreamDictionary();
    33 	CTestStreamDictionary();
    33 //*	TUid Uid(TInt aInt) { return (*iEntryList)[aInt].iUid; }
    34 	TUid Uid(TInt aInt) 
    34 //*	TStreamId StreamId(TInt aInt) { return (*iEntryList)[aInt].iStreamId; }
    35 	    { 
    35 //*	TInt Count() { return iEntryList->Count(); }
    36 	    return (*iCheat)[aInt].uid; 
    36 	TUid Uid(TInt aInt) { return (*iCheat)[aInt].uid; }
    37 	    }
    37 	TStreamId StreamId(TInt aInt) { return (*iCheat)[aInt].id; }
    38 	TStreamId StreamId(TInt aInt) 
    38 	TInt Count() { return iCheat->Count(); }
    39 	    { 
       
    40 	    return (*iCheat)[aInt].id; 
       
    41 	    }
       
    42 	TInt Count() 
       
    43 	    { 
       
    44 	    return iCheat->Count(); 
       
    45 	    }
    39 private:
    46 private:
    40 	CArrayFixSeg<TEntry>* iCheat;
    47 	CArrayFixSeg<TEntry>* iCheat;
    41 	};
    48 	};
    42 
    49 
    43 CTestStreamDictionary* CTestStreamDictionary::NewL()
    50 CTestStreamDictionary* CTestStreamDictionary::NewL()
    44 	{
    51 	{
    45 	CTestStreamDictionary* thing=new(ELeave) CTestStreamDictionary();
    52 	CTestStreamDictionary* thing=new(ELeave) CTestStreamDictionary();
    46 //*	CleanupStack::PushL(thing);
       
    47 //*	thing->ConstructL();
       
    48 //*	CleanupStack::Pop();
       
    49 	return thing;
    53 	return thing;
    50 	}
    54 	}
    51 
    55 
    52 CTestStreamDictionary::CTestStreamDictionary()
    56 CTestStreamDictionary::CTestStreamDictionary()
    53 	: iCheat((CArrayFixSeg<TEntry>*)&iCheat-1) // modification to get to count etc
    57 	: iCheat((CArrayFixSeg<TEntry>*)&iCheat-1) //Now iCheat points to the base class' private data member: 
    54 	{}
    58 	                                           //CStreamDictionary::iArray.
       
    59                                                //This way it is possible to call iArray's methods (even though it is private).
       
    60 	{
       
    61 	}
    55 
    62 
    56 
    63 
    57 //
    64 //
    58 // Test code
    65 // Test code
    59 //
    66 //
    60 
    67 
    61 const TInt KTestCleanupStack=0x40;
       
    62 const TInt KTestExpandSize=0x20;
    68 const TInt KTestExpandSize=0x20;
    63 
    69 
    64 LOCAL_D RTest test(_L("t_stordict"));
    70 static RTest TheTest(_L("t_stordict"));
    65 LOCAL_D CTrapCleanup* TheTrapCleanup;
       
    66 
    71 
    67 // some uid-stream pairs to use for testing
    72 // some uid-stream pairs to use for testing
    68 const TUid testUid1={1};
    73 const TUid testUid1={1};
    69 LOCAL_D TStreamId testStreamId1=TStreamId(1);
    74 static TStreamId testStreamId1=TStreamId(1);
    70 //
    75 //
    71 const TUid testUid2={57};
    76 const TUid testUid2={57};
    72 LOCAL_D TStreamId testStreamId2=TStreamId(57);
    77 static TStreamId testStreamId2=TStreamId(57);
    73 //
    78 //
    74 const TUid testUid3={99999};
    79 const TUid testUid3={99999};
    75 LOCAL_D TStreamId testStreamId3=TStreamId(425);
    80 static TStreamId testStreamId3=TStreamId(425);
    76 //
    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__)
    77 
   111 
    78 /**
   112 /**
    79 @SYMTestCaseID          SYSLIB-STORE-CT-1201
   113 @SYMTestCaseID          SYSLIB-STORE-CT-1201
    80 @SYMTestCaseDesc	    Tests for copy operations on dictionary files
   114 @SYMTestCaseDesc	    Tests for copy operations on dictionary files
    81 @SYMTestPriority 	    High
   115 @SYMTestPriority 	    High
    84 @SYMREQ                 REQ0000
   118 @SYMREQ                 REQ0000
    85 */
   119 */
    86 template <class T1,class T2>
   120 template <class T1,class T2>
    87 void testCopyL(T1& aCopy,const T2& anOriginal)
   121 void testCopyL(T1& aCopy,const T2& anOriginal)
    88 	{
   122 	{
    89 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1201 "));
   123 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1201 "));
    90 	CBufSeg* buf=0;
   124 	CBufSeg* buf=0;
    91 	TRAPD(r,buf=CBufSeg::NewL(KTestExpandSize));
   125 	TRAPD(r,buf=CBufSeg::NewL(KTestExpandSize));
    92 	if (r!=KErrNone)
   126     TEST2(r, KErrNone);
    93 		test.Panic(_L("Allocating buffer"));
       
    94 //
   127 //
    95 // Write anOriginal out to the buffer.
   128 // Write anOriginal out to the buffer.
    96 //
   129 //
    97 	RBufWriteStream out;
   130 	RBufWriteStream out;
    98 	out.Append(*buf);
   131 	out.Append(*buf);
    99 	TRAP(r,out<<anOriginal);
   132 	TRAP(r,out<<anOriginal);
   100 	test(r==KErrNone);
   133 	TEST2(r, KErrNone);
   101 	TRAP(r,out.CommitL());
   134 	TRAP(r,out.CommitL());
   102 	if (r!=KErrNone)
   135     TEST2(r, KErrNone);
   103 		test.Panic(_L("Committing write stream"));
       
   104 //
   136 //
   105 // Read anOriginal in from the buffer.
   137 // Read anOriginal in from the buffer.
   106 //
   138 //
   107 	RBufReadStream in(*buf);
   139 	RBufReadStream in(*buf);
   108 	TRAP(r,in>>aCopy);
   140 	TRAP(r,in>>aCopy);
   109 	test(r==KErrNone);
   141     TEST2(r, KErrNone);
   110 //
   142 //
   111 // See if it's consumed the lot.
   143 // See if it's consumed the lot.
   112 //
   144 //
   113 	TUint8 b;
   145 	TUint8 b;
   114 	test(in.Source()->ReadL(&b,1)==0);
   146 	TEST2(in.Source()->ReadL(&b,1), 0);
   115 //
   147 //
   116 	delete buf;
   148 	delete buf;
   117 	}
   149 	}
   118 
   150 
   119 /**
   151 /**
   124 @SYMTestExpectedResults Test must not fail
   156 @SYMTestExpectedResults Test must not fail
   125 @SYMREQ                 REQ0000
   157 @SYMREQ                 REQ0000
   126 */
   158 */
   127 void testIsEqual(CTestStreamDictionary* aCopy,CTestStreamDictionary* aOrig)
   159 void testIsEqual(CTestStreamDictionary* aCopy,CTestStreamDictionary* aOrig)
   128 	{
   160 	{
   129 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1202 "));
   161 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1202 "));
   130 	TInt origCount=aOrig->Count();
   162 	TInt origCount=aOrig->Count();
   131 	test(origCount==aCopy->Count());
   163 	TEST(origCount==aCopy->Count());
   132 	//
   164 	//
   133 	for (TInt i=0 ; i<origCount ; i++)
   165 	for (TInt i=0 ; i<origCount ; i++)
   134 		{
   166 		{
   135 		test(aOrig->Uid(i)==aCopy->Uid(i));
   167 		TEST(aOrig->Uid(i)==aCopy->Uid(i));
   136 		test(aOrig->StreamId(i)==aCopy->StreamId(i));
   168 		TEST(aOrig->StreamId(i)==aCopy->StreamId(i));
   137 		}
   169 		}
   138 	}
   170 	}
   139 
   171 
   140 /**
   172 /**
   141 @SYMTestCaseID          SYSLIB-STORE-CT-1203
   173 @SYMTestCaseID          SYSLIB-STORE-CT-1203
   147 */
   179 */
   148 LOCAL_C void simpleTestsL()
   180 LOCAL_C void simpleTestsL()
   149 	{
   181 	{
   150 	CTestStreamDictionary* dic=CTestStreamDictionary::NewL();
   182 	CTestStreamDictionary* dic=CTestStreamDictionary::NewL();
   151 	// attempt finding and removing with an empty dictionary
   183 	// attempt finding and removing with an empty dictionary
   152 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1203 Manipulating an empty dictionary "));
   184 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1203 Manipulating an empty dictionary "));
   153 	test(dic->Count()==0);
   185 	TEST2(dic->Count(), 0);
   154 	test(dic->At(testUid1)==KNullStreamId);
   186 	TEST(dic->At(testUid1)==KNullStreamId);
   155 	dic->Remove(testUid1);
   187 	dic->Remove(testUid1);
   156 	test(dic->Count()==0);
   188 	TEST2(dic->Count(), 0);
   157 	test(dic->IsNull());
   189 	TEST(dic->IsNull());
   158 	//
   190 	//
   159 	// assign an entry
   191 	// assign an entry
   160 	test.Next(_L("Assigning entries and manipulating them"));
   192 	TheTest.Next(_L("Assigning entries and manipulating them"));
   161 	TRAPD(ret,dic->AssignL(testUid1,testStreamId1));
   193 	TRAPD(ret,dic->AssignL(testUid1,testStreamId1));
   162 	test(ret==KErrNone);
   194 	TEST2(ret, KErrNone);
   163 	test(dic->Count()==1);
   195 	TEST2(dic->Count(), 1);
   164 	test(!dic->IsNull());
   196 	TEST(!dic->IsNull());
   165 	test(dic->At(testUid1)==testStreamId1);
   197 	TEST(dic->At(testUid1)==testStreamId1);
   166 	//
   198 	//
   167 	// assign another entry
   199 	// assign another entry
   168 	TRAP(ret,dic->AssignL(testUid2,testStreamId2));
   200 	TRAP(ret,dic->AssignL(testUid2,testStreamId2));
   169 	test(ret==KErrNone);
   201 	TEST2(ret, KErrNone);
   170 	test(dic->Count()==2);
   202 	TEST2(dic->Count(), 2);
   171 	test(dic->At(testUid2)==testStreamId2);
   203 	TEST(dic->At(testUid2)==testStreamId2);
   172 	//
   204 	//
   173 	// re-assign uid1
   205 	// re-assign uid1
   174 	TRAP(ret,dic->AssignL(testUid1,testStreamId3));
   206 	TRAP(ret,dic->AssignL(testUid1,testStreamId3));
   175 	test(ret==KErrNone);
   207 	TEST2(ret, KErrNone);
   176 	test(dic->Count()==2);
   208 	TEST2(dic->Count(), 2);
   177 	test(dic->At(testUid1)==testStreamId3);
   209 	TEST(dic->At(testUid1)==testStreamId3);
   178 	//
   210 	//
   179 	// test finding and removing a non-existant entry from a non-empty dictionary
   211 	// test finding and removing a non-existant entry from a non-empty dictionary
   180 	test(dic->At(testUid3)==KNullStreamId);
   212 	TEST(dic->At(testUid3)==KNullStreamId);
   181 	dic->Remove(testUid3);
   213 	dic->Remove(testUid3);
   182 	test(dic->Count()==2);
   214 	TEST2(dic->Count(), 2);
   183 	//
   215 	//
   184 	// test removing an entry
   216 	// test removing an entry
   185 	dic->Remove(testUid1);
   217 	dic->Remove(testUid1);
   186 	test(dic->Count()==1);
   218 	TEST2(dic->Count(), 1);
   187 	test(dic->At(testUid1)==KNullStreamId);
   219 	TEST(dic->At(testUid1)==KNullStreamId);
   188 	test(dic->At(testUid2)==testStreamId2);
   220 	TEST(dic->At(testUid2)==testStreamId2);
   189 	test(!dic->IsNull());
   221 	TEST(!dic->IsNull());
   190 	//
   222 	//
   191 	// test removing the other entry
   223 	// test removing the other entry
   192 	dic->Remove(testUid2);
   224 	dic->Remove(testUid2);
   193 	test(dic->Count()==0);
   225 	TEST2(dic->Count(), 0);
   194 	test(dic->IsNull());
   226 	TEST(dic->IsNull());
   195 	test(dic->At(testUid1)==KNullStreamId);
   227 	TEST(dic->At(testUid1)==KNullStreamId);
   196 	test(dic->At(testUid2)==KNullStreamId);
   228 	TEST(dic->At(testUid2)==KNullStreamId);
   197 	//
   229 	//
   198 	delete dic;
   230 	delete dic;
   199 	}
   231 	}
   200 
   232 
   201 /**
   233 /**
   211 	{
   243 	{
   212 	CTestStreamDictionary* orig=CTestStreamDictionary::NewL();
   244 	CTestStreamDictionary* orig=CTestStreamDictionary::NewL();
   213 	CTestStreamDictionary* copy=CTestStreamDictionary::NewL();
   245 	CTestStreamDictionary* copy=CTestStreamDictionary::NewL();
   214 	//
   246 	//
   215 	// copy an empty dictionary
   247 	// copy an empty dictionary
   216 	test.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1204 Streaming an empty dictionary "));
   248 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-STORE-CT-1204 Streaming an empty dictionary "));
   217 	test(orig->IsNull());
   249 	TEST(orig->IsNull());
   218 	testCopyL(*copy,*orig);
   250 	testCopyL(*copy,*orig);
   219 	test(copy->IsNull());
   251 	TEST(copy->IsNull());
   220 	//
   252 	//
   221 	// copy a dictionary containing a range of entries
   253 	// copy a dictionary containing a range of entries
   222 	test.Next(_L("Streaming a dictionary containing entries"));
   254 	TheTest.Next(_L("Streaming a dictionary containing entries"));
   223 	TRAPD(ret,orig->AssignL(testUid1,testStreamId1));
   255 	TRAPD(ret,orig->AssignL(testUid1,testStreamId1));
   224 	TRAP(ret,orig->AssignL(testUid2,testStreamId2));
   256 	TRAP(ret,orig->AssignL(testUid2,testStreamId2));
   225 	TRAP(ret,orig->AssignL(testUid3,testStreamId3));
   257 	TRAP(ret,orig->AssignL(testUid3,testStreamId3));
   226 	testCopyL(*copy,*orig);
   258 	testCopyL(*copy,*orig);
   227 	testIsEqual(copy,orig);
   259 	testIsEqual(copy,orig);
   228 	test(!copy->IsNull());
   260 	TEST(!copy->IsNull());
   229 	//
   261 	//
   230 	delete orig;
   262 	delete orig;
   231 	delete copy;
   263 	delete copy;
   232 	}
   264 	}
   233 
   265 
   234 
   266 void DoTestL()
   235 //
   267     {
   236 // Initialise the cleanup stack.
   268     simpleTestsL();
   237 //
   269     streamingTestsL();
   238 LOCAL_C void setupCleanup()
   270     }
   239     {
   271 
   240 	TheTrapCleanup=CTrapCleanup::New();
   272 TInt E32Main()
   241 	TRAPD(r,\
   273 	{
   242 		{\
   274     __UHEAP_MARK;
   243 		for (TInt i=KTestCleanupStack;i>0;i--)\
   275 	
   244 			CleanupStack::PushL((TAny*)1);\
   276     TheTest.Title();
   245 		test(r==KErrNone);\
   277 	
   246 		CleanupStack::Pop(KTestCleanupStack);\
   278     CTrapCleanup* trapCleanup = CTrapCleanup::New();
   247 		});
   279     TheTest(trapCleanup != NULL);
   248 	}
   280 
   249 
   281 	TheTest.Start(_L("Testing CStreamDictionary..."));
   250 
   282 	
   251 GLDEF_C TInt E32Main()
   283 	TRAPD(err, DoTestL());
   252 	{
   284     TEST2(err, KErrNone);
   253 	setupCleanup();
   285 
   254 	//
   286     DeleteDataFiles();
   255 	test.Title();
   287     
   256 	test.Start(_L("Testing CStreamDictionary..."));
   288     TheTest.End();
   257 	//
   289     TheTest.Close();
   258 	// run the testcode (inside an alloc heaven harness)
   290 
   259 	__UHEAP_MARK;
   291     delete trapCleanup;
   260 
   292     
   261 	TRAPD(r,simpleTestsL());
   293     __UHEAP_MARKEND;
   262 		test(r==KErrNone);
   294     
   263 
       
   264 	TRAP(r,streamingTestsL());
       
   265 		test(r==KErrNone);
       
   266 
       
   267 	__UHEAP_MARKEND;
       
   268 
       
   269  	test.End();
       
   270 	test.Close();
       
   271 
       
   272 	delete TheTrapCleanup;
       
   273 	return KErrNone;
   295 	return KErrNone;
   274 	}
   296 	}
   275 
   297