File: bits2.c

package info (click to toggle)
schroedinger 1.0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,480 kB
  • sloc: ansic: 97,380; sh: 11,238; xml: 6,509; makefile: 386
file content (83 lines) | stat: -rw-r--r-- 1,566 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <schroedinger/schro.h>

void
dump_bits (SchroBits *bits, int n)
{
  int i;
  
  for(i=0;i<n*8;i++){
    printf(" %d", (bits->buffer->data[(i>>3)] >> (7 - (i&7))) & 1);
  }
  printf("\n");
}

#define N 100

int ref[N];

int
main (int argc, char *argv[])
{
  int i;
  SchroBuffer *buffer = schro_buffer_new_and_alloc (300);
  SchroBits *bits;
  int value;
  int fail = 0;
  int n;

  schro_init();

  srand(time(NULL));

  bits = schro_bits_new();

  printf("unsigned int\n");
  schro_bits_encode_init (bits, buffer);
  for(i=0;i<N;i++) {
    ref[i] = rand() & 0x7;
    schro_bits_encode_uint(bits,ref[i]);
  }
  schro_bits_flush (bits);
  n = schro_bits_get_offset (bits);
  schro_bits_free (bits);

  bits = schro_bits_new();
  schro_bits_decode_init (bits, buffer);
  for(i=0;i<N;i++) {
    value = schro_bits_decode_uint (bits);
    if (value != ref[i]) {
      printf("decode failed (%d != %d) at offset %d\n", value, ref[i], i);
      fail = 1;
    }
  }

  printf("signed int\n");
  schro_bits_encode_init (bits, buffer);
  for(i=0;i<N;i++) {
    ref[i] = (rand() & 0xf) - 8;
    schro_bits_encode_sint (bits,ref[i]);
  }
  schro_bits_flush (bits);
  n = schro_bits_get_offset (bits);
  schro_bits_free (bits);

  bits = schro_bits_new();
  schro_bits_decode_init (bits, buffer);
  for(i=0;i<N;i++) {
    value = schro_bits_decode_sint (bits);
    if (value != ref[i]) {
      printf("decode failed (%d != %d) at offset %d\n", value, ref[i], i);
      fail = 1;
    }
  }

  return fail;
}