os/kernelhwsrv/kerneltest/e32test/buffer/t_varray.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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_varray.cpp
sl@0
    15
// Overview:
sl@0
    16
// Test variable record length array classes.
sl@0
    17
// API Information:
sl@0
    18
// CArrayVarFlat, CArrayVarSeg.
sl@0
    19
// Details:
sl@0
    20
// - Create an array of variable length text using a flat dynamic & segmented
sl@0
    21
// buffer and verify that:
sl@0
    22
// - number of elements held in the array is 0.
sl@0
    23
// - array is compressed and reset as expected.
sl@0
    24
// - the elements of the array are sorted as expected.
sl@0
    25
// - insertion of a text character into the array at specified position 
sl@0
    26
// and length of the array is as expected.
sl@0
    27
// - return value is 0 when available element is searched using sequential 
sl@0
    28
// search technique within the array.
sl@0
    29
// - removal of first element from the array is successful.
sl@0
    30
// - number of elements held in the array is 1 after appending a single 
sl@0
    31
// element at the end of empty array.
sl@0
    32
// - insertion of a single element with specified key is successful.
sl@0
    33
// - the element is found when searched using binary search technique
sl@0
    34
// - KErrAlreadyExists is returned if an element is inserted with the same 
sl@0
    35
// key already exists within the array.
sl@0
    36
// - Create an array of variable length text character implemented using a flat 
sl@0
    37
// dynamic & segmented buffer.
sl@0
    38
// - append some strings onto the end of the array, check the contents and 
sl@0
    39
// number of elements held in the array are as expected.
sl@0
    40
// - insert some strings and verify that the change in the content of array 
sl@0
    41
// and number of elements held in the array are as expected.
sl@0
    42
// - remove a single, multiple elements from the array and verify that the 
sl@0
    43
// Delete method is as expected.
sl@0
    44
// - Create an array of variable length text character contained within a flat 
sl@0
    45
// dynamic & segmented buffer.
sl@0
    46
// - append some strings of specified length onto the end of the array, compress 
sl@0
    47
// the array and verify that the number of elements held in the array is as specified.
sl@0
    48
// - insert a string at specified location, check the contents and reset the array.
sl@0
    49
// - append some strings at the end, insert some strings at specified position, 
sl@0
    50
// compress the array and verify that content, number of strings in the array 
sl@0
    51
// are as expected.
sl@0
    52
// - test that the number of elements and contents of the array are sorted as expected.
sl@0
    53
// - verify the correct position of the element and return value is zero when 
sl@0
    54
// an element is found using binary, sequential search technique and nonzero 
sl@0
    55
// if not present in the array.
sl@0
    56
// - insert some strings at the specified location and check that correct position
sl@0
    57
// is returned when searched using binary search technique.
sl@0
    58
// - Create an array of variable length integer contained within a flat dynamic &
sl@0
    59
// segmented buffer.
sl@0
    60
// - insert some elements with same key which is already present within the array 
sl@0
    61
// and check that KErrAlreadyExists is returned. 
sl@0
    62
// - Test whether the heap has been corrupted by all the tests.
sl@0
    63
// Platforms/Drives/Compatibility:
sl@0
    64
// All 
sl@0
    65
// Assumptions/Requirement/Pre-requisites:
sl@0
    66
// Failures and causes:
sl@0
    67
// Base Port information:
sl@0
    68
// 
sl@0
    69
//
sl@0
    70
sl@0
    71
#include <e32std.h>
sl@0
    72
#include <e32std_private.h>
sl@0
    73
#include <e32base.h>
sl@0
    74
#include <e32base_private.h>
sl@0
    75
#include <e32test.h>
sl@0
    76
#include <e32svr.h>
sl@0
    77
#include <e32ver.h>
sl@0
    78
sl@0
    79
const TInt KTestGranularity=0x02;
sl@0
    80
sl@0
    81
LOCAL_D RTest test(_L("T_VARRAY"));
sl@0
    82
sl@0
    83
LOCAL_C void testAllMethods(CArrayVar<TText>& aVar)
sl@0
    84
    {
sl@0
    85
	test.Next(_L("Test all methods"));
sl@0
    86
	test(aVar.Count()==0);
sl@0
    87
	aVar.Compress();
sl@0
    88
	test(TRUE);
sl@0
    89
	aVar.Reset();
sl@0
    90
	test(TRUE);
sl@0
    91
	TKeyArrayVar kk(sizeof(TText),ECmpNormal,0);
sl@0
    92
	test(TRUE);
sl@0
    93
	aVar.Sort(kk);
sl@0
    94
	test(TRUE);
sl@0
    95
	const TText* aa=_S("a");
sl@0
    96
	aVar.InsertL(0,*aa,sizeof(TText));
sl@0
    97
	test(aVar.Length(0)==sizeof(TText));
sl@0
    98
	test(TRUE);
sl@0
    99
	TInt pp;
sl@0
   100
	test(aVar.Find(*aa,kk,pp)==0);
sl@0
   101
	test(pp==0);
sl@0
   102
	aVar.Delete(0);
sl@0
   103
	aVar.AppendL(*aa,1);
sl@0
   104
	test(aVar.Count()==1);
sl@0
   105
	aVar.InsertIsqAllowDuplicatesL(*aa,0,kk);
sl@0
   106
	test(TRUE);
sl@0
   107
	test(aVar.FindIsq(*aa,kk,pp)==0);
sl@0
   108
	test(pp==0);
sl@0
   109
	TRAPD(r,aVar.InsertIsqL(*aa,0,kk));
sl@0
   110
	test(r==KErrAlreadyExists);
sl@0
   111
    }
sl@0
   112
sl@0
   113
LOCAL_C void test1(CArrayVar<TText>& aVar)
sl@0
   114
//
sl@0
   115
	{
sl@0
   116
	test.Next(_L("AppendL and InsertL chars"));
sl@0
   117
	aVar.AppendL(*_S("abcd"),5*sizeof(TText)); // abcd
sl@0
   118
	TBuf<0x10> des1(&aVar[0]);
sl@0
   119
	test(des1==_L("abcd"));
sl@0
   120
	test(aVar.Count()==1);
sl@0
   121
	aVar.AppendL(*_S("wxyz"),5*sizeof(TText)); // abcd wxyz
sl@0
   122
	des1=&aVar[1];
sl@0
   123
	test(des1==_L("wxyz"));
sl@0
   124
	test(aVar.Count()==2);
sl@0
   125
	aVar.InsertL(1,*_S("ef"),3*sizeof(TText)); // abcd ef wxyz
sl@0
   126
	des1=&aVar[1];
sl@0
   127
	test(des1==_L("ef"));
sl@0
   128
	test(aVar.Count()==3);	
sl@0
   129
	aVar.AppendL(*_S("z"),2*sizeof(TText)); // abcd ef wxyz z
sl@0
   130
	des1=&aVar[3];
sl@0
   131
	test(des1==_L("z"));
sl@0
   132
	aVar.InsertL(0,*_S("y"),2*sizeof(TText)); // y abcd ef wxyz z
sl@0
   133
	des1=&aVar[0];
sl@0
   134
	test(des1==_L("y"));
sl@0
   135
	test(aVar.Length(0)==2*sizeof(TText));
sl@0
   136
	test(aVar.Length(1)==5*sizeof(TText));
sl@0
   137
	test(aVar.Length(2)==3*sizeof(TText));
sl@0
   138
	test(aVar.Length(3)==5*sizeof(TText));
sl@0
   139
	test(aVar.Length(4)==2*sizeof(TText));
sl@0
   140
	des1=&aVar[1];
sl@0
   141
	test(des1==_L("abcd"));
sl@0
   142
	test(aVar.Count()==5);
sl@0
   143
//
sl@0
   144
	test.Next(_L("Delete chars"));
sl@0
   145
	aVar.Delete(3,1); // y abcd ef z
sl@0
   146
	des1=&aVar[2];
sl@0
   147
	test(des1==_L("ef"));
sl@0
   148
	des1=&aVar[1];
sl@0
   149
	test(des1==_L("abcd"));
sl@0
   150
	des1=&aVar[3];
sl@0
   151
	test(des1==_L("z"));
sl@0
   152
	aVar.Delete(1,2); // y z
sl@0
   153
	des1=&aVar[0];
sl@0
   154
	test(des1==_L("y"));
sl@0
   155
	des1=&aVar[1];
sl@0
   156
	test(des1==_L("z"));
sl@0
   157
	test(aVar.Count()==2);
sl@0
   158
	}
sl@0
   159
sl@0
   160
LOCAL_C void test2(CArrayVar<TText>& aVar)
sl@0
   161
//
sl@0
   162
	{
sl@0
   163
	test.Next(_L("Reset and Compress"));
sl@0
   164
	TBuf<0x10> des1(_L("abcde"));
sl@0
   165
	TBuf<0x10> des2(_L("fgh"));
sl@0
   166
	TBuf<0x10> des3(_L("wxyz"));
sl@0
   167
	aVar.AppendL(*(TText*)des1.Ptr(),des1.Size());
sl@0
   168
	aVar.AppendL(*(TText*)des2.Ptr(),des2.Size());
sl@0
   169
	aVar.Compress();
sl@0
   170
	test(aVar.Count()==2);
sl@0
   171
	TPtrC des4((TText*)&aVar[0],des1.Length());
sl@0
   172
	test(des1==des4);
sl@0
   173
	TPtrC des5((TText*)&aVar[1],des2.Length());
sl@0
   174
	test(des2==des5);
sl@0
   175
	aVar.InsertL(1,*(TText*)des3.Ptr(),des3.Size());
sl@0
   176
	test(aVar.Count()==3);	
sl@0
   177
	TPtrC des6((TText*)&aVar[0],des1.Length());
sl@0
   178
	test(des1==des6);
sl@0
   179
	TPtrC des7((TText*)&aVar[2],des2.Length());
sl@0
   180
	test(des2==des7);
sl@0
   181
	TPtrC des8((TText*)&aVar[1],des3.Length());
sl@0
   182
	test(des3==des8);
sl@0
   183
	aVar.Reset();
sl@0
   184
sl@0
   185
	TBuf<0x10> buf1=_L("abcdef");
sl@0
   186
	TBuf<0x10> buf2=_L("wxyz");
sl@0
   187
	TBuf<0x10> buf3=_L("lmnop");
sl@0
   188
	TBuf<0x10> buf4=_L("aa");
sl@0
   189
	aVar.AppendL(*buf1.Ptr(),buf1.Size());
sl@0
   190
	aVar.InsertL(0,*buf2.Ptr(),buf2.Size());
sl@0
   191
	aVar.AppendL(*buf3.Ptr(),buf3.Size());
sl@0
   192
	aVar.InsertL(1,*buf4.Ptr(),buf4.Size());
sl@0
   193
	aVar.Compress();
sl@0
   194
	TPtrC rd1((TText*)&aVar[2],buf1.Length());
sl@0
   195
	test(buf1==rd1);
sl@0
   196
	TPtrC rd2((TText*)&aVar[0],buf2.Length());
sl@0
   197
	test(buf2==rd2);
sl@0
   198
	TPtrC rd3((TText*)&aVar[3],buf3.Length());
sl@0
   199
	test(buf3==rd3);
sl@0
   200
	TPtrC rd4((TText*)&aVar[1],buf4.Length());
sl@0
   201
	test(buf4==rd4);
sl@0
   202
	test(aVar.Count()==4);
sl@0
   203
	
sl@0
   204
	TKeyArrayVar kk(0,ECmpNormal,3);		// Compare 3 characters
sl@0
   205
	TKeyArrayVar kk1(0,ECmpNormal,2);	// Compare 2 characters
sl@0
   206
	test.Next(_L("Sort"));
sl@0
   207
	aVar.Sort(kk);
sl@0
   208
	TPtrC rd5((TText*)&aVar[1],buf1.Length());
sl@0
   209
	test(buf1==rd5);
sl@0
   210
	TPtrC rd6((TText*)&aVar[3],buf2.Length());
sl@0
   211
	test(buf2==rd6);
sl@0
   212
	TPtrC rd7((TText*)&aVar[2],buf3.Length());
sl@0
   213
	test(buf3==rd7);
sl@0
   214
	TPtrC rd8((TText*)&aVar[0],buf4.Length());
sl@0
   215
	test(buf4==rd8);
sl@0
   216
	test(aVar.Count()==4);	
sl@0
   217
sl@0
   218
	test.Next(_L("Find and FindIsq"));
sl@0
   219
	TBuf<0x10> buf5=_L("ffff");
sl@0
   220
	test(aVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==2);
sl@0
   221
	TRAPD(r,aVar.InsertIsqL(*(TText*)buf5.Ptr(),buf5.Size(),kk))
sl@0
   222
	test(r==KErrAlreadyExists);
sl@0
   223
	test(aVar.InsertIsqAllowDuplicatesL(*(TText*)buf5.Ptr(),buf5.Size(),kk)==3);
sl@0
   224
	TInt aPos;
sl@0
   225
	test(aVar.Find(*_S("abc"),kk,aPos)==0);	// Second parameter 'aLength' is unused. 
sl@0
   226
	test(aPos==1);
sl@0
   227
	test(aVar.Find(*_S("aa"),kk1,aPos)==0);
sl@0
   228
	test(aPos==0);
sl@0
   229
	test(aVar.Find(*_S("wxyz"),kk,aPos)==0);
sl@0
   230
	test(aPos==5);
sl@0
   231
	test(aVar.Find(*_S("fgh"),kk,aPos)!=0);		// Returns !=0 if string not found.
sl@0
   232
	test(aPos==6);								// Not present in list, aPos set to last position
sl@0
   233
	test(aVar.Find(*_S("ffff"),kk,aPos)==0);	
sl@0
   234
	test(aPos==2);
sl@0
   235
	test(aVar.Find(*_S("lmn"),kk,aPos)==0);
sl@0
   236
	test(aPos==4);
sl@0
   237
	test(aVar.FindIsq(*_S("abc"),kk,aPos)==0);
sl@0
   238
	test(aPos==1);
sl@0
   239
	test(aVar.FindIsq(*_S("aa"),kk1,aPos)==0);
sl@0
   240
	test(aPos==0);
sl@0
   241
	test(aVar.FindIsq(*_S("wxyz"),kk,aPos)==0);
sl@0
   242
	test(aPos==5);
sl@0
   243
	test(aVar.FindIsq(*_S("fgh"),kk,aPos)!=0);	// Returns result of last test
sl@0
   244
	test(aPos==4);		  // Not present in list, aPos set to last position tested
sl@0
   245
	TBuf<0x10> buf7=_L("fgh");
sl@0
   246
	test(aVar.InsertIsqL(*(TText*)buf7.Ptr(),buf7.Size(),kk)==4);
sl@0
   247
	test(aVar.FindIsq(*_S("fgh"),kk,aPos)==0);	// Returns result of last test
sl@0
   248
	test(aPos==4);
sl@0
   249
									
sl@0
   250
	test(aVar.FindIsq(*_S("ffff"),kk,aPos)==0);
sl@0
   251
	test(aPos==3);
sl@0
   252
	test(aVar.FindIsq(*_S("lmn"),kk,aPos)==0);
sl@0
   253
	test(aPos==5); 
sl@0
   254
	}
sl@0
   255
sl@0
   256
LOCAL_C void test3(CArrayVar<TInt>& aVar)
sl@0
   257
	{
sl@0
   258
sl@0
   259
	test.Next(_L("InsertIsqL"));
sl@0
   260
	TKeyArrayVar 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,aVar.InsertIsqL(i,sizeof(TInt),kk))
sl@0
   272
		else
sl@0
   273
			{
sl@0
   274
			TRAP(ret,pos=aVar.InsertIsqL(i,sizeof(TInt),kk))
sl@0
   275
			if (ret==KErrNone)
sl@0
   276
				test(aVar[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(aVar.FindIsq(i,kk,pos)==0);
sl@0
   286
		test(pos==i);
sl@0
   287
		TRAPD(r,aVar.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
//
sl@0
   296
    {
sl@0
   297
sl@0
   298
	test.Title();
sl@0
   299
	__UHEAP_MARK;
sl@0
   300
	test.Start(_L("class CArrayFixFlat"));
sl@0
   301
//
sl@0
   302
	CArrayVarFlat<TText>* pVarFlat=new CArrayVarFlat<TText>(KTestGranularity);
sl@0
   303
	if (pVarFlat==NULL)
sl@0
   304
		test.Panic(_L("Allocating array"));
sl@0
   305
    testAllMethods(*pVarFlat);
sl@0
   306
    delete pVarFlat;
sl@0
   307
//
sl@0
   308
	CArrayVarFlat<TText>* pVarFlatChar=new CArrayVarFlat<TText>(KTestGranularity);
sl@0
   309
	test1(*pVarFlatChar);
sl@0
   310
	delete pVarFlatChar; 
sl@0
   311
//		
sl@0
   312
	CArrayVarFlat<TText>* pVarFlatArr=new CArrayVarFlat<TText>(KTestGranularity);
sl@0
   313
	test2(*pVarFlatArr);
sl@0
   314
	delete pVarFlatArr;
sl@0
   315
//
sl@0
   316
	CArrayVarFlat<TInt>* pVarFlatInt=new CArrayVarFlat<TInt>(KTestGranularity);
sl@0
   317
	test3(*pVarFlatInt);
sl@0
   318
	delete pVarFlatInt;
sl@0
   319
//	
sl@0
   320
	test.Next(_L("class CArrayVarSeg"));
sl@0
   321
	CArrayVarSeg<TText>* pVarSeg=new CArrayVarSeg<TText>(KTestGranularity);
sl@0
   322
	if (pVarSeg==NULL)
sl@0
   323
		test.Panic(_L("Allocating array"));
sl@0
   324
	testAllMethods(*pVarSeg);
sl@0
   325
	delete pVarSeg;
sl@0
   326
//
sl@0
   327
	CArrayVarSeg<TText>* pVarSegChar=new CArrayVarSeg<TText>(KTestGranularity);
sl@0
   328
	test1(*pVarSegChar);
sl@0
   329
	delete pVarSegChar;
sl@0
   330
//		
sl@0
   331
	CArrayVarSeg<TText>* pVarSegArr=new CArrayVarSeg<TText>(KTestGranularity);
sl@0
   332
	test2(*pVarSegArr);
sl@0
   333
	delete pVarSegArr;
sl@0
   334
//
sl@0
   335
	CArrayVarSeg<TInt>* pVarSegInt=new CArrayVarSeg<TInt>(KTestGranularity);
sl@0
   336
	test3(*pVarSegInt);
sl@0
   337
	delete pVarSegInt;
sl@0
   338
//
sl@0
   339
	test.End();
sl@0
   340
	__UHEAP_MARKEND;
sl@0
   341
	return(0);
sl@0
   342
    }
sl@0
   343