Fix the bug that was causing the last byte of the stream to be returned incorrectly, as well as undo jm's reversion, which can cause ec_dec_tell() to operate incorrectly at the end of the stream.

A few other minor updates are included as well.

git-svn-id: http://svn.xiph.org/trunk/ghost@14427 0101bb08-14d6-0310-b084-bc0e0c8e3800
diff --git a/libentcode/ectest.c b/libentcode/ectest.c
index 85994dc..deedad2 100644
--- a/libentcode/ectest.c
+++ b/libentcode/ectest.c
@@ -1,3 +1,4 @@
+#include <stdlib.h>
 #include <stdio.h>
 #include <math.h>
 #include "probenc.h"
@@ -139,6 +140,32 @@
      ldexp(nbits2,-4),ldexp(nbits,-4));
   }
   ec_byte_writeclear(&buf);
-  fprintf(stderr,"All tests passed.\n");
+  fprintf(stderr,"Testing random streams...\n");
+  srand(0);
+  for(i=0;i<1024;i++){
+    unsigned *data;
+    int       j;
+    ft=rand()/((RAND_MAX>>9)+1)+512;
+    sz=rand()/((RAND_MAX>>9)+1);
+    data=(unsigned *)malloc(sz*sizeof(*data));
+    ec_byte_writeinit(&buf);
+    ec_enc_init(&enc,&buf);
+    for(j=0;j<sz;j++){
+      data[j]=rand()%ft;
+      ec_enc_uint(&enc,data[j],ft);
+    }
+    ec_enc_done(&enc);
+    ec_byte_readinit(&buf,ec_byte_get_buffer(&buf),ec_byte_bytes(&buf));
+    ec_dec_init(&dec,&buf);
+    for(j=0;j<sz;j++){
+      sym=ec_dec_uint(&dec,ft);
+      if(sym!=data[j]){
+        fprintf(stderr,
+         "Decoded %i instead of %i with ft of %i at position %i of %i.\n",
+         sym,data[j],ft,j,sz);
+      }
+    }
+    ec_byte_writeclear(&buf);
+  }
   return 0;
 }