| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include <esl_config.h> |
| 9 |
|
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include "easel.h" |
| 13 |
#include "esl_bitfield.h" |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
ESL_BITFIELD * |
| 28 |
esl_bitfield_Create(int nb) |
| 29 |
{ |
| 30 |
ESL_BITFIELD *b = NULL; |
| 31 |
int nu = (nb + 63) / 64; |
| 32 |
int status; |
| 33 |
|
| 34 |
ESL_DASSERT1(( nb >= 1 )); |
| 35 |
|
| 36 |
ESL_ALLOC(b, sizeof(ESL_BITFIELD)); |
| 37 |
b->b = NULL; |
| 38 |
|
| 39 |
ESL_ALLOC(b->b, sizeof(uint64_t) * nu); |
| 40 |
memset((void *) b->b, 0, sizeof(uint64_t) * nu); |
| 41 |
b->nb = nb; |
| 42 |
return b; |
| 43 |
|
| 44 |
ERROR: |
| 45 |
esl_bitfield_Destroy(b); |
| 46 |
return NULL; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
int |
| 55 |
esl_bitfield_Count(const ESL_BITFIELD *b) |
| 56 |
{ |
| 57 |
int n = 0; |
| 58 |
int i; |
| 59 |
|
| 60 |
for (i = 0; i < b->nb; i++) |
| 61 |
if (esl_bitfield_IsSet(b, i)) n++; |
| 62 |
return n; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
void |
| 70 |
esl_bitfield_Destroy(ESL_BITFIELD *b) |
| 71 |
{ |
| 72 |
if (b) free(b->b); |
| 73 |
free(b); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
#ifdef eslBITFIELD_TESTDRIVE |
| 81 |
|
| 82 |
#include "esl_random.h" |
| 83 |
#include "esl_vectorops.h" |
| 84 |
|
| 85 |
static void |
| 86 |
utest_randpattern(ESL_RANDOMNESS *rng) |
| 87 |
{ |
| 88 |
char msg[] = "bitfield randpattern utest failed"; |
| 89 |
int nb = 1 + esl_rnd_Roll(rng, 192); |
| 90 |
int nu = (nb + 63) / 64; |
| 91 |
int nset = esl_rnd_Roll(rng, nb+1); |
| 92 |
int *deal = NULL; |
| 93 |
int *bigflags = NULL; |
| 94 |
ESL_BITFIELD *b = esl_bitfield_Create(nb); |
| 95 |
int i; |
| 96 |
int status; |
| 97 |
|
| 98 |
if (nset > 0) ESL_ALLOC(deal, sizeof(int) * nset); |
| 99 |
ESL_ALLOC(bigflags, sizeof(int) * nb); |
| 100 |
esl_vec_ISet(bigflags, nb, FALSE); |
| 101 |
|
| 102 |
if (nset > 0) esl_rnd_Deal(rng, nset, nb, deal); |
| 103 |
for (i = 0; i < nset; i++) bigflags[deal[i]] = TRUE; |
| 104 |
for (i = 0; i < nset; i++) esl_bitfield_Set(b, deal[i]); |
| 105 |
|
| 106 |
if (esl_bitfield_Count(b) != nset) esl_fatal(msg); |
| 107 |
|
| 108 |
for (i = 0; i < nb; i++) if (bigflags[i] != esl_bitfield_IsSet(b, i)) esl_fatal(msg); |
| 109 |
for (i = 0; i < nb; i++) esl_bitfield_Toggle(b, i); |
| 110 |
|
| 111 |
if (esl_bitfield_Count(b) != nb - nset) esl_fatal(msg); |
| 112 |
|
| 113 |
for (i = 0; i < nb; i++) if (bigflags[i] == esl_bitfield_IsSet(b, i)) esl_fatal(msg); |
| 114 |
for (i = 0; i < nb; i++) esl_bitfield_Clear(b, i); |
| 115 |
|
| 116 |
if (esl_bitfield_Count(b) != 0) esl_fatal(msg); |
| 117 |
for (i = 0; i < nu; i++) if (b->b[i]) esl_fatal(msg); |
| 118 |
|
| 119 |
free(deal); |
| 120 |
free(bigflags); |
| 121 |
esl_bitfield_Destroy(b); |
| 122 |
return; |
| 123 |
|
| 124 |
ERROR: |
| 125 |
esl_fatal(msg); |
| 126 |
} |
| 127 |
#endif |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
#ifdef eslBITFIELD_TESTDRIVE |
| 133 |
|
| 134 |
#include "esl_getopts.h" |
| 135 |
|
| 136 |
static ESL_OPTIONS options[] = { |
| 137 |
|
| 138 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 139 |
{ "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 140 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 141 |
}; |
| 142 |
static char usage[] = "[-options]"; |
| 143 |
static char banner[] = "test driver for bitfield module"; |
| 144 |
|
| 145 |
int |
| 146 |
main(int argc, char **argv) |
| 147 |
{ |
| 148 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 149 |
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 150 |
|
| 151 |
fprintf(stderr, "## %s\n", argv[0]); |
| 152 |
fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 153 |
|
| 154 |
utest_randpattern(rng); |
| 155 |
|
| 156 |
fprintf(stderr, "# status = ok\n"); |
| 157 |
|
| 158 |
esl_randomness_Destroy(rng); |
| 159 |
esl_getopts_Destroy(go); |
| 160 |
return eslOK; |
| 161 |
} |
| 162 |
#endif |