tests: fix build on os/x
[schroedinger.git] / testsuite / bits3.c
blobce4f4a062a6a0a082f24db7979371c3d5459ebf5
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <time.h>
6 #include <schroedinger/schro.h>
8 int fail;
10 void
11 dump_bits (SchroBits *bits, int n)
13 int i;
15 for(i=0;i<n*8;i++){
16 printf(" %d", (bits->buffer->data[(i>>3)] >> (7 - (i&7))) & 1);
18 printf("\n");
22 int test (void)
24 SchroBuffer *buffer = schro_buffer_new_and_alloc (300);
25 SchroBits *bits;
26 SchroBits bits2;
27 int i;
28 int chunk1;
29 int chunk2;
30 int ref[10];
31 int x;
33 chunk1 = rand()&0x1f;
35 bits = schro_bits_new ();
36 schro_bits_encode_init (bits, buffer);
38 for(i=0;i<chunk1;i++){
39 schro_bits_encode_bit (bits, rand()&1);
42 chunk2 = 0;
43 for(i=0;i<10;i++){
44 ref[i] = rand() & 0xf;
45 schro_bits_encode_sint (bits, ref[i]);
46 chunk2 += schro_bits_estimate_sint (ref[i]);
49 for(i=0;i<100;i++){
50 schro_bits_encode_bit (bits, rand()&1);
53 schro_bits_flush (bits);
54 schro_bits_free (bits);
57 bits = schro_bits_new ();
58 schro_bits_decode_init (bits, buffer);
60 schro_bits_copy (&bits2, bits);
62 schro_bits_skip_bits (&bits2, chunk1);
63 schro_bits_set_length (&bits2, chunk2);
65 printf("chunk1 %d\n", chunk1);
66 for(i=0;i<10;i++){
67 x = schro_bits_decode_sint (&bits2);
68 printf("%d %d\n", ref[i], x);
69 if (x != ref[i]) fail = TRUE;
72 schro_bits_free (bits);
74 return 0;
77 int
78 main (int argc, char *argv[])
80 schro_init();
82 srand(time(NULL));
84 test();
86 return fail;