1 /*
2  * Copyright © 2009 CNRS
3  * Copyright © 2009-2011 inria.  All rights reserved.
4  * Copyright © 2009, 2012 Université Bordeaux 1
5  * Copyright © 2011 Cisco Systems, Inc.  All rights reserved.
6  * See COPYING in top-level directory.
7  */
8 
9 #include <hwloc.h>
10 
11 #include <assert.h>
12 
13 /* check misc bitmap stuff */
14 
main(void)15 int main(void)
16 {
17   hwloc_bitmap_t set;
18 
19   /* check an empty bitmap */
20   set = hwloc_bitmap_alloc();
21   assert(hwloc_bitmap_to_ulong(set) == 0UL);
22   assert(hwloc_bitmap_to_ith_ulong(set, 0) == 0UL);
23   assert(hwloc_bitmap_to_ith_ulong(set, 1) == 0UL);
24   assert(hwloc_bitmap_to_ith_ulong(set, 23) == 0UL);
25   /* check a non-empty bitmap */
26   hwloc_bitmap_from_ith_ulong(set, 4, 0xff);
27   assert(hwloc_bitmap_to_ith_ulong(set, 4) == 0xff);
28   assert(hwloc_bitmap_to_ulong(set) == 0UL);
29   assert(hwloc_bitmap_to_ith_ulong(set, 0) == 0UL);
30   assert(hwloc_bitmap_to_ith_ulong(set, 1) == 0UL);
31   assert(hwloc_bitmap_to_ith_ulong(set, 23) == 0UL);
32   /* check a two-long bitmap */
33   hwloc_bitmap_from_ith_ulong(set, 4, 0xfe);
34   hwloc_bitmap_set_ith_ulong(set, 6, 0xef);
35   assert(hwloc_bitmap_to_ith_ulong(set, 4) == 0xfe);
36   assert(hwloc_bitmap_to_ith_ulong(set, 6) == 0xef);
37   assert(hwloc_bitmap_to_ulong(set) == 0UL);
38   assert(hwloc_bitmap_to_ith_ulong(set, 0) == 0UL);
39   assert(hwloc_bitmap_to_ith_ulong(set, 1) == 0UL);
40   assert(hwloc_bitmap_to_ith_ulong(set, 23) == 0UL);
41   /* check a zeroed bitmap */
42   hwloc_bitmap_zero(set);
43   assert(hwloc_bitmap_to_ulong(set) == 0UL);
44   assert(hwloc_bitmap_to_ith_ulong(set, 0) == 0UL);
45   assert(hwloc_bitmap_to_ith_ulong(set, 1) == 0UL);
46   assert(hwloc_bitmap_to_ith_ulong(set, 4) == 0UL);
47   assert(hwloc_bitmap_to_ith_ulong(set, 23) == 0UL);
48   hwloc_bitmap_free(set);
49 
50   /* check a full bitmap */
51   set = hwloc_bitmap_alloc_full();
52   assert(hwloc_bitmap_to_ulong(set) == ~0UL);
53   assert(hwloc_bitmap_to_ith_ulong(set, 0) == ~0UL);
54   assert(hwloc_bitmap_to_ith_ulong(set, 1) == ~0UL);
55   assert(hwloc_bitmap_to_ith_ulong(set, 23) == ~0UL);
56   /* check a almost full bitmap */
57   hwloc_bitmap_set_ith_ulong(set, 4, 0xff);
58   assert(hwloc_bitmap_to_ith_ulong(set, 4) == 0xff);
59   assert(hwloc_bitmap_to_ulong(set) == ~0UL);
60   assert(hwloc_bitmap_to_ith_ulong(set, 0) == ~0UL);
61   assert(hwloc_bitmap_to_ith_ulong(set, 1) == ~0UL);
62   assert(hwloc_bitmap_to_ith_ulong(set, 23) == ~0UL);
63   /* check a almost empty bitmap */
64   hwloc_bitmap_from_ith_ulong(set, 4, 0xff);
65   assert(hwloc_bitmap_to_ith_ulong(set, 4) == 0xff);
66   assert(hwloc_bitmap_to_ulong(set) == 0UL);
67   assert(hwloc_bitmap_to_ith_ulong(set, 0) == 0UL);
68   assert(hwloc_bitmap_to_ith_ulong(set, 1) == 0UL);
69   assert(hwloc_bitmap_to_ith_ulong(set, 23) == 0UL);
70   hwloc_bitmap_free(set);
71 
72   /* check ranges */
73   set = hwloc_bitmap_alloc();
74   assert(hwloc_bitmap_weight(set) == 0);
75   /* 23-45 */
76   hwloc_bitmap_set_range(set, 23, 45);
77   assert(hwloc_bitmap_weight(set) == 23);
78   /* 23-45,78- */
79   hwloc_bitmap_set_range(set, 78, -1);
80   assert(hwloc_bitmap_weight(set) == -1);
81   /* 23- */
82   hwloc_bitmap_set_range(set, 44, 79);
83   assert(hwloc_bitmap_weight(set) == -1);
84   assert(hwloc_bitmap_first(set) == 23);
85   assert(!hwloc_bitmap_isfull(set));
86   /* 0- */
87   hwloc_bitmap_set_range(set, 0, 22);
88   assert(hwloc_bitmap_weight(set) == -1);
89   assert(hwloc_bitmap_isfull(set));
90   /* 0-34,57- */
91   hwloc_bitmap_clr_range(set, 35, 56);
92   assert(hwloc_bitmap_weight(set) == -1);
93   assert(!hwloc_bitmap_isfull(set));
94   /* 0-34,57 */
95   hwloc_bitmap_clr_range(set, 58, -1);
96   assert(hwloc_bitmap_weight(set) == 36);
97   assert(hwloc_bitmap_last(set) == 57);
98   assert(hwloc_bitmap_next(set, 34) == 57);
99   /* 0-34 */
100   hwloc_bitmap_clr(set, 57);
101   assert(hwloc_bitmap_weight(set) == 35);
102   assert(hwloc_bitmap_last(set) == 34);
103   /* empty */
104   hwloc_bitmap_clr_range(set, 0, 34);
105   assert(hwloc_bitmap_weight(set) == 0);
106   assert(hwloc_bitmap_first(set) == -1);
107   hwloc_bitmap_free(set);
108 
109   /* check miscellaneous other functions */
110   set = hwloc_bitmap_alloc();
111   /* from_ulong */
112   hwloc_bitmap_from_ulong(set, 0x0ff0);
113   assert(hwloc_bitmap_first(set) == 4);
114   assert(hwloc_bitmap_last(set) == 11);
115   assert(hwloc_bitmap_weight(set) == 8);
116   assert(hwloc_bitmap_to_ith_ulong(set, 0) == 0xff0);
117   assert(hwloc_bitmap_to_ith_ulong(set, 1) == 0);
118   /* from_ith_ulong */
119   hwloc_bitmap_zero(set);
120   assert(hwloc_bitmap_weight(set) == 0);
121   hwloc_bitmap_from_ith_ulong(set, 2, 0xff00);
122   assert(hwloc_bitmap_weight(set) == 8);
123   assert(hwloc_bitmap_to_ith_ulong(set, 0) == 0);
124   assert(hwloc_bitmap_to_ith_ulong(set, 1) == 0);
125   assert(hwloc_bitmap_to_ith_ulong(set, 2) == 0xff00);
126   assert(hwloc_bitmap_to_ith_ulong(set, 3) == 0);
127   /* allbut and not */
128   hwloc_bitmap_allbut(set, 153);
129   assert(hwloc_bitmap_weight(set) == -1);
130   hwloc_bitmap_not(set, set);
131   assert(hwloc_bitmap_weight(set) == 1);
132   assert(hwloc_bitmap_first(set) == 153);
133   assert(hwloc_bitmap_last(set) == 153);
134   /* clr_range */
135   hwloc_bitmap_fill(set);
136   hwloc_bitmap_clr_range(set, 178, 3589);
137   hwloc_bitmap_not(set, set);
138   assert(hwloc_bitmap_weight(set) == 3589-178+1);
139   assert(hwloc_bitmap_first(set) == 178);
140   assert(hwloc_bitmap_last(set) == 3589);
141   /* singlify */
142   hwloc_bitmap_zero(set);
143   hwloc_bitmap_set_range(set, 0, 127);
144   assert(hwloc_bitmap_weight(set) == 128);
145   hwloc_bitmap_not(set, set);
146   assert(hwloc_bitmap_weight(set) == -1);
147   hwloc_bitmap_singlify(set);
148   assert(hwloc_bitmap_weight(set) == 1);
149   assert(hwloc_bitmap_first(set) == 128);
150   assert(hwloc_bitmap_last(set) == 128);
151 
152   hwloc_bitmap_free(set);
153 
154   return 0;
155 }
156