BRL-CAD
bu_bitv.c
Go to the documentation of this file.
1 /* B U _ B I T V . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1985-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "bu.h"
28 #include "../bu_internals.h"
29 #include "./test_internals.h"
30 
31 
32 static int
33 test_bu_bitv_master()
34 {
35  /*
36  * This tests a round trip capability to
37  *
38  * 1. a. Get a random hex nine-byte string and convert it to a bitv.
39  * b. Convert the bitv to a hex string.
40  * c. Compare the two hex strings successfully.
41  * 2. a. Get a random binary nine-byte string and convert it to a bitv.
42  * b. Convert the bitv to a binary string.
43  * c. Compare the two binary strings successfully.
44  */
45  struct bu_vls rand_hex_str = BU_VLS_INIT_ZERO;
46  struct bu_vls rand_bin_str = BU_VLS_INIT_ZERO;
47  struct bu_vls bv2hex_str = BU_VLS_INIT_ZERO;
48  struct bu_vls bv2bin_str = BU_VLS_INIT_ZERO;
49  struct bu_bitv *bv_rhs;
50  struct bu_bitv *bv_rbs;
51  int test_results = CTEST_FAIL;
52  int thex, tbin;
53 
54  /* get the random strings */
55  random_hex_or_binary_string(&rand_hex_str, HEX_RAW, 9);
56  random_hex_or_binary_string(&rand_bin_str, BINARY, 9);
57 
58  /* convert to bitvs */
59  bv_rhs = bu_hex_to_bitv(bu_vls_cstr(&rand_hex_str));
60  bv_rbs = bu_binary_to_bitv(bu_vls_cstr(&rand_bin_str));
61 
62  /* check for failures */
63  if (bv_rhs == NULL) {
64  bu_log("\nERROR: NULL from bu_hex_to_bitv.");
65  test_results = CTEST_FAIL;
66  goto ERROR_RETURN;
67  }
68  if (bv_rbs == NULL) {
69  bu_log("\nERROR: NULL from bu_binary_to_bitv.");
70  test_results = CTEST_FAIL;
71  goto ERROR_RETURN;
72  }
73 
74 
75  /* bitvs back to strings */
76  bu_bitv_to_hex(&bv2hex_str, bv_rhs);
77  bu_bitv_to_binary(&bv2bin_str, bv_rbs);
78 
79  /* finally, the comparisons (not case sensitive) */
80  thex = BU_STR_EQUIV(bu_vls_cstr(&rand_hex_str), bu_vls_cstr(&bv2hex_str));
81  tbin = BU_STR_EQUIV(bu_vls_cstr(&rand_bin_str), bu_vls_cstr(&bv2bin_str));
82 
83  if (thex)
84  printf("\nbu_hex_bitv_master PASSED");
85  else
86  printf("\nbu_hex_bitv_master FAILED");
87 
88  printf("\n Input: '%s'", bu_vls_cstr(&rand_hex_str));
89  printf("\n Output: '%s'", bu_vls_cstr(&bv2hex_str));
90 
91  if (tbin)
92  printf("\nbu_bin_bitv_master PASSED");
93  else
94  printf("\nbu_bin_bitv_master FAILED");
95 
96  printf("\n Input: '%s'", bu_vls_cstr(&rand_bin_str));
97  printf("\n Output: '%s'", bu_vls_cstr(&bv2bin_str));
98 
99  /* both tests must pass for success */
100  if (thex && tbin)
101  test_results = CTEST_PASS;
102  else
103  test_results = CTEST_FAIL;
104 
105 ERROR_RETURN:
106 
107  if (bv_rhs)
108  bu_bitv_free(bv_rhs);
109  if (bv_rbs)
110  bu_bitv_free(bv_rbs);
111  bu_vls_free(&rand_hex_str);
112  bu_vls_free(&rand_bin_str);
113  bu_vls_free(&bv2hex_str);
114  bu_vls_free(&bv2bin_str);
115 
116  return test_results;
117 }
118 
119 
120 static int
121 test_bu_hex_to_bitv(int argc, char **argv)
122 {
123  /* argv[1] argv[2] argv[3]
124  * inputs: <func num> <input hex string> <expected bitv dump>
125  */
126  struct bu_bitv *res_bitv ;
127  struct bu_vls v = BU_VLS_INIT_ZERO;
128  int test_results = CTEST_FAIL;
129  const char *input, *expected;
130 
131  if (argc < 4)
132  bu_exit(1, "ERROR: input format: function_num function_test_args [%s]\n", argv[0]);
133 
134  input = argv[2];
135  expected = argv[3];
136 
137  res_bitv = bu_hex_to_bitv(input);
138  if (res_bitv == NULL) {
139  printf("\nbu_hex_to_bitv FAILED Input: '%s' Output: 'NULL' Expected: '%s'",
140  input, expected);
141  test_results = CTEST_FAIL;
142  goto ERROR_RETURN;
143  }
144 
145  bu_bitv_vls(&v, res_bitv);
146 
147  if (BU_STR_EQUAL(bu_vls_cstr(&v), expected)) {
148  test_results = CTEST_PASS;
149  printf("\nbu_hex_to_bitv PASSED");
150  } else {
151  test_results = CTEST_FAIL;
152  printf("\nbu_hex_to_bitv FAILED");
153  }
154 
155  printf("\n Input: '%s'", input);
156  printf("\n Output: '%s'", bu_vls_cstr(&v));
157  printf("\n Expected: '%s'", expected);
158 
159 ERROR_RETURN:
160 
161  if (res_bitv != NULL)
162  bu_bitv_free(res_bitv);
163  bu_vls_free(&v);
164 
165  return test_results;
166 }
167 
168 
169 static int
170 test_bu_bitv_vls(int argc, char **argv)
171 {
172  /* argv[1] argv[2] argv[3]
173  * inputs: <func num> <input hex string> <expected bitv dump>
174  */
175  int test_results = CTEST_FAIL;
176  struct bu_vls a = BU_VLS_INIT_ZERO;
177  struct bu_bitv *res_bitv;
178  const char *input, *expected;
179 
180  if (argc < 4)
181  bu_exit(1, "ERROR: input format: function_num function_test_args [%s]\n", argv[0]);
182 
183  input = argv[2];
184  expected = argv[3];
185 
186  res_bitv = bu_hex_to_bitv(input);
187  if (res_bitv == NULL) {
188  bu_log("\nERROR: NULL from bu_hex_to_bitv.");
189  test_results = CTEST_FAIL;
190  goto ERROR_RETURN;
191  }
192 
193  bu_bitv_vls(&a, res_bitv);
194 
195  if (BU_STR_EQUAL(bu_vls_cstr(&a), expected)) {
196  test_results = CTEST_PASS;
197  printf("\nbu_bitv_vls test PASSED");
198  } else {
199  test_results = CTEST_FAIL;
200  printf("\nbu_bitv_vls test FAILED");
201  }
202 
203  printf("\n Input: '%s'", input);
204  printf("\n Output: '%s'", bu_vls_cstr(&a));
205  printf("\n Expected: '%s'", expected);
206 
207 ERROR_RETURN:
208 
209  bu_vls_free(&a);
210  bu_bitv_free(res_bitv);
211 
212  return test_results;
213 }
214 
215 
216 static int
217 test_bu_bitv_to_hex(int argc, char **argv)
218 {
219  /* argv[1] argv[2] argv[3] argv[4]
220  * inputs: <func num> <input char string> <expected hex string> <expected bitv length>
221  */
222  struct bu_vls result = BU_VLS_INIT_ZERO;
223  struct bu_bitv *result_bitv;
224  int length;
225  const char *input, *expected;
226  int test_results = CTEST_FAIL;
227 
228  if (argc < 5)
229  bu_exit(1, "ERROR: input format: function_num function_test_args [%s]\n", argv[0]);
230 
231  length = atoi(argv[4]);
232  if (length < (int)BITS_PER_BYTE || length % BITS_PER_BYTE) {
233  bu_exit(1, "ERROR: input format is function_num function_test_args [%s]\n", argv[0]);
234  }
235 
236  input = argv[2];
237  expected = argv[3];
238 
239  result_bitv = bu_bitv_new(length);
240 
241  if (result_bitv == NULL) {
242  bu_log("\nERROR: NULL from bu_bitv_new.");
243  test_results = CTEST_FAIL;
244  goto ERROR_RETURN;
245  }
246 
247  /* accessing the bits array directly as a char* is not safe since
248  * there's no bounds checking and assumes implementation is
249  * contiguous memory.
250  */
251  bu_strlcpy((char*)result_bitv->bits, input, length/BITS_PER_BYTE);
252 
253  bu_bitv_to_hex(&result, result_bitv);
254 
255  if (BU_STR_EQUAL(bu_vls_cstr(&result), expected)) {
256  test_results = CTEST_PASS;
257  printf("\nbu_bitv_to_hex PASSED");
258  } else {
259  test_results = CTEST_FAIL;
260  printf("\nbu_bitv_to_hex FAILED");
261  }
262 
263  printf("\n Input: '%s'", input);
264  printf("\n Output: '%s'", bu_vls_cstr(&result));
265  printf("\n Expected: '%s'", expected);
266 
267 ERROR_RETURN:
268 
269  if (result_bitv)
270  bu_bitv_free(result_bitv);
271  bu_vls_free(&result);
272 
273  return test_results;
274 }
275 
276 
277 static unsigned int
278 power(const unsigned int base, const int exponent)
279 {
280  int i ;
281  unsigned int product = 1;
282 
283  for (i = 0; i < exponent; i++)
284  product *= base;
285 
286  return product;
287 }
288 
289 
290 static int
291 test_bu_bitv_shift()
292 {
293  int res;
294  int test_results = CTEST_FAIL;
295 
296  printf("\nTesting bu_bitv_shift...");
297 
298  /*test bu_bitv_shift*/
299  res = bu_bitv_shift();
300 
301  if (power(2, res) <= (sizeof(bitv_t) * BITS_PER_BYTE)
302  && power(2, res + 1) > (sizeof(bitv_t) * BITS_PER_BYTE)) {
303  test_results = CTEST_PASS;
304  printf("\nPASSED: bu_bitv_shift working");
305  } else {
306  printf("\nFAILED: bu_bitv_shift incorrect");
307  test_results = CTEST_FAIL;
308  }
309 
310  return test_results;
311 }
312 
313 
314 static int
315 test_bu_bitv_or(int argc , char **argv)
316 {
317  /* argv[1] argv[2] argv[3] argv[4]
318  * inputs: <func num> <input hex string 1> <input hex string 2> <expected hex result>
319  */
320  struct bu_bitv *res_bitv , *res_bitv1 , *result;
321  struct bu_vls a = BU_VLS_INIT_ZERO;
322  struct bu_vls b = BU_VLS_INIT_ZERO;
323  int test_results = CTEST_FAIL;
324  const char *input1, *input2, *expected;
325 
326  if (argc < 5)
327  bu_exit(1, "ERROR: input format: function_num function_test_args [%s]\n", argv[0]);
328 
329  input1 = argv[2];
330  input2 = argv[3];
331  expected = argv[4];
332 
333  res_bitv1 = res_bitv = result = NULL;
334  res_bitv1 = bu_hex_to_bitv(input1);
335  if (res_bitv1 == NULL) {
336  bu_log("\nERROR: NULL from bu_hex_to_bitv.");
337  test_results = CTEST_FAIL;
338  goto ERROR_RETURN;
339  }
340  res_bitv = bu_hex_to_bitv(input2);
341  if (res_bitv == NULL) {
342  bu_log("\nERROR: NULL from bu_hex_to_bitv.");
343  test_results = CTEST_FAIL;
344  goto ERROR_RETURN;
345  }
346  result = bu_hex_to_bitv(expected);
347  if (result == NULL) {
348  bu_log("\nERROR: NULL from bu_hex_to_bitv.");
349  test_results = CTEST_FAIL;
350  goto ERROR_RETURN;
351  }
352 
353  bu_bitv_or(res_bitv1, res_bitv);
354  bu_bitv_vls(&a, res_bitv1);
355  bu_bitv_vls(&b, result);
356 
357  if (BU_STR_EQUAL(bu_vls_cstr(&a), bu_vls_cstr(&b))) {
358  test_results = CTEST_PASS;
359  printf("\nbu_bitv_or test PASSED");
360  } else {
361  test_results = CTEST_FAIL;
362  printf("\nbu_bitv_or test FAILED");
363  }
364 
365  printf("\n Input1: '%s'", input1);
366  printf("\n Input2: '%s'", input2);
367  printf("\n Expected: '%s'", expected);
368 
369 ERROR_RETURN:
370 
371  bu_vls_free(&a);
372  bu_vls_free(&b);
373  if (res_bitv)
374  bu_bitv_free(res_bitv);
375  if (res_bitv1)
376  bu_bitv_free(res_bitv1);
377  if (result)
378  bu_bitv_free(result);
379 
380  return test_results;
381 }
382 
383 
384 static int
385 test_bu_bitv_and(int argc, char **argv)
386 {
387  /* argv[1] argv[2] argv[3] argv[4]
388  * inputs: <func_num> <input hex string 1> <input hex string 2> <expected hex result>
389  */
390  struct bu_bitv *res_bitv , *res_bitv1 , *result;
391  struct bu_vls a = BU_VLS_INIT_ZERO;
392  struct bu_vls b = BU_VLS_INIT_ZERO;
393  int test_results = CTEST_FAIL;
394  const char *input1, *input2, *expected;
395 
396  if (argc < 5) {
397  bu_exit(1, "ERROR: input format: function_num function_test_args [%s]\n", argv[0]);
398  }
399 
400  input1 = argv[2];
401  input2 = argv[3];
402  expected = argv[4];
403 
404  res_bitv1 = res_bitv = result = NULL;
405  res_bitv1 = bu_hex_to_bitv(input1);
406  if (res_bitv1 == NULL) {
407  bu_log("\nERROR: NULL from bu_hex_to_bitv.");
408  test_results = CTEST_FAIL;
409  goto ERROR_RETURN;
410  }
411  res_bitv = bu_hex_to_bitv(input2);
412  if (res_bitv == NULL) {
413  bu_log("\nERROR: NULL from bu_hex_to_bitv.");
414  test_results = CTEST_FAIL;
415  goto ERROR_RETURN;
416  }
417  result = bu_hex_to_bitv(expected);
418  if (result == NULL) {
419  bu_log("\nERROR: NULL from bu_hex_to_bitv.");
420  test_results = CTEST_FAIL;
421  goto ERROR_RETURN;
422  }
423 
424  bu_bitv_and(res_bitv1, res_bitv);
425  bu_bitv_vls(&a, res_bitv1);
426  bu_bitv_vls(&b, result);
427 
428  if (BU_STR_EQUAL(bu_vls_cstr(&a), bu_vls_cstr(&b))) {
429  test_results = CTEST_PASS;
430  printf("\nbu_bitv_and test PASSED");
431  } else {
432  test_results = CTEST_FAIL;
433  printf("\nbu_bitv_and test FAILED");
434  }
435 
436  printf(" Input1: '%s'", input1);
437  printf(" Input2: '%s'", input2);
438  printf(" Output: '%s'", bu_vls_cstr(&b));
439  printf(" Expected: '%s'", expected);
440 
441 ERROR_RETURN:
442 
443  bu_vls_free(&a);
444  bu_vls_free(&b);
445  if (res_bitv)
446  bu_bitv_free(res_bitv);
447  if (res_bitv1)
448  bu_bitv_free(res_bitv1);
449  if (result)
450  bu_bitv_free(result);
451 
452  return test_results;
453 }
454 
455 
456 static int
457 test_bu_bitv_compare_equal(int argc, char **argv)
458 {
459  /* argv[1] argv[2] argv[3] argv[4] argv[5]
460  * inputs: <func num> <binary string> <nbytes> <binary string> <nbytes>
461  */
462  int test_results = CTEST_FAIL;
463  struct bu_vls v1 = BU_VLS_INIT_ZERO;
464  struct bu_vls v2 = BU_VLS_INIT_ZERO;
465  struct bu_bitv *b1, *b2;
466  int lenbytes1 = atoi(argv[3]);
467  int lenbytes2 = atoi(argv[5]);
468 
469  if (argc < 6)
470  bu_exit(1, "ERROR: input format is function_num function_test_args [%s]\n", argv[0]);
471 
472  bu_vls_strcpy(&v1, argv[2]);
473  bu_vls_strcpy(&v2, argv[4]);
474 
475  b1 = bu_binary_to_bitv2(bu_vls_cstr(&v1), lenbytes1);
476  b2 = bu_binary_to_bitv2(bu_vls_cstr(&v2), lenbytes2);
477 
478  BU_CK_VLS(&v1);
479  BU_CK_VLS(&v2);
480  BU_CK_BITV(b1);
481  BU_CK_BITV(b2);
482 
483  printf("\nInput bitv 1: '%s'", bu_vls_cstr(&v1));
484  printf("\nExpected bitv dump:");
485  dump_bitv(b1);
486 
487  printf("\nInput bitv 2: '%s'", bu_vls_cstr(&v2));
488  printf("\nExpected bitv dump:");
489  dump_bitv(b2);
490 
491  /* see what another function does */
492  bu_vls_trunc(&v1, 0);
493  bu_vls_trunc(&v2, 0);
494  bu_bitv_vls(&v1, b1);
495  bu_bitv_vls(&v2, b2);
496  printf("\nInput bitv 1 from vls: '%s'", bu_vls_cstr(&v1));
497  printf("\nInput bitv 2 from vls: '%s'", bu_vls_cstr(&v2));
498 
499  test_results = bu_bitv_compare_equal(b1, b2);
500 
501  bu_vls_free(&v1);
502  bu_vls_free(&v2);
503  bu_bitv_free(b1);
504  bu_bitv_free(b2);
505 
506  /* a true return above is a pass, invert the value to
507  * CTEST_PASS/CTEST_FAIL */
508  return !test_results;
509 }
510 
511 
512 static int
513 test_bu_bitv_compare_equal2(int argc, char **argv)
514 {
515  /* argv[1] argv[2] argv[3] argv[4] argv[5]
516  * inputs: <func num> <binary string> <nbytes> <binary string> <nbytes>
517  */
518  int test_results = CTEST_FAIL;
519  struct bu_vls v1 = BU_VLS_INIT_ZERO;
520  struct bu_vls v2 = BU_VLS_INIT_ZERO;
521  struct bu_bitv *b1, *b2;
522  int lenbytes1 = atoi(argv[3]);
523  int lenbytes2 = atoi(argv[5]);
524 
525  if (argc < 6)
526  bu_exit(1, "ERROR: input format is function_num function_test_args [%s]\n", argv[0]);
527 
528  bu_vls_strcpy(&v1, argv[2]);
529  bu_vls_strcpy(&v2, argv[4]);
530 
531  b1 = bu_binary_to_bitv2(bu_vls_cstr(&v1), lenbytes1);
532  b2 = bu_binary_to_bitv2(bu_vls_cstr(&v2), lenbytes2);
533 
534  BU_CK_VLS(&v1);
535  BU_CK_VLS(&v2);
536  BU_CK_BITV(b1);
537  BU_CK_BITV(b2);
538 
539  printf("\nInput bitv 1: '%s'", bu_vls_cstr(&v1));
540  printf("\nExpected bitv dump:");
541  dump_bitv(b1);
542 
543  printf("\nInput bitv 2: '%s'", bu_vls_cstr(&v2));
544  printf("\nExpected bitv dump:");
545  dump_bitv(b2);
546 
547  /* see what another function does */
548  bu_vls_trunc(&v1, 0);
549  bu_vls_trunc(&v2, 0);
550  bu_bitv_vls(&v1, b1);
551  bu_bitv_vls(&v2, b2);
552  printf("\nInput bitv 1 from vls: '%s'", bu_vls_cstr(&v1));
553  printf("\nInput bitv 2 from vls: '%s'", bu_vls_cstr(&v2));
554 
555  test_results = bu_bitv_compare_equal2(b1, b2);
556 
557  bu_vls_free(&v1);
558  bu_vls_free(&v2);
559  bu_bitv_free(b1);
560  bu_bitv_free(b2);
561 
562  /* a true return above is a pass, invert the value to
563  * CTEST_PASS/CTEST_FAIL */
564  return !test_results;
565 }
566 
567 
568 
569 static int
570 test_bu_bitv_to_binary(int argc, char **argv)
571 {
572  /* argv[1] argv[2] argv[3]
573  * inputs: <func num> <input hex num> <expected binary string>
574  */
575  int test_results = CTEST_FAIL;
576  unsigned long int input_num = strtoul(argv[2], (char **)NULL, 16);
577  struct bu_vls v = BU_VLS_INIT_ZERO;
578  struct bu_bitv *b1, *b2;
579  unsigned len;
580  int i;
581 
582  if (argc < 4)
583  bu_exit(1, "ERROR: input format is function_num function_test_args [%s]\n", argv[0]);
584 
585  bu_vls_strcpy(&v, argv[3]);
586  b1 = bu_bitv_new(sizeof(input_num) * BITS_PER_BYTE);
587  b2 = bu_binary_to_bitv(bu_vls_cstr(&v));
588  printf("\nExpected bitv: '%s'", bu_vls_cstr(&v));
589  printf("\nExpected bitv dump:");
590  dump_bitv(b2);
591 
592  BU_CK_VLS(&v);
593  BU_CK_BITV(b1);
594  BU_CK_BITV(b2);
595 
596  len = sizeof(input_num) * BITS_PER_BYTE;
597 
598  /* set the bits of the new bitv */
599  for (i = 0; i < (int)len; ++i) {
600  /* get the expected bit */
601  unsigned long i1 = (input_num >> i) & 0x1;
602  if (i1)
603  BU_BITSET(b1, i);
604  }
605 
606  printf("\nInput hex: '0x%lx'", (unsigned long)input_num);
607  printf("\nExpected binary string: '%s'", argv[3]);
608 
609  printf("\nOutput bitv: '%s'", argv[3]);
610  printf("\nOutput bitv dump:");
611  dump_bitv(b1);
612 
613  /* compare the two bitvs (without regard to length) */
614  test_results = bu_bitv_compare_equal2(b1, b2);
615 
616  bu_vls_free(&v);
617  bu_bitv_free(b1);
618  bu_bitv_free(b2);
619 
620  /* a true return above is a pass, invert the value to CTEST_PASS/CTEST_FAIL */
621  return !test_results;
622 }
623 
624 
625 static int
626 test_bu_binary_to_bitv(int argc, char **argv)
627 {
628  /* argv[1] argv[2] argv[3]
629  * inputs: <func num> <binary string> <expected hex num>
630  */
631  int test_results = CTEST_FAIL;
632  unsigned long int expected_num = strtol(argv[3], (char **)NULL, 16);
633  struct bu_vls v = BU_VLS_INIT_ZERO;
634  struct bu_bitv *b;
635  unsigned i, len, err = 0;
636  unsigned ull_size = sizeof(unsigned long long);
637 
638  if (argc < 4)
639  bu_exit(1, "ERROR: input format is function_num function_test_args [%s]\n", argv[0]);
640 
641  bu_vls_strcpy(&v, argv[2]);
643  printf("\nNew bitv: '%s'", bu_vls_cstr(&v));
644  printf("\nNew bitv dump:");
645  dump_bitv(b);
646 
647  BU_CK_VLS(&v);
648  BU_CK_BITV(b);
649 
650  len = b->nbits;
651 
652  /* now compare the bitv with the expected int */
653  len = len > ull_size ? ull_size : len;
654  for (i = 0; i < len; ++i) {
655  /* get the expected bit */
656  unsigned long i1 = (expected_num >> i) & 0x01;
657  unsigned long i2 = BU_BITTEST(b, i) ? 1 : 0;
658  if (i1 != i2) {
659  ++err;
660  break;
661  }
662  }
663 
664  printf("\n Input binary string: '%s'", argv[2]);
665  printf("\n Expected hex: '0x%x'", (unsigned)expected_num);
666 
667  bu_vls_trunc(&v, 0);
668  bu_bitv_vls(&v, b);
669  printf("\nInput bitv: '%s'", bu_vls_cstr(&v));
670  printf("\nInput bitv dump:");
671  dump_bitv(b);
672 
673  test_results = err ? CTEST_FAIL : CTEST_PASS;
674 
675  bu_vls_free(&v);
676  bu_bitv_free(b);
677 
678  return test_results;
679 }
680 
681 
682 static int
683 test_bu_binary_to_bitv2(int argc, char **argv)
684 {
685  /* argv[1] argv[2] argv[3] argv[4]
686  * inputs: <func num> <binary string> <min nbytes> <expected hex num>
687  */
688  int test_results = CTEST_FAIL;
689  unsigned nbytes = (unsigned)strtol(argv[3], (char **)NULL, 10);
690  unsigned long long int expected_num = strtol(argv[4], (char **)NULL, 16);
691  unsigned ull_size = sizeof(unsigned long long);
692  struct bu_vls v = BU_VLS_INIT_ZERO;
693  struct bu_bitv *b;
694  unsigned i, len, err = 0;
695 
696  if (argc < 5)
697  bu_exit(1, "ERROR: input format is function_num function_test_args [%s]\n", argv[0]);
698 
699  bu_vls_strcpy(&v, argv[2]);
700  b = bu_binary_to_bitv2(bu_vls_cstr(&v), nbytes);
701  printf("\nNew bitv: '%s'", bu_vls_cstr(&v));
702  printf("\nNew bitv dump:");
703  dump_bitv(b);
704 
705  BU_CK_VLS(&v);
706  BU_CK_BITV(b);
707 
708  len = b->nbits;
709 
710  /* now compare the bitv with the expected int */
711  len = len > ull_size ? ull_size : len;
712  for (i = 0; i < len; ++i) {
713  /* get the expected bit */
714  unsigned long long i1 = (expected_num >> i) & 0x01;
715  unsigned long long i2 = BU_BITTEST(b, i) ? 1 : 0;
716  if (i1 != i2) {
717  ++err;
718  break;
719  }
720  }
721 
722  printf("\n Input binary string: '%s'", argv[4]);
723  printf("\n Expected hex: '0x%x'", (unsigned)expected_num);
724 
725  bu_vls_trunc(&v, 0);
726  bu_bitv_vls(&v, b);
727  printf("\nInput bitv: '%s'", bu_vls_cstr(&v));
728  printf("\nInput bitv dump:");
729  dump_bitv(b);
730 
731  test_results = err ? CTEST_FAIL : CTEST_PASS;
732 
733  bu_vls_free(&v);
734  bu_bitv_free(b);
735 
736  return test_results;
737 }
738 
739 
740 static int
741 test_bu_binstr_to_hexstr(int argc, char **argv)
742 {
743  /* argv[1] argv[2] argv[3]
744  * inputs: <func num> <input binary string> <expected hex string>
745  */
746  int test_results = CTEST_FAIL;
747  const char *input, *expected;
748  struct bu_vls v = BU_VLS_INIT_ZERO;
749 
750  if (argc < 4)
751  bu_exit(1, "ERROR: input format: function_num function_test_args [%s]\n", argv[0]);
752 
753  input = argv[2];
754  expected = argv[3];
755 
756  bu_binstr_to_hexstr(input, &v);
757 
758  if (BU_STR_EQUAL(expected, bu_vls_cstr(&v))) {
759  test_results = CTEST_PASS;
760  printf("\nbu_binstr_to_hexstr PASSED");
761  } else {
762  test_results = CTEST_FAIL;
763  printf("\nbu_binstr_to_hexstr FAILED");
764  }
765 
766  printf("\n Input: '%s'", input);
767  printf("\n Output: '%s'", bu_vls_cstr(&v));
768  printf("\n Expected: '%s'", expected);
769 
770  bu_vls_free(&v);
771 
772  return test_results;
773 }
774 
775 
776 static int
777 test_bu_binstr_to_hexstr_empty_input(int argc, char **argv)
778 {
779  /* argv[1] "" argv[2]
780  * inputs: <func num> <input binary string> <expected hex string>
781  */
782  int test_results = CTEST_FAIL;
783  const char *input = "";
784  const char *expected;
785  struct bu_vls v = BU_VLS_INIT_ZERO;
786 
787  if (argc < 3)
788  bu_exit(1, "ERROR: input format: function_num function_test_args [%s]\n", argv[0]);
789 
790  expected = argv[2];
791 
792  bu_binstr_to_hexstr(input, &v);
793 
794  if (BU_STR_EQUAL(expected, bu_vls_cstr(&v))) {
795  test_results = CTEST_PASS;
796  printf("\nbu_binstr_to_hexstr PASSED");
797  } else {
798  test_results = CTEST_FAIL;
799  printf("\nbu_binstr_to_hexstr FAILED");
800  }
801 
802  printf("\n Input: '%s'", input);
803  printf("\n Output: '%s'", bu_vls_cstr(&v));
804  printf("\n Expected: '%s'", expected);
805 
806  bu_vls_free(&v);
807 
808  return test_results;
809 }
810 
811 
812 static int
813 test_bu_hexstr_to_binstr(int argc, char **argv)
814 {
815  /* argv[1] argv[2] argv[3]
816  * inputs: <func num> <input hex string> <expected binary string>
817  */
818  int test_results = CTEST_FAIL;
819  const char *input, *expected;
820  struct bu_vls v = BU_VLS_INIT_ZERO;
821 
822  if (argc < 4)
823  bu_exit(1, "ERROR: input format: function_num function_test_args [%s]\n", argv[0]);
824 
825  input = argv[2];
826  expected = argv[3];
827 
828  bu_hexstr_to_binstr(input, &v);
829 
830  if (BU_STR_EQUAL(expected, bu_vls_cstr(&v))) {
831  test_results = CTEST_PASS;
832  printf("\nbu_hexstr_to_binstr PASSED");
833  } else {
834  test_results = CTEST_FAIL;
835  printf("\nbu_hexstr_to_binstr FAILED");
836  }
837 
838  printf("\n Input: '%s'", input);
839  printf("\n Output: '%s'", bu_vls_cstr(&v));
840  printf("\n Expected: '%s'", expected);
841 
842  bu_vls_free(&v);
843 
844  return test_results;
845 }
846 
847 
848 int
849 main(int argc, char **argv)
850 {
851  int function_num = 0;
852 
853  if (argc < 2) {
854  bu_exit(1, "ERROR: input format is function_num function_test_args [%s]\n", argv[0]);
855  }
856 
857  sscanf(argv[1], "%d", &function_num);
858 
859  switch (function_num) {
860  case 0:
861  return test_bu_bitv_master();
862  break;
863  case 1:
864  return test_bu_binary_to_bitv(argc, argv);
865  break;
866  case 2:
867  return test_bu_bitv_to_binary(argc, argv);
868  break;
869  case 3:
870  return test_bu_bitv_compare_equal(argc, argv);
871  break;
872  case 4:
873  return test_bu_bitv_compare_equal2(argc, argv);
874  break;
875  case 5:
876  return test_bu_binary_to_bitv2(argc, argv);
877  break;
878  case 6:
879  return test_bu_bitv_and(argc, argv);
880  break;
881  case 7:
882  return test_bu_bitv_or(argc, argv);
883  break;
884  case 8:
885  return test_bu_bitv_shift();
886  break;
887  case 9:
888  return test_bu_bitv_vls(argc, argv);
889  break;
890  case 10:
891  return test_bu_bitv_to_hex(argc, argv);
892  break;
893  case 11:
894  return test_bu_hex_to_bitv(argc, argv);
895  break;
896  case 12:
897  return test_bu_hexstr_to_binstr(argc, argv);
898  break;
899  case 13:
900  return test_bu_binstr_to_hexstr(argc, argv);
901  case 14:
902  return test_bu_binstr_to_hexstr_empty_input(argc, argv);
903  break;
904 
905  }
906 
907  bu_log("ERROR: function_num %d is not valid [%s]\n", function_num, argv[0]);
908  return 1;
909 }
910 
911 
912 /*
913  * Local Variables:
914  * mode: C
915  * tab-width: 8
916  * indent-tabs-mode: t
917  * c-file-style: "stroustrup"
918  * End:
919  * ex: shiftwidth=4 tabstop=8
920  */
struct bu_bitv * bu_binary_to_bitv(const char *str)
Definition: bitv.c:377
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
struct bu_bitv * bu_bitv_new(size_t nbits)
Definition: bitv.c:91
int main(int argc, char **argv)
Definition: bu_bitv.c:849
unsigned char bitv_t
Definition: bitv.h:59
bitv_t bits[2]
Definition: bitv.h:108
struct bu_bitv * bu_binary_to_bitv2(const char *str, const int nbytes)
Definition: bitv.c:406
void dump_bitv(const struct bu_bitv *b)
Definition: test_funcs.c:60
size_t nbits
Definition: bitv.h:107
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
int bu_hexstr_to_binstr(const char *hexstr, struct bu_vls *b)
Definition: bitv.c:751
Header file for the BRL-CAD common definitions.
int bu_bitv_compare_equal(const struct bu_bitv *, const struct bu_bitv *)
Definition: bitv.c:556
#define BU_BITSET(_bv, bit)
Definition: bitv.h:183
const int CTEST_FAIL
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
#define BU_CK_VLS(_vp)
Definition: vls.h:69
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
#define BU_BITTEST(_bv, bit)
Definition: bitv.h:168
#define bu_strlcpy(dst, src, size)
Definition: str.h:60
void bu_bitv_or(struct bu_bitv *ov, const struct bu_bitv *iv)
Definition: bitv.c:132
size_t bu_bitv_shift(void)
Definition: bitv.c:80
#define BU_STR_EQUIV(s1, s2)
Definition: str.h:135
void bu_bitv_vls(struct bu_vls *v, const struct bu_bitv *bv)
const char * bu_vls_cstr(const struct bu_vls *vp)
Definition: vls.c:103
void bu_bitv_to_hex(struct bu_vls *v, const struct bu_bitv *bv)
Definition: bitv.c:222
int bu_binstr_to_hexstr(const char *binstr, struct bu_vls *h)
Definition: bitv.c:636
void bu_bitv_to_binary(struct bu_vls *v, const struct bu_bitv *bv)
Definition: bitv.c:349
void bu_bitv_free(struct bu_bitv *bv)
Definition: bitv.c:113
void bu_bitv_and(struct bu_bitv *ov, const struct bu_bitv *iv)
Definition: bitv.c:155
void bu_vls_strcpy(struct bu_vls *vp, const char *s)
Definition: vls.c:310
bn_poly_t input[3]
Definition: bn_poly_add.c:36
void random_hex_or_binary_string(struct bu_vls *v, const hex_bin_enum_t typ, const int nbytes)
Definition: test_funcs.c:151
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
int bu_bitv_compare_equal2(const struct bu_bitv *, const struct bu_bitv *)
Definition: bitv.c:581
Definition: bitv.h:105
#define BITS_PER_BYTE
Definition: bu_internals.h:29
Definition: vls.h:56
struct bu_bitv * bu_hex_to_bitv(const char *str)
Definition: bitv.c:248
#define BU_CK_BITV(_bp)
Definition: bitv.h:116
const int CTEST_PASS
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126