Loading [MathJax]/extensions/TeX/AMSsymbols.js
LAL 7.7.0.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
LALBitsetTest.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017 Karl Wette
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19
20#include <stdlib.h>
21#include <gsl/gsl_rng.h>
22#include <lal/LALStdio.h>
23#include <lal/LALBitset.h>
24
25int main( void )
26{
27
28 /* Create hash table */
29 LALBitset *bs = XLALBitsetCreate();
30 XLAL_CHECK_MAIN( bs != NULL, XLAL_EFUNC );
31
32 /* Create some random bits */
33 BOOLEAN XLAL_INIT_DECL( bits, [4096] );
34 gsl_rng *r = gsl_rng_alloc( gsl_rng_mt19937 );
35 XLAL_CHECK_MAIN( r != NULL, XLAL_ESYS );
36 int nbits = 0;
37 for ( size_t n = 0; n < XLAL_NUM_ELEM( bits ); ++n ) {
38 bits[n] = ( gsl_rng_uniform( r ) > 0.44 );
39 nbits += bits[n] ? 1 : 0;
40 }
41
42 /* Create random index offset into bitset */
43 const UINT8 n0 = gsl_rng_get( r ) % 65536;
44
45 /* Print information */
46 printf("nbits = %i, n0 = %"LAL_UINT8_FORMAT"\n", nbits, n0);
47
48 /* Set bits */
49 for ( size_t n = 0; n < XLAL_NUM_ELEM( bits ); ++n ) {
50 XLAL_CHECK_MAIN( XLALBitsetSet( bs, n0 + n, bits[n] ) == XLAL_SUCCESS, XLAL_EFUNC );
51 }
52
53 /* Get bits */
54 for ( size_t n = 0; n < XLAL_NUM_ELEM( bits ); ++n ) {
55 BOOLEAN is_set = 0;
56 XLAL_CHECK_MAIN( XLALBitsetGet( bs, n0 + n, &is_set ) == XLAL_SUCCESS, XLAL_EFUNC );
57 XLAL_CHECK_MAIN( !is_set == !bits[n], XLAL_EFAILED, "Inconsistent bit at index %"LAL_UINT8_FORMAT": LALBitset=%i, reference=%i", n0 + n, is_set, bits[n] );
58 }
59
60 /* Clear bitset */
62 for ( size_t n = 0; n < XLAL_NUM_ELEM( bits ); ++n ) {
63 BOOLEAN is_set = 0;
64 XLAL_CHECK_MAIN( XLALBitsetGet( bs, n0 + n, &is_set ) == XLAL_SUCCESS, XLAL_EFUNC );
65 XLAL_CHECK_MAIN( !is_set, XLAL_EFAILED, "Bit still set at index %"LAL_UINT8_FORMAT, n0 + n );
66 }
67
68 /* Cleanup */
69 gsl_rng_free( r );
71
72 /* Check for memory leaks */
74
75 return EXIT_SUCCESS;
76
77}
int main(void)
Definition: LALBitsetTest.c:25
void LALCheckMemoryLeaks(void)
Definition: LALMalloc.c:784
LALBitset * XLALBitsetCreate(void)
Create a bitset.
Definition: LALBitset.c:51
int XLALBitsetClear(LALBitset *bs)
Clear a bitset.
Definition: LALBitset.c:78
void XLALBitsetDestroy(LALBitset *bs)
Destroy a bitset and its elements.
Definition: LALBitset.c:68
int XLALBitsetGet(const LALBitset *bs, const UINT8 idx, BOOLEAN *is_set)
Get whether a bit in the bitset is set.
Definition: LALBitset.c:131
int XLALBitsetSet(LALBitset *bs, const UINT8 idx, const BOOLEAN is_set)
Set/unset a bit in the bitset.
Definition: LALBitset.c:93
unsigned char BOOLEAN
Boolean logical type, see Headers LAL(Atomic)Datatypes.h for more details.
#define XLAL_NUM_ELEM(x)
MACRO which gives the number of elements in a fixed-size array.
uint64_t UINT8
Eight-byte unsigned integer; on some platforms this is equivalent to unsigned long int instead.
#define XLAL_INIT_DECL(var,...)
C99 MACRO to declare and zero-initialize a variable, use as "type XLAL_INIT_DECL(var);".
#define LAL_UINT8_FORMAT
Definition: LALStdio.h:131
static const INT4 r
Definition: Random.c:82
#define XLAL_CHECK_MAIN(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a C main() routine.
Definition: XLALError.h:885
@ XLAL_SUCCESS
Success return value (not an error number)
Definition: XLALError.h:401
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
Definition: XLALError.h:462
@ XLAL_ESYS
System error.
Definition: XLALError.h:442
@ XLAL_EFAILED
Generic failure.
Definition: XLALError.h:418