1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/buffer/t_circ.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,292 @@
1.4 +// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\buffer\t_circ.cpp
1.18 +// Overview:
1.19 +// Test methods of CCirBuffer and CCirBuf template class.
1.20 +// API Information:
1.21 +// CCirBuffer, CCirBuf.
1.22 +// Details:
1.23 +// - Create a circular buffer, add integers to the circular buffer and check that
1.24 +// they are added as specified.
1.25 +// - Verify Remove and Add work as expected.
1.26 +// - Check that all the added objects are removed.
1.27 +// - Create a circular buffer, add concrete data type object one, many at a time to the
1.28 +// circular buffer and check that they are added as specified.
1.29 +// - Remove the concrete data type object one, many at a time from the circular buffer
1.30 +// and check that they are removed as expected.
1.31 +// - Check that all the added objects are removed.
1.32 +// - Create a circular buffer, add 8-bit unsigned character object one, many at a time
1.33 +// to the circular buffer and check that they are added as specified.
1.34 +// - Remove multiple 8-bit unsigned character objects from the circular buffer and
1.35 +// check the added and removed objects are same.
1.36 +// - Create a circular buffer of unsigned integers, add single, multiple objects to the
1.37 +// buffer and check that they are added as specified.
1.38 +// - Remove multiple objects from the circular buffer and check the added and removed
1.39 +// objects are same.
1.40 +// - Create a circular buffer, add multiple signed integer objects to it, remove
1.41 +// the objects and verify that the added objects are removed.
1.42 +// - Create a circular buffer, add integers to the circular buffer and check that
1.43 +// they are added as specified.
1.44 +// - Verify Remove works as expected.
1.45 +// - Test whether the heap has been corrupted by all the tests.
1.46 +// Platforms/Drives/Compatibility:
1.47 +// All
1.48 +// Assumptions/Requirement/Pre-requisites:
1.49 +// Failures and causes:
1.50 +// Base Port information:
1.51 +//
1.52 +//
1.53 +
1.54 +#include <e32test.h>
1.55 +
1.56 +class VTester
1.57 + {
1.58 +public:
1.59 + VTester(){a=b=c=d=0;}
1.60 + VTester(TInt anInt){a=b=c=d=anInt;}
1.61 + VTester& operator=(TInt anInt){a=b=c=d=anInt;return *this;}
1.62 + TBool operator==(const VTester& aVal) const{if (a==aVal.a && b==aVal.b && c==aVal.c && d==aVal.d) return 1; else return 0;}
1.63 + TBool operator==(const TInt& aVal) const{if (a==aVal && b==aVal && c==aVal && d==aVal) return 1; else return 0;}
1.64 +public:
1.65 + TInt a;
1.66 + TInt b;
1.67 + TInt c;
1.68 + TInt d;
1.69 + };
1.70 +
1.71 +LOCAL_D RTest test(_L("T_CIRC"));
1.72 +LOCAL_D TText8* theCharArray=(TText8*)"abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!\xA3$%^&*()\0";
1.73 +
1.74 +LOCAL_C void TestInt()
1.75 +//
1.76 +// Test with unsigned integers
1.77 +//
1.78 + {
1.79 +
1.80 + CCirBuf<TInt>* cbInt=new CCirBuf<TInt>;
1.81 + TRAPD(ret,cbInt->SetLengthL(5));
1.82 + test(ret==KErrNone);
1.83 + TInt one(1);
1.84 + TInt two(2);
1.85 + TInt three(3);
1.86 + TInt four(4);
1.87 + TInt five(5);
1.88 + TInt six(6);
1.89 + TInt dummy;
1.90 + TInt numbers[]={7,8,9,10,11,12,13,14,15};
1.91 + TInt* numBuf=new TInt[10];
1.92 + test(numBuf!=NULL);
1.93 + test(cbInt->Add(&one)==1);
1.94 + test(cbInt->Add(&two)==1);
1.95 + test(cbInt->Add(&three)==1);
1.96 + test(cbInt->Add(&four)==1);
1.97 + test(cbInt->Add(&five)==1);
1.98 + one=two=three=four=0;
1.99 + test(cbInt->Add(&six)==0);
1.100 + test(cbInt->Remove(&one)==1);
1.101 + test(cbInt->Add(&six)==1);
1.102 + test(cbInt->Add(&dummy)==0);
1.103 + test(cbInt->Remove(&two)==1);
1.104 + test(cbInt->Remove(&three)==1);
1.105 + test(cbInt->Remove(&four)==1);
1.106 + test(one==1);
1.107 + test(two==2);
1.108 + test(three==3);
1.109 + test(four==4);
1.110 + test(cbInt->Add(numbers,6)==3);
1.111 + test(cbInt->Add(numbers,3)==0);
1.112 + test(cbInt->Remove(numBuf,7)==5);
1.113 + test(cbInt->Remove(numBuf,5)==0);
1.114 + for(TInt j(0);j<5;j++)
1.115 + test(numBuf[j]==j+5);
1.116 + delete [] numBuf;
1.117 + delete cbInt;
1.118 + }
1.119 +
1.120 +LOCAL_C void TestClass()
1.121 +//
1.122 +// Test with objects
1.123 +//
1.124 + {
1.125 +
1.126 + CCirBuf<VTester>* cbInt=new CCirBuf<VTester>;
1.127 + TRAPD(ret,cbInt->SetLengthL(5));
1.128 + test(ret==KErrNone);
1.129 + VTester one(1);
1.130 + VTester two(2);
1.131 + VTester three(3);
1.132 + VTester four(4);
1.133 + VTester five(5);
1.134 + VTester six(6);
1.135 + VTester dummy(0xffff);
1.136 + VTester numbers[]={7,8,9,10,11,12,13,14,15};
1.137 + VTester* numBuf=new VTester[10];
1.138 + test(numBuf!=NULL);
1.139 + test(cbInt->Add(&one)==1);
1.140 + test(cbInt->Add(&two)==1);
1.141 + test(cbInt->Add(&three)==1);
1.142 + test(cbInt->Add(&four)==1);
1.143 + test(cbInt->Add(&five)==1);
1.144 + one=two=three=four=0;
1.145 + test(cbInt->Add(&six)==0);
1.146 + test(cbInt->Remove(&one)==1);
1.147 + test(cbInt->Add(&six)==1);
1.148 + test(cbInt->Add(&dummy)==0);
1.149 + test(cbInt->Remove(&two)==1);
1.150 + test(cbInt->Remove(&three)==1);
1.151 + test(cbInt->Remove(&four)==1);
1.152 + test(one==1);
1.153 + test(two==2);
1.154 + test(three==3);
1.155 + test(four==4);
1.156 + test(cbInt->Add(numbers,6)==3);
1.157 + test(cbInt->Add(numbers,3)==0);
1.158 + test(cbInt->Remove(numBuf,7)==5);
1.159 + test(cbInt->Remove(numBuf,5)==0);
1.160 + for(TInt j(0);j<5;j++)
1.161 + test(numBuf[j]==j+5);
1.162 + delete [] numBuf;
1.163 + delete cbInt;
1.164 + }
1.165 +
1.166 +LOCAL_C void TestText()
1.167 +//
1.168 +// Test with text
1.169 +//
1.170 + {
1.171 +
1.172 + TInt i,j;
1.173 + TInt arraySize=User::StringLength(theCharArray);
1.174 + TText8* buf=new TText8[arraySize+1];
1.175 + Mem::FillZ(buf,arraySize);
1.176 + CCirBuf<TText8>* cbInt=new CCirBuf<TText8>;
1.177 + TRAPD(ret,cbInt->SetLengthL(arraySize+11));
1.178 + test(ret==KErrNone);
1.179 + for (i=0;i<10;i++)
1.180 + {
1.181 + test(cbInt->Add(theCharArray,arraySize)==arraySize);
1.182 + test(cbInt->Add(theCharArray+i)==1);
1.183 + test(cbInt->Remove(buf,arraySize)==arraySize);
1.184 + }
1.185 + TRAP(ret,cbInt->SetLengthL(arraySize*60));
1.186 + test(ret==KErrNone);
1.187 + for (j=0;j<10;j++)
1.188 + {
1.189 + for (i=0;i<9;i++)
1.190 + test(cbInt->Add(theCharArray,arraySize)==arraySize);
1.191 + for (i=0;i<arraySize;i++)
1.192 + test(cbInt->Add(theCharArray+i)==1);
1.193 + for (i=0;i<5;i++)
1.194 + {
1.195 + Mem::FillZ(buf,arraySize);
1.196 + test(cbInt->Remove(buf,arraySize)==arraySize);
1.197 + test(Mem::Compare(buf,arraySize,theCharArray,arraySize)==KErrNone);
1.198 + }
1.199 + }
1.200 + delete [] buf;
1.201 + delete cbInt;
1.202 + }
1.203 +
1.204 +void TestBuf()
1.205 +//
1.206 +// Test with buffers
1.207 +//
1.208 + {
1.209 +
1.210 + TInt i,j;
1.211 + TInt arraySize=User::StringLength(theCharArray);
1.212 + TText8* buf=new TText8[arraySize+1];
1.213 + Mem::FillZ(buf,arraySize);
1.214 + CCirBuffer* cbInt=new CCirBuffer;
1.215 + TRAPD(ret,cbInt->SetLengthL(arraySize+11));
1.216 + test(ret==KErrNone);
1.217 + for (i=0;i<10;i++)
1.218 + {
1.219 + test(cbInt->Add(theCharArray,arraySize)==arraySize);
1.220 + test(cbInt->Add(theCharArray+i)==1);
1.221 + test(cbInt->Remove(buf,arraySize)==arraySize);
1.222 + }
1.223 + TRAP(ret,cbInt->SetLengthL(arraySize*60));
1.224 + test(ret==KErrNone);
1.225 + for (j=0;j<10;j++)
1.226 + {
1.227 + for (i=0;i<9;i++)
1.228 + test(cbInt->Add(theCharArray,arraySize)==arraySize);
1.229 + for (i=0;i<arraySize;i++)
1.230 + test(cbInt->Add(theCharArray+i)==1);
1.231 + for (i=0;i<5;i++)
1.232 + {
1.233 + Mem::FillZ(buf,arraySize);
1.234 + test(cbInt->Remove(buf,arraySize)==arraySize);
1.235 + test(Mem::Compare(buf,arraySize,theCharArray,arraySize)==KErrNone);
1.236 + }
1.237 + }
1.238 + delete [] buf;
1.239 + delete cbInt;
1.240 + }
1.241 +
1.242 +LOCAL_C void TestRemove()
1.243 +//
1.244 +// Show remove bug (fixed in rel 050)
1.245 +//
1.246 +{
1.247 +
1.248 + CCirBuf<TInt>* cbInt=new CCirBuf<TInt>; TRAPD(ret,cbInt->SetLengthL(5));
1.249 + test(ret==KErrNone);
1.250 + TInt numbers[]={1,2,3,4,5};
1.251 + TInt* result=new TInt[5];
1.252 +
1.253 + test(cbInt->Add(numbers,5)==5);
1.254 + test(cbInt->Remove(result,2)==2);
1.255 + test(result[0]==1);
1.256 + test(result[1]==2);
1.257 + test(cbInt->Remove(result,3)==3);
1.258 + test(result[0]==3);
1.259 + test(result[1]==4);
1.260 + test(result[2]==5);
1.261 +
1.262 + delete [] result;
1.263 + delete cbInt;
1.264 + }
1.265 +
1.266 +
1.267 +TInt E32Main()
1.268 +//
1.269 +// Test CCirBuf<T>
1.270 +//
1.271 + {
1.272 +
1.273 + test.Title();
1.274 + __UHEAP_MARK;
1.275 +//
1.276 + test.Start(_L("Testing with built in Type"));
1.277 + TestInt();
1.278 +//
1.279 + test.Next(_L("Testing with concrete data type"));
1.280 + TestClass();
1.281 +//
1.282 + test.Next(_L("Testing with text"));
1.283 + TestText();
1.284 +//
1.285 + test.Next(_L("Testing character buffer"));
1.286 + TestBuf();
1.287 +//
1.288 + test.Next(_L("Testing Remove"));
1.289 + TestRemove();
1.290 +
1.291 + __UHEAP_MARKEND;
1.292 + test.End();
1.293 + return(KErrNone);
1.294 + }
1.295 +