]>
Commit | Line | Data |
---|---|---|
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 | ||
36 | extern void dump_bitmap_next(bitmap_t *map, uint nbits); | |
37 | extern void dump_bitmap_lsb(bitmap_t *map, uint nbits); | |
38 | extern void test_bitmap(void); | |
39 | extern kern_return_t bitmap_post_test(void); | |
40 | ||
41 | void | |
42 | dump_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 | ||
50 | void | |
51 | dump_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 | ||
66 | void | |
67 | test_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 | } | |
76 | ||
77 | int expected_result = nbits - 1; | |
78 | for (int i = bitmap_first(map, nbits); i >= 0; i = bitmap_next(map, i)) { | |
79 | assert(i == expected_result); | |
80 | expected_result--; | |
81 | } | |
82 | assert(expected_result == -1); | |
83 | ||
84 | expected_result = 0; | |
85 | for (int i = bitmap_lsb_first(map, nbits); i >= 0; i = bitmap_lsb_next(map, nbits, i)) { | |
86 | assert(i == expected_result); | |
87 | expected_result++; | |
88 | } | |
89 | assert(expected_result == (int)nbits); | |
90 | ||
91 | for (uint i = 0; i < nbits; i++) { | |
92 | bitmap_clear(map, i); | |
93 | } | |
94 | assert(bitmap_first(map, nbits) == -1); | |
95 | assert(bitmap_lsb_first(map, nbits) == -1); | |
96 | ||
97 | bitmap_free(map, nbits); | |
98 | } | |
99 | } | |
100 | ||
101 | kern_return_t | |
102 | bitmap_post_test(void) | |
103 | { | |
104 | test_bitmap(); | |
105 | ||
106 | kern_return_t ret = KERN_SUCCESS; | |
107 | ||
108 | T_ASSERT(ret == KERN_SUCCESS, NULL); | |
109 | ||
110 | return ret; | |
111 | } | |
112 | #endif |