BRL-CAD
test_funcs.c
Go to the documentation of this file.
1 /* T E S T _ F U N C S . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This program 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 program 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 /** @file test_funcs.c
21  *
22  * Miscellaneous functions for libbu tests.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 
32 #ifdef HAVE_STDINT_H
33 # include <stdint.h>
34 #endif
35 
36 #include "bio.h"
37 
38 #include "../bu_internals.h"
39 #include "./test_internals.h"
40 
41 
42 long int
44 {
45  int rdata = open("/dev/urandom", O_RDONLY);
46  long int rnum;
47 
48  ssize_t result = read(rdata, (char*)&rnum, sizeof(rnum));
49  if (result < 0) {
50  bu_log("ERROR: Unable to read '/dev/urandom'.\n");
51  bu_exit(1, NULL);
52  }
53  close(rdata);
54 
55  return rnum;
56 }
57 
58 
59 void
60 dump_bitv(const struct bu_bitv *b)
61 {
62  const size_t len = b->nbits;
63  size_t x;
64  int i, j, k;
65  int bit, ipad = 0, jpad = 0;
66  size_t word_count;
67  size_t chunksize = 0;
68  volatile size_t BVS = sizeof(bitv_t); /* should be 1 byte as defined in bu.h */
69  unsigned bytes;
70  struct bu_vls *v = bu_vls_vlsinit();
71 
72  bytes = len / BITS_PER_BYTE; /* eight digits per byte */
73  word_count = bytes / BVS;
74  chunksize = bytes % BVS;
75 
76  if (chunksize == 0) {
77  chunksize = BVS;
78  } else {
79  /* handle partial chunk before using chunksize == BVS */
80  word_count++;
81  }
82 
83  while (word_count--) {
84  while (chunksize--) {
85  /* get the appropriate bits in the bit vector */
86  unsigned long lval = (unsigned long)((b->bits[word_count] & ((bitv_t)(0xff)<<(chunksize * BITS_PER_BYTE))) >> (chunksize * BITS_PER_BYTE)) & (bitv_t)0xff;
87 
88  /* get next eight binary digits from bitv */
89  int ii;
90  for (ii = 7; ii >= 0; --ii) {
91  unsigned long uval = lval >> ii & 0x1;
92  bu_vls_printf(v, "%1lx", uval);
93  }
94  }
95  chunksize = BVS;
96  }
97 
98 
99  /* print out one set of data per X bits */
100  x = 16; /* X number of bits per line */
101  x = x > len ? len : x;
102  /* we want even output lines (lengths in multiples of BITS_PER_BYTE) */
103  if (len % x) {
104  ipad = jpad = x - (len % x);
105  };
106 
107  if (ipad > 0)
108  i = j = -ipad;
109  else
110  i = j = 0;
111 
112  bit = len - 1;
113 
114  bu_log("\n=====================================================================");
115  bu_log("\nbitv dump:");
116  bu_log("\n nbits = %zu", len);
117 
118  NEXT:
119 
120  k = i + x;
121  bu_log("\n---------------------------------------------------------------------");
122  bu_log("\n bit ");
123  for (; i < k; ++i, --ipad) {
124  if (ipad > 0)
125  bu_log(" %3s", " ");
126  else
127  bu_log(" %3d", bit--);
128  }
129 
130  bu_log("\n val ");
131  /* the actual values are a little tricky due to the actual structure and number of words */
132  for (; j < k; ++j, --jpad) {
133  if (jpad > 0)
134  bu_log(" %3s", " ");
135  else
136  bu_log(" %c", bu_vls_cstr(v)[j]);
137  }
138 
139  if ((size_t)i < len - 1) {
140  goto NEXT;
141  }
142 
143  bu_log("\n---------------------------------------------------------------------");
144  bu_log("\n=====================================================================");
145 
146  bu_vls_free(v);
147 }
148 
149 
150 void
151 random_hex_or_binary_string(struct bu_vls *v, const hex_bin_enum_t typ, const int nbytes)
152 {
153  const char hex_chars[] = "0123456789abcdef";
154  const char bin_chars[] = "01";
155  const int nstrchars = (typ & HEX) ? nbytes * HEXCHARS_PER_BYTE : nbytes * BITS_PER_BYTE;
156  const char *chars = (typ & HEX) ? hex_chars : bin_chars;
157  const int nchars = (typ & HEX) ? sizeof(hex_chars)/sizeof(char) : sizeof(bin_chars)/sizeof(char);
158  int i;
159  long int seed;
160 
161  /* get a random seed from system entropy to seed "random()" */
162  seed = bu_get_urandom_number();
163  srand(seed);
164 
165  bu_vls_trunc(v, 0);
166  bu_vls_extend(v, nchars);
167  for (i = 0; i < nstrchars; ++i) {
168  long int r = rand();
169  int n = r ? (int)(r % (nchars - 1)) : 0;
170  char c = chars[n];
171  bu_vls_putc(v, c);
172  }
173 
174  if (typ == HEX) {
175  bu_vls_prepend(v, "0x");
176  } else if (typ == BINARY) {
177  bu_vls_prepend(v, "0b");
178  }
179 
180 }
181 
182 
183 /*
184  * Local Variables:
185  * mode: C
186  * tab-width: 8
187  * indent-tabs-mode: t
188  * c-file-style: "stroustrup"
189  * End:
190  * ex: shiftwidth=4 tabstop=8
191  */
192 
ptrdiff_t ssize_t
Definition: common.h:119
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
unsigned char bitv_t
Definition: bitv.h:59
bitv_t bits[2]
Definition: bitv.h:108
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
Header file for the BRL-CAD common definitions.
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
#define HEXCHARS_PER_BYTE
Definition: bu_internals.h:31
struct bu_vls * bu_vls_vlsinit(void)
Definition: vls.c:91
const char * bu_vls_cstr(const struct bu_vls *vp)
Definition: vls.c:103
long int bu_get_urandom_number()
Definition: test_funcs.c:43
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
void bu_vls_extend(struct bu_vls *vp, size_t extra)
Definition: vls.c:136
hex_bin_enum_t
void random_hex_or_binary_string(struct bu_vls *v, const hex_bin_enum_t typ, const int nbytes)
Definition: test_funcs.c:151
void bu_vls_prepend(struct bu_vls *vp, char *str)
Definition: vls.c:796
Definition: bitv.h:105
#define BITS_PER_BYTE
Definition: bu_internals.h:29
Definition: vls.h:56
void bu_vls_putc(struct bu_vls *vp, int c)
Definition: vls.c:666