os/kernelhwsrv/kerneltest/e32test/buffer/t_parray.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\buffer\t_parray.cpp
sl@0
    15
// Overview:
sl@0
    16
// Test the variable record length array classes.
sl@0
    17
// Test only covers the Flat type implementation.
sl@0
    18
// API Information:
sl@0
    19
// CArrayPakFlat.
sl@0
    20
// Details:
sl@0
    21
// - Create an array of variable length text character objects packed into a flat buffer.
sl@0
    22
// - check the number of elements held in the array is 0.
sl@0
    23
// - test that Compress and Reset methods are as expected.
sl@0
    24
// - sort the array and check that no error is returned.
sl@0
    25
// - insert an element and check the length is as expected.
sl@0
    26
// - search for the inserted element and check it is found successfully.
sl@0
    27
// - remove all the elements, append an element and verify that number of elements
sl@0
    28
// held in the array is 1.
sl@0
    29
// - insert an element into the array, search for that element using sequential,
sl@0
    30
// binary search technique and verify that it is found at the expected position.
sl@0
    31
// - insert another element with the same key and check that KErrAlreadyExists is 
sl@0
    32
// returned.
sl@0
    33
// - Create an array of variable length text character objects packed into a flat buffer.
sl@0
    34
// - append some strings at the end, insert some strings at the specified location and
sl@0
    35
// verify that the length of each string and number of strings held in the array are 
sl@0
    36
// as expected.
sl@0
    37
// - delete some strings and check the remaining strings in the array are as expected.
sl@0
    38
// - Create an array of variable length text character objects packed into a flat buffer.
sl@0
    39
// - append some strings at the end and insert some stings at specified location.
sl@0
    40
// - compress the array and verify strings and the number of strings held in the 
sl@0
    41
// array are as expected.
sl@0
    42
// - reset the array and verify the number of elements held in the array is 0.
sl@0
    43
// - sort the array and check that array is sorted as expected.
sl@0
    44
// - test that KErrAlreadyExists is returned if another element with the same key 
sl@0
    45
// type is inserted.
sl@0
    46
// - search for strings using sequential, binary search and verify that 0 is returned 
sl@0
    47
// if found else nonzero.
sl@0
    48
// - delete some text from the array and check the results are as expected.
sl@0
    49
// - Create an array of variable length integer objects packed into a flat buffer.
sl@0
    50
// - Insert some elements with same key which already exists within the array and 
sl@0
    51
// check that KErrAlreadyExists is returned.
sl@0
    52
// - Test whether the heap has been corrupted by all the tests.
sl@0
    53
// Platforms/Drives/Compatibility:
sl@0
    54
// All 
sl@0
    55
// Assumptions/Requirement/Pre-requisites:
sl@0
    56
// Failures and causes:
sl@0
    57
// Base Port information:
sl@0
    58
// 
sl@0
    59
//
sl@0
    60
sl@0
    61
#include <e32std.h>
sl@0
    62
#include <e32std_private.h>
sl@0
    63
#include <e32base.h>
sl@0
    64
#include <e32base_private.h>
sl@0
    65
#include <e32test.h>
sl@0
    66
#include <e32svr.h>
sl@0
    67
#include <e32ver.h>
sl@0
    68
sl@0
    69
const TInt KTestGranularity=0x10;
sl@0
    70
sl@0
    71
LOCAL_D RTest test(_L("T_PARRAY"));
sl@0
    72
sl@0
    73
LOCAL_C void testAllMethods(CArrayPak<TText>& aPakVar)
sl@0
    74
    {
sl@0
    75
	test.Next(_L("Test all methods"));
sl@0
    76
	test(aPakVar.Count()==0);
sl@0
    77
	aPakVar.Compress();
sl@0
    78
	test(TRUE);
sl@0
    79
	aPakVar.Reset();
sl@0
    80
	test(TRUE);
sl@0
    81
	TKeyArrayPak kk(sizeof(TText),ECmpNormal,0);
sl@0
    82
	TKeyArrayVar vv(sizeof(TText),ECmpNormal,0);
sl@0
    83
	test(TRUE);
sl@0
    84
	TRAPD(res,aPakVar.SortL(vv));
sl@0
    85
	test(res==KErrNone);
sl@0
    86
	const TText* aa=_S("a");
sl@0
    87
	aPakVar.InsertL(0,*aa,sizeof(TText));
sl@0
    88
	TBuf<0x10> des1(1);
sl@0
    89
	des1[0]=aPakVar[0];
sl@0
    90
	test(des1==_L("a"));
sl@0
    91
	test(aPakVar.Length(0)==sizeof(TText));
sl@0
    92
	test(TRUE);
sl@0
    93
	TInt pp;
sl@0
    94
	test(aPakVar.Find(*aa,kk,pp)==0);
sl@0
    95
	test(pp==0);
sl@0
    96
	aPakVar.Delete(0);
sl@0
    97
	aPakVar.AppendL(*aa,1);
sl@0
    98
	test(aPakVar.Count()==1);
sl@0
    99
	aPakVar.InsertIsqAllowDuplicatesL(*aa,0,kk);
sl@0
   100
	test(TRUE);
sl@0
   101
	test(aPakVar.FindIsq(*aa,kk,pp)==0);
sl@0
   102
	test(pp==0);
sl@0
   103
	TRAPD(r,aPakVar.InsertIsqL(*aa,0,kk));
sl@0
   104
	test(r==KErrAlreadyExists);
sl@0
   105
	}
sl@0
   106
sl@0
   107
LOCAL_C void test1(CArrayPak<TText>& aPakVar)
sl@0
   108
//
sl@0
   109
	{
sl@0
   110
	test.Next(_L("AppendL and InsertL chars"));
sl@0
   111
	aPakVar.AppendL(*_S("abcd"),5*sizeof(TText)); // abcd
sl@0
   112
	TBuf<0x10> des1(&aPakVar[0]);
sl@0
   113
	test(des1==_L("abcd"));
sl@0
   114
	test(aPakVar.Count()==1);
sl@0
   115
	aPakVar.AppendL(*_S("wxyz"),5*sizeof(TText)); // abcd wxyz
sl@0
   116
	des1=&aPakVar[1];
sl@0
   117
	test(des1==_L("wxyz"));
sl@0
   118
	test(aPakVar.Count()==2);
sl@0
   119
	aPakVar.InsertL(1,*_S("ef"),3*sizeof(TText)); // abcd ef wxyz
sl@0
   120
	des1=&aPakVar[1];
sl@0
   121
	test(des1==_L("ef"));
sl@0
   122
	test(aPakVar.Count()==3);	
sl@0
   123
	aPakVar.AppendL(*_S("z"),2*sizeof(TText)); // abcd ef wxyz z
sl@0
   124
	des1=&aPakVar[3];
sl@0
   125
	test(des1==_L("z"));
sl@0
   126
	aPakVar.InsertL(0,*_S("y"),2*sizeof(TText)); // y abcd ef wxyz z
sl@0
   127
	des1=&aPakVar[0];
sl@0
   128
	test(des1==_L("y"));
sl@0
   129
	test(aPakVar.Length(0)==2*sizeof(TText));
sl@0
   130
	test(aPakVar.Length(1)==5*sizeof(TText));
sl@0
   131
	test(aPakVar.Length(2)==3*sizeof(TText));
sl@0
   132
	test(aPakVar.Length(3)==5*sizeof(TText));
sl@0
   133
	test(aPakVar.Length(4)==2*sizeof(TText));
sl@0
   134
	des1=&aPakVar[1];
sl@0
   135
	test(des1==_L("abcd"));
sl@0
   136
	test(aPakVar.Count()==5);
sl@0
   137
//
sl@0
   138
	test.Next(_L("Delete chars"));
sl@0
   139
	aPakVar.Delete(3,1); // y abcd ef z
sl@0
   140
	des1=&aPakVar[2];
sl@0
   141
	test(des1==_L("ef"));
sl@0
   142
	des1=&aPakVar[1];
sl@0
   143
	test(des1==_L("abcd"));
sl@0
   144
	des1=&aPakVar[3];
sl@0
   145
	test(des1==_L("z"));
sl@0
   146
	aPakVar.Delete(1,2); // y z
sl@0
   147
	des1=&aPakVar[0];
sl@0
   148
	test(des1==_L("y"));
sl@0
   149
	des1=&aPakVar[1];
sl@0
   150
	test(des1==_L("z"));
sl@0
   151
	test(aPakVar.Count()==2);
sl@0
   152
	}
sl@0
   153
sl@0
   154
LOCAL_C void test2(CArrayPak<TText>& aPakVar)
sl@0
   155
//
sl@0
   156
	{
sl@0
   157
	test.Next(_L("Reset and Compress"));
sl@0
   158
	TBuf<0x10> des1(_L("abcde"));
sl@0
   159
	TBuf<0x10> des2(_L("fgh"));
sl@0
   160
	TBuf<0x10> des3(_L("wxyz"));
sl@0
   161
	aPakVar.AppendL(*(TText*)des1.Ptr(),des1.Size());
sl@0
   162
	aPakVar.AppendL(*(TText*)des2.Ptr(),des2.Size());
sl@0
   163
	aPakVar.Compress();
sl@0
   164
	test(aPakVar.Count()==2);
sl@0
   165
	TPtrC des4((TText*)&aPakVar[0],des1.Length());
sl@0
   166
	test(des1==des4);
sl@0
   167
	TPtrC des5((TText*)&aPakVar[1],des2.Length());
sl@0
   168
	test(des2==des5);
sl@0
   169
	aPakVar.InsertL(1,*(TText*)des3.Ptr(),des3.Size());
sl@0
   170
	test(aPakVar.Count()==3);	
sl@0
   171
	TPtrC des6((TText*)&aPakVar[0],des1.Length());
sl@0
   172
	test(des1==des6);
sl@0
   173
	TPtrC des7((TText*)&aPakVar[2],des2.Length());
sl@0
   174
	test(des2==des7);
sl@0
   175
	TPtrC des8((TText*)&aPakVar[1],des3.Length());
sl@0
   176
	test(des3==des8);
sl@0
   177
	aPakVar.Reset();
sl@0
   178
	// So nothing in this array
sl@0
   179
	test(aPakVar.Count()==0);
sl@0
   180
	TKeyArrayPak kk(0,ECmpNormal,3);		// Compare 3 characters
sl@0
   181
	TKeyArrayPak kk1(0,ECmpNormal,2);	// Compare 2 characters
sl@0
   182
	TKeyArrayVar vv(0,ECmpNormal,3);		// Compare 3 characters
sl@0
   183
	TBuf<0x10> buf1=_L("abcdef");
sl@0
   184
	TBuf<0x10> buf2=_L("wxyz");
sl@0
   185
	TBuf<0x10> buf3=_L("lmnop");
sl@0
   186
	TBuf<0x10> buf4=_L("aa");
sl@0
   187
	aPakVar.AppendL(*buf1.Ptr(),buf1.Size());
sl@0
   188
	aPakVar.InsertL(0,*buf2.Ptr(),buf2.Size());
sl@0
   189
	aPakVar.AppendL(*buf3.Ptr(),buf3.Size());
sl@0
   190
	aPakVar.InsertL(1,*buf4.Ptr(),buf4.Size());
sl@0
   191
	aPakVar.Compress();
sl@0
   192
	TPtrC rd1((TText*)&aPakVar[2],buf1.Length());
sl@0
   193
	test(buf1==rd1);
sl@0
   194
	TPtrC rd2((TText*)&aPakVar[0],buf2.Length());
sl@0
   195
	test(buf2==rd2);
sl@0
   196
	TPtrC rd3((TText*)&aPakVar[3],buf3.Length());
sl@0
   197
	test(buf3==rd3);
sl@0
   198
	TPtrC rd4((TText*)&aPakVar[1],buf4.Length());
sl@0
   199
	test(buf4==rd4);
sl@0
   200
	test(aPakVar.Count()==4);
sl@0
   201
sl@0
   202
	test.Next(_L("Sort"));
sl@0
   203
	TRAPD(res,aPakVar.SortL(vv));
sl@0
   204
	test(res==KErrNone);
sl@0
   205
	/**/
sl@0
   206
	TPtrC rd5((TText*)&aPakVar[1],buf1.Length());
sl@0
   207
	test(buf1==rd5);
sl@0
   208
	TPtrC rd6((TText*)&aPakVar[3],buf2.Length());
sl@0
   209
	test(buf2==rd6);
sl@0
   210
	TPtrC rd7((TText*)&aPakVar[2],buf3.Length());
sl@0
   211
	test(buf3==rd7);
sl@0
   212
	TPtrC rd8((TText*)&aPakVar[0],buf4.Length());
sl@0
   213
	test(buf4==rd8);
sl@0
   214
	test(aPakVar.Count()==4);	
sl@0
   215
	/**/
sl@0
   216
	test.Next(_L("Find and FindIsq"));
sl@0
   217
	TBuf<0x10> buf5=_L("ffff");
sl@0
   218
	test(aPakVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==2);
sl@0
   219
	TRAPD(r,aPakVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk))
sl@0
   220
	test(r==KErrAlreadyExists);
sl@0
   221
	test(aPakVar.InsertIsqAllowDuplicatesL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==3);
sl@0
   222
	TInt aPos;
sl@0
   223
	test(aPakVar.Find(*_S("abc"),kk,aPos)==0);	// Second parameter 'aLength' is unused. 
sl@0
   224
	test(aPos==1);
sl@0
   225
	test(aPakVar.Find(*_S("aa"),kk1,aPos)==0);
sl@0
   226
	test(aPos==0);
sl@0
   227
	test(aPakVar.Find(*_S("wxyz"),kk,aPos)==0);
sl@0
   228
	test(aPos==5);
sl@0
   229
	test(aPakVar.Find(*_S("fgh"),kk,aPos)!=0);		// Returns non zero if string not found.
sl@0
   230
	test(aPos==6);								// Not present in list, aPos set to last position
sl@0
   231
	test(aPakVar.Find(*_S("ffff"),kk,aPos)==0);	
sl@0
   232
	test(aPos==2);
sl@0
   233
	test(aPakVar.Find(*_S("lmn"),kk,aPos)==0);
sl@0
   234
	test(aPos==4);			  //15
sl@0
   235
	test(aPakVar.FindIsq(*_S("abc"),kk,aPos)==0);
sl@0
   236
	test(aPos==1);
sl@0
   237
	test(aPakVar.FindIsq(*_S("aa"),kk1,aPos)==0);
sl@0
   238
	test(aPos==0);
sl@0
   239
	test(aPakVar.FindIsq(*_S("wxyz"),kk,aPos)==0);
sl@0
   240
	test(aPakVar.FindIsq(*_S("fgh"),kk,aPos)!=0);	// 22 Returns result of last test
sl@0
   241
	test(aPos==4);		// Not present in list, aPos set to last position tested
sl@0
   242
	//This test shows problem with BinarySearch
sl@0
   243
	TBuf<0x10> buf7=_L("fghij");
sl@0
   244
	test(aPakVar.InsertIsqL(*(TText*)buf7.Ptr(),buf7.Size(),kk)==4);
sl@0
   245
	test(aPakVar.FindIsq(*_S("fgh"),kk,aPos)==0);	// Returns result of last test
sl@0
   246
	test(aPos==4);
sl@0
   247
	test(aPakVar.FindIsq(*_S("ffff"),kk,aPos)==0);
sl@0
   248
	test(aPos==3);
sl@0
   249
	test(aPakVar.FindIsq(*_S("lmn"),kk,aPos)==0);
sl@0
   250
	test(aPos==5); 
sl@0
   251
	aPakVar.Delete(4,1);
sl@0
   252
	test(aPakVar.Find(*_S("wxyz"),kk,aPos)==0);
sl@0
   253
	test(aPos==5);
sl@0
   254
	}
sl@0
   255
sl@0
   256
LOCAL_C void test3(CArrayPak<TInt>& aPak)
sl@0
   257
sl@0
   258
	{
sl@0
   259
	test.Next(_L("InsertIsqL"));
sl@0
   260
	TKeyArrayPak kk(0,ECmpTInt);
sl@0
   261
sl@0
   262
	TInt pos=0;
sl@0
   263
	TInt mod=47;
sl@0
   264
	TInt inc=23;
sl@0
   265
sl@0
   266
	TInt i=0;	
sl@0
   267
	FOREVER
sl@0
   268
		{
sl@0
   269
		TInt ret;
sl@0
   270
		if (i&1)
sl@0
   271
			TRAP(ret,aPak.InsertIsqL(i,sizeof(TInt),kk))
sl@0
   272
		else
sl@0
   273
			{
sl@0
   274
			TRAP(ret,pos=aPak.InsertIsqL(i,sizeof(TInt),kk))
sl@0
   275
			if (ret==KErrNone)
sl@0
   276
				test(aPak[pos]==i);
sl@0
   277
			}
sl@0
   278
		if (ret==KErrAlreadyExists)
sl@0
   279
			break;
sl@0
   280
		i=(i+inc)%mod;
sl@0
   281
		}
sl@0
   282
	for(i=0;i<mod;i++)
sl@0
   283
		{
sl@0
   284
		pos=(-1);
sl@0
   285
		test(aPak.FindIsq(i,kk,pos)==0);
sl@0
   286
		test(pos==i);
sl@0
   287
		TRAPD(r,aPak.InsertIsqL(i,sizeof(TInt),kk))
sl@0
   288
		test(r==KErrAlreadyExists);
sl@0
   289
		}
sl@0
   290
	}
sl@0
   291
sl@0
   292
GLDEF_C TInt E32Main()
sl@0
   293
//
sl@0
   294
// Test the variable record length array classes.
sl@0
   295
// Initially test only covers the Flat type implementation.
sl@0
   296
//
sl@0
   297
    {
sl@0
   298
	test.Title();
sl@0
   299
	__UHEAP_MARK;
sl@0
   300
	test.Start(_L("class CArrayPakFlat"));
sl@0
   301
//
sl@0
   302
    CArrayPakFlat<TText>* pPakFlat=new CArrayPakFlat<TText>(KTestGranularity);
sl@0
   303
	testAllMethods(*pPakFlat);
sl@0
   304
    delete pPakFlat;
sl@0
   305
//
sl@0
   306
	CArrayPakFlat<TText>* pPakFlatChar=new CArrayPakFlat<TText>(KTestGranularity);
sl@0
   307
	test1(*pPakFlatChar);
sl@0
   308
	delete pPakFlatChar; 
sl@0
   309
//
sl@0
   310
 	CArrayPakFlat<TText>* pPakFlatArr=new CArrayPakFlat<TText>(KTestGranularity);
sl@0
   311
	test2(*pPakFlatArr);
sl@0
   312
	delete pPakFlatArr;
sl@0
   313
//
sl@0
   314
	CArrayPakFlat<TInt>* pPakFlatInt=new CArrayPakFlat<TInt>(KTestGranularity);
sl@0
   315
	test3(*pPakFlatInt);
sl@0
   316
	delete pPakFlatInt;
sl@0
   317
//
sl@0
   318
	test.End();
sl@0
   319
	__UHEAP_MARKEND;
sl@0
   320
	return(0);
sl@0
   321
    }
sl@0
   322