aboutsummaryrefslogtreecommitdiff
path: root/core/test/run-buddy.c
blob: a0a933dca378e72f80410cdd4e69495ab94e4522 (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
/* Copyright 2016-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 <buddy.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

static void *zalloc(size_t size)
{
        return calloc(size, 1);
}

#include "../buddy.c"
#include "../bitmap.c"

#define BUDDY_ORDER	8

int main(void)
{
	struct buddy *b;
	int i, a[10];

	b = buddy_create(BUDDY_ORDER);
	assert(b);

	buddy_reserve(b, 127, 0);
	buddy_reserve(b, 0, 4);
	assert(buddy_reserve(b, 0, 4) == false);

	a[0] = buddy_alloc(b, 0);
	assert(a[0] >= 0);
	a[1] = buddy_alloc(b, 0);
	assert(a[1] >= 0);
	a[2] = buddy_alloc(b, 3);
	assert(a[2] >= 0);
	a[3] = buddy_alloc(b, 4);
	assert(a[3] >= 0);
	a[4] = buddy_alloc(b, 5);
	assert(a[4] >= 0);
	a[5] = buddy_alloc(b, 4);
	assert(a[5] >= 0);
	a[6] = buddy_alloc(b, 3);
	assert(a[6] >= 0);
	a[7] = buddy_alloc(b, 2);
	assert(a[7] >= 0);
	a[8] = buddy_alloc(b, 1);
	assert(a[8] >= 0);
	a[9] = buddy_alloc(b, 8);
	assert(a[9] < 0);

	buddy_free(b, a[0], 0);
	buddy_free(b, a[8], 1);
	buddy_free(b, a[1], 0);
	buddy_free(b, a[7], 2);
	buddy_free(b, a[2], 3);
	buddy_free(b, a[6], 3);
	buddy_free(b, a[3], 4);
	buddy_free(b, a[5], 4);
	buddy_free(b, a[4], 5);

	buddy_free(b, 127, 0);
	buddy_free(b, 0, 4);

	for (i = 2; i < buddy_map_size(b); i++)
		assert(bitmap_tst_bit(b->map, i));
	assert(!bitmap_tst_bit(b->map, 1));

	buddy_destroy(b);
	return 0;
}