Add Russian translation provided by Валерий Крувялис <valkru@mail.ru>
[xiph-mirror.git] / w3d / _test_rle.c
blobd25c72f0c6d09d6655884384ecbaa5d295b0df01
1 /**
2 * RLEcoder regression test
3 */
5 #define TEST(x) \
6 if(!(x)) { \
7 fprintf(stderr, "Test ("#x") FAILED (i == %i) !!!\n", i); \
8 exit (-1); \
11 #undef BITCODER
12 #undef RLECODER
13 #define RLECODER
15 #include <time.h>
16 #include "rle.h"
17 #include "mem.h"
18 #include "mem.c" /* FIXME !! */
22 #define MAX_COUNT 650000 /* write/read up to 650000 bits */
23 #define N_TESTS 100 /* test 100 times */
26 int main ()
28 uint32_t j;
30 fprintf(stdout, "\nRLECoder Test (N_TESTS == %u, bitstreams up to %u bits)\n",
31 N_TESTS, MAX_COUNT);
33 for (j=1; j<=N_TESTS; j++) {
34 uint8_t *bitstream;
35 uint8_t *bit;
36 uint32_t limit = 0;
37 uint32_t count;
39 fprintf (stdout, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%u/%u", j, N_TESTS);
40 fflush (stdout);
42 srand (time(NULL));
44 while (limit == 0)
45 limit = rand() * 1.0 * MAX_COUNT / RAND_MAX;
47 bit = (uint8_t*) MALLOC (limit);
49 /**
50 * check encoding ...
53 ENTROPY_CODER encoder;
54 uint32_t i;
56 ENTROPY_ENCODER_INIT(&encoder, limit);
58 for (i=0; i<limit; i++) {
59 bit[i] = (rand() > RAND_MAX/1000) ? 0 : 1; /* avg. runlength 1000 */
60 OUTPUT_BIT(&encoder, bit[i]);
63 count = ENTROPY_ENCODER_FLUSH(&encoder);
65 bitstream = (uint8_t*) MALLOC (count);
66 memcpy (bitstream, ENTROPY_CODER_BITSTREAM(&encoder), count);
68 ENTROPY_ENCODER_DONE(&encoder);
71 /**
72 * decoding ...
75 ENTROPY_CODER decoder;
76 uint32_t i;
78 ENTROPY_DECODER_INIT(&decoder, bitstream, count);
80 for (i=0; i<limit; i++) {
81 int b;
82 int skip;
84 b = INPUT_BIT(&decoder);
85 TEST(bit[i] == b);
87 skip = ENTROPY_CODER_RUNLENGTH(&decoder);
88 if (skip > 0 && ENTROPY_CODER_SYMBOL(&decoder) == 0) {
89 int j;
90 for (j=0; j<skip; j++)
91 TEST(bit[i+j] == 0);
92 ENTROPY_CODER_SKIP(&decoder, skip);
93 i += skip;
98 FREE (bit);
99 FREE (bitstream);
102 fprintf (stdout, "\ndone.\n\n");
104 return 0;