symport/e32test/buffer/t_circ.cpp
changeset 1 0a7b44b10206
child 2 806186ab5e14
equal deleted inserted replaced
0:c55016431358 1:0a7b44b10206
       
     1 // Copyright (c) 1995-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 the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32test\buffer\t_circ.cpp
       
    15 // Overview:
       
    16 // Test methods of CCirBuffer and CCirBuf template class.
       
    17 // API Information:
       
    18 // CCirBuffer, CCirBuf.
       
    19 // Details:
       
    20 // - Create a circular buffer, add integers to the circular buffer and check that 
       
    21 // they are added as specified.
       
    22 // - Verify Remove and Add work as expected.
       
    23 // - Check that all the added objects are removed.
       
    24 // - Create a circular buffer, add concrete data type object one, many at a time to the 
       
    25 // circular buffer and check that they are added as specified.
       
    26 // - Remove the concrete data type object one, many at a time from the circular buffer
       
    27 // and check that they are removed as expected.
       
    28 // - Check that all the added objects are removed.
       
    29 // - Create a circular buffer, add 8-bit unsigned character object one, many at a time
       
    30 // to the circular buffer and check that they are added as specified.
       
    31 // - Remove multiple 8-bit unsigned character objects from the circular buffer and 
       
    32 // check the added and removed objects are same.
       
    33 // - Create a circular buffer of unsigned integers, add single, multiple objects to the 
       
    34 // buffer and check that they are added as specified.
       
    35 // - Remove multiple objects from the circular buffer and check the added and removed 
       
    36 // objects are same.
       
    37 // - Create a circular buffer, add multiple signed integer objects to it, remove 
       
    38 // the objects and verify that the added objects are removed.
       
    39 // - Create a circular buffer, add integers to the circular buffer and check that 
       
    40 // they are added as specified.
       
    41 // - Verify Remove works as expected.
       
    42 // - Test whether the heap has been corrupted by all the tests.
       
    43 // Platforms/Drives/Compatibility:
       
    44 // All 
       
    45 // Assumptions/Requirement/Pre-requisites:
       
    46 // Failures and causes:
       
    47 // Base Port information:
       
    48 // 
       
    49 //
       
    50 
       
    51 #include <e32test.h>
       
    52 
       
    53 class VTester
       
    54 	{
       
    55 public:
       
    56 	VTester(){a=b=c=d=0;}
       
    57 	VTester(TInt anInt){a=b=c=d=anInt;}
       
    58 	VTester& operator=(TInt anInt){a=b=c=d=anInt;return *this;}
       
    59 	TBool operator==(const VTester& aVal) const{if (a==aVal.a && b==aVal.b && c==aVal.c && d==aVal.d) return 1; else return 0;}
       
    60 	TBool operator==(const TInt& aVal) const{if (a==aVal && b==aVal && c==aVal && d==aVal) return 1; else return 0;}
       
    61 public:
       
    62 	TInt a;
       
    63 	TInt b;
       
    64 	TInt c;
       
    65 	TInt d;
       
    66 	};
       
    67 
       
    68 LOCAL_D RTest test(_L("T_CIRC"));
       
    69 LOCAL_D TText8* theCharArray=(TText8*)"abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!\xA3$%^&*()\0";
       
    70 
       
    71 LOCAL_C void TestInt()
       
    72 //
       
    73 // Test with unsigned integers
       
    74 //
       
    75 	{
       
    76 	
       
    77 	CCirBuf<TInt>* cbInt=new CCirBuf<TInt>;
       
    78 	TRAPD(ret,cbInt->SetLengthL(5));
       
    79 	test(ret==KErrNone);
       
    80 	TInt one(1);
       
    81 	TInt two(2);
       
    82 	TInt three(3);
       
    83 	TInt four(4);
       
    84 	TInt five(5);
       
    85 	TInt six(6);
       
    86 	TInt dummy;
       
    87 	TInt numbers[]={7,8,9,10,11,12,13,14,15};
       
    88 	TInt* numBuf=new TInt[10];
       
    89 	test(numBuf!=NULL);
       
    90 	test(cbInt->Add(&one)==1);
       
    91 	test(cbInt->Add(&two)==1);
       
    92 	test(cbInt->Add(&three)==1);
       
    93 	test(cbInt->Add(&four)==1);
       
    94 	test(cbInt->Add(&five)==1);
       
    95 	one=two=three=four=0;
       
    96 	test(cbInt->Add(&six)==0);
       
    97 	test(cbInt->Remove(&one)==1);
       
    98 	test(cbInt->Add(&six)==1);
       
    99 	test(cbInt->Add(&dummy)==0);
       
   100 	test(cbInt->Remove(&two)==1);
       
   101 	test(cbInt->Remove(&three)==1);
       
   102 	test(cbInt->Remove(&four)==1);
       
   103 	test(one==1);
       
   104 	test(two==2);
       
   105 	test(three==3);
       
   106 	test(four==4);
       
   107 	test(cbInt->Add(numbers,6)==3);
       
   108 	test(cbInt->Add(numbers,3)==0);
       
   109 	test(cbInt->Remove(numBuf,7)==5);
       
   110 	test(cbInt->Remove(numBuf,5)==0);
       
   111 	for(TInt j(0);j<5;j++)
       
   112 		test(numBuf[j]==j+5);
       
   113 	delete [] numBuf;
       
   114 	delete cbInt;
       
   115 	}
       
   116 
       
   117 LOCAL_C void TestClass()
       
   118 //
       
   119 // Test with objects
       
   120 //
       
   121 	{
       
   122 
       
   123 	CCirBuf<VTester>* cbInt=new CCirBuf<VTester>;
       
   124 	TRAPD(ret,cbInt->SetLengthL(5));
       
   125 	test(ret==KErrNone);
       
   126 	VTester one(1);
       
   127 	VTester two(2);
       
   128 	VTester three(3);
       
   129 	VTester four(4);
       
   130 	VTester five(5);
       
   131 	VTester six(6);
       
   132 	VTester dummy(0xffff);
       
   133 	VTester numbers[]={7,8,9,10,11,12,13,14,15};
       
   134 	VTester* numBuf=new VTester[10];
       
   135 	test(numBuf!=NULL);
       
   136 	test(cbInt->Add(&one)==1);
       
   137 	test(cbInt->Add(&two)==1);
       
   138 	test(cbInt->Add(&three)==1);
       
   139 	test(cbInt->Add(&four)==1);
       
   140 	test(cbInt->Add(&five)==1);
       
   141 	one=two=three=four=0;
       
   142 	test(cbInt->Add(&six)==0);
       
   143 	test(cbInt->Remove(&one)==1);
       
   144 	test(cbInt->Add(&six)==1);
       
   145 	test(cbInt->Add(&dummy)==0);
       
   146 	test(cbInt->Remove(&two)==1);
       
   147 	test(cbInt->Remove(&three)==1);
       
   148 	test(cbInt->Remove(&four)==1);
       
   149 	test(one==1);
       
   150 	test(two==2);
       
   151 	test(three==3);
       
   152 	test(four==4);
       
   153 	test(cbInt->Add(numbers,6)==3);
       
   154 	test(cbInt->Add(numbers,3)==0);
       
   155 	test(cbInt->Remove(numBuf,7)==5);
       
   156 	test(cbInt->Remove(numBuf,5)==0);
       
   157 	for(TInt j(0);j<5;j++)
       
   158 		test(numBuf[j]==j+5);
       
   159 	delete [] numBuf;
       
   160 	delete cbInt;
       
   161 	}
       
   162 
       
   163 LOCAL_C void TestText()
       
   164 //
       
   165 // Test with text
       
   166 //
       
   167 	{
       
   168 
       
   169 	TInt i,j;
       
   170 	TInt arraySize=User::StringLength(theCharArray);
       
   171 	TText8* buf=new TText8[arraySize+1];
       
   172 	Mem::FillZ(buf,arraySize);
       
   173 	CCirBuf<TText8>* cbInt=new CCirBuf<TText8>;
       
   174 	TRAPD(ret,cbInt->SetLengthL(arraySize+11));
       
   175 	test(ret==KErrNone);
       
   176 	for (i=0;i<10;i++)
       
   177 		{
       
   178 		test(cbInt->Add(theCharArray,arraySize)==arraySize);
       
   179 		test(cbInt->Add(theCharArray+i)==1);
       
   180 		test(cbInt->Remove(buf,arraySize)==arraySize);
       
   181 		}
       
   182 	TRAP(ret,cbInt->SetLengthL(arraySize*60));
       
   183 	test(ret==KErrNone);
       
   184 	for (j=0;j<10;j++)
       
   185 		{
       
   186 		for (i=0;i<9;i++)
       
   187 			test(cbInt->Add(theCharArray,arraySize)==arraySize);
       
   188 		for (i=0;i<arraySize;i++)
       
   189 			test(cbInt->Add(theCharArray+i)==1);
       
   190 		for (i=0;i<5;i++)
       
   191 			{
       
   192 			Mem::FillZ(buf,arraySize);
       
   193 			test(cbInt->Remove(buf,arraySize)==arraySize);
       
   194 			test(Mem::Compare(buf,arraySize,theCharArray,arraySize)==KErrNone);
       
   195 			}
       
   196 		}
       
   197 	delete [] buf;
       
   198 	delete cbInt;
       
   199 	}
       
   200 
       
   201 void TestBuf()
       
   202 //
       
   203 // Test with buffers
       
   204 //
       
   205 	{
       
   206 
       
   207 	TInt i,j;
       
   208 	TInt arraySize=User::StringLength(theCharArray);
       
   209 	TText8* buf=new TText8[arraySize+1];
       
   210 	Mem::FillZ(buf,arraySize);
       
   211 	CCirBuffer* cbInt=new CCirBuffer;
       
   212 	TRAPD(ret,cbInt->SetLengthL(arraySize+11));
       
   213 	test(ret==KErrNone);
       
   214 	for (i=0;i<10;i++)
       
   215 		{
       
   216 		test(cbInt->Add(theCharArray,arraySize)==arraySize);
       
   217 		test(cbInt->Add(theCharArray+i)==1);
       
   218 		test(cbInt->Remove(buf,arraySize)==arraySize);
       
   219 		}
       
   220 	TRAP(ret,cbInt->SetLengthL(arraySize*60));
       
   221 	test(ret==KErrNone);
       
   222 	for (j=0;j<10;j++)
       
   223 		{
       
   224 		for (i=0;i<9;i++)
       
   225 			test(cbInt->Add(theCharArray,arraySize)==arraySize);
       
   226 		for (i=0;i<arraySize;i++)
       
   227 			test(cbInt->Add(theCharArray+i)==1);
       
   228 		for (i=0;i<5;i++)
       
   229 			{
       
   230 			Mem::FillZ(buf,arraySize);
       
   231 			test(cbInt->Remove(buf,arraySize)==arraySize);
       
   232 			test(Mem::Compare(buf,arraySize,theCharArray,arraySize)==KErrNone);
       
   233 			}
       
   234 		}
       
   235 	delete [] buf;
       
   236 	delete cbInt;
       
   237 	}
       
   238 
       
   239 LOCAL_C void TestRemove()
       
   240 //
       
   241 // Show remove bug (fixed in rel 050)
       
   242 //
       
   243 {
       
   244 
       
   245 	CCirBuf<TInt>* cbInt=new CCirBuf<TInt>; TRAPD(ret,cbInt->SetLengthL(5));
       
   246 	test(ret==KErrNone);
       
   247 	TInt numbers[]={1,2,3,4,5};
       
   248 	TInt* result=new TInt[5];
       
   249 
       
   250 	test(cbInt->Add(numbers,5)==5);
       
   251 	test(cbInt->Remove(result,2)==2);
       
   252 	test(result[0]==1);
       
   253 	test(result[1]==2);
       
   254 	test(cbInt->Remove(result,3)==3);
       
   255 	test(result[0]==3);
       
   256 	test(result[1]==4);
       
   257 	test(result[2]==5);
       
   258 
       
   259 	delete [] result;
       
   260 	delete cbInt;
       
   261 	}
       
   262 
       
   263 
       
   264 TInt E32Main()
       
   265 //
       
   266 // Test CCirBuf<T>
       
   267 //
       
   268     {
       
   269 
       
   270 	test.Title();
       
   271 	__UHEAP_MARK;
       
   272 //
       
   273 	test.Start(_L("Testing with built in Type"));
       
   274 	TestInt();
       
   275 //
       
   276 	test.Next(_L("Testing with concrete data type"));
       
   277 	TestClass();
       
   278 //	
       
   279 	test.Next(_L("Testing with text"));
       
   280 	TestText();
       
   281 //
       
   282 	test.Next(_L("Testing character buffer"));
       
   283 	TestBuf();
       
   284 //
       
   285 	test.Next(_L("Testing Remove"));
       
   286 	TestRemove();
       
   287 
       
   288 	__UHEAP_MARKEND;
       
   289 	test.End();
       
   290 	return(KErrNone);
       
   291 	}
       
   292