]> git.saurik.com Git - apple/xnu.git/blame - osfmk/tests/bitmap_test.c
xnu-7195.60.75.tar.gz
[apple/xnu.git] / osfmk / tests / bitmap_test.c
CommitLineData
d9a64523
A
1/*
2 * Copyright (c) 2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
d9a64523
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
0a7de745 14 *
d9a64523
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
d9a64523
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
0a7de745 25 *
d9a64523
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29
30#if DEVELOPMENT || DEBUG
31
32#include <tests/xnupost.h>
33#include <kern/kalloc.h>
34#include <kern/bits.h>
35
36extern void dump_bitmap_next(bitmap_t *map, uint nbits);
37extern void dump_bitmap_lsb(bitmap_t *map, uint nbits);
38extern void test_bitmap(void);
39extern kern_return_t bitmap_post_test(void);
40
41void
42dump_bitmap_next(bitmap_t *map, uint nbits)
43{
44 for (int i = bitmap_first(map, nbits); i >= 0; i = bitmap_next(map, i)) {
45 printf(" %d", i);
46 }
47 printf("\n");
48}
49
50void
51dump_bitmap_lsb(bitmap_t *map, uint nbits)
52{
53 for (int i = bitmap_lsb_first(map, nbits); i >= 0; i = bitmap_lsb_next(map, nbits, i)) {
54 printf(" %d", i);
55 }
56 printf("\n");
57}
58
59#ifdef NOTDEF
60#ifdef assert
61#undef assert
62#endif
0a7de745 63#define assert(x) T_ASSERT(x, NULL)
d9a64523
A
64#endif
65
66void
67test_bitmap(void)
68{
69 uint start = 60;
70 for (uint nbits = start; nbits <= 192; nbits++) {
71 bitmap_t *map = bitmap_alloc(nbits);
72
73 for (uint i = 0; i < nbits; i++) {
74 bitmap_set(map, i);
75 }
f427ee49 76 assert(bitmap_is_full(map, nbits));
d9a64523
A
77
78 int expected_result = nbits - 1;
79 for (int i = bitmap_first(map, nbits); i >= 0; i = bitmap_next(map, i)) {
80 assert(i == expected_result);
81 expected_result--;
82 }
83 assert(expected_result == -1);
84
f427ee49
A
85 bitmap_zero(map, nbits);
86
87 assert(bitmap_first(map, nbits) == -1);
88 assert(bitmap_lsb_first(map, nbits) == -1);
89
90 bitmap_full(map, nbits);
91 assert(bitmap_is_full(map, nbits));
92
93 expected_result = nbits - 1;
94 for (int i = bitmap_first(map, nbits); i >= 0; i = bitmap_next(map, i)) {
95 assert(i == expected_result);
96 expected_result--;
97 }
98 assert(expected_result == -1);
99
d9a64523
A
100 expected_result = 0;
101 for (int i = bitmap_lsb_first(map, nbits); i >= 0; i = bitmap_lsb_next(map, nbits, i)) {
102 assert(i == expected_result);
103 expected_result++;
104 }
105 assert(expected_result == (int)nbits);
106
f427ee49
A
107 for (uint i = 0; i < nbits; i++) {
108 bitmap_clear(map, i);
109 assert(!bitmap_is_full(map, nbits));
110 bitmap_set(map, i);
111 assert(bitmap_is_full(map, nbits));
112 }
113
d9a64523
A
114 for (uint i = 0; i < nbits; i++) {
115 bitmap_clear(map, i);
116 }
117 assert(bitmap_first(map, nbits) == -1);
118 assert(bitmap_lsb_first(map, nbits) == -1);
119
120 bitmap_free(map, nbits);
121 }
122}
123
124kern_return_t
125bitmap_post_test(void)
126{
127 test_bitmap();
128
129 kern_return_t ret = KERN_SUCCESS;
130
131 T_ASSERT(ret == KERN_SUCCESS, NULL);
132
133 return ret;
134}
135#endif