File: bits.c

package info (click to toggle)
schroedinger 1.0.11-2.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,480 kB
  • ctags: 6,139
  • sloc: ansic: 97,380; sh: 11,238; xml: 6,509; makefile: 387
file content (78 lines) | stat: -rw-r--r-- 1,560 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

#include <stdio.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(" (%d bytes)\n", n);
}


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

  schro_init();

  printf("unsigned int\n");
  for(i=0;i<21;i++) {
    bits = schro_bits_new();
    schro_bits_encode_init (bits, buffer);
    schro_bits_encode_uint(bits,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);
    printf("%3d:", i);
    dump_bits (bits, n);
    value = schro_bits_decode_uint (bits);
    if (value != i) {
      printf("decode failed (%d != %d)\n", value, i);
      fail = 1;
    }
    schro_bits_free (bits);
  }
  printf("\n");

  printf("signed int\n");
  for(i=-5;i<6;i++) {
    bits = schro_bits_new();
    schro_bits_encode_init (bits, buffer);
    schro_bits_encode_sint(bits,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);
    printf("%3d:", i);
    dump_bits (bits, n);
    value = schro_bits_decode_sint (bits);
    if (value != i) {
      printf("decode failed (%d != %d)\n", value, i);
      fail = 1;
    }
    schro_bits_free (bits);
  }
  printf("\n");

  return fail;
}