1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/buffer/int_arr.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,1177 @@
1.4 +// Copyright (c) 1994-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\int_arr.cpp
1.18 +//
1.19 +//
1.20 +
1.21 +#include <e32test.h>
1.22 +#include <e32math.h>
1.23 +
1.24 +#define NUM_TESTS 500
1.25 +
1.26 +GLREF_D RTest test;
1.27 +GLREF_C TInt Random();
1.28 +
1.29 +struct SInt
1.30 + {
1.31 + SInt(TInt a) {i=a;}
1.32 + operator TInt() {return i;}
1.33 +
1.34 + TInt i;
1.35 + };
1.36 +
1.37 +LOCAL_C TInt IntAppendAndAccessTest(TInt aCount, TInt aNumTests, TInt aRange)
1.38 + {
1.39 + TInt n;
1.40 + for (n=0; n<aNumTests; n++)
1.41 + {
1.42 + RArray<TInt> a;
1.43 + TInt *pA=(TInt*)User::Alloc(aCount*sizeof(TInt));
1.44 + if (!pA)
1.45 + {
1.46 + a.Close();
1.47 + return -65535;
1.48 + }
1.49 + TInt i;
1.50 + for (i=0; i<aCount; i++)
1.51 + {
1.52 + TInt x=Random()&aRange;
1.53 + a.Append(x);
1.54 + pA[i]=x;
1.55 + }
1.56 + if (a.Count()!=aCount)
1.57 + {
1.58 + a.Close();
1.59 + return -1;
1.60 + }
1.61 + for (i=0; i<aCount; i++)
1.62 + {
1.63 + if (a[i]!=pA[i])
1.64 + {
1.65 + a.Close();
1.66 + return -2;
1.67 + }
1.68 + TInt x=Random()&aRange;
1.69 + a[i]=x;
1.70 + pA[i]=x;
1.71 + }
1.72 + if (a.Count()!=aCount)
1.73 + {
1.74 + a.Close();
1.75 + return -3;
1.76 + }
1.77 + for (i=0; i<aCount; i++)
1.78 + {
1.79 + if (a[i]!=pA[i])
1.80 + {
1.81 + a.Close();
1.82 + return -4;
1.83 + }
1.84 + }
1.85 + delete pA;
1.86 + a.Close();
1.87 + }
1.88 + return KErrNone;
1.89 + }
1.90 +
1.91 +LOCAL_C TInt IntRemoveTest()
1.92 + {
1.93 + TInt m=32;
1.94 + TInt n=m*m+1;
1.95 + RArray<TInt> a;
1.96 + TInt i;
1.97 + for (i=0; i<n; i++)
1.98 + {
1.99 + a.Append(i);
1.100 + }
1.101 + TInt p=2;
1.102 + for (i=2; i<=m; i++)
1.103 + {
1.104 + TInt j;
1.105 + for (j=0; j<(2*i-2); j++)
1.106 + {
1.107 + a.Remove(p);
1.108 + }
1.109 + p++;
1.110 + }
1.111 + if (a.Count()!=m+1)
1.112 + {
1.113 + a.Close();
1.114 + return -1;
1.115 + }
1.116 + for (i=0; i<m; i++)
1.117 + {
1.118 + if (a[i]!=i*i)
1.119 + {
1.120 + a.Close();
1.121 + return -2;
1.122 + }
1.123 + }
1.124 + a.Close();
1.125 + return KErrNone;
1.126 + }
1.127 +
1.128 +LOCAL_C TInt IntFindTest(TInt aCount, TInt aNumTests, TInt aRange)
1.129 + {
1.130 + TInt n;
1.131 + for (n=0; n<aNumTests; n++)
1.132 + {
1.133 + RArray<TInt> a;
1.134 + TInt *pA=(TInt*)User::Alloc(aCount*sizeof(TInt));
1.135 + if (!pA)
1.136 + {
1.137 + a.Close();
1.138 + return -65535;
1.139 + }
1.140 + TInt i;
1.141 + for (i=0; i<aCount; i++)
1.142 + {
1.143 + TInt x=Random()&aRange;
1.144 + a.Append(x);
1.145 + pA[i]=x;
1.146 + }
1.147 + if (a.Count()!=aCount)
1.148 + {
1.149 + a.Close();
1.150 + return -1;
1.151 + }
1.152 + for (i=0; i<aCount; i++)
1.153 + {
1.154 + TInt r=a.Find(pA[i]);
1.155 + if (r<0 || pA[r]!=pA[i] || r>i)
1.156 + {
1.157 + a.Close();
1.158 + return -2;
1.159 + }
1.160 + TInt x=Random()&aRange;
1.161 + r=a.Find(x);
1.162 + if (r<0)
1.163 + {
1.164 + TInt j;
1.165 + for (j=0; j<aCount; j++)
1.166 + {
1.167 + if (pA[j]==x)
1.168 + {
1.169 + a.Close();
1.170 + return -3;
1.171 + }
1.172 + }
1.173 + }
1.174 + else if (pA[r]!=x)
1.175 + {
1.176 + a.Close();
1.177 + return -4;
1.178 + }
1.179 + else
1.180 + {
1.181 + TInt j;
1.182 + for (j=0; j<r; j++)
1.183 + {
1.184 + if (pA[j]==x)
1.185 + {
1.186 + a.Close();
1.187 + return -5;
1.188 + }
1.189 + }
1.190 + }
1.191 + }
1.192 + delete pA;
1.193 + a.Close();
1.194 + }
1.195 + return KErrNone;
1.196 + }
1.197 +
1.198 +LOCAL_C TInt IntFindInOrderTest(TInt aCount, TInt aNumTests, TInt aRange)
1.199 +// require aRange*aCount<2^32
1.200 + {
1.201 + TInt n;
1.202 + for (n=0; n<aNumTests; n++)
1.203 + {
1.204 + RArray<TInt> a;
1.205 + TInt *pA=(TInt*)User::Alloc(aCount*sizeof(TInt));
1.206 + if (!pA)
1.207 + {
1.208 + a.Close();
1.209 + return -65535;
1.210 + }
1.211 + TInt i=0;
1.212 + TInt y=-256;
1.213 + for(i=0; i<aCount; i++)
1.214 + {
1.215 + TInt x=Random()&aRange; // this is always >=0
1.216 + a.Append(y);
1.217 + pA[i]=y;
1.218 + y+=x;
1.219 + }
1.220 + if (a.Count()!=aCount)
1.221 + {
1.222 + a.Close();
1.223 + return -1;
1.224 + }
1.225 + for (i=0; i<aCount; i++)
1.226 + {
1.227 + TInt r=a.FindInOrder(pA[i]);
1.228 + if (r<0 || pA[r]!=pA[i])
1.229 + {
1.230 + a.Close();
1.231 + return -2;
1.232 + }
1.233 + TInt x=Random()&aRange;
1.234 + r=a.FindInOrder(x);
1.235 + if (r<0)
1.236 + {
1.237 + TInt j;
1.238 + for (j=0; j<aCount; j++)
1.239 + {
1.240 + if (pA[j]==x)
1.241 + {
1.242 + a.Close();
1.243 + return -3;
1.244 + }
1.245 + }
1.246 + }
1.247 + else if (pA[r]!=x)
1.248 + {
1.249 + a.Close();
1.250 + return -4;
1.251 + }
1.252 + }
1.253 + delete pA;
1.254 + a.Close();
1.255 + }
1.256 + return KErrNone;
1.257 + }
1.258 +
1.259 +LOCAL_C TInt IntFindInOrderTest2()
1.260 + {
1.261 + TInt i;
1.262 + RArray<TInt> sq;
1.263 + for (i=0; i<1024; i++)
1.264 + {
1.265 + TInt j=i*i;
1.266 + sq.Append(j);
1.267 + }
1.268 + for (i=0; i<10000; i++)
1.269 + {
1.270 + TInt x=Random()&1023;
1.271 + TInt y=x*x;
1.272 + TInt r=sq.FindInOrder(y);
1.273 + if (r!=x)
1.274 + {
1.275 + sq.Close();
1.276 + return -1;
1.277 + }
1.278 + }
1.279 + sq.Close();
1.280 + return 0;
1.281 + }
1.282 +
1.283 +LOCAL_C TInt IntInsertInOrderTest(TInt aCount, TInt aNumTests, TInt aRange)
1.284 + {
1.285 + TInt n;
1.286 + for (n=0; n<aNumTests; n++)
1.287 + {
1.288 + RArray<TInt> a;
1.289 + RArray<TInt> b;
1.290 + RArray<TInt> c;
1.291 + TInt i;
1.292 + TInt cc=0;
1.293 + for (i=0; i<aCount; i++)
1.294 + {
1.295 + TInt x=Random()&aRange;
1.296 + a.Append(x);
1.297 + b.InsertInOrderAllowRepeats(x);
1.298 + TInt r=c.InsertInOrder(x);
1.299 + if (r==KErrNone)
1.300 + cc++;
1.301 + }
1.302 + if (a.Count()!=aCount)
1.303 + {
1.304 + a.Close();
1.305 + b.Close();
1.306 + c.Close();
1.307 + return -1;
1.308 + }
1.309 + if (b.Count()!=aCount)
1.310 + {
1.311 + a.Close();
1.312 + b.Close();
1.313 + c.Close();
1.314 + return -2;
1.315 + }
1.316 + for (i=0; i<aCount-1; i++)
1.317 + {
1.318 + if (b[i]>b[i+1])
1.319 + {
1.320 + a.Close();
1.321 + b.Close();
1.322 + c.Close();
1.323 + return -3;
1.324 + }
1.325 + }
1.326 + for (i=0; i<aCount; i++)
1.327 + {
1.328 + if (a.Find(b[i])<0)
1.329 + {
1.330 + a.Close();
1.331 + b.Close();
1.332 + c.Close();
1.333 + return -4;
1.334 + }
1.335 + if (b.Find(a[i])<0)
1.336 + {
1.337 + a.Close();
1.338 + b.Close();
1.339 + c.Close();
1.340 + return -5;
1.341 + }
1.342 + if (c.Find(a[i])<0)
1.343 + {
1.344 + a.Close();
1.345 + b.Close();
1.346 + c.Close();
1.347 + return -6;
1.348 + }
1.349 + }
1.350 + if (c.Count()!=cc)
1.351 + {
1.352 + a.Close();
1.353 + b.Close();
1.354 + c.Close();
1.355 + return -7;
1.356 + }
1.357 + for (i=0; i<c.Count()-1; i++)
1.358 + {
1.359 + if (c[i]>=c[i+1])
1.360 + {
1.361 + a.Close();
1.362 + b.Close();
1.363 + c.Close();
1.364 + return -8;
1.365 + }
1.366 + if (a.Find(c[i])<0)
1.367 + {
1.368 + a.Close();
1.369 + b.Close();
1.370 + c.Close();
1.371 + return -9;
1.372 + }
1.373 + }
1.374 + a.Close();
1.375 + b.Close();
1.376 + c.Close();
1.377 + }
1.378 + return KErrNone;
1.379 + }
1.380 +
1.381 +LOCAL_C TInt IntInsertInOrderTest2()
1.382 + {
1.383 + TInt i;
1.384 + RArray<TInt> sq;
1.385 + for (i=0; i<1024; i++)
1.386 + {
1.387 + TInt j=i*i;
1.388 + sq.InsertInOrder(j);
1.389 + sq.InsertInOrder(-j);
1.390 + }
1.391 + if (sq.Count()!=2047)
1.392 + {
1.393 + sq.Close();
1.394 + return -1;
1.395 + }
1.396 + for (i=0; i<2047; i++)
1.397 + {
1.398 + TInt y=0;
1.399 + if (i<1023)
1.400 + y=-(1023-i)*(1023-i);
1.401 + else if (i==1023)
1.402 + y=0;
1.403 + else if (i>1023)
1.404 + y=(i-1023)*(i-1023);
1.405 + if (sq[i]!=y)
1.406 + {
1.407 + sq.Close();
1.408 + return -2;
1.409 + }
1.410 + }
1.411 + sq.Close();
1.412 + return 0;
1.413 + }
1.414 +
1.415 +LOCAL_C TInt IntSortTest(TInt aCount, TInt aNumTests, TInt aRange)
1.416 + {
1.417 + TInt n;
1.418 + for (n=0; n<aNumTests; n++)
1.419 + {
1.420 + RArray<TInt> a;
1.421 + RArray<TInt> b;
1.422 + TInt i;
1.423 + for (i=0; i<aCount; i++)
1.424 + {
1.425 + TInt x=Random()&aRange;
1.426 + a.Append(x);
1.427 + b.InsertInOrderAllowRepeats(x);
1.428 + }
1.429 + a.Sort();
1.430 + if (a.Count()!=aCount)
1.431 + {
1.432 + a.Close();
1.433 + b.Close();
1.434 + return -1;
1.435 + }
1.436 + if (b.Count()!=aCount)
1.437 + {
1.438 + a.Close();
1.439 + b.Close();
1.440 + return -2;
1.441 + }
1.442 + for (i=0; i<aCount; i++)
1.443 + {
1.444 + if (a[i]!=b[i])
1.445 + {
1.446 + a.Close();
1.447 + b.Close();
1.448 + return -3;
1.449 + }
1.450 + }
1.451 + a.Close();
1.452 + b.Close();
1.453 + }
1.454 + return KErrNone;
1.455 + }
1.456 +
1.457 +LOCAL_C TInt SIntAppendAndAccessTest(TInt aCount, TInt aNumTests, TInt aRange)
1.458 + {
1.459 + TInt n;
1.460 + for (n=0; n<aNumTests; n++)
1.461 + {
1.462 + RArray<SInt> a;
1.463 + TInt *pA=(TInt*)User::Alloc(aCount*sizeof(TInt));
1.464 + if (!pA)
1.465 + {
1.466 + a.Close();
1.467 + return -65535;
1.468 + }
1.469 + TInt i;
1.470 + for (i=0; i<aCount; i++)
1.471 + {
1.472 + TInt x=Random()&aRange;
1.473 + a.Append(x);
1.474 + pA[i]=x;
1.475 + }
1.476 + if (a.Count()!=aCount)
1.477 + {
1.478 + a.Close();
1.479 + return -1;
1.480 + }
1.481 + for (i=0; i<aCount; i++)
1.482 + {
1.483 + if (a[i]!=pA[i])
1.484 + {
1.485 + a.Close();
1.486 + return -2;
1.487 + }
1.488 + TInt x=Random()&aRange;
1.489 + a[i]=x;
1.490 + pA[i]=x;
1.491 + }
1.492 + if (a.Count()!=aCount)
1.493 + {
1.494 + a.Close();
1.495 + return -3;
1.496 + }
1.497 + for (i=0; i<aCount; i++)
1.498 + {
1.499 + if (a[i]!=pA[i])
1.500 + {
1.501 + a.Close();
1.502 + return -4;
1.503 + }
1.504 + }
1.505 + delete pA;
1.506 + a.Close();
1.507 + }
1.508 + return KErrNone;
1.509 + }
1.510 +
1.511 +LOCAL_C TInt SIntRemoveTest()
1.512 + {
1.513 + TInt m=32;
1.514 + TInt n=m*m+1;
1.515 + RArray<SInt> a;
1.516 + TInt i;
1.517 + for (i=0; i<n; i++)
1.518 + {
1.519 + a.Append(i);
1.520 + }
1.521 + TInt p=2;
1.522 + for (i=2; i<=m; i++)
1.523 + {
1.524 + TInt j;
1.525 + for (j=0; j<(2*i-2); j++)
1.526 + {
1.527 + a.Remove(p);
1.528 + }
1.529 + p++;
1.530 + }
1.531 + if (a.Count()!=m+1)
1.532 + {
1.533 + a.Close();
1.534 + return -1;
1.535 + }
1.536 + for (i=0; i<m; i++)
1.537 + {
1.538 + if (a[i]!=i*i)
1.539 + {
1.540 + a.Close();
1.541 + return -2;
1.542 + }
1.543 + }
1.544 + a.Close();
1.545 + return KErrNone;
1.546 + }
1.547 +
1.548 +LOCAL_C TInt SIntFindTest(TInt aCount, TInt aNumTests, TInt aRange)
1.549 + {
1.550 + TInt n;
1.551 + for (n=0; n<aNumTests; n++)
1.552 + {
1.553 + RArray<SInt> a;
1.554 + TInt *pA=(TInt*)User::Alloc(aCount*sizeof(TInt));
1.555 + if (!pA)
1.556 + {
1.557 + a.Close();
1.558 + return -65535;
1.559 + }
1.560 + TInt i;
1.561 + for (i=0; i<aCount; i++)
1.562 + {
1.563 + TInt x=Random()&aRange;
1.564 + a.Append(x);
1.565 + pA[i]=x;
1.566 + }
1.567 + if (a.Count()!=aCount)
1.568 + {
1.569 + a.Close();
1.570 + return -1;
1.571 + }
1.572 + for (i=0; i<aCount; i++)
1.573 + {
1.574 + TInt r=a.Find(pA[i]);
1.575 + if (r<0 || pA[r]!=pA[i] || r>i)
1.576 + {
1.577 + a.Close();
1.578 + return -2;
1.579 + }
1.580 + TInt x=Random()&aRange;
1.581 + r=a.Find(x);
1.582 + if (r<0)
1.583 + {
1.584 + TInt j;
1.585 + for (j=0; j<aCount; j++)
1.586 + {
1.587 + if (pA[j]==x)
1.588 + {
1.589 + a.Close();
1.590 + return -3;
1.591 + }
1.592 + }
1.593 + }
1.594 + else if (pA[r]!=x)
1.595 + {
1.596 + a.Close();
1.597 + return -4;
1.598 + }
1.599 + else
1.600 + {
1.601 + TInt j;
1.602 + for (j=0; j<r; j++)
1.603 + {
1.604 + if (pA[j]==x)
1.605 + {
1.606 + a.Close();
1.607 + return -5;
1.608 + }
1.609 + }
1.610 + }
1.611 + }
1.612 + delete pA;
1.613 + a.Close();
1.614 + }
1.615 + return KErrNone;
1.616 + }
1.617 +
1.618 +LOCAL_C TInt SIntFindInOrderTest(TInt aCount, TInt aNumTests, TInt aRange)
1.619 +// require aRange*aCount<2^32
1.620 + {
1.621 + TInt n;
1.622 + for (n=0; n<aNumTests; n++)
1.623 + {
1.624 + RArray<SInt> a;
1.625 + TInt *pA=(TInt*)User::Alloc(aCount*sizeof(TInt));
1.626 + if (!pA)
1.627 + {
1.628 + a.Close();
1.629 + return -65535;
1.630 + }
1.631 + TInt i=0;
1.632 + TInt y=-256;
1.633 + for(i=0; i<aCount; i++)
1.634 + {
1.635 + TInt x=Random()&aRange; // this is always >=0
1.636 + a.Append(y);
1.637 + pA[i]=y;
1.638 + y+=x;
1.639 + }
1.640 + if (a.Count()!=aCount)
1.641 + {
1.642 + a.Close();
1.643 + return -1;
1.644 + }
1.645 + for (i=0; i<aCount; i++)
1.646 + {
1.647 + TInt r=a.FindInSignedKeyOrder(pA[i]);
1.648 + if (r<0 || pA[r]!=pA[i])
1.649 + {
1.650 + a.Close();
1.651 + return -2;
1.652 + }
1.653 + TInt x=Random()&aRange;
1.654 + r=a.FindInSignedKeyOrder(x);
1.655 + if (r<0)
1.656 + {
1.657 + TInt j;
1.658 + for (j=0; j<aCount; j++)
1.659 + {
1.660 + if (pA[j]==x)
1.661 + {
1.662 + a.Close();
1.663 + return -3;
1.664 + }
1.665 + }
1.666 + }
1.667 + else if (pA[r]!=x)
1.668 + {
1.669 + a.Close();
1.670 + return -4;
1.671 + }
1.672 + }
1.673 + delete pA;
1.674 + a.Close();
1.675 + }
1.676 + return KErrNone;
1.677 + }
1.678 +
1.679 +LOCAL_C TInt SIntFindInOrderTest2()
1.680 + {
1.681 + TInt i;
1.682 + RArray<SInt> sq;
1.683 + for (i=0; i<1024; i++)
1.684 + {
1.685 + TInt j=i*i;
1.686 + sq.Append(j);
1.687 + }
1.688 + for (i=0; i<10000; i++)
1.689 + {
1.690 + TInt x=Random()&1023;
1.691 + TInt y=x*x;
1.692 + TInt r=sq.FindInSignedKeyOrder(y);
1.693 + if (r!=x)
1.694 + {
1.695 + sq.Close();
1.696 + return -1;
1.697 + }
1.698 + }
1.699 + sq.Close();
1.700 + return 0;
1.701 + }
1.702 +
1.703 +LOCAL_C TInt SIntInsertInOrderTest(TInt aCount, TInt aNumTests, TInt aRange)
1.704 + {
1.705 + TInt n;
1.706 + for (n=0; n<aNumTests; n++)
1.707 + {
1.708 + RArray<SInt> a;
1.709 + RArray<SInt> b;
1.710 + RArray<SInt> c;
1.711 + TInt i;
1.712 + TInt cc=0;
1.713 + for (i=0; i<aCount; i++)
1.714 + {
1.715 + TInt x=Random()&aRange;
1.716 + a.Append(x);
1.717 + b.InsertInSignedKeyOrderAllowRepeats(x);
1.718 + TInt r=c.InsertInSignedKeyOrder(x);
1.719 + if (r==KErrNone)
1.720 + cc++;
1.721 + }
1.722 + if (a.Count()!=aCount)
1.723 + {
1.724 + a.Close();
1.725 + b.Close();
1.726 + c.Close();
1.727 + return -1;
1.728 + }
1.729 + if (b.Count()!=aCount)
1.730 + {
1.731 + a.Close();
1.732 + b.Close();
1.733 + c.Close();
1.734 + return -2;
1.735 + }
1.736 + for (i=0; i<aCount-1; i++)
1.737 + {
1.738 + if (b[i]>b[i+1])
1.739 + {
1.740 + a.Close();
1.741 + b.Close();
1.742 + c.Close();
1.743 + return -3;
1.744 + }
1.745 + }
1.746 + for (i=0; i<aCount; i++)
1.747 + {
1.748 + if (a.Find(b[i])<0)
1.749 + {
1.750 + a.Close();
1.751 + b.Close();
1.752 + c.Close();
1.753 + return -4;
1.754 + }
1.755 + if (b.Find(a[i])<0)
1.756 + {
1.757 + a.Close();
1.758 + b.Close();
1.759 + c.Close();
1.760 + return -5;
1.761 + }
1.762 + if (c.Find(a[i])<0)
1.763 + {
1.764 + a.Close();
1.765 + b.Close();
1.766 + c.Close();
1.767 + return -6;
1.768 + }
1.769 + }
1.770 + if (c.Count()!=cc)
1.771 + {
1.772 + a.Close();
1.773 + b.Close();
1.774 + c.Close();
1.775 + return -7;
1.776 + }
1.777 + for (i=0; i<c.Count()-1; i++)
1.778 + {
1.779 + if (c[i]>=c[i+1])
1.780 + {
1.781 + a.Close();
1.782 + b.Close();
1.783 + c.Close();
1.784 + return -8;
1.785 + }
1.786 + if (a.Find(c[i])<0)
1.787 + {
1.788 + a.Close();
1.789 + b.Close();
1.790 + c.Close();
1.791 + return -9;
1.792 + }
1.793 + }
1.794 + a.Close();
1.795 + b.Close();
1.796 + c.Close();
1.797 + }
1.798 + return KErrNone;
1.799 + }
1.800 +
1.801 +LOCAL_C TInt SIntInsertInOrderTest2()
1.802 + {
1.803 + TInt i;
1.804 + RArray<SInt> sq;
1.805 + for (i=0; i<1024; i++)
1.806 + {
1.807 + TInt j=i*i;
1.808 + sq.InsertInSignedKeyOrder(j);
1.809 + sq.InsertInSignedKeyOrder(-j);
1.810 + }
1.811 + if (sq.Count()!=2047)
1.812 + {
1.813 + sq.Close();
1.814 + return -1;
1.815 + }
1.816 + for (i=0; i<2047; i++)
1.817 + {
1.818 + TInt y=0;
1.819 + if (i<1023)
1.820 + y=-(1023-i)*(1023-i);
1.821 + else if (i==1023)
1.822 + y=0;
1.823 + else if (i>1023)
1.824 + y=(i-1023)*(i-1023);
1.825 + if (sq[i]!=y)
1.826 + {
1.827 + sq.Close();
1.828 + return -2;
1.829 + }
1.830 + }
1.831 + sq.Close();
1.832 + return 0;
1.833 + }
1.834 +
1.835 +LOCAL_C TInt SIntSortTest(TInt aCount, TInt aNumTests, TInt aRange)
1.836 + {
1.837 + TInt n;
1.838 + for (n=0; n<aNumTests; n++)
1.839 + {
1.840 + RArray<SInt> a;
1.841 + RArray<SInt> b;
1.842 + TInt i;
1.843 + for (i=0; i<aCount; i++)
1.844 + {
1.845 + TInt x=Random()&aRange;
1.846 + a.Append(x);
1.847 + b.InsertInSignedKeyOrderAllowRepeats(x);
1.848 + }
1.849 + a.SortSigned();
1.850 + if (a.Count()!=aCount)
1.851 + {
1.852 + a.Close();
1.853 + b.Close();
1.854 + return -1;
1.855 + }
1.856 + if (b.Count()!=aCount)
1.857 + {
1.858 + a.Close();
1.859 + b.Close();
1.860 + return -2;
1.861 + }
1.862 + for (i=0; i<aCount; i++)
1.863 + {
1.864 + if (a[i]!=b[i])
1.865 + {
1.866 + a.Close();
1.867 + b.Close();
1.868 + return -3;
1.869 + }
1.870 + }
1.871 + a.Close();
1.872 + b.Close();
1.873 + }
1.874 + return KErrNone;
1.875 + }
1.876 +
1.877 +TInt IntSpecificFindTests1()
1.878 + {
1.879 + RArray<TInt> a;
1.880 + TInt i;
1.881 + TInt v = 0;
1.882 + TInt first, last, any;
1.883 + for (i=0; i<101; ++i)
1.884 + {
1.885 + if (v*v<i)
1.886 + ++v;
1.887 + TInt r = a.InsertInOrderAllowRepeats(v);
1.888 + test(r==KErrNone);
1.889 + }
1.890 + test (a.Count()==101);
1.891 + for (v=0; v<=10; ++v)
1.892 + {
1.893 + first = a.SpecificFindInOrder(v, EArrayFindMode_First);
1.894 + last = a.SpecificFindInOrder(v, EArrayFindMode_Last);
1.895 + any = a.SpecificFindInOrder(v, EArrayFindMode_Any);
1.896 + test(first>=0 && first<a.Count());
1.897 + test(last>=0 && last<=a.Count());
1.898 + test(any>=first && any<last);
1.899 + if (v==0)
1.900 + {
1.901 + test(first==0 && last==1);
1.902 + }
1.903 + else if (v==1)
1.904 + {
1.905 + test(first==1 && last==2);
1.906 + }
1.907 + else
1.908 + {
1.909 + TInt expf = (v-1)*(v-1)+1;
1.910 + TInt expl = v*v+1;
1.911 + test(expf==first && expl==last);
1.912 + }
1.913 + TInt n = last - first;
1.914 + TInt expected = v ? 2*v-1 : 1;
1.915 + test(n == expected);
1.916 + }
1.917 + first = a.SpecificFindInOrder(11, EArrayFindMode_First);
1.918 + test(first == KErrNotFound);
1.919 + last = a.SpecificFindInOrder(11, EArrayFindMode_Last);
1.920 + test(last == KErrNotFound);
1.921 + any = a.SpecificFindInOrder(11, EArrayFindMode_Any);
1.922 + test(any == KErrNotFound);
1.923 + a.Close();
1.924 + return KErrNone;
1.925 + }
1.926 +
1.927 +TInt IntSpecificFindTests2(TInt aCount, TInt aNumTests, TInt aRange)
1.928 + {
1.929 + TInt n;
1.930 + TInt nmiss = 0;
1.931 + TInt nrpt = 0;
1.932 + TInt ntot = 0;
1.933 + for (n=0; n<aNumTests; n++)
1.934 + {
1.935 + RArray<TInt> a;
1.936 + RArray<TInt> b;
1.937 + TInt i;
1.938 + for (i=0; i<aCount; i++)
1.939 + {
1.940 + TInt x=Random()&aRange;
1.941 + x-=(aRange>>1);
1.942 + a.Append(x);
1.943 + b.InsertInOrderAllowRepeats(x);
1.944 + }
1.945 + a.Sort();
1.946 + test(a.Count()==aCount);
1.947 + test(b.Count()==aCount);
1.948 + for (i=0; i<aCount; i++)
1.949 + test(a[i]==b[i]);
1.950 + for (i=-(aRange>>1); i<=(aRange>>1); ++i)
1.951 + {
1.952 + TInt first = a.SpecificFindInOrder(i, EArrayFindMode_First);
1.953 + TInt last = a.SpecificFindInOrder(i, EArrayFindMode_Last);
1.954 + TInt any = a.SpecificFindInOrder(i, EArrayFindMode_Any);
1.955 + TInt fi, li, ai;
1.956 + TInt first2 = a.SpecificFindInOrder(i, fi, EArrayFindMode_First);
1.957 + TInt last2 = a.SpecificFindInOrder(i, li, EArrayFindMode_Last);
1.958 + TInt any2 = a.SpecificFindInOrder(i, ai, EArrayFindMode_Any);
1.959 + ++ntot;
1.960 + if (first < 0)
1.961 + {
1.962 + test(first == KErrNotFound);
1.963 + test(first == last);
1.964 + test(first == any);
1.965 + test(first == first2);
1.966 + test(first == last2);
1.967 + test(first == any2);
1.968 + test(fi == li);
1.969 + test(fi == ai);
1.970 + test(li==aCount || a[li]>i);
1.971 + test(li==0 || a[li-1]<i);
1.972 + ++nmiss;
1.973 + }
1.974 + else
1.975 + {
1.976 + test(first2 == KErrNone);
1.977 + test(last2 == KErrNone);
1.978 + test(any2 == KErrNone);
1.979 + test(first == fi);
1.980 + test(last == li);
1.981 + test(any == ai);
1.982 + test(a[fi] == i);
1.983 + test(a[li-1] == i);
1.984 + test(li==aCount || a[li]>i);
1.985 + test(ai>=fi && ai<li);
1.986 + test(a[ai] == i);
1.987 + if (li-fi > 1)
1.988 + ++nrpt;
1.989 + }
1.990 + }
1.991 + a.Close();
1.992 + b.Close();
1.993 + }
1.994 + test.Printf(_L("ntot=%d nmiss=%d nrpt=%d\n"), ntot, nmiss, nrpt);
1.995 + return KErrNone;
1.996 + }
1.997 +
1.998 +TInt SIntSpecificFindTests2(TInt aCount, TInt aNumTests, TInt aRange)
1.999 + {
1.1000 + TInt n;
1.1001 + TInt nmiss = 0;
1.1002 + TInt nrpt = 0;
1.1003 + TInt ntot = 0;
1.1004 + for (n=0; n<aNumTests; n++)
1.1005 + {
1.1006 + RArray<SInt> a;
1.1007 + RArray<SInt> b;
1.1008 + TInt i;
1.1009 + for (i=0; i<aCount; i++)
1.1010 + {
1.1011 + TInt x=Random()&aRange;
1.1012 + x-=(aRange>>1);
1.1013 + a.Append(x);
1.1014 + b.InsertInSignedKeyOrderAllowRepeats(x);
1.1015 + }
1.1016 + a.SortSigned();
1.1017 + test(a.Count()==aCount);
1.1018 + test(b.Count()==aCount);
1.1019 + for (i=0; i<aCount; i++)
1.1020 + test(a[i]==b[i]);
1.1021 + for (i=-(aRange>>1); i<=(aRange>>1); ++i)
1.1022 + {
1.1023 + TInt first = a.SpecificFindInSignedKeyOrder(i, EArrayFindMode_First);
1.1024 + TInt last = a.SpecificFindInSignedKeyOrder(i, EArrayFindMode_Last);
1.1025 + TInt any = a.SpecificFindInSignedKeyOrder(i, EArrayFindMode_Any);
1.1026 + TInt fi, li, ai;
1.1027 + TInt first2 = a.SpecificFindInSignedKeyOrder(i, fi, EArrayFindMode_First);
1.1028 + TInt last2 = a.SpecificFindInSignedKeyOrder(i, li, EArrayFindMode_Last);
1.1029 + TInt any2 = a.SpecificFindInSignedKeyOrder(i, ai, EArrayFindMode_Any);
1.1030 + ++ntot;
1.1031 + if (first < 0)
1.1032 + {
1.1033 + test(first == KErrNotFound);
1.1034 + test(first == last);
1.1035 + test(first == any);
1.1036 + test(first == first2);
1.1037 + test(first == last2);
1.1038 + test(first == any2);
1.1039 + test(fi == li);
1.1040 + test(fi == ai);
1.1041 + test(li==aCount || a[li]>i);
1.1042 + test(li==0 || a[li-1]<i);
1.1043 + ++nmiss;
1.1044 + }
1.1045 + else
1.1046 + {
1.1047 + test(first2 == KErrNone);
1.1048 + test(last2 == KErrNone);
1.1049 + test(any2 == KErrNone);
1.1050 + test(first == fi);
1.1051 + test(last == li);
1.1052 + test(any == ai);
1.1053 + test(a[fi] == i);
1.1054 + test(a[li-1] == i);
1.1055 + test(li==aCount || a[li]>i);
1.1056 + test(ai>=fi && ai<li);
1.1057 + test(a[ai] == i);
1.1058 + if (li-fi > 1)
1.1059 + ++nrpt;
1.1060 + }
1.1061 + }
1.1062 + a.Close();
1.1063 + b.Close();
1.1064 + }
1.1065 + test.Printf(_L("ntot=%d nmiss=%d nrpt=%d\n"), ntot, nmiss, nrpt);
1.1066 + return KErrNone;
1.1067 + }
1.1068 +
1.1069 +GLDEF_C void DoIntArrayTests()
1.1070 + {
1.1071 + test.Start(_L("Signed Integer Arrays..."));
1.1072 +
1.1073 + test.Next(_L("AppendAndAccess tests..."));
1.1074 + test.Next(_L("Count 10 Range 15"));
1.1075 + test(IntAppendAndAccessTest(10,NUM_TESTS,15)==KErrNone);
1.1076 + test.Next(_L("Count 20 Range 255"));
1.1077 + test(IntAppendAndAccessTest(20,NUM_TESTS,255)==KErrNone);
1.1078 + test.Next(_L("Count 100 Range all"));
1.1079 + test(IntAppendAndAccessTest(100,NUM_TESTS,-1)==KErrNone);
1.1080 +
1.1081 + test.Next(_L("Remove tests..."));
1.1082 + test(IntRemoveTest()==KErrNone);
1.1083 +
1.1084 + test.Next(_L("Find tests..."));
1.1085 + test.Next(_L("Count 10 Range 15"));
1.1086 + test(IntFindTest(10,NUM_TESTS,15)==KErrNone);
1.1087 + test.Next(_L("Count 20 Range 255"));
1.1088 + test(IntFindTest(20,NUM_TESTS,255)==KErrNone);
1.1089 + test.Next(_L("Count 100 Range all"));
1.1090 + test(IntFindTest(100,NUM_TESTS,-1)==KErrNone);
1.1091 +
1.1092 + test.Next(_L("FindInOrder tests..."));
1.1093 + test.Next(_L("Count 10 Range 15"));
1.1094 + test(IntFindInOrderTest(10,NUM_TESTS,15)==KErrNone);
1.1095 + test.Next(_L("Count 20 Range 255"));
1.1096 + test(IntFindInOrderTest(20,NUM_TESTS,255)==KErrNone);
1.1097 + test.Next(_L("Count 100 Range 4095"));
1.1098 + test(IntFindInOrderTest(100,NUM_TESTS,4095)==KErrNone);
1.1099 + test.Next(_L("Squares"));
1.1100 + test(IntFindInOrderTest2()==KErrNone);
1.1101 +
1.1102 + test.Next(_L("InsertInOrder tests..."));
1.1103 + test.Next(_L("Count 10 Range 15"));
1.1104 + test(IntInsertInOrderTest(10,NUM_TESTS,15)==KErrNone);
1.1105 + test.Next(_L("Count 20 Range 255"));
1.1106 + test(IntInsertInOrderTest(20,NUM_TESTS,255)==KErrNone);
1.1107 + test.Next(_L("Count 100 Range all"));
1.1108 + test(IntInsertInOrderTest(100,NUM_TESTS,-1)==KErrNone);
1.1109 + test.Next(_L("Squares"));
1.1110 + test(IntInsertInOrderTest2()==KErrNone);
1.1111 +
1.1112 + test.Next(_L("Sort tests..."));
1.1113 + test.Next(_L("Count 10 Range 15"));
1.1114 + test(IntSortTest(10,NUM_TESTS,15)==KErrNone);
1.1115 + test.Next(_L("Count 20 Range 255"));
1.1116 + test(IntSortTest(20,NUM_TESTS,255)==KErrNone);
1.1117 + test.Next(_L("Count 100 Range all"));
1.1118 + test(IntSortTest(100,NUM_TESTS,-1)==KErrNone);
1.1119 +
1.1120 + test.End();
1.1121 +
1.1122 + test.Start(_L("4 byte RArrays..."));
1.1123 +
1.1124 + test.Next(_L("AppendAndAccess tests..."));
1.1125 + test.Next(_L("Count 10 Range 15"));
1.1126 + test(SIntAppendAndAccessTest(10,NUM_TESTS,15)==KErrNone);
1.1127 + test.Next(_L("Count 20 Range 255"));
1.1128 + test(SIntAppendAndAccessTest(20,NUM_TESTS,255)==KErrNone);
1.1129 + test.Next(_L("Count 100 Range all"));
1.1130 + test(SIntAppendAndAccessTest(100,NUM_TESTS,-1)==KErrNone);
1.1131 +
1.1132 + test.Next(_L("Remove tests..."));
1.1133 + test(SIntRemoveTest()==KErrNone);
1.1134 +
1.1135 + test.Next(_L("Find tests..."));
1.1136 + test.Next(_L("Count 10 Range 15"));
1.1137 + test(SIntFindTest(10,NUM_TESTS,15)==KErrNone);
1.1138 + test.Next(_L("Count 20 Range 255"));
1.1139 + test(SIntFindTest(20,NUM_TESTS,255)==KErrNone);
1.1140 + test.Next(_L("Count 100 Range all"));
1.1141 + test(SIntFindTest(100,NUM_TESTS,-1)==KErrNone);
1.1142 +
1.1143 + test.Next(_L("FindInOrder tests..."));
1.1144 + test.Next(_L("Count 10 Range 15"));
1.1145 + test(SIntFindInOrderTest(10,NUM_TESTS,15)==KErrNone);
1.1146 + test.Next(_L("Count 20 Range 255"));
1.1147 + test(SIntFindInOrderTest(20,NUM_TESTS,255)==KErrNone);
1.1148 + test.Next(_L("Count 100 Range 4095"));
1.1149 + test(SIntFindInOrderTest(100,NUM_TESTS,4095)==KErrNone);
1.1150 + test.Next(_L("Squares"));
1.1151 + test(SIntFindInOrderTest2()==KErrNone);
1.1152 +
1.1153 + test.Next(_L("InsertInOrder tests..."));
1.1154 + test.Next(_L("Count 10 Range 15"));
1.1155 + test(SIntInsertInOrderTest(10,NUM_TESTS,15)==KErrNone);
1.1156 + test.Next(_L("Count 20 Range 255"));
1.1157 + test(SIntInsertInOrderTest(20,NUM_TESTS,255)==KErrNone);
1.1158 + test.Next(_L("Count 100 Range all"));
1.1159 + test(SIntInsertInOrderTest(100,NUM_TESTS,-1)==KErrNone);
1.1160 + test.Next(_L("Squares"));
1.1161 + test(SIntInsertInOrderTest2()==KErrNone);
1.1162 +
1.1163 + test.Next(_L("Sort tests..."));
1.1164 + test.Next(_L("Count 10 Range 15"));
1.1165 + test(SIntSortTest(10,NUM_TESTS,15)==KErrNone);
1.1166 + test.Next(_L("Count 20 Range 255"));
1.1167 + test(SIntSortTest(20,NUM_TESTS,255)==KErrNone);
1.1168 + test.Next(_L("Count 100 Range all"));
1.1169 + test(SIntSortTest(100,NUM_TESTS,-1)==KErrNone);
1.1170 +
1.1171 + test.Next(_L("IntSpecificFindTests..."));
1.1172 + test(IntSpecificFindTests1()==KErrNone);
1.1173 + test(IntSpecificFindTests2(100, 10, 15)==KErrNone);
1.1174 + test(IntSpecificFindTests2(100, 10, 127)==KErrNone);
1.1175 +
1.1176 + test.Next(_L("SIntSpecificFindTests..."));
1.1177 + test(SIntSpecificFindTests2(100, 10, 15)==KErrNone);
1.1178 + test(SIntSpecificFindTests2(100, 10, 127)==KErrNone);
1.1179 + test.End();
1.1180 + }