CUBRID Engine  latest
unittests_bit.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * unittest_bit.c : unit tests for bit operations
21  */
22 
23 #include "bit.h"
24 #include "system.h"
25 
26 #include <assert.h>
27 #include <cstring>
28 #include <stdlib.h>
29 
30 static int
31 count_bits (const unsigned char *mem, int nbits)
32 {
33  int byte, bit;
34  int count = 0;
35 
36  for (byte = 0; byte < nbits / 8; byte++)
37  {
38  for (bit = 0; bit < 8; bit++)
39  {
40  if ((mem[byte] & (((char) 1) << bit)) != 0)
41  {
42  count++;
43  }
44  }
45  }
46  return count;
47 }
48 
49 static int
50 ctz8 (UINT8 x)
51 {
52  int i;
53  for (i = 0; i < 8; i++)
54  {
55  if ((x & (((UINT8) 1) << i)) != 0)
56  {
57  break;
58  }
59  }
60  return i;
61 }
62 
63 static int
64 ctz16 (UINT16 x)
65 {
66  int i;
67  for (i = 0; i < 16; i++)
68  {
69  if ((x & (((UINT16) 1) << i)) != 0)
70  {
71  break;
72  }
73  }
74  return i;
75 }
76 
77 static int
78 ctz32 (UINT32 x)
79 {
80  int i;
81  for (i = 0; i < 32; i++)
82  {
83  if ((x & (((UINT32) 1) << i)) != 0)
84  {
85  break;
86  }
87  }
88  return i;
89 }
90 
91 static int
92 ctz64 (UINT64 x)
93 {
94  int i;
95  for (i = 0; i < 16; i++)
96  {
97  if ((x & (((UINT64) 1) << i)) != 0)
98  {
99  break;
100  }
101  }
102  return i;
103 }
104 
105 static void
106 bitset_string (const unsigned char *mem, int nbits, char *bitset)
107 {
108  int byte, bit;
109  int count = 0;
110 
111  for (byte = 0; byte < nbits / 8; byte++)
112  {
113  for (bit = 0; bit < 8; bit++)
114  {
115  bitset[count++] = ((mem[byte] & (((char) 1) << bit)) != 0) ? '1' : '0';
116  }
117  bitset[count++] = ' ';
118  }
119  bitset[count] = '\0';
120 }
121 
122 int
123 main (int ignore_argc, char **ignore_argv)
124 {
125  int i;
126  UINT8 ub;
127  UINT16 us;
128  UINT32 ui;
129  UINT64 ull;
130 
131  int unittest_result;
132  int bit_result;
133 
134  char bitset_buf[100];
135 
136  int rands[2];
137 
138  printf ("start test\n");
139 
140  srand (time (NULL));
141 
142  for (i = 0; i < 256; i++)
143  {
144  rands[0] = rand ();
145  rands[1] = rand ();
146 
147  /* check bit8 */
148  ub = i;
149  /* check bit count */
150  unittest_result = count_bits (&ub, 8);
151  bit_result = bit8_count_ones (ub);
152  if (unittest_result != bit_result)
153  {
154  bitset_string (&ub, 8, bitset_buf);
155  printf ("error bit8_count_ones: \n"
156  "bitset: %s\n unit test: %d\n bit8: %d\n", bitset_buf, unittest_result, bit_result);
157  abort ();
158  }
159  unittest_result = 8 - unittest_result;
160  bit_result = bit8_count_zeros (ub);
161  if (unittest_result != bit_result)
162  {
163  bitset_string (&ub, 8, bitset_buf);
164  printf ("error bit8_count_zeros: \n"
165  "bitset: %s\n unit test: %d\n bit8: %d\n", bitset_buf, unittest_result, bit_result);
166  abort ();
167  }
168  /* check count trailing zeros/ones */
169  unittest_result = ctz8 (ub);
170  bit_result = bit8_count_trailing_zeros (ub);
171  if (unittest_result != bit_result)
172  {
173  bitset_string (&ub, 8, bitset_buf);
174  printf ("error bit8_count_trailing_zeros: \n"
175  "bitset: %s\n unit test: %d\n bit8: %d\n", bitset_buf, unittest_result, bit_result);
176  abort ();
177  }
178  unittest_result = ctz8 (~ub);
179  bit_result = bit8_count_trailing_ones (ub);
180  if (unittest_result != bit_result)
181  {
182  bitset_string (&ub, 8, bitset_buf);
183  printf ("error bit8_count_trailing_ones: \n"
184  "bitset: %s\n unit test: %d\n bit8: %d\n", bitset_buf, unittest_result, bit_result);
185  abort ();
186  }
187 
188  /* check bit16 */
189  std::memcpy (&us, rands, sizeof (UINT16));
190  /* check bit count */
191  unittest_result = count_bits ((unsigned char *) &us, 16);
192  bit_result = bit16_count_ones (us);
193  if (unittest_result != bit_result)
194  {
195  bitset_string ((unsigned char *) &us, 16, bitset_buf);
196  printf ("error bit16_count_ones: \n"
197  "bitset: %s\n unit test: %d\n bit16: %d\n", bitset_buf, unittest_result, bit_result);
198  abort ();
199  }
200  unittest_result = 16 - unittest_result;
201  bit_result = bit16_count_zeros (us);
202  if (unittest_result != bit_result)
203  {
204  bitset_string ((unsigned char *) &us, 16, bitset_buf);
205  printf ("error bit16_count_zeros: \n"
206  "bitset: %s\n unit test: %d\n bit16: %d\n", bitset_buf, unittest_result, bit_result);
207  abort ();
208  }
209  /* check count trailing zeros/ones */
210  unittest_result = ctz16 (us);
211  bit_result = bit16_count_trailing_zeros (us);
212  if (unittest_result != bit_result)
213  {
214  bitset_string ((unsigned char *) &us, 16, bitset_buf);
215  printf ("error bit16_count_trailing_zeros: \n"
216  "bitset: %s\n unit test: %d\n bit16: %d\n", bitset_buf, unittest_result, bit_result);
217  abort ();
218  }
219  unittest_result = ctz16 (~us);
220  bit_result = bit16_count_trailing_ones (us);
221  if (unittest_result != bit_result)
222  {
223  bitset_string ((unsigned char *) &us, 16, bitset_buf);
224  printf ("error bit16_count_trailing_ones: \n"
225  "bitset: %s\n unit test: %d\n bit16: %d\n", bitset_buf, unittest_result, bit_result);
226  abort ();
227  }
228 
229  /* check bit32 */
230  ui = *(UINT32 *) rands;
231  /* check bit count */
232  unittest_result = count_bits ((unsigned char *) &ui, 32);
233  bit_result = bit32_count_ones (ui);
234  if (unittest_result != bit_result)
235  {
236  bitset_string ((unsigned char *) &ui, 32, bitset_buf);
237  printf ("error bit32_count_ones: \n"
238  "bitset: %s\n unit test: %d\n bit32: %d\n", bitset_buf, unittest_result, bit_result);
239  abort ();
240  }
241  unittest_result = 32 - unittest_result;
242  bit_result = bit32_count_zeros (ui);
243  if (unittest_result != bit_result)
244  {
245  bitset_string ((unsigned char *) &ui, 32, bitset_buf);
246  printf ("error bit32_count_zeros: \n"
247  "bitset: %s\n unit test: %d\n bit32: %d\n", bitset_buf, unittest_result, bit_result);
248  abort ();
249  }
250  /* check count trailing zeros/ones */
251  unittest_result = ctz32 (ui);
252  bit_result = bit32_count_trailing_zeros (ui);
253  if (unittest_result != bit_result)
254  {
255  bitset_string ((unsigned char *) &ui, 32, bitset_buf);
256  printf ("error bit32_count_trailing_zeros: \n"
257  "bitset: %s\n unit test: %d\n bit32: %d\n", bitset_buf, unittest_result, bit_result);
258  abort ();
259  }
260  unittest_result = ctz32 (~ui);
261  bit_result = bit32_count_trailing_ones (ui);
262  if (unittest_result != bit_result)
263  {
264  bitset_string ((unsigned char *) &ui, 32, bitset_buf);
265  printf ("error bit32_count_trailing_ones: \n"
266  "bitset: %s\n unit test: %d\n bit32: %d\n", bitset_buf, unittest_result, bit_result);
267  abort ();
268  }
269 
270  /* check bit64 */
271  std::memcpy (&ull, rands, sizeof (UINT64));
272  /* check bit count */
273  unittest_result = count_bits ((unsigned char *) &ull, 64);
274  bit_result = bit64_count_ones (ull);
275  if (unittest_result != bit_result)
276  {
277  bitset_string ((unsigned char *) &ull, 64, bitset_buf);
278  printf ("error bit64_count_ones: \n"
279  "bitset: %s\n unit test: %d\n bit64: %d\n", bitset_buf, unittest_result, bit_result);
280  abort ();
281  }
282  unittest_result = 64 - unittest_result;
283  bit_result = bit64_count_zeros (ull);
284  if (unittest_result != bit_result)
285  {
286  bitset_string ((unsigned char *) &ull, 64, bitset_buf);
287  printf ("error bit64_count_zeros: \n"
288  "bitset: %s\n unit test: %d\n bit64: %d\n", bitset_buf, unittest_result, bit_result);
289  abort ();
290  }
291  /* check count trailing zeros/ones */
292  unittest_result = ctz64 (ull);
293  bit_result = bit64_count_trailing_zeros (ull);
294  if (unittest_result != bit_result)
295  {
296  bitset_string ((unsigned char *) &ull, 64, bitset_buf);
297  printf ("error bit64_count_trailing_zeros: \n"
298  "bitset: %s\n unit test: %d\n bit64: %d\n", bitset_buf, unittest_result, bit_result);
299  abort ();
300  }
301  unittest_result = ctz64 (~ull);
302  bit_result = bit64_count_trailing_ones (ull);
303  if (unittest_result != bit_result)
304  {
305  bitset_string ((unsigned char *) &ull, 64, bitset_buf);
306  printf ("error bit64_count_trailing_ones: \n"
307  "bitset: %s\n unit test: %d\n bit64: %d\n", bitset_buf, unittest_result, bit_result);
308  abort ();
309  }
310  }
311  printf ("success\n");
312  return 0;
313 }
int bit64_count_trailing_zeros(UINT64 i)
Definition: bit.c:466
static int count_bits(const unsigned char *mem, int nbits)
Definition: unittests_bit.c:31
int bit8_count_trailing_ones(UINT8 i)
Definition: bit.c:94
int bit16_count_zeros(UINT16 i)
Definition: bit.c:180
int bit64_count_trailing_ones(UINT64 i)
Definition: bit.c:513
int bit8_count_trailing_zeros(UINT8 i)
Definition: bit.c:65
int bit32_count_zeros(UINT32 i)
Definition: bit.c:313
static int ctz16(UINT16 x)
Definition: unittests_bit.c:64
int bit16_count_trailing_ones(UINT16 i)
Definition: bit.c:220
int bit16_count_trailing_zeros(UINT16 i)
Definition: bit.c:187
static int ctz64(UINT64 x)
Definition: unittests_bit.c:92
int bit8_count_zeros(UINT8 i)
Definition: bit.c:58
static int ctz8(UINT8 x)
Definition: unittests_bit.c:50
int bit32_count_trailing_zeros(UINT32 i)
Definition: bit.c:319
#define NULL
Definition: freelistheap.h:34
int count(int &result, const cub_regex_object &reg, const std::string &src, const int position, const INTL_CODESET codeset)
int bit16_count_ones(UINT16 i)
Definition: bit.c:173
static int ctz32(UINT32 x)
Definition: unittests_bit.c:78
int bit32_count_ones(UINT32 i)
Definition: bit.c:304
int bit64_count_zeros(UINT64 i)
Definition: bit.c:460
static const char nbits[]
Definition: query_bitset.c:44
int main(int ignore_argc, char **ignore_argv)
int bit32_count_trailing_ones(UINT32 i)
Definition: bit.c:362
int i
Definition: dynamic_load.c:954
int bit64_count_ones(UINT64 i)
Definition: bit.c:451
int bit8_count_ones(UINT8 i)
Definition: bit.c:51
static void bitset_string(const unsigned char *mem, int nbits, char *bitset)