aboutsummaryrefslogtreecommitdiff
path: root/core/test/run-bitmap.c
blob: 7c68460a89bc731fa1377768835663498a7f6f2c (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
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
/* Copyright 2017 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "../bitmap.c"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main(void)
{
	bitmap_t *map = malloc(sizeof(bitmap_elem_t));
	int i;
	memset(map, 0, sizeof(bitmap_elem_t));

	assert(BITMAP_ELEMS(16) == (BITMAP_ELEMS(8)));
	assert(BITMAP_ELEMS(128) == (BITMAP_ELEMS(64)*2));

	assert(BITMAP_BYTES(64) == 8);
	assert(BITMAP_BYTES(128) == 16);

	assert(BITMAP_BIT(1) == 0x1);
	assert(BITMAP_BIT(2) == 0x2);
	assert(BITMAP_BIT(3) == 0x3);
	assert(BITMAP_BIT(8) == 0x8);

	assert(BITMAP_MASK(0) == 0x1);
	assert(BITMAP_MASK(1) == 0x2);
	assert(BITMAP_MASK(8) == 0x100);
	assert(BITMAP_MASK(9) == 0x200);

	assert(BITMAP_ELEM(1) == 0);
	assert(BITMAP_ELEM(128) == BITMAP_ELEMS(128));

	bitmap_set_bit(*map, 0);
	assert(*(unsigned long*)map == 0x1);
	assert(bitmap_tst_bit(*map, 0) == true);
	bitmap_clr_bit(*map, 0);
	assert(*(unsigned long*)map == 0x00);

	bitmap_set_bit(*map, 8);
	assert(*(unsigned long*)map == 0x100);
	assert(bitmap_tst_bit(*map, 0) == false);
	assert(bitmap_tst_bit(*map, 1) == false);
	assert(bitmap_tst_bit(*map, 2) == false);
	assert(bitmap_tst_bit(*map, 3) == false);
	assert(bitmap_tst_bit(*map, 4) == false);
	assert(bitmap_tst_bit(*map, 5) == false);
	assert(bitmap_tst_bit(*map, 6) == false);
	assert(bitmap_tst_bit(*map, 7) == false);
	assert(bitmap_tst_bit(*map, 8) == true);
	assert(bitmap_tst_bit(*map, 9) == false);
	assert(bitmap_tst_bit(*map, 10) == false);
	assert(bitmap_tst_bit(*map, 11) == false);
	assert(bitmap_tst_bit(*map, 12) == false);
	assert(bitmap_tst_bit(*map, 13) == false);
	assert(bitmap_tst_bit(*map, 14) == false);
	assert(bitmap_tst_bit(*map, 15) == false);
	assert(bitmap_find_one_bit(*map, 0, 16) == 8);
	bitmap_clr_bit(*map, 8);
	assert(bitmap_find_one_bit(*map, 0, 16) == -1);
	assert(*(unsigned long*)map == 0x00);
	assert(bitmap_tst_bit(*map, 8) == false);

	bitmap_for_each_zero(*map, 7, i) {
		bitmap_set_bit(*map, i);
	}

	for (i = 0; i < 7; i++)
		assert(bitmap_tst_bit(*map, i) == true);

	assert(bitmap_tst_bit(*map, 8) == false);


	free(map);

	return 0;
}