// Do an actual count of each char in the message
for (i=0;i<N+1;i++) nfreq[c[i]]++;
// Test codeone
Huffcode huff=new Huffcode(NCHAR,nfreq);
nb.val=0;
byte[] code = new byte[N];
for (i=0;i<N+1;i++) huff.codeone(c[i],code,nb);
System.out.printf("Number of bits used: %d\n", nb.val);
System.out.printf("Compression ratio: %f\n", (double)(N*3)/nb.val); // Normally 3 bits each for 5 characters
// Recover the text
nb.val=0;
byte[]d=new byte[N+1];
for (i=0;i<N+1;i++) {
d[i]=(byte)huff.decodeone(code,nb);
localflag = localflag || (c[i] != d[i]);
}
globalflag = globalflag || localflag;
if (localflag) {
fail("*** Huffcode: Recovered message not the same as the original");