Recognizes if input is ogg or not.
[xiph/unicode.git] / w3d / _test_bitcoder.c
blob6f8c8a49387b3985ccd82fc36a979d47af24a31f
1 /**
2 * Bitcoder regression test
3 */
5 #define TEST(x) \
6 if(!(x)) { \
7 fprintf(stderr, "line %i: Test ("#x") FAILED !!!\n", __LINE__); \
8 exit (-1); \
12 #undef BITCODER
13 #undef RLECODER
14 #define BITCODER
16 #include <time.h>
17 #include "bitcoder.h"
18 #include "mem.h"
19 #include "mem.c" /* FIXME !! */
23 #define MAX_COUNT 650000 /* write/read up to 650000 bits */
24 #define N_TESTS 100 /* test 100 times */
27 int main ()
29 uint32_t j;
31 fprintf(stdout, "\nBitCoder Test (N_TESTS == %u, bitstreams up to %u bits)\n",
32 N_TESTS, MAX_COUNT);
34 for (j=1; j<=N_TESTS; j++) {
35 uint8_t *bitstream;
36 uint8_t *bit;
37 uint32_t limit = 0;
38 uint32_t count;
40 fprintf (stdout, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%u/%u", j, N_TESTS);
41 fflush (stdout);
43 srand (time(NULL));
45 while (limit == 0)
46 limit = rand() * 1.0 * MAX_COUNT / RAND_MAX;
47 limit &= ~0x7;
49 bit = (uint8_t*) MALLOC (limit);
51 /**
52 * check encoding ...
55 BitCoderState encoder;
56 uint32_t i;
58 bitcoder_encoder_init (&encoder, limit);
60 for (i=0; i<limit; i++) {
61 bit[i] = (rand() > RAND_MAX/2) ? 0 : 1;
62 bitcoder_write_bit (&encoder, bit[i]);
65 count = bitcoder_flush (&encoder);
67 TEST(count == limit/8);
69 bitstream = (uint8_t*) MALLOC (count);
70 memcpy (bitstream, encoder.bitstream, count);
72 bitcoder_encoder_done (&encoder);
75 /**
76 * decoding ...
79 BitCoderState decoder;
80 uint32_t i;
82 bitcoder_decoder_init (&decoder, bitstream, count);
84 for (i=0; i<limit; i++) {
85 TEST(!decoder.eos);
86 TEST(bit[i] == bitcoder_read_bit (&decoder));
89 TEST(!decoder.eos);
90 for (i=0; i<limit; i++) {
91 TEST(bitcoder_read_bit (&decoder) == 0);
92 TEST(decoder.eos);
96 FREE (bit);
97 FREE (bitstream);
100 fprintf (stdout, "\ndone.\n\n");
102 return 0;