File: bitops_test.c

package info (click to toggle)
bird3 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,600 kB
  • sloc: ansic: 85,199; sh: 3,807; perl: 3,484; lex: 976; python: 726; makefile: 527; xml: 520; sed: 13
file content (149 lines) | stat: -rw-r--r-- 2,860 bytes parent folder | download | duplicates (2)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 *	BIRD Library -- Generic Bit Operations Tests
 *
 *	(c) 2015 CZ.NIC z.s.p.o.
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "test/birdtest.h"
#include "test/bt-utils.h" /* naive_pow() */

#include "lib/bitops.h"

#define MAX_NUM 1000
#define CHECK_BIT(var,pos) ((var) & (u32)(1<<(pos)))

static int
t_mkmask(void)
{
  int i;
  u32 compute, expect;

  bt_assert(u32_mkmask(0) == 0x00000000);
  for (i = 1; i <= 32; i++)
  {
    compute = u32_mkmask(i);
    expect  = (u32) (0xffffffff << (32-i));
    bt_assert_msg(compute == expect, "u32_mkmask(%d) = 0x%08X, expected 0x%08X", i, compute, expect);
  }

  return 1;
}

static int
u32_masklen_expected(u32 mask)
{
  int j, expect = 0;

  int valid = 0;
  for (j = 0; j <= 32; j++)
    if (mask == (j ? (0xffffffff << (32-j)) : 0)) /* Shifting 32-bit value by 32 bits is undefined behavior */
	valid = 1;

  if (!valid && mask != 0)
    expect = 255;
  else
    for (j = 0; j <= 31; j++)
      if (CHECK_BIT(mask, (31-j)))
	expect = j+1;
      else
	break;
  return expect;
}

static void
check_mask(u32 mask)
{
  int expected, masklen;

  expected = u32_masklen_expected(mask);
  masklen = u32_masklen(mask);
  int ok = (expected == masklen);
  bt_debug("u32_masklen(Ox%08x) = %d, expected %d  %s\n", mask, masklen, expected, ok ? "OK" : "FAIL!");
  bt_assert(ok);
}

static int
t_masklen(void)
{
  u32 i;

  check_mask(0x82828282);
  check_mask(0x00000000);

  for (i = 0; i <= 32; i++)
    check_mask(((u32) (i ? (0xffffffff << (32-i)) : 0)) & 0xffffffff); /* Shifting 32-bit value by 32 bits is undefined behavior */

  for (i = 0; i <= MAX_NUM; i++)
    check_mask(bt_random());

  return 1;
}

static void
check_log2(u32 n)
{
  u32 log  = u32_log2(n);
  u32 low  = bt_naive_pow(2, log);
  u32 high = bt_naive_pow(2, log+1);

  bt_assert_msg(n >= low && n < high,
		"u32_log2(%u) = %u, %u should be in the range <%u, %u)",
		n, log, n, low, high);
}

static int
t_log2(void)
{
  u32 i;

  for (i = 0; i < 31; i++)
    bt_assert(u32_log2(bt_naive_pow(2, i+1)) == i+1);

  for (i = 1; i < MAX_NUM; i++)
    check_log2(i);

  for (i = 1; i < MAX_NUM; i++)
    check_log2(((u32) bt_random()) % 0x0fffffff);

  return 1;
}

static void
check_bitflip(u32 n)
{
  u32 rot = u32_bitflip(n);
  for (int i = 0; i < 16; i++)
  {
    bt_assert(!((n >> i) & 1) == !((rot << i) & 0x80000000));
    bt_assert(!((rot >> i) & 1) == !((n << i) & 0x80000000));
  }
}

static int
t_bitflip(void)
{
  u32 i;

  for (i = 0; i < MAX_NUM; i++)
  {
    check_bitflip(i);
    check_bitflip((u32) bt_random());
  }

  return 1;
}

int
main(int argc, char *argv[])
{
  bt_init(argc, argv);

  bt_test_suite(t_mkmask, "u32_mkmask()");
  bt_test_suite(t_masklen, "u32_masklen()");
  bt_test_suite(t_log2, "u32_log2()");
  bt_test_suite(t_bitflip, "u32_bitflip()");

  return bt_exit_value();
}