os/kernelhwsrv/kerneltest/e32test/nkernsa/testutils.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/nkernsa/testutils.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,212 @@
     1.4 +// Copyright (c) 2006-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\nkernsa\testutils.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include <nktest/nkutils.h>
    1.22 +
    1.23 +extern "C" {
    1.24 +TUint32 random(TUint32* aSeed)
    1.25 +	{
    1.26 +	TUint32 x = aSeed[0];
    1.27 +	TUint32 r3 = x >> 1;
    1.28 +	r3 |= (aSeed[1] << 31);
    1.29 +	aSeed[1] = x & 1;
    1.30 +	r3 ^= (x << 12);
    1.31 +	x = r3 ^ (r3 >> 20);
    1.32 +	aSeed[0] = x;
    1.33 +	return x;
    1.34 +	}
    1.35 +
    1.36 +void setup_block(TUint32* aBlock, TInt aNumWords)
    1.37 +	{
    1.38 +	TUint32 seed[2];
    1.39 +	seed[0] = aBlock[0];
    1.40 +	seed[1] = 0;
    1.41 +	TInt i;
    1.42 +	for (i=1; i<aNumWords; ++i)
    1.43 +		*++aBlock = random(seed);
    1.44 +	}
    1.45 +
    1.46 +void setup_block_cpu(TUint32* aBlock, TInt aNumWords)
    1.47 +	{
    1.48 +	TUint32 seed[2];
    1.49 +	seed[0] = aBlock[0];
    1.50 +	seed[1] = 0;
    1.51 +	aBlock[1] = NKern::CurrentCpu();
    1.52 +	TInt i;
    1.53 +	for (i=2; i<aNumWords; ++i)
    1.54 +		aBlock[i] = random(seed) ^ NKern::CurrentCpu();
    1.55 +	}
    1.56 +
    1.57 +TBool verify_block(const TUint32* aBlock, TInt aNumWords)
    1.58 +	{
    1.59 +	TUint32 seed[2];
    1.60 +	seed[0] = aBlock[0];
    1.61 +	seed[1] = 0;
    1.62 +	TInt i;
    1.63 +	for (i=1; i<aNumWords; ++i)
    1.64 +		{
    1.65 +		TUint32 x = random(seed);
    1.66 +		if (aBlock[i] != x)
    1.67 +			{
    1.68 +			KPrintf("Verify block failed: expected %08x got %08x", x, aBlock[i]);
    1.69 +			return FALSE;
    1.70 +			}
    1.71 +		}
    1.72 +	return TRUE;
    1.73 +	}
    1.74 +
    1.75 +TBool verify_block_no_trace(const TUint32* aBlock, TInt aNumWords)
    1.76 +	{
    1.77 +	TUint32 seed[2];
    1.78 +	seed[0] = aBlock[0];
    1.79 +	seed[1] = 0;
    1.80 +	TInt i;
    1.81 +	for (i=1; i<aNumWords; ++i)
    1.82 +		{
    1.83 +		TUint32 x = random(seed);
    1.84 +		if (aBlock[i] != x)
    1.85 +			return FALSE;
    1.86 +		}
    1.87 +	return TRUE;
    1.88 +	}
    1.89 +
    1.90 +TInt verify_block_cpu(const TUint32* aBlock, TInt aNumWords)
    1.91 +	{
    1.92 +	TUint32 seed[2];
    1.93 +	seed[0] = aBlock[0];
    1.94 +	seed[1] = 0;
    1.95 +	TUint32 cpu = aBlock[1];
    1.96 +	TInt i;
    1.97 +	for (i=2; i<aNumWords; ++i)
    1.98 +		{
    1.99 +		TUint32 x = random(seed);
   1.100 +		TUint32 y = aBlock[i] ^ x;
   1.101 +		if (y != cpu)
   1.102 +			{
   1.103 +			KPrintf("Verify block with CPU failed: expected %08x got %08x (XOR %08x)", x, aBlock[i], y);
   1.104 +			return -1;
   1.105 +			}
   1.106 +		}
   1.107 +	return cpu;
   1.108 +	}
   1.109 +
   1.110 +TInt verify_block_cpu_no_trace(const TUint32* aBlock, TInt aNumWords)
   1.111 +	{
   1.112 +	TUint32 seed[2];
   1.113 +	seed[0] = aBlock[0];
   1.114 +	seed[1] = 0;
   1.115 +	TUint32 cpu = aBlock[1];
   1.116 +	TInt i;
   1.117 +	for (i=2; i<aNumWords; ++i)
   1.118 +		{
   1.119 +		TUint32 x = random(seed);
   1.120 +		TUint32 y = aBlock[i] ^ x;
   1.121 +		if (y != cpu)
   1.122 +			return -1;
   1.123 +		}
   1.124 +	return cpu;
   1.125 +	}
   1.126 +}
   1.127 +
   1.128 +
   1.129 +CircBuf* CircBuf::New(TInt aSlots)
   1.130 +	{
   1.131 +	CircBuf* p = new CircBuf();
   1.132 +	p->iSlotCount = aSlots;
   1.133 +	p->iGetIndex = 0;
   1.134 +	p->iPutIndex = 0;
   1.135 +	p->iBufBase = (TUint32*)malloc(aSlots*sizeof(TUint32));
   1.136 +	if (!p->iBufBase)
   1.137 +		{
   1.138 +		delete p;
   1.139 +		p = 0;
   1.140 +		}
   1.141 +	return p;
   1.142 +	}
   1.143 +
   1.144 +CircBuf::CircBuf()
   1.145 +	:	iLock(TSpinLock::EOrderGenericIrqLow1)
   1.146 +	{
   1.147 +	}
   1.148 +
   1.149 +CircBuf::~CircBuf()
   1.150 +	{
   1.151 +	free(iBufBase);
   1.152 +	}
   1.153 +
   1.154 +TInt CircBuf::TryGet(TUint32& aOut)
   1.155 +	{
   1.156 +	TInt r = KErrUnderflow;
   1.157 +	TInt irq = __SPIN_LOCK_IRQSAVE(iLock);
   1.158 +	if (iGetIndex != iPutIndex)
   1.159 +		{
   1.160 +		aOut = iBufBase[iGetIndex++];
   1.161 +		if (iGetIndex == iSlotCount)
   1.162 +			iGetIndex = 0;
   1.163 +		r = KErrNone;
   1.164 +		}
   1.165 +	__SPIN_UNLOCK_IRQRESTORE(iLock,irq);
   1.166 +	return r;
   1.167 +	}
   1.168 +
   1.169 +TInt CircBuf::TryPut(TUint32 aIn)
   1.170 +	{
   1.171 +	TInt r = KErrOverflow;
   1.172 +	TInt irq = __SPIN_LOCK_IRQSAVE(iLock);
   1.173 +	TInt nextPut = iPutIndex + 1;
   1.174 +	if (nextPut == iSlotCount)
   1.175 +		nextPut = 0;
   1.176 +	if (iGetIndex != nextPut)
   1.177 +		{
   1.178 +		iBufBase[iPutIndex] = aIn;
   1.179 +		iPutIndex = nextPut;
   1.180 +		r = KErrNone;
   1.181 +		}
   1.182 +	__SPIN_UNLOCK_IRQRESTORE(iLock,irq);
   1.183 +	return r;
   1.184 +	}
   1.185 +
   1.186 +TUint32 CircBuf::Get()
   1.187 +	{
   1.188 +	TUint32 x;
   1.189 +	while(TryGet(x)!=KErrNone)
   1.190 +		NKern::Sleep(1);
   1.191 +	return x;
   1.192 +	}
   1.193 +
   1.194 +void CircBuf::Put(TUint32 aIn)
   1.195 +	{
   1.196 +	while(TryPut(aIn)!=KErrNone)
   1.197 +		NKern::Sleep(1);
   1.198 +	}
   1.199 +
   1.200 +void CircBuf::Reset()
   1.201 +	{
   1.202 +	TInt irq = __SPIN_LOCK_IRQSAVE(iLock);
   1.203 +	iPutIndex = iGetIndex = 0;
   1.204 +	__SPIN_UNLOCK_IRQRESTORE(iLock,irq);
   1.205 +	}
   1.206 +
   1.207 +TInt CircBuf::Count()
   1.208 +	{
   1.209 +	TInt irq = __SPIN_LOCK_IRQSAVE(iLock);
   1.210 +	TInt r = iPutIndex - iGetIndex;
   1.211 +	if (r < 0)
   1.212 +		r += iSlotCount;
   1.213 +	__SPIN_UNLOCK_IRQRESTORE(iLock,irq);
   1.214 +	return r;
   1.215 +	}