sl@0
|
1 |
// Copyright (c) 2004-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_huff.cpp
|
sl@0
|
15 |
// Overview:
|
sl@0
|
16 |
// Test methods of the Huffman, TBitInput and TBitOutput classes.
|
sl@0
|
17 |
// API Information:
|
sl@0
|
18 |
// Huffman, TBitInput, TBitOutput
|
sl@0
|
19 |
// Details:
|
sl@0
|
20 |
// - Test and verify the results of TBitInput bit reading:
|
sl@0
|
21 |
// - test and verify single bit reads, multiple bit reads and 32-bit reads
|
sl@0
|
22 |
// - test and verify single bit reads and multiple bit reads from a
|
sl@0
|
23 |
// fractured input.
|
sl@0
|
24 |
// - test and verify overrun reads
|
sl@0
|
25 |
// - Test and verify the results of TBitOutput bit writing:
|
sl@0
|
26 |
// - test and verify bitstream padding
|
sl@0
|
27 |
// - test and verify single bit and multiple bit writes
|
sl@0
|
28 |
// - test and verify overflow writes
|
sl@0
|
29 |
// - Test and verify the results of a Huffman decoder using Huffman class
|
sl@0
|
30 |
// static methods, TBitOutput and TBitInput objects.
|
sl@0
|
31 |
// - Test and verify the results of a Huffman generator for known distributions:
|
sl@0
|
32 |
// flat, power-of-2 and Fibonacci.
|
sl@0
|
33 |
// - Test and verify the results of a Huffman generator for random distributions:
|
sl@0
|
34 |
// - generate random frequency distributions and verify:
|
sl@0
|
35 |
// (a) the Huffman generator creates a mathematically 'optimal code'
|
sl@0
|
36 |
// (b) the canonical encoding is canonical
|
sl@0
|
37 |
// (c) the decoding tree correctly decodes each code
|
sl@0
|
38 |
// (d) the encoding can be correctly externalised and internalised
|
sl@0
|
39 |
// Platforms/Drives/Compatibility:
|
sl@0
|
40 |
// All
|
sl@0
|
41 |
// Assumptions/Requirement/Pre-requisites:
|
sl@0
|
42 |
// Failures and causes:
|
sl@0
|
43 |
// Base Port information:
|
sl@0
|
44 |
//
|
sl@0
|
45 |
//
|
sl@0
|
46 |
|
sl@0
|
47 |
#include <e32test.h>
|
sl@0
|
48 |
#include <e32math.h>
|
sl@0
|
49 |
#include <e32huffman.h>
|
sl@0
|
50 |
|
sl@0
|
51 |
RTest test(RProcess().FileName());
|
sl@0
|
52 |
|
sl@0
|
53 |
const Uint64 KTestData=UI64LIT(0x6f1b09a7e8c523d4);
|
sl@0
|
54 |
const TUint8 KTestBuffer[] = {0x6f,0x1b,0x09,0xa7,0xe8,0xc5,0x23,0xd4};
|
sl@0
|
55 |
const TInt KTestBytes=sizeof(KTestBuffer);
|
sl@0
|
56 |
const TInt KTestBits=KTestBytes*8;
|
sl@0
|
57 |
|
sl@0
|
58 |
// Input stream: bit and multi-bit read tests with exhsautive buffer reload testing
|
sl@0
|
59 |
|
sl@0
|
60 |
typedef TBool (*TestFn)(TBitInput& aIn, Uint64 aBits, TInt aCount);
|
sl@0
|
61 |
|
sl@0
|
62 |
class TAlignedBitInput : public TBitInput
|
sl@0
|
63 |
{
|
sl@0
|
64 |
public:
|
sl@0
|
65 |
TAlignedBitInput(const TUint8*,TInt,TInt);
|
sl@0
|
66 |
private:
|
sl@0
|
67 |
void UnderflowL();
|
sl@0
|
68 |
private:
|
sl@0
|
69 |
const TUint8* iRemainder;
|
sl@0
|
70 |
TInt iCount;
|
sl@0
|
71 |
};
|
sl@0
|
72 |
|
sl@0
|
73 |
TAlignedBitInput::TAlignedBitInput(const TUint8* aPtr,TInt aCount,TInt aOffset)
|
sl@0
|
74 |
:TBitInput(aPtr,32-aOffset,aOffset), iRemainder(aPtr+4), iCount(aOffset+aCount-32)
|
sl@0
|
75 |
{}
|
sl@0
|
76 |
|
sl@0
|
77 |
void TAlignedBitInput::UnderflowL()
|
sl@0
|
78 |
{
|
sl@0
|
79 |
if (!iRemainder)
|
sl@0
|
80 |
User::Leave(KErrUnderflow);
|
sl@0
|
81 |
else
|
sl@0
|
82 |
{
|
sl@0
|
83 |
Set(iRemainder,iCount);
|
sl@0
|
84 |
iRemainder=0;
|
sl@0
|
85 |
}
|
sl@0
|
86 |
}
|
sl@0
|
87 |
|
sl@0
|
88 |
class TSplitBitInput : public TBitInput
|
sl@0
|
89 |
{
|
sl@0
|
90 |
public:
|
sl@0
|
91 |
TSplitBitInput(const TUint8*,TInt,TInt,TInt);
|
sl@0
|
92 |
private:
|
sl@0
|
93 |
void UnderflowL();
|
sl@0
|
94 |
private:
|
sl@0
|
95 |
const TUint8* iBase;
|
sl@0
|
96 |
TInt iBlockSize;
|
sl@0
|
97 |
TInt iOffset;
|
sl@0
|
98 |
TInt iAvail;
|
sl@0
|
99 |
};
|
sl@0
|
100 |
|
sl@0
|
101 |
TSplitBitInput::TSplitBitInput(const TUint8* aPtr,TInt aLength,TInt aOffset,TInt aSize)
|
sl@0
|
102 |
:TBitInput(aPtr,aSize,aOffset), iBase(aPtr), iBlockSize(aSize), iOffset(aOffset+aSize), iAvail(aLength-aSize)
|
sl@0
|
103 |
{}
|
sl@0
|
104 |
|
sl@0
|
105 |
void TSplitBitInput::UnderflowL()
|
sl@0
|
106 |
{
|
sl@0
|
107 |
TInt len=Min(iBlockSize,iAvail);
|
sl@0
|
108 |
if (len==0)
|
sl@0
|
109 |
User::Leave(KErrUnderflow);
|
sl@0
|
110 |
Set(iBase,len,iOffset);
|
sl@0
|
111 |
iOffset+=len;
|
sl@0
|
112 |
iAvail-=len;
|
sl@0
|
113 |
}
|
sl@0
|
114 |
|
sl@0
|
115 |
class TAlternateBitInput : public TBitInput
|
sl@0
|
116 |
{
|
sl@0
|
117 |
public:
|
sl@0
|
118 |
TAlternateBitInput(const TUint8*,TInt,TInt);
|
sl@0
|
119 |
private:
|
sl@0
|
120 |
void UnderflowL();
|
sl@0
|
121 |
private:
|
sl@0
|
122 |
const TUint8* iBase;
|
sl@0
|
123 |
TInt iOffset;
|
sl@0
|
124 |
TInt iAvail;
|
sl@0
|
125 |
};
|
sl@0
|
126 |
|
sl@0
|
127 |
TAlternateBitInput::TAlternateBitInput(const TUint8* aPtr,TInt aLength,TInt aOffset)
|
sl@0
|
128 |
:TBitInput(aPtr,1,aOffset), iBase(aPtr), iOffset(aOffset+2), iAvail(aLength-2)
|
sl@0
|
129 |
{}
|
sl@0
|
130 |
|
sl@0
|
131 |
void TAlternateBitInput::UnderflowL()
|
sl@0
|
132 |
{
|
sl@0
|
133 |
if (iAvail<=0)
|
sl@0
|
134 |
User::Leave(KErrUnderflow);
|
sl@0
|
135 |
Set(iBase,1,iOffset);
|
sl@0
|
136 |
iOffset+=2;
|
sl@0
|
137 |
iAvail-=2;
|
sl@0
|
138 |
}
|
sl@0
|
139 |
|
sl@0
|
140 |
void TestReader(TBitInput& aIn, TestFn aFunc, Uint64 aBits, TInt aCount)
|
sl@0
|
141 |
{
|
sl@0
|
142 |
TBool eof=EFalse;
|
sl@0
|
143 |
TRAPD(r,eof=aFunc(aIn,aBits,aCount));
|
sl@0
|
144 |
test (r==KErrNone);
|
sl@0
|
145 |
if (eof)
|
sl@0
|
146 |
{
|
sl@0
|
147 |
TRAP(r,aIn.ReadL());
|
sl@0
|
148 |
test (r==KErrUnderflow);
|
sl@0
|
149 |
}
|
sl@0
|
150 |
}
|
sl@0
|
151 |
|
sl@0
|
152 |
void TestBits(TInt aOffset, TInt aCount, TestFn aFunc)
|
sl@0
|
153 |
{
|
sl@0
|
154 |
Uint64 bits=KTestData;
|
sl@0
|
155 |
if (aOffset)
|
sl@0
|
156 |
bits<<=aOffset;
|
sl@0
|
157 |
if (aCount<64)
|
sl@0
|
158 |
bits&=~((Uint64(1)<<(64-aCount))-1);
|
sl@0
|
159 |
// test with direct input
|
sl@0
|
160 |
TBitInput in1(KTestBuffer,aCount,aOffset);
|
sl@0
|
161 |
TestReader(in1,aFunc,bits,aCount);
|
sl@0
|
162 |
// test with aligned input
|
sl@0
|
163 |
if (aOffset<32 && aOffset+aCount>32)
|
sl@0
|
164 |
{
|
sl@0
|
165 |
TAlignedBitInput in2(KTestBuffer,aCount,aOffset);
|
sl@0
|
166 |
TestReader(in2,aFunc,bits,aCount);
|
sl@0
|
167 |
}
|
sl@0
|
168 |
// test with blocked input
|
sl@0
|
169 |
for (TInt block=aCount;--block>0;)
|
sl@0
|
170 |
{
|
sl@0
|
171 |
TSplitBitInput in3(KTestBuffer,aCount,aOffset,block);
|
sl@0
|
172 |
TestReader(in3,aFunc,bits,aCount);
|
sl@0
|
173 |
}
|
sl@0
|
174 |
}
|
sl@0
|
175 |
|
sl@0
|
176 |
void TestAlternateBits(TInt aOffset, TInt aCount, TestFn aFunc)
|
sl@0
|
177 |
{
|
sl@0
|
178 |
Uint64 bits=0;
|
sl@0
|
179 |
TInt c=0;
|
sl@0
|
180 |
for (TInt ix=aOffset;ix<aOffset+aCount;ix+=2)
|
sl@0
|
181 |
{
|
sl@0
|
182 |
if (KTestData<<ix>>63)
|
sl@0
|
183 |
bits|=Uint64(1)<<(63-c);
|
sl@0
|
184 |
++c;
|
sl@0
|
185 |
}
|
sl@0
|
186 |
// test with alternate input
|
sl@0
|
187 |
TAlternateBitInput in1(KTestBuffer,aCount,aOffset);
|
sl@0
|
188 |
TestReader(in1,aFunc,bits,c);
|
sl@0
|
189 |
}
|
sl@0
|
190 |
|
sl@0
|
191 |
void PermBits(TestFn aFunc, TInt aMinCount=1, TInt aMaxCount=64)
|
sl@0
|
192 |
{
|
sl@0
|
193 |
for (TInt offset=0;offset<KTestBits;++offset)
|
sl@0
|
194 |
for (TInt count=Min(KTestBits-offset,aMaxCount);count>=aMinCount;--count)
|
sl@0
|
195 |
TestBits(offset,count,aFunc);
|
sl@0
|
196 |
}
|
sl@0
|
197 |
|
sl@0
|
198 |
void AlternateBits(TestFn aFunc, TInt aMinCount=1)
|
sl@0
|
199 |
{
|
sl@0
|
200 |
for (TInt offset=0;offset<KTestBits;++offset)
|
sl@0
|
201 |
for (TInt count=KTestBits-offset;count>=aMinCount;--count)
|
sl@0
|
202 |
TestAlternateBits(offset,count,aFunc);
|
sl@0
|
203 |
}
|
sl@0
|
204 |
|
sl@0
|
205 |
TBool SingleBitRead(TBitInput& aIn, Uint64 aBits, TInt aCount)
|
sl@0
|
206 |
{
|
sl@0
|
207 |
while (--aCount>=0)
|
sl@0
|
208 |
{
|
sl@0
|
209 |
test (aIn.ReadL() == (aBits>>63));
|
sl@0
|
210 |
aBits<<=1;
|
sl@0
|
211 |
}
|
sl@0
|
212 |
return ETrue;
|
sl@0
|
213 |
}
|
sl@0
|
214 |
|
sl@0
|
215 |
TBool MultiBitRead(TBitInput& aIn, Uint64 aBits, TInt aCount)
|
sl@0
|
216 |
{
|
sl@0
|
217 |
TInt c=aCount/2;
|
sl@0
|
218 |
TUint v=aIn.ReadL(c);
|
sl@0
|
219 |
if (c==0)
|
sl@0
|
220 |
test (v==0);
|
sl@0
|
221 |
else
|
sl@0
|
222 |
{
|
sl@0
|
223 |
test (v==TUint(aBits>>(64-c)));
|
sl@0
|
224 |
aBits<<=c;
|
sl@0
|
225 |
}
|
sl@0
|
226 |
c=aCount-c;
|
sl@0
|
227 |
v=aIn.ReadL(c);
|
sl@0
|
228 |
if (c==0)
|
sl@0
|
229 |
test (v==0);
|
sl@0
|
230 |
else
|
sl@0
|
231 |
test (v==TUint(aBits>>(64-c)));
|
sl@0
|
232 |
return ETrue;
|
sl@0
|
233 |
}
|
sl@0
|
234 |
|
sl@0
|
235 |
TBool LongShortRead(TBitInput& aIn, Uint64 aBits, TInt aCount)
|
sl@0
|
236 |
{
|
sl@0
|
237 |
TUint v=aIn.ReadL(32);
|
sl@0
|
238 |
test (v==TUint(aBits>>32));
|
sl@0
|
239 |
aBits<<=32;
|
sl@0
|
240 |
TInt c=aCount-32;
|
sl@0
|
241 |
v=aIn.ReadL(c);
|
sl@0
|
242 |
if (c==0)
|
sl@0
|
243 |
test (v==0);
|
sl@0
|
244 |
else
|
sl@0
|
245 |
test (v==TUint(aBits>>(64-c)));
|
sl@0
|
246 |
return ETrue;
|
sl@0
|
247 |
}
|
sl@0
|
248 |
|
sl@0
|
249 |
TBool ShortLongRead(TBitInput& aIn, Uint64 aBits, TInt aCount)
|
sl@0
|
250 |
{
|
sl@0
|
251 |
TInt c=aCount-32;
|
sl@0
|
252 |
TUint v=aIn.ReadL(c);
|
sl@0
|
253 |
if (c==0)
|
sl@0
|
254 |
test (v==0);
|
sl@0
|
255 |
else
|
sl@0
|
256 |
{
|
sl@0
|
257 |
test (v==TUint(aBits>>(64-c)));
|
sl@0
|
258 |
aBits<<=c;
|
sl@0
|
259 |
}
|
sl@0
|
260 |
v=aIn.ReadL(32);
|
sl@0
|
261 |
test (v==TUint(aBits>>32));
|
sl@0
|
262 |
return ETrue;
|
sl@0
|
263 |
}
|
sl@0
|
264 |
|
sl@0
|
265 |
TBool EofRead(TBitInput& aIn, Uint64, TInt aCount)
|
sl@0
|
266 |
{
|
sl@0
|
267 |
TRAPD(r,aIn.ReadL(aCount+1));
|
sl@0
|
268 |
test(r==KErrUnderflow);
|
sl@0
|
269 |
return EFalse;
|
sl@0
|
270 |
}
|
sl@0
|
271 |
|
sl@0
|
272 |
void TestBitReading()
|
sl@0
|
273 |
{
|
sl@0
|
274 |
test.Start(_L("Test single bit reads"));
|
sl@0
|
275 |
PermBits(&SingleBitRead);
|
sl@0
|
276 |
test.Next(_L("Test multi bit reads"));
|
sl@0
|
277 |
PermBits(&MultiBitRead);
|
sl@0
|
278 |
test.Next(_L("Test 32-bit reads"));
|
sl@0
|
279 |
PermBits(&LongShortRead,32);
|
sl@0
|
280 |
PermBits(&ShortLongRead,32);
|
sl@0
|
281 |
test.Next(_L("Test single bit reads (fractured input)"));
|
sl@0
|
282 |
AlternateBits(&SingleBitRead);
|
sl@0
|
283 |
test.Next(_L("Test multi bit reads (fractured input)"));
|
sl@0
|
284 |
AlternateBits(&MultiBitRead);
|
sl@0
|
285 |
test.Next(_L("Test overrun reads"));
|
sl@0
|
286 |
PermBits(&EofRead,1,31);
|
sl@0
|
287 |
test.End();
|
sl@0
|
288 |
}
|
sl@0
|
289 |
|
sl@0
|
290 |
// Bit output testing (assumes bit input is correct)
|
sl@0
|
291 |
|
sl@0
|
292 |
void TestPadding()
|
sl@0
|
293 |
{
|
sl@0
|
294 |
TUint8 buffer[4];
|
sl@0
|
295 |
TBitOutput out(buffer,4);
|
sl@0
|
296 |
test(out.Ptr()==buffer);
|
sl@0
|
297 |
test(out.BufferedBits()==0);
|
sl@0
|
298 |
out.PadL(0);
|
sl@0
|
299 |
test(out.Ptr()==buffer);
|
sl@0
|
300 |
test(out.BufferedBits()==0);
|
sl@0
|
301 |
out.WriteL(0,0);
|
sl@0
|
302 |
out.PadL(0);
|
sl@0
|
303 |
test(out.Ptr()==buffer);
|
sl@0
|
304 |
test(out.BufferedBits()==0);
|
sl@0
|
305 |
|
sl@0
|
306 |
TInt i;
|
sl@0
|
307 |
for (i=1;i<=8;++i)
|
sl@0
|
308 |
{
|
sl@0
|
309 |
out.Set(buffer,4);
|
sl@0
|
310 |
out.WriteL(0,i);
|
sl@0
|
311 |
test(out.BufferedBits()==(i%8));
|
sl@0
|
312 |
out.PadL(1);
|
sl@0
|
313 |
test(out.BufferedBits()==0);
|
sl@0
|
314 |
out.WriteL(0,i);
|
sl@0
|
315 |
test(out.BufferedBits()==(i%8));
|
sl@0
|
316 |
out.PadL(1);
|
sl@0
|
317 |
test(out.BufferedBits()==0);
|
sl@0
|
318 |
test (out.Ptr()==buffer+2);
|
sl@0
|
319 |
test (buffer[0]==(0xff>>i));
|
sl@0
|
320 |
test (buffer[1]==(0xff>>i));
|
sl@0
|
321 |
}
|
sl@0
|
322 |
|
sl@0
|
323 |
for (i=1;i<=8;++i)
|
sl@0
|
324 |
{
|
sl@0
|
325 |
out.Set(buffer,4);
|
sl@0
|
326 |
out.WriteL(0xff,i);
|
sl@0
|
327 |
out.PadL(0);
|
sl@0
|
328 |
test (out.Ptr()==buffer+1);
|
sl@0
|
329 |
test (buffer[0]==(0xff^(0xff>>i)));
|
sl@0
|
330 |
}
|
sl@0
|
331 |
}
|
sl@0
|
332 |
|
sl@0
|
333 |
void TestBitWrites()
|
sl@0
|
334 |
{
|
sl@0
|
335 |
TUint8 buffer[KTestBytes];
|
sl@0
|
336 |
TBitOutput out(buffer,KTestBytes);
|
sl@0
|
337 |
TBitInput in(KTestBuffer,KTestBits);
|
sl@0
|
338 |
TInt i;
|
sl@0
|
339 |
for (i=KTestBits;--i>=0;)
|
sl@0
|
340 |
out.WriteL(in.ReadL(),1);
|
sl@0
|
341 |
test (Mem::Compare(buffer,KTestBytes,KTestBuffer,KTestBytes)==0);
|
sl@0
|
342 |
|
sl@0
|
343 |
Mem::FillZ(buffer,KTestBytes);
|
sl@0
|
344 |
out.Set(buffer,KTestBytes);
|
sl@0
|
345 |
Uint64 bits=KTestData;
|
sl@0
|
346 |
for (i=KTestBits;--i>=0;)
|
sl@0
|
347 |
out.WriteL(TUint(bits>>i),1);
|
sl@0
|
348 |
test (Mem::Compare(buffer,KTestBytes,KTestBuffer,KTestBytes)==0);
|
sl@0
|
349 |
}
|
sl@0
|
350 |
|
sl@0
|
351 |
void TestMultiBitWrites()
|
sl@0
|
352 |
{
|
sl@0
|
353 |
TInt i=0;
|
sl@0
|
354 |
for (TInt j=0;j<32;++j)
|
sl@0
|
355 |
for (TInt k=0;k<32;++k)
|
sl@0
|
356 |
{
|
sl@0
|
357 |
++i;
|
sl@0
|
358 |
if (i+j+k>KTestBits)
|
sl@0
|
359 |
i=0;
|
sl@0
|
360 |
TUint8 buffer[KTestBytes];
|
sl@0
|
361 |
TBitInput in(KTestBuffer,KTestBits);
|
sl@0
|
362 |
TBitOutput out(buffer,KTestBytes);
|
sl@0
|
363 |
in.ReadL(i);
|
sl@0
|
364 |
out.WriteL(in.ReadL(j),j);
|
sl@0
|
365 |
out.WriteL(in.ReadL(k),k);
|
sl@0
|
366 |
out.PadL(0);
|
sl@0
|
367 |
const TUint8* p=out.Ptr();
|
sl@0
|
368 |
test (p-buffer == (j+k+7)/8);
|
sl@0
|
369 |
Uint64 v=0;
|
sl@0
|
370 |
while (p>buffer)
|
sl@0
|
371 |
v=(v>>8) | Uint64(*--p)<<56;
|
sl@0
|
372 |
Uint64 res=KTestData;
|
sl@0
|
373 |
if (i+j+k<KTestBits)
|
sl@0
|
374 |
res>>=KTestBits-i-j-k;
|
sl@0
|
375 |
if (j+k<KTestBits)
|
sl@0
|
376 |
res<<=KTestBits-j-k;
|
sl@0
|
377 |
test (v==res);
|
sl@0
|
378 |
}
|
sl@0
|
379 |
}
|
sl@0
|
380 |
|
sl@0
|
381 |
void TestAlternatingWrites()
|
sl@0
|
382 |
{
|
sl@0
|
383 |
const TInt KBufferSize=(1+32)*32;
|
sl@0
|
384 |
TUint8 buffer[(7+KBufferSize)/8];
|
sl@0
|
385 |
TBitOutput out(buffer,sizeof(buffer));
|
sl@0
|
386 |
TInt i;
|
sl@0
|
387 |
for (i=0;i<=32;++i)
|
sl@0
|
388 |
out.WriteL(i&1?0xffffffff:0,i);
|
sl@0
|
389 |
while (--i>=0)
|
sl@0
|
390 |
out.WriteL(i&1?0:0xffffffff,i);
|
sl@0
|
391 |
out.PadL(0);
|
sl@0
|
392 |
TBitInput in(buffer,KBufferSize);
|
sl@0
|
393 |
for (i=0;i<=32;++i)
|
sl@0
|
394 |
{
|
sl@0
|
395 |
TUint v=in.ReadL(i);
|
sl@0
|
396 |
if (i&1)
|
sl@0
|
397 |
test (v == (1u<<i)-1u);
|
sl@0
|
398 |
else
|
sl@0
|
399 |
test (v == 0);
|
sl@0
|
400 |
}
|
sl@0
|
401 |
while (--i>=0)
|
sl@0
|
402 |
{
|
sl@0
|
403 |
TUint v=in.ReadL(i);
|
sl@0
|
404 |
if (i&1)
|
sl@0
|
405 |
test (v == 0);
|
sl@0
|
406 |
else if (i==32)
|
sl@0
|
407 |
test (v == 0xffffffffu);
|
sl@0
|
408 |
else
|
sl@0
|
409 |
test (v == (1u<<i)-1u);
|
sl@0
|
410 |
}
|
sl@0
|
411 |
}
|
sl@0
|
412 |
|
sl@0
|
413 |
class TOverflowOutput : public TBitOutput
|
sl@0
|
414 |
{
|
sl@0
|
415 |
public:
|
sl@0
|
416 |
TOverflowOutput();
|
sl@0
|
417 |
private:
|
sl@0
|
418 |
void OverflowL();
|
sl@0
|
419 |
private:
|
sl@0
|
420 |
TUint8 iBuf[1];
|
sl@0
|
421 |
TInt iIx;
|
sl@0
|
422 |
};
|
sl@0
|
423 |
|
sl@0
|
424 |
TOverflowOutput::TOverflowOutput()
|
sl@0
|
425 |
:iIx(0)
|
sl@0
|
426 |
{}
|
sl@0
|
427 |
|
sl@0
|
428 |
void TOverflowOutput::OverflowL()
|
sl@0
|
429 |
{
|
sl@0
|
430 |
if (Ptr()!=0)
|
sl@0
|
431 |
{
|
sl@0
|
432 |
test (Ptr()-iBuf == 1);
|
sl@0
|
433 |
test (iBuf[0] == KTestBuffer[iIx]);
|
sl@0
|
434 |
if (++iIx==KTestBytes)
|
sl@0
|
435 |
User::Leave(KErrOverflow);
|
sl@0
|
436 |
}
|
sl@0
|
437 |
Set(iBuf,1);
|
sl@0
|
438 |
}
|
sl@0
|
439 |
|
sl@0
|
440 |
void OverflowTestL(TBitOutput& out, TInt j)
|
sl@0
|
441 |
{
|
sl@0
|
442 |
for (;;) out.WriteL(0xffffffffu,j);
|
sl@0
|
443 |
}
|
sl@0
|
444 |
|
sl@0
|
445 |
void TestOverflow()
|
sl@0
|
446 |
{
|
sl@0
|
447 |
test.Start(_L("Test default constructed output"));
|
sl@0
|
448 |
TBitOutput out;
|
sl@0
|
449 |
TInt i;
|
sl@0
|
450 |
for (i=1;i<=8;++i)
|
sl@0
|
451 |
{
|
sl@0
|
452 |
TRAPD(r,out.WriteL(1,1));
|
sl@0
|
453 |
if (i<8)
|
sl@0
|
454 |
{
|
sl@0
|
455 |
test (out.BufferedBits() == i);
|
sl@0
|
456 |
test (r == KErrNone);
|
sl@0
|
457 |
}
|
sl@0
|
458 |
else
|
sl@0
|
459 |
test (r == KErrOverflow);
|
sl@0
|
460 |
}
|
sl@0
|
461 |
|
sl@0
|
462 |
test.Next(_L("Test overflow does not overrun the buffer"));
|
sl@0
|
463 |
i=0;
|
sl@0
|
464 |
for (TInt j=1;j<=32;++j)
|
sl@0
|
465 |
{
|
sl@0
|
466 |
if (++i>KTestBytes)
|
sl@0
|
467 |
i=1;
|
sl@0
|
468 |
TUint8 buffer[KTestBytes+1];
|
sl@0
|
469 |
Mem::FillZ(buffer,sizeof(buffer));
|
sl@0
|
470 |
out.Set(buffer,i);
|
sl@0
|
471 |
TRAPD(r,OverflowTestL(out,j));
|
sl@0
|
472 |
test (r == KErrOverflow);
|
sl@0
|
473 |
TInt k=0;
|
sl@0
|
474 |
while (buffer[k]==0xff)
|
sl@0
|
475 |
{
|
sl@0
|
476 |
++k;
|
sl@0
|
477 |
test (k<TInt(sizeof(buffer)));
|
sl@0
|
478 |
}
|
sl@0
|
479 |
test (k <= i);
|
sl@0
|
480 |
test ((i-k)*8 < j);
|
sl@0
|
481 |
while (k<TInt(sizeof(buffer)))
|
sl@0
|
482 |
{
|
sl@0
|
483 |
test (buffer[k]==0);
|
sl@0
|
484 |
++k;
|
sl@0
|
485 |
}
|
sl@0
|
486 |
}
|
sl@0
|
487 |
|
sl@0
|
488 |
test.Next(_L("Test overflow handler"));
|
sl@0
|
489 |
TOverflowOutput vout;
|
sl@0
|
490 |
TBitInput in(KTestBuffer,KTestBits);
|
sl@0
|
491 |
for (i=KTestBits;--i>=0;)
|
sl@0
|
492 |
vout.WriteL(in.ReadL(),1);
|
sl@0
|
493 |
test(vout.BufferedBits() == 0);
|
sl@0
|
494 |
TRAPD(r,vout.WriteL(0,1));
|
sl@0
|
495 |
test (r == KErrNone);
|
sl@0
|
496 |
TRAP(r,vout.PadL(0));
|
sl@0
|
497 |
test (r == KErrOverflow);
|
sl@0
|
498 |
test.End();
|
sl@0
|
499 |
}
|
sl@0
|
500 |
|
sl@0
|
501 |
void TestBitWriting()
|
sl@0
|
502 |
{
|
sl@0
|
503 |
test.Start(_L("Test padding"));
|
sl@0
|
504 |
TestPadding();
|
sl@0
|
505 |
test.Next(_L("Test bit writes"));
|
sl@0
|
506 |
TestBitWrites();
|
sl@0
|
507 |
test.Next(_L("Test multi-bit writes"));
|
sl@0
|
508 |
TestMultiBitWrites();
|
sl@0
|
509 |
TestAlternatingWrites();
|
sl@0
|
510 |
test.Next(_L("Test overflow writes"));
|
sl@0
|
511 |
TestOverflow();
|
sl@0
|
512 |
test.End();
|
sl@0
|
513 |
}
|
sl@0
|
514 |
|
sl@0
|
515 |
// Huffman decode testing
|
sl@0
|
516 |
#ifdef __ARMCC__
|
sl@0
|
517 |
#pragma Onoinline
|
sl@0
|
518 |
#endif
|
sl@0
|
519 |
void Dummy(volatile TInt & /*x*/)
|
sl@0
|
520 |
{
|
sl@0
|
521 |
}
|
sl@0
|
522 |
|
sl@0
|
523 |
void TestHuffmanL()
|
sl@0
|
524 |
{
|
sl@0
|
525 |
const TInt KTestBits=32*32;
|
sl@0
|
526 |
|
sl@0
|
527 |
// build the huffman decoding tree for
|
sl@0
|
528 |
// 0: '0'
|
sl@0
|
529 |
// 1: '10'
|
sl@0
|
530 |
// 2: '110' etc
|
sl@0
|
531 |
TUint32 huffman[Huffman::KMaxCodeLength+1];
|
sl@0
|
532 |
TInt i;
|
sl@0
|
533 |
for (i=0;i<Huffman::KMaxCodeLength;++i)
|
sl@0
|
534 |
huffman[i]=i+1;
|
sl@0
|
535 |
huffman[Huffman::KMaxCodeLength]=Huffman::KMaxCodeLength;
|
sl@0
|
536 |
Huffman::Decoding(huffman,Huffman::KMaxCodeLength+1,huffman);
|
sl@0
|
537 |
|
sl@0
|
538 |
TUint8 buffer[KTestBits/8];
|
sl@0
|
539 |
for (TInt sz=0;sz<Huffman::KMaxCodeLength;++sz)
|
sl@0
|
540 |
{
|
sl@0
|
541 |
const TInt rep=KTestBits/(sz+1);
|
sl@0
|
542 |
TBitOutput out(buffer,sizeof(buffer));
|
sl@0
|
543 |
for (i=0;i<rep;++i)
|
sl@0
|
544 |
{
|
sl@0
|
545 |
out.WriteL(0xffffffff,sz);
|
sl@0
|
546 |
out.WriteL(0,1);
|
sl@0
|
547 |
}
|
sl@0
|
548 |
out.PadL(1);
|
sl@0
|
549 |
for (TInt blk=1;blk<=64;++blk)
|
sl@0
|
550 |
{
|
sl@0
|
551 |
TSplitBitInput in(buffer,rep*(sz+1)-1,0,blk);
|
sl@0
|
552 |
for (i=0;i<rep-1;++i)
|
sl@0
|
553 |
{
|
sl@0
|
554 |
TInt v=-1;
|
sl@0
|
555 |
TRAPD(r,v=in.HuffmanL(huffman));
|
sl@0
|
556 |
test (r==KErrNone);
|
sl@0
|
557 |
test (sz==v);
|
sl@0
|
558 |
}
|
sl@0
|
559 |
volatile TInt v=-1;
|
sl@0
|
560 |
Dummy(v);
|
sl@0
|
561 |
TRAPD(r, v=in.HuffmanL(huffman));
|
sl@0
|
562 |
test (v==-1);
|
sl@0
|
563 |
test (r==KErrUnderflow);
|
sl@0
|
564 |
}
|
sl@0
|
565 |
}
|
sl@0
|
566 |
}
|
sl@0
|
567 |
|
sl@0
|
568 |
// Huffman generator testing with known but atypical distributions
|
sl@0
|
569 |
|
sl@0
|
570 |
void FlatHuffman(TInt aMaxCount)
|
sl@0
|
571 |
{
|
sl@0
|
572 |
TUint32* tab=new TUint32[aMaxCount];
|
sl@0
|
573 |
test (tab!=NULL);
|
sl@0
|
574 |
|
sl@0
|
575 |
// test empty distribution
|
sl@0
|
576 |
Mem::FillZ(tab,sizeof(TUint32)*aMaxCount);
|
sl@0
|
577 |
TRAPD(r, Huffman::HuffmanL(tab,aMaxCount,tab));
|
sl@0
|
578 |
test (r==KErrNone);
|
sl@0
|
579 |
TInt i;
|
sl@0
|
580 |
for (i=0;i<aMaxCount;++i)
|
sl@0
|
581 |
test (tab[i]==0);
|
sl@0
|
582 |
Huffman::Decoding(tab,aMaxCount,tab);
|
sl@0
|
583 |
|
sl@0
|
584 |
// test single-symbol distribution
|
sl@0
|
585 |
Mem::FillZ(tab,sizeof(TUint32)*aMaxCount);
|
sl@0
|
586 |
tab[0]=100;
|
sl@0
|
587 |
TRAP(r, Huffman::HuffmanL(tab,aMaxCount,tab));
|
sl@0
|
588 |
test (r==KErrNone);
|
sl@0
|
589 |
test (tab[0]==1);
|
sl@0
|
590 |
for (i=1;i<aMaxCount;++i)
|
sl@0
|
591 |
test (tab[i]==0);
|
sl@0
|
592 |
Huffman::Decoding(tab,aMaxCount,tab,200);
|
sl@0
|
593 |
TUint8 bits=0;
|
sl@0
|
594 |
TBitInput in(&bits,1);
|
sl@0
|
595 |
test (in.HuffmanL(tab)==200);
|
sl@0
|
596 |
|
sl@0
|
597 |
// test flat distributions with 2..aMaxCount symbols
|
sl@0
|
598 |
TInt len=0;
|
sl@0
|
599 |
for (TInt c=2;c<aMaxCount;++c)
|
sl@0
|
600 |
{
|
sl@0
|
601 |
if ((2<<len)==c)
|
sl@0
|
602 |
++len;
|
sl@0
|
603 |
Mem::FillZ(tab,sizeof(TUint32)*aMaxCount);
|
sl@0
|
604 |
for (i=0;i<c;++i)
|
sl@0
|
605 |
tab[i]=100;
|
sl@0
|
606 |
TRAP(r, Huffman::HuffmanL(tab,aMaxCount,tab));
|
sl@0
|
607 |
test (r==KErrNone);
|
sl@0
|
608 |
TInt small=0;
|
sl@0
|
609 |
for (i=0;i<c;++i)
|
sl@0
|
610 |
{
|
sl@0
|
611 |
if (TInt(tab[i])==len)
|
sl@0
|
612 |
++small;
|
sl@0
|
613 |
else
|
sl@0
|
614 |
test (TInt(tab[i])==len+1);
|
sl@0
|
615 |
}
|
sl@0
|
616 |
for (;i<aMaxCount;++i)
|
sl@0
|
617 |
test (tab[i]==0);
|
sl@0
|
618 |
test (small == (2<<len)-c);
|
sl@0
|
619 |
}
|
sl@0
|
620 |
|
sl@0
|
621 |
delete [] tab;
|
sl@0
|
622 |
}
|
sl@0
|
623 |
|
sl@0
|
624 |
void Power2Huffman()
|
sl@0
|
625 |
//
|
sl@0
|
626 |
// Test Huffman generator for the distribution 2^0,2^0,2^1,2^2,2^3,...
|
sl@0
|
627 |
//
|
sl@0
|
628 |
{
|
sl@0
|
629 |
TUint32 tab[Huffman::KMaxCodeLength+2];
|
sl@0
|
630 |
|
sl@0
|
631 |
for (TInt c=1;c<=Huffman::KMaxCodeLength+1;c++)
|
sl@0
|
632 |
{
|
sl@0
|
633 |
tab[c]=tab[c-1]=1;
|
sl@0
|
634 |
TInt i;
|
sl@0
|
635 |
for (i=c-1;--i>=0;)
|
sl@0
|
636 |
tab[i]=2*tab[i+1];
|
sl@0
|
637 |
|
sl@0
|
638 |
TRAPD(r,Huffman::HuffmanL(tab,c+1,tab));
|
sl@0
|
639 |
if (c>Huffman::KMaxCodeLength)
|
sl@0
|
640 |
{
|
sl@0
|
641 |
test (r==KErrOverflow);
|
sl@0
|
642 |
continue;
|
sl@0
|
643 |
}
|
sl@0
|
644 |
|
sl@0
|
645 |
test (TInt(tab[c]) == c);
|
sl@0
|
646 |
for (i=0;i<c;++i)
|
sl@0
|
647 |
test (TInt(tab[i]) == i+1);
|
sl@0
|
648 |
|
sl@0
|
649 |
Huffman::Decoding(tab,c+1,tab);
|
sl@0
|
650 |
for (i=0;i<=c;++i)
|
sl@0
|
651 |
{
|
sl@0
|
652 |
TUint8 buf[4];
|
sl@0
|
653 |
TBitOutput out(buf,4);
|
sl@0
|
654 |
out.WriteL(0xffffffff,i);
|
sl@0
|
655 |
out.WriteL(0,1);
|
sl@0
|
656 |
out.PadL(1);
|
sl@0
|
657 |
TBitInput in(buf,Min(i+1,c));
|
sl@0
|
658 |
TInt ix=-1;
|
sl@0
|
659 |
TRAP(r, ix=in.HuffmanL(tab));
|
sl@0
|
660 |
test (r==KErrNone);
|
sl@0
|
661 |
test (ix==i);
|
sl@0
|
662 |
TRAP(r, in.HuffmanL(tab));
|
sl@0
|
663 |
test (r==KErrUnderflow);
|
sl@0
|
664 |
}
|
sl@0
|
665 |
}
|
sl@0
|
666 |
}
|
sl@0
|
667 |
|
sl@0
|
668 |
void FibonacciHuffman()
|
sl@0
|
669 |
//
|
sl@0
|
670 |
// Test Huffman generator for the distribution 1,1,2,3,5,8,13,21,...
|
sl@0
|
671 |
//
|
sl@0
|
672 |
{
|
sl@0
|
673 |
TUint32 tab[Huffman::KMaxCodeLength+2];
|
sl@0
|
674 |
|
sl@0
|
675 |
for (TInt c=1;c<=Huffman::KMaxCodeLength+1;c++)
|
sl@0
|
676 |
{
|
sl@0
|
677 |
tab[c]=tab[c-1]=1;
|
sl@0
|
678 |
TInt i;
|
sl@0
|
679 |
for (i=c-1;--i>=0;)
|
sl@0
|
680 |
tab[i]=tab[i+1]+tab[i+2];
|
sl@0
|
681 |
|
sl@0
|
682 |
TRAPD(r,Huffman::HuffmanL(tab,c+1,tab));
|
sl@0
|
683 |
if (c>Huffman::KMaxCodeLength)
|
sl@0
|
684 |
{
|
sl@0
|
685 |
test (r==KErrOverflow);
|
sl@0
|
686 |
continue;
|
sl@0
|
687 |
}
|
sl@0
|
688 |
|
sl@0
|
689 |
test (TInt(tab[c]) == c);
|
sl@0
|
690 |
for (i=0;i<c;++i)
|
sl@0
|
691 |
test (TInt(tab[i]) == i+1);
|
sl@0
|
692 |
|
sl@0
|
693 |
Huffman::Decoding(tab,c+1,tab);
|
sl@0
|
694 |
for (i=0;i<=c;++i)
|
sl@0
|
695 |
{
|
sl@0
|
696 |
TUint8 buf[4];
|
sl@0
|
697 |
TBitOutput out(buf,4);
|
sl@0
|
698 |
out.WriteL(0xffffffff,i);
|
sl@0
|
699 |
out.WriteL(0,1);
|
sl@0
|
700 |
out.PadL(1);
|
sl@0
|
701 |
TBitInput in(buf,Min(i+1,c));
|
sl@0
|
702 |
TInt ix=-1;
|
sl@0
|
703 |
TRAP(r, ix=in.HuffmanL(tab));
|
sl@0
|
704 |
test (r==KErrNone);
|
sl@0
|
705 |
test (ix==i);
|
sl@0
|
706 |
TRAP(r, in.HuffmanL(tab));
|
sl@0
|
707 |
test (r==KErrUnderflow);
|
sl@0
|
708 |
}
|
sl@0
|
709 |
}
|
sl@0
|
710 |
}
|
sl@0
|
711 |
|
sl@0
|
712 |
void SpecificHuffman(TInt aMaxCount)
|
sl@0
|
713 |
{
|
sl@0
|
714 |
test.Start(_L("Flat distributions"));
|
sl@0
|
715 |
FlatHuffman(aMaxCount);
|
sl@0
|
716 |
test.Next(_L("Power-of-2 distributions"));
|
sl@0
|
717 |
Power2Huffman();
|
sl@0
|
718 |
test.Next(_L("Fibonacci distributions"));
|
sl@0
|
719 |
FibonacciHuffman();
|
sl@0
|
720 |
test.End();
|
sl@0
|
721 |
}
|
sl@0
|
722 |
|
sl@0
|
723 |
// Huffman generator validity testing. Checking code properties for a sequence of random
|
sl@0
|
724 |
// frequency distributions.
|
sl@0
|
725 |
|
sl@0
|
726 |
TInt64 RSeed(KTestData);
|
sl@0
|
727 |
|
sl@0
|
728 |
inline TInt Random(TInt aLimit)
|
sl@0
|
729 |
{return aLimit>0 ? (Math::Rand(RSeed)%aLimit) : 0;}
|
sl@0
|
730 |
|
sl@0
|
731 |
void GenerateFreq(TUint32* aTable, TInt aCount, TInt aTotalFreq, TInt aVariance, TInt aZeros)
|
sl@0
|
732 |
//
|
sl@0
|
733 |
// Generate a random frequency table
|
sl@0
|
734 |
//
|
sl@0
|
735 |
{
|
sl@0
|
736 |
for (TInt i=0;i<aCount;++i)
|
sl@0
|
737 |
{
|
sl@0
|
738 |
if (aZeros && Random(aCount-i)<aZeros)
|
sl@0
|
739 |
{
|
sl@0
|
740 |
aTable[i]=0;
|
sl@0
|
741 |
--aZeros;
|
sl@0
|
742 |
}
|
sl@0
|
743 |
else if (aCount-aZeros-i == 1)
|
sl@0
|
744 |
{
|
sl@0
|
745 |
aTable[i]=aTotalFreq;
|
sl@0
|
746 |
aTotalFreq=0;
|
sl@0
|
747 |
}
|
sl@0
|
748 |
else
|
sl@0
|
749 |
{
|
sl@0
|
750 |
TInt ave=aTotalFreq/(aCount-aZeros-i);
|
sl@0
|
751 |
if (aVariance==0)
|
sl@0
|
752 |
{
|
sl@0
|
753 |
aTable[i]=ave;
|
sl@0
|
754 |
aTotalFreq-=ave;
|
sl@0
|
755 |
}
|
sl@0
|
756 |
else
|
sl@0
|
757 |
{
|
sl@0
|
758 |
TInt var=I64INT(TInt64(ave)<<aVariance>>8);
|
sl@0
|
759 |
TInt min=Max(1,ave-var);
|
sl@0
|
760 |
TInt max=Min(1+aTotalFreq-(aCount-aZeros-i),ave+var);
|
sl@0
|
761 |
TInt f = max<=min ? ave : min+Random(max-min);
|
sl@0
|
762 |
aTable[i] = f;
|
sl@0
|
763 |
aTotalFreq-=f;
|
sl@0
|
764 |
}
|
sl@0
|
765 |
}
|
sl@0
|
766 |
}
|
sl@0
|
767 |
}
|
sl@0
|
768 |
|
sl@0
|
769 |
TInt NumericalSort(const TUint32& aLeft, const TUint32& aRight)
|
sl@0
|
770 |
{
|
sl@0
|
771 |
return aLeft-aRight;
|
sl@0
|
772 |
}
|
sl@0
|
773 |
|
sl@0
|
774 |
TInt64 VerifyOptimalCode(const TUint32* aFreq, const TUint32* aCode, TInt aCount, TInt aTotalFreqLog2)
|
sl@0
|
775 |
//
|
sl@0
|
776 |
// We can show tht the expected code length is at least as short as a Shannon-Fano encoding
|
sl@0
|
777 |
//
|
sl@0
|
778 |
{
|
sl@0
|
779 |
TInt64 totalHuff=0;
|
sl@0
|
780 |
TInt64 totalSF=0;
|
sl@0
|
781 |
TInt i;
|
sl@0
|
782 |
for (i=0;i<aCount;++i)
|
sl@0
|
783 |
{
|
sl@0
|
784 |
TInt f=aFreq[i];
|
sl@0
|
785 |
TInt l=aCode[i];
|
sl@0
|
786 |
if (f == 0)
|
sl@0
|
787 |
{
|
sl@0
|
788 |
test (l == 0);
|
sl@0
|
789 |
continue;
|
sl@0
|
790 |
}
|
sl@0
|
791 |
totalHuff+=f*l;
|
sl@0
|
792 |
TInt s=1;
|
sl@0
|
793 |
while ((f<<s>>aTotalFreqLog2)!=1)
|
sl@0
|
794 |
++s;
|
sl@0
|
795 |
totalSF+=f*s;
|
sl@0
|
796 |
}
|
sl@0
|
797 |
test (totalHuff<=totalSF);
|
sl@0
|
798 |
|
sl@0
|
799 |
RPointerArray<TUint32> index(aCount);
|
sl@0
|
800 |
CleanupClosePushL(index);
|
sl@0
|
801 |
for (i=0;i<aCount;++i)
|
sl@0
|
802 |
{
|
sl@0
|
803 |
if (aFreq[i] != 0)
|
sl@0
|
804 |
User::LeaveIfError(index.InsertInOrderAllowRepeats(aFreq+i,&NumericalSort));
|
sl@0
|
805 |
}
|
sl@0
|
806 |
|
sl@0
|
807 |
TInt smin,smax;
|
sl@0
|
808 |
smin=smax=aCode[index[0]-aFreq];
|
sl@0
|
809 |
for (i=1;i<index.Count();++i)
|
sl@0
|
810 |
{
|
sl@0
|
811 |
TInt pix=index[i-1]-aFreq;
|
sl@0
|
812 |
TInt nix=index[i]-aFreq;
|
sl@0
|
813 |
TInt pf=aFreq[pix];
|
sl@0
|
814 |
TInt nf=aFreq[nix];
|
sl@0
|
815 |
TInt ps=aCode[pix];
|
sl@0
|
816 |
TInt ns=aCode[nix];
|
sl@0
|
817 |
|
sl@0
|
818 |
if (nf==pf)
|
sl@0
|
819 |
{
|
sl@0
|
820 |
smin=Min(smin,ns);
|
sl@0
|
821 |
smax=Max(smax,ns);
|
sl@0
|
822 |
test (smin==smax || smin+1==smax);
|
sl@0
|
823 |
}
|
sl@0
|
824 |
else
|
sl@0
|
825 |
{
|
sl@0
|
826 |
test (nf>pf);
|
sl@0
|
827 |
test (ns<=ps);
|
sl@0
|
828 |
smin=smax=ns;
|
sl@0
|
829 |
}
|
sl@0
|
830 |
}
|
sl@0
|
831 |
CleanupStack::PopAndDestroy();
|
sl@0
|
832 |
|
sl@0
|
833 |
return totalHuff;
|
sl@0
|
834 |
}
|
sl@0
|
835 |
|
sl@0
|
836 |
TInt LexicalSort(const TUint32& aLeft, const TUint32& aRight)
|
sl@0
|
837 |
{
|
sl@0
|
838 |
const TUint32 KCodeMask=(1<<Huffman::KMaxCodeLength)-1;
|
sl@0
|
839 |
return (aLeft&KCodeMask)-(aRight&KCodeMask);
|
sl@0
|
840 |
}
|
sl@0
|
841 |
|
sl@0
|
842 |
void VerifyCanonicalEncodingL(const TUint32* aCode, const TUint32* aEncode, TInt aCount)
|
sl@0
|
843 |
//
|
sl@0
|
844 |
// A canonical encoding assigns codes from '0' in increasing code length order, and
|
sl@0
|
845 |
// in increasing index in the table for equal code length.
|
sl@0
|
846 |
//
|
sl@0
|
847 |
// Huffman is also a 'prefix-free' code, so we check this property of the encoding
|
sl@0
|
848 |
//
|
sl@0
|
849 |
{
|
sl@0
|
850 |
TInt i;
|
sl@0
|
851 |
for (i=0;i<aCount;++i)
|
sl@0
|
852 |
test (aCode[i] == aEncode[i]>>Huffman::KMaxCodeLength);
|
sl@0
|
853 |
|
sl@0
|
854 |
RPointerArray<TUint32> index(aCount);
|
sl@0
|
855 |
CleanupClosePushL(index);
|
sl@0
|
856 |
for (i=0;i<aCount;++i)
|
sl@0
|
857 |
{
|
sl@0
|
858 |
if (aCode[i] != 0)
|
sl@0
|
859 |
User::LeaveIfError(index.InsertInOrder(aEncode+i,&LexicalSort));
|
sl@0
|
860 |
}
|
sl@0
|
861 |
|
sl@0
|
862 |
for (i=1;i<index.Count();++i)
|
sl@0
|
863 |
{
|
sl@0
|
864 |
TInt pix=index[i-1]-aEncode;
|
sl@0
|
865 |
TInt nix=index[i]-aEncode;
|
sl@0
|
866 |
test (aCode[pix]<=aCode[nix]); // code lengths are always increasing
|
sl@0
|
867 |
test (aCode[pix]<aCode[nix] || pix<nix); // same code length => index order preserved
|
sl@0
|
868 |
|
sl@0
|
869 |
// check that a code is not a prefix of the next one. This is sufficent for checking the
|
sl@0
|
870 |
// prefix condition as we have already sorted the codes in lexicographical order
|
sl@0
|
871 |
TUint32 pc=aEncode[pix]<<(32-Huffman::KMaxCodeLength);
|
sl@0
|
872 |
TUint32 nc=aEncode[nix]<<(32-Huffman::KMaxCodeLength);
|
sl@0
|
873 |
TInt plen=aCode[pix];
|
sl@0
|
874 |
test ((pc>>(32-plen)) != (nc>>(32-plen))); // pc is not a prefix for nc
|
sl@0
|
875 |
}
|
sl@0
|
876 |
CleanupStack::PopAndDestroy(&index);
|
sl@0
|
877 |
}
|
sl@0
|
878 |
|
sl@0
|
879 |
void VerifyCanonicalDecoding(const TUint32* aEncode, const TUint32* aDecode, TInt aCount, TInt aBase)
|
sl@0
|
880 |
//
|
sl@0
|
881 |
// We've checked the encoding is valid, so now we check that the decoding can correctly
|
sl@0
|
882 |
// decode every code
|
sl@0
|
883 |
//
|
sl@0
|
884 |
{
|
sl@0
|
885 |
TUint8 buffer[(Huffman::KMaxCodeLength+7)/8];
|
sl@0
|
886 |
TBitInput in;
|
sl@0
|
887 |
TBitOutput out;
|
sl@0
|
888 |
|
sl@0
|
889 |
while (--aCount>=0)
|
sl@0
|
890 |
{
|
sl@0
|
891 |
if (aEncode[aCount])
|
sl@0
|
892 |
{
|
sl@0
|
893 |
out.Set(buffer,sizeof(buffer));
|
sl@0
|
894 |
out.HuffmanL(aEncode[aCount]);
|
sl@0
|
895 |
out.PadL(0);
|
sl@0
|
896 |
in.Set(buffer,aEncode[aCount]>>Huffman::KMaxCodeLength);
|
sl@0
|
897 |
TInt v=-1;
|
sl@0
|
898 |
TRAPD(r,v=in.HuffmanL(aDecode));
|
sl@0
|
899 |
test (r==KErrNone);
|
sl@0
|
900 |
test (v==aCount+aBase);
|
sl@0
|
901 |
TRAP(r,in.ReadL());
|
sl@0
|
902 |
test (r==KErrUnderflow);
|
sl@0
|
903 |
}
|
sl@0
|
904 |
}
|
sl@0
|
905 |
}
|
sl@0
|
906 |
|
sl@0
|
907 |
TInt TestExternalizeL(const TUint32* aCode, TUint8* aExtern, TUint32* aIntern, TInt aCount)
|
sl@0
|
908 |
{
|
sl@0
|
909 |
TBitOutput out(aExtern,aCount*4);
|
sl@0
|
910 |
Huffman::ExternalizeL(out,aCode,aCount);
|
sl@0
|
911 |
TInt bits=out.BufferedBits()+8*(out.Ptr()-aExtern);
|
sl@0
|
912 |
out.PadL(0);
|
sl@0
|
913 |
TBitInput in(aExtern,bits);
|
sl@0
|
914 |
TRAPD(r,Huffman::InternalizeL(in,aIntern,aCount));
|
sl@0
|
915 |
test (r == KErrNone);
|
sl@0
|
916 |
test (Mem::Compare((TUint8*)aCode,aCount*sizeof(TUint32),(TUint8*)aIntern,aCount*sizeof(TUint32)) == 0);
|
sl@0
|
917 |
TRAP(r,in.ReadL());
|
sl@0
|
918 |
test (r == KErrUnderflow);
|
sl@0
|
919 |
return bits;
|
sl@0
|
920 |
}
|
sl@0
|
921 |
|
sl@0
|
922 |
void RandomHuffmanL(TInt aIter, TInt aMaxSymbols)
|
sl@0
|
923 |
//
|
sl@0
|
924 |
// generate random frequency distributions and verify
|
sl@0
|
925 |
// (a) the Huffman generator creates a mathematically 'optimal code'
|
sl@0
|
926 |
// (b) the canonical encoding is the canonical encoding
|
sl@0
|
927 |
// (c) the decoding tree correctly decodes each code.
|
sl@0
|
928 |
// (d) the encoding can be correctly externalised and internalised
|
sl@0
|
929 |
//
|
sl@0
|
930 |
{
|
sl@0
|
931 |
TReal KLog2;
|
sl@0
|
932 |
Math::Ln(KLog2,2);
|
sl@0
|
933 |
const TInt KTotalFreqLog2=24;
|
sl@0
|
934 |
const TInt KTotalFreq=1<<KTotalFreqLog2;
|
sl@0
|
935 |
|
sl@0
|
936 |
while (--aIter >= 0)
|
sl@0
|
937 |
{
|
sl@0
|
938 |
TInt num=2+Random(aMaxSymbols-1);
|
sl@0
|
939 |
|
sl@0
|
940 |
TUint32* const freq = new(ELeave) TUint32[num*3];
|
sl@0
|
941 |
CleanupArrayDeletePushL(freq);
|
sl@0
|
942 |
TUint32* const code = freq+num;
|
sl@0
|
943 |
TUint32* const encoding = code+num;
|
sl@0
|
944 |
TUint32* const decoding = freq;
|
sl@0
|
945 |
TUint8* const exter = (TUint8*)encoding;
|
sl@0
|
946 |
TUint32* const intern = freq;
|
sl@0
|
947 |
|
sl@0
|
948 |
TInt var=Random(24);
|
sl@0
|
949 |
TInt zero=Random(num-2);
|
sl@0
|
950 |
GenerateFreq(freq,num,KTotalFreq,var,zero);
|
sl@0
|
951 |
|
sl@0
|
952 |
Huffman::HuffmanL(freq,num,code);
|
sl@0
|
953 |
VerifyOptimalCode(freq,code,num,KTotalFreqLog2);
|
sl@0
|
954 |
|
sl@0
|
955 |
Huffman::Encoding(code,num,encoding);
|
sl@0
|
956 |
VerifyCanonicalEncodingL(code,encoding,num);
|
sl@0
|
957 |
|
sl@0
|
958 |
TInt base=Random(Huffman::KMaxCodes-num);
|
sl@0
|
959 |
Huffman::Decoding(code,num,decoding,base);
|
sl@0
|
960 |
VerifyCanonicalDecoding(encoding,decoding,num,base);
|
sl@0
|
961 |
|
sl@0
|
962 |
TestExternalizeL(code,exter,intern,num);
|
sl@0
|
963 |
CleanupStack::PopAndDestroy();
|
sl@0
|
964 |
}
|
sl@0
|
965 |
}
|
sl@0
|
966 |
|
sl@0
|
967 |
///
|
sl@0
|
968 |
|
sl@0
|
969 |
void MainL()
|
sl@0
|
970 |
{
|
sl@0
|
971 |
test.Start(_L("Test Bit reader"));
|
sl@0
|
972 |
TestBitReading();
|
sl@0
|
973 |
test.Next(_L("Test Bit writer"));
|
sl@0
|
974 |
TestBitWriting();
|
sl@0
|
975 |
test.Next(_L("Test Huffman decoder"));
|
sl@0
|
976 |
TestHuffmanL();
|
sl@0
|
977 |
test.Next(_L("Test Huffman generator for known distributions"));
|
sl@0
|
978 |
SpecificHuffman(800);
|
sl@0
|
979 |
test.Next(_L("Test Huffman generator for random distributions"));
|
sl@0
|
980 |
TRAPD(r,RandomHuffmanL(1000,800));
|
sl@0
|
981 |
test (r==KErrNone);
|
sl@0
|
982 |
test.End();
|
sl@0
|
983 |
}
|
sl@0
|
984 |
|
sl@0
|
985 |
TInt E32Main()
|
sl@0
|
986 |
{
|
sl@0
|
987 |
test.Title();
|
sl@0
|
988 |
CTrapCleanup* c=CTrapCleanup::New();
|
sl@0
|
989 |
test (c!=0);
|
sl@0
|
990 |
TRAPD(r,MainL());
|
sl@0
|
991 |
test (r==KErrNone);
|
sl@0
|
992 |
delete c;
|
sl@0
|
993 |
test.Close();
|
sl@0
|
994 |
return r;
|
sl@0
|
995 |
}
|