summaryrefslogtreecommitdiff
path: root/libbitarray/t-bit-array.c
blob: 4d2827597e70b95135d8240d9aa04d23295e32e1 (plain)
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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>
#include <stdio.h>
#include <string.h>

#include "bit-array.h"

static unsigned char array[16];

int
main (int argc, char *argv[])
{
  /* Make sure we can set all the bits when the bit to set is the one
     under the hint.  */
  int i;
  for (i = 0; i < sizeof (array) * 8; i ++)
    assert (bit_alloc (array, sizeof (array), i) == i);
  assert (bit_alloc (array, sizeof (array), 1) == -1);

  memset (array, 0, sizeof (array));

  /* Make sure we can set all the bits when the bit to set is not
     (necessarily) under the hint.  */
  for (i = 0; i < sizeof (array) * 8; i ++)
    {
      int b = (10 + i) % (sizeof (array) * 8);
      assert (bit_alloc (array, sizeof (array), 10) == b);
    }
  assert (bit_alloc (array, sizeof (array), 1) == -1);

  /* Clear one bit and make sure that independent of the start hint,
     we find that bit.  */
  for (i = 0; i < sizeof (array) * 8; i ++)
    {
      bit_dealloc (array, 75);

      assert (bit_alloc (array, sizeof (array), i) == 75);
      assert (bit_alloc (array, sizeof (array), i) == -1);
    }

  /* See if we can set a bit in the middle of a byte.  */
  bit_dealloc (array, 11);
  assert (bit_set (array, sizeof (array), 11) == true);
  assert (bit_set (array, sizeof (array), 11) == false);

  /* And at the start of a byte.  */
  bit_dealloc (array, 24);
  assert (bit_set (array, sizeof (array), 24) == true);
  assert (bit_set (array, sizeof (array), 24) == false);

  return 0;
}