├── CHANGELOG ├── INSTALL ├── LICENSE ├── Makefile.in ├── README ├── README.md ├── bitfield-internals.h ├── bitfield.c ├── bitfield.h ├── config.h.in ├── configure ├── configure.ac ├── examples ├── Makefile.in ├── bfand.c ├── bfcat.c ├── bfnormalize.c ├── bfor.c ├── bfxor.c ├── int2bf.c ├── random.c └── rule30.c ├── libbitfield.pc.in ├── man ├── Makefile.in ├── bf_converters.3.in ├── bfand.3.in ├── bfcat.3.in ├── bfclone.3.in ├── bfcmp.3.in ├── bfcpy.3.in ├── bfhamming.3.in ├── bfisempty.3.in ├── bfnew.3.in ├── bfnormalize.3.in ├── bfpopcount.3.in ├── bfpos.3.in ├── bfprint.3.in ├── bfresize.3.in ├── bfrev.3.in ├── bfsetall.3.in ├── bfsetbit.3.in ├── bfshift.3.in ├── bfsize.3.in ├── bfsub.3.in └── bitfield.3.in └── tests ├── Makefile.in ├── test1.c ├── test10.c ├── test11.c ├── test12.c ├── test13.c ├── test14.c ├── test15.c ├── test16.c ├── test17.c ├── test18.c ├── test19.c ├── test2.c ├── test20.c ├── test21.c ├── test22.c ├── test23.c ├── test24.c ├── test25.c ├── test26.c ├── test27.c ├── test28.c ├── test29.c ├── test3.c ├── test30.c ├── test31.c ├── test32.c ├── test33.c ├── test34.c ├── test35.c ├── test36.c ├── test37.c ├── test38.c ├── test39.c ├── test4.c ├── test40.c ├── test41.c ├── test42.c ├── test43.c ├── test44.c ├── test5.c ├── test6.c ├── test7.c ├── test8.c └── test9.c /CHANGELOG: -------------------------------------------------------------------------------- 1 | Changelog 2 | 3 | 1.0.1 4 | * Minor bugfixes. 5 | 6 | 1.0.0 7 | * Stabilized API. 8 | * Minor bugfixes. 9 | 10 | 0.6.3 11 | * Rewrote bfand(), bfor() and bfxor() to accept variable number of arguments. 12 | 13 | 0.6.2 14 | * Fixed memory leaks in shift_ip() and bfnormalize(). 15 | * Fixed bugs in uint64tobf() and bftouint64(). 16 | * Rewrote uint**tobf(), uint**tobf_ip(), bftouint**(), bftouint**_ip(), 17 | long2bf(), long2bf_ip(), bf2long(), bf2long_ip(). 18 | * Replaced ll2bf(), ll2bf_ip(), bf2ll() and bf2ll_ip() with macros (assume 19 | long long is always same length as uint64_t). 20 | * Minor optimization in shift_ip() and bftouint32(). 21 | * Removed internal functions from the list of exported symbols. 22 | * Rewrote tests so they do not rely on internat functions from bitfield library. 23 | 24 | 0.6.1 25 | * Fixed a bug (memory-corruption in bfcat()) 26 | * Added some magic to makefiles ('make test' as alias to 'make check', 'make 27 | distclean' to also run 'make clean') 28 | * Fixed some compile-time warnings (explicitly added 'endian.h', rewrote 29 | always-false "uint < 0") 30 | 31 | 0.6 32 | * Completed converters to/from common data types. New functions: bf2char(), 33 | bf2char_ip(), bf2short(), bf2short_ip(), bf2int_ip(), bf2long_ip(), bf2ll(), 34 | bf2ll_ip(), char2bf(), char2bf_ip(), short2bf(), short2bf_ip(), int2bf_ip(), 35 | long2bf_ip(), ll2bf(), ll2bf_ip(). 36 | * Added converters to/from fixed-width data types: bftouint8(), bftouint8_ip(), 37 | bftouint16(), bftouint16_ip(), bftouint32(), bftouint32_ip(), bftouint64(), 38 | bftouint64_ip(), uint8tobf(), uint8tobf_ip(), uint16tobf(), uint16tobf_ip(), 39 | uint32tobf(), uint32tobf_ip(), uint64tobf(), uint64tobf_ip(). 40 | * bfprint() splitted into bfprint_lsb() and bfrint_msb(). 41 | * Minor optimization in bfshift(). 42 | * bfcat() modified to accept variable number of arguments. 43 | * Fixed some memory leaks. 44 | * Added some magic to makefiles to rebuild on change only. 45 | * Cleanup in man pages. 46 | * Added support for big-endian architectures. 47 | 48 | 0.5.1 49 | * Fixed error in bfnormalize(). 50 | 51 | 0.5 52 | * Added bf2int(), int2bf() and bfnormalize(). 53 | 54 | 0.4 55 | * Added bfpos(), long2bf(). 56 | * Minor change in bfcmp(). Now it's safe to pass NULL as err_msg argument. 57 | 58 | 0.3 59 | * Renamed str2bf() to str2bf_ip() and bf2str() to bf2str_ip(). 60 | * Added new str2bf(), bf2str(). 61 | * Changed the algo of bfrev() to a quicker one. 62 | 63 | 0.2 64 | * Added bfcmp(), bfsub(), bfshift(), bfclearall(), bfsetall(), bfresize(), 65 | bfsetbit(), bfclearbit(), bfrev(), bfrev_ip(), bfsize(), bfgetbit(), 66 | bftogglebit(), bfpopcount(), bfhamming(), bf2long() and bfisempty(). 67 | * Renamed char2bf() to str2bf() and bf2char() to bf2str() (thus incompatible 68 | with versions 0.1.x). 69 | 70 | 0.1.1 71 | * Added bfnew_quick(), bfnew_ones(), bfclone(), bfcat(), bfshift_ip(), 72 | bfnot_ip(), bfcleartail(), bfcpy(). 73 | 74 | 0.1 75 | * Initial release. 76 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | INSTALLATION 2 | ------------ 3 | 4 | To compile bitfield from source code: 5 | 6 | $ autoconf 7 | $ ./configure --prefix=DIRECTORY 8 | $ make 9 | 10 | This will build both a static and a shared version. To build just one of them, 11 | replace "make" with "make static" or "make shared". 12 | 13 | To test the compiled library: 14 | 15 | $ make check 16 | 17 | The generic way to install the compiled files is: 18 | 19 | $ make install 20 | $ ldconfig 21 | 22 | However, one might prefer to use distro-specific installation mechanism (like 23 | "checkinstall") instead. 24 | 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | -------------------------------------------------------------------------------- /Makefile.in: -------------------------------------------------------------------------------- 1 | CC=@CC@ 2 | export CC 3 | datarootdir=@datarootdir@ 4 | prefix=@prefix@ 5 | exec_prefix=@prefix@ 6 | libdir=@libdir@ 7 | mandir=@mandir@ 8 | includedir=@includedir@ 9 | AR=ar 10 | LIBS=-I. 11 | CFLAGS=@CFLAGS@ 12 | export CFLAGS 13 | VERSION=@PACKAGE_VERSION@ 14 | export VERSION 15 | RELEASE_DATE=August 19, 2017 16 | export RELEASE_DATE 17 | TESTDIR=tests 18 | EXAMPLEDIR=examples 19 | LD_LIBRARY_PATH := $(shell pwd):$(LD_LIBRARY_PATH) 20 | export LD_LIBRARY_PATH 21 | 22 | .PHONY: test check man shared static install install-lib install-man install-header clean distclean uninstall uninstall-lib uninstall-man uninstall-header example examples 23 | 24 | all: shared static man 25 | 26 | shared: libbitfield.so.$(VERSION) 27 | libbitfield.so.$(VERSION): bitfield.c 28 | $(CC) $(CFLAGS) -c -fpic bitfield.c -o bf-shared.o -I. $(LIBS) 29 | $(CC) $(CFLAGS) -shared -Wl,-soname,libbitfield.so.1 -o libbitfield.so.$(VERSION) bf-shared.o $(LIBS) 30 | for i in libbitfield.so.1 libbitfield.so; do ln -svf libbitfield.so.$(VERSION) $$i; done 31 | 32 | static: libbitfield.a 33 | libbitfield.a: bitfield.c 34 | $(CC) $(CFLAGS) -c bitfield.c -o bf-static.o $(LIBS) 35 | $(AR) rcs libbitfield.a bf-static.o 36 | 37 | man: 38 | $(MAKE) -C man 39 | 40 | test: check 41 | 42 | check: 43 | $(MAKE) -C $(TESTDIR) 44 | 45 | examples: example 46 | 47 | example: 48 | $(MAKE) -C $(EXAMPLEDIR) 49 | 50 | install: install-lib install-man install-header install-pc 51 | 52 | install-lib: 53 | mkdir -p $(DESTDIR)$(libdir) 54 | for i in libbitfield.so.1 libbitfield.so libbitfield.so.$(VERSION) libbitfield.a; do if [ -e $(DESTDIR)$(libdir)/$$i ]; then rm -v $(DESTDIR)$(libdir)/$$i; fi; done 55 | install -m 0644 libbitfield.so.$(VERSION) $(DESTDIR)$(libdir) 56 | ln -sv libbitfield.so.$(VERSION) $(DESTDIR)$(libdir)/libbitfield.so.1 57 | ln -sv libbitfield.so.$(VERSION) $(DESTDIR)$(libdir)/libbitfield.so 58 | install -m 0644 libbitfield.a $(DESTDIR)$(libdir) 59 | 60 | install-man: 61 | $(MAKE) -C man install 62 | 63 | install-header: 64 | mkdir -p $(DESTDIR)$(includedir) 65 | install -m 644 bitfield.h $(DESTDIR)$(includedir)/bitfield.h 66 | 67 | install-pc: libbitfield.pc 68 | install -d $(DESTDIR)$(libdir)/pkgconfig 69 | install -m 0644 $< $(DESTDIR)$(libdir)/pkgconfig 70 | 71 | clean: 72 | rm -rvf *.a *.o *.so* 73 | $(MAKE) -C examples clean 74 | $(MAKE) -C $(TESTDIR) clean 75 | $(MAKE) -C man clean 76 | 77 | distclean: clean 78 | rm config.log config.status config.h Makefile libbitfield.pc 79 | $(MAKE) -C man distclean 80 | $(MAKE) -C examples distclean 81 | $(MAKE) -C $(TESTDIR) distclean 82 | 83 | uninstall: uninstall-lib uninstall-man uninstall-header uninstall-pc 84 | 85 | uninstall-lib: 86 | for i in libbitfield.so.1 libbitfield.so libbitfield.so.$(VERSION) libbitfield.a; do if [ -e $(DESTDIR)$(libdir)/$$i ]; then rm -v $(DESTDIR)$(libdir)/$$i; fi; done 87 | 88 | uninstall-man: 89 | $(MAKE) -C man uninstall 90 | 91 | uninstall-header: 92 | if [ -e $(DESTDIR)$(includedir)/bitfield.h ]; then rm -v $(DESTDIR)$(includedir)/bitfield.h; fi 93 | 94 | uninstall-pc: 95 | if [ -e $(DESTDIR)$(libdir)/pkgconfig/libbitfield.pc ]; then rm -v $(DESTDIR)$(libdir)/pkgconfig/libbitfield.pc; fi 96 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | bitfield 2 | ======== 3 | 4 | Version 1.0.1 (August, 2017) 5 | 6 | bitfield is a library of functions for creating, modifying and destroying bit 7 | fields (or bit arrays), i.e. series of zeroes and ones spread across an array 8 | of storage units (unsigned long integers). 9 | 10 | Installation 11 | ------------ 12 | 13 | To compile bitfield from source code: 14 | 15 | $ autoconf 16 | $ ./configure --prefix=DIRECTORY 17 | $ make 18 | 19 | This will build both a static and a shared version. To build just one of them, 20 | replace "make" with "make static" or "make shared". 21 | 22 | To test the compiled library: 23 | 24 | $ make test 25 | 26 | To build and run some example applications: 27 | 28 | $ make example 29 | 30 | The generic way to install the compiled files is: 31 | 32 | $ make install 33 | $ ldconfig 34 | 35 | However, one might prefer to use distro-specific installation mechanism (like 36 | "checkinstall") instead. 37 | 38 | Usage 39 | ----- 40 | 41 | Using the functions provided by bitfield library in a project is 42 | straightforward. 43 | If bitfield library files are installed system-wide in standard locations, then 44 | one needs to (1) include a system version of the header file to the source 45 | 46 | #include 47 | 48 | and (2) add the "-lbitfield" flag to compiler instructions 49 | 50 | gcc ... -lbitfield 51 | 52 | Alternatively, if bitfield library is integrated into a project, then one needs 53 | to (1) include a local version of the header file to the source 54 | 55 | #include "PATH_TO_HEADER/bitfield.h" 56 | 57 | and (2) add the "-lbitfield" flag, along with the path to the header and path 58 | to the library to compiler instructions 59 | 60 | gcc ... -I$(PATH_TO_HEADER) -L$(PATH_TO_LIBRARY) -lbitfield 61 | 62 | Bitfield structure 63 | ------------------ 64 | 65 | Bit arrays are stored in data structures called 'bitfield'. A bitfield structure 66 | has two elements: an array of unsigned long integers 'field' for storing the bits 67 | and an integer 'size' for storing the number of bits in the array. This library 68 | provides APIs for accessing and modifying bit arrays (see 'Functions'). 69 | 70 | Functions 71 | --------- 72 | 73 | For function syntax, see "bitfield.h". For details on every function, see its 74 | manual page. 75 | 76 | Creating and deleting bit arrays: 77 | 78 | bfnew() creates an empty bitfield structure, and returns a pointer to it 79 | 80 | bfnew_ones() creates a bitfield structure, sets all its bits and returns a 81 | pointer to it 82 | 83 | bfnew_quick() creates a bitfield structure and returns a pointer to it 84 | 85 | bfdel() destroys a bitfield structure and frees memory 86 | 87 | Operations with bit arrays: 88 | 89 | bfresize() resizes an existing bitfield 90 | 91 | bfcat() concatenates several bitfields into one 92 | 93 | bfclearall() clears all bits in a bitfield (i.e. fills it with zeroes) 94 | 95 | bfclone() creates a copy of an existing bitfield 96 | 97 | bfcpy() copies the contents of a bitfield into another pre-existing bitfield 98 | 99 | bfhamming() counts the Hamming distance between two bitfields 100 | 101 | bfisempty() checks whether all bit of an array are unset 102 | 103 | bfnormalize() represents a bitfield as a smallest value of a closed ring 104 | 105 | bfpopcount() counts the set bits in a bitfield 106 | 107 | bfpos() checks whether an array of bits contains a sub-array 108 | 109 | bfrev() reverses the order of bits in a bitfield and returns result in new 110 | bitfield 111 | 112 | bfrev_ip() reverses the order of bits in an existing bitfield 113 | 114 | bfsetall() sets all bits in a bitfield (i.e. fills it with ones) 115 | 116 | bfshift() circular-shifts the contents of a bitfield and returns the result in 117 | new bitfield 118 | 119 | bfshift_ip() circular-shifts the contents of an existing bitfield 120 | 121 | bfsub() extracts a sub-bitfield in a new bitfield 122 | 123 | Operations with individual bits: 124 | 125 | bfgetbit() checks the state of a bit in a bitfield 126 | 127 | bfsetbit() sets one bit in a bitfield 128 | 129 | bfclearbit() clears one bit in a bitfield 130 | 131 | bftogglebit() toggles (i.e. reverses the state of) a bit in a bitfield 132 | 133 | Printing bit arrays: 134 | 135 | bfprint_lsb() prints a bitfield as a series of ones and zeroes, left to right, the 136 | least significant bit first 137 | 138 | bfprint_msb() prints a bitfield as a series of ones and zeroes, left to right, the 139 | most significant bit first 140 | 141 | Logical operations with bit arrays: 142 | 143 | bfand() performs bitwise AND over a pair of bitfields 144 | 145 | bfnot() reverses all bits in a bitfield and return the result in a new bitfield 146 | 147 | bfnot_ip() reverses all bits in an existing bitfield 148 | 149 | bfor() performs bitwise inclusive OR over a pair of bitfields 150 | 151 | bfxor() performs bitwise exclusive OR over a pair of bitfields 152 | 153 | bfcmp() compares two bitfields and returns 0 if same or non-zero and error 154 | message if different 155 | 156 | Converting between different data types: 157 | 158 | These functions convert between bitfield structure and standard data types. 159 | Supported data types are: unsigned char, unsigned short, unsigned int, unsigned 160 | long, unsigned long long, uint8_t, uint16_t, uint32_t and uint64_t. Unsigned 161 | chars can be treated in two ways. Functions with "str" in their name treat each 162 | char as a storage of one character, '0' or '1', while functions with "char" in 163 | their name treat each char as a storage of several (usually 8) bits. 164 | 165 | Bitfield-to-something functions: 166 | 167 | bf2char() converts a bitfield structure into an array of unsigned chars 168 | 169 | bf2str() converts into a character string of '1's and '0's 170 | 171 | bf2short() converts into an array of short integers 172 | 173 | bf2int() converts into an array of integers 174 | 175 | bf2long() converts into an array of long integers 176 | 177 | bf2ll() converts into an array of long long integers 178 | 179 | bftouint8() converts into an array of uint8_t 180 | 181 | bftouint16() converts into an array of uint16_t 182 | 183 | bftouint32() converts into an array of uint32_t 184 | 185 | bftouint64() converts into an array of uin64_t 186 | 187 | "In-place" bitfield-to-something functions are same as above, except that 188 | instead of creating a new array, these functions fill an existing one: 189 | 190 | bf2char_ip() 191 | 192 | bf2str_ip() 193 | 194 | bf2short_ip() 195 | 196 | bf2int_ip() 197 | 198 | bf2long_ip() 199 | 200 | bf2ll_ip() 201 | 202 | bftouint8_ip() 203 | 204 | bftouint16_ip() 205 | 206 | bftouint32_ip() 207 | 208 | bftouint64_ip() 209 | 210 | "Something-to-bitfield" functions: 211 | 212 | char2bf() converts an array of unsigned chars into a bitfield structure 213 | 214 | str2bf() converts a character string of '1's and '0's 215 | 216 | short2bf() converts an array of short integers 217 | 218 | int2bf() converts an array of integers 219 | 220 | long2bf() converts an array of long integers 221 | 222 | ll2bf() converts an array of long long integers 223 | 224 | uint8tobf() converts an array of uint8_t 225 | 226 | uint16tobf() converts an array of uint16_t 227 | 228 | uint32tobf() converts an array of uint32_t 229 | 230 | uint64tobf() converts an array of uint64_t 231 | 232 | "In-place" something-to-bitfield functions are same as above, except that 233 | instead of creating a new bitfield, these functions fill an existing one: 234 | 235 | char2bf_ip() 236 | 237 | str2bf_ip() 238 | 239 | short2bf_ip() 240 | 241 | int2bf_ip() 242 | 243 | long2bf_ip() 244 | 245 | ll2bf_ip() 246 | 247 | uint8tobf_ip() 248 | 249 | uint16tobf_ip() 250 | 251 | uint32tobf_ip() 252 | 253 | uint64tobf_ip() 254 | 255 | Miscellaneous functions: 256 | 257 | bfsize() obtains the number of bits of a bitfield 258 | 259 | Please, see "examples" directory for working examples. 260 | 261 | Versioning 262 | ---------- 263 | 264 | The versioning scheme is MAJOR.MINOR.PATCH, where 265 | * MAJOR version changes with incompatible API/ABI changes, 266 | * MINOR version changes with backwards-compatible changes (like adding new functionality), 267 | * PATCH version changes with backwards-compatible bug fixes. 268 | 269 | Licensing 270 | --------- 271 | 272 | bitfield is free software, and is released under the terms of the GNU General 273 | Public License version 3 or any later version. Please see the file called 274 | LICENSE. 275 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | bitfield 2 | ======== 3 | 4 | Version 1.0.1 (August, 2017) 5 | 6 | bitfield is a library of functions for creating, modifying and destroying bit 7 | fields (or bit arrays), i.e. series of zeroes and ones spread across an array 8 | of storage units (unsigned long integers). 9 | 10 | Installation 11 | ------------ 12 | 13 | To compile bitfield from source code: 14 | 15 | $ autoconf 16 | $ ./configure --prefix=DIRECTORY 17 | $ make 18 | 19 | This will build both a static and a shared version. To build just one of them, 20 | replace "make" with "make static" or "make shared". 21 | 22 | To test the compiled library: 23 | 24 | $ make test 25 | 26 | To build and run some example applications: 27 | 28 | $ make example 29 | 30 | The generic way to install the compiled files is: 31 | 32 | $ make install 33 | $ ldconfig 34 | 35 | However, one might prefer to use distro-specific installation mechanism (like 36 | "checkinstall") instead. 37 | 38 | Usage 39 | ----- 40 | 41 | Using the functions provided by bitfield library in a project is 42 | straightforward. 43 | If bitfield library files are installed system-wide in standard locations, then 44 | one needs to (1) include a system version of the header file to the source 45 | 46 | #include 47 | 48 | and (2) add the "-lbitfield" flag to compiler instructions 49 | 50 | gcc ... -lbitfield 51 | 52 | Alternatively, if bitfield library is integrated into a project, then one needs 53 | to (1) include a local version of the header file to the source 54 | 55 | #include "PATH_TO_HEADER/bitfield.h" 56 | 57 | and (2) add the "-lbitfield" flag, along with the path to the header and path 58 | to the library to compiler instructions 59 | 60 | gcc ... -I$(PATH_TO_HEADER) -L$(PATH_TO_LIBRARY) -lbitfield 61 | 62 | Bitfield structure 63 | ------------------ 64 | 65 | Bit arrays are stored in data structures called 'bitfield'. A bitfield structure 66 | has two elements: an array of unsigned long integers 'field' for storing the bits 67 | and an integer 'size' for storing the number of bits in the array. This library 68 | provides APIs for accessing and modifying bit arrays (see 'Functions'). 69 | 70 | Functions 71 | --------- 72 | 73 | For function syntax, see "bitfield.h". For details on every function, see its 74 | manual page. 75 | 76 | Creating and deleting bit arrays: 77 | 78 | bfnew() creates an empty bitfield structure, and returns a pointer to it 79 | 80 | bfnew_ones() creates a bitfield structure, sets all its bits and returns a 81 | pointer to it 82 | 83 | bfnew_quick() creates a bitfield structure and returns a pointer to it 84 | 85 | bfdel() destroys a bitfield structure and frees memory 86 | 87 | Operations with bit arrays: 88 | 89 | bfresize() resizes an existing bitfield 90 | 91 | bfcat() concatenates several bitfields into one 92 | 93 | bfclearall() clears all bits in a bitfield (i.e. fills it with zeroes) 94 | 95 | bfclone() creates a copy of an existing bitfield 96 | 97 | bfcpy() copies the contents of a bitfield into another pre-existing bitfield 98 | 99 | bfhamming() counts the Hamming distance between two bitfields 100 | 101 | bfisempty() checks whether all bit of an array are unset 102 | 103 | bfnormalize() represents a bitfield as a smallest value of a closed ring 104 | 105 | bfpopcount() counts the set bits in a bitfield 106 | 107 | bfpos() checks whether an array of bits contains a sub-array 108 | 109 | bfrev() reverses the order of bits in a bitfield and returns result in new 110 | bitfield 111 | 112 | bfrev_ip() reverses the order of bits in an existing bitfield 113 | 114 | bfsetall() sets all bits in a bitfield (i.e. fills it with ones) 115 | 116 | bfshift() circular-shifts the contents of a bitfield and returns the result in 117 | new bitfield 118 | 119 | bfshift_ip() circular-shifts the contents of an existing bitfield 120 | 121 | bfsub() extracts a sub-bitfield in a new bitfield 122 | 123 | Operations with individual bits: 124 | 125 | bfgetbit() checks the state of a bit in a bitfield 126 | 127 | bfsetbit() sets one bit in a bitfield 128 | 129 | bfclearbit() clears one bit in a bitfield 130 | 131 | bftogglebit() toggles (i.e. reverses the state of) a bit in a bitfield 132 | 133 | Printing bit arrays: 134 | 135 | bfprint_lsb() prints a bitfield as a series of ones and zeroes, left to right, the 136 | least significant bit first 137 | 138 | bfprint_msb() prints a bitfield as a series of ones and zeroes, left to right, the 139 | most significant bit first 140 | 141 | Logical operations with bit arrays: 142 | 143 | bfand() performs bitwise AND over a pair of bitfields 144 | 145 | bfnot() reverses all bits in a bitfield and return the result in a new bitfield 146 | 147 | bfnot_ip() reverses all bits in an existing bitfield 148 | 149 | bfor() performs bitwise inclusive OR over a pair of bitfields 150 | 151 | bfxor() performs bitwise exclusive OR over a pair of bitfields 152 | 153 | bfcmp() compares two bitfields and returns 0 if same or non-zero and error 154 | message if different 155 | 156 | Converting between different data types: 157 | 158 | These functions convert between bitfield structure and standard data types. 159 | Supported data types are: unsigned char, unsigned short, unsigned int, unsigned 160 | long, unsigned long long, uint8_t, uint16_t, uint32_t and uint64_t. Unsigned 161 | chars can be treated in two ways. Functions with "str" in their name treat each 162 | char as a storage of one character, '0' or '1', while functions with "char" in 163 | their name treat each char as a storage of several (usually 8) bits. 164 | 165 | Bitfield-to-something functions: 166 | 167 | bf2char() converts a bitfield structure into an array of unsigned chars 168 | 169 | bf2str() converts into a character string of '1's and '0's 170 | 171 | bf2short() converts into an array of short integers 172 | 173 | bf2int() converts into an array of integers 174 | 175 | bf2long() converts into an array of long integers 176 | 177 | bf2ll() converts into an array of long long integers 178 | 179 | bftouint8() converts into an array of uint8_t 180 | 181 | bftouint16() converts into an array of uint16_t 182 | 183 | bftouint32() converts into an array of uint32_t 184 | 185 | bftouint64() converts into an array of uin64_t 186 | 187 | "In-place" bitfield-to-something functions are same as above, except that 188 | instead of creating a new array, these functions fill an existing one: 189 | 190 | bf2char_ip() 191 | 192 | bf2str_ip() 193 | 194 | bf2short_ip() 195 | 196 | bf2int_ip() 197 | 198 | bf2long_ip() 199 | 200 | bf2ll_ip() 201 | 202 | bftouint8_ip() 203 | 204 | bftouint16_ip() 205 | 206 | bftouint32_ip() 207 | 208 | bftouint64_ip() 209 | 210 | "Something-to-bitfield" functions: 211 | 212 | char2bf() converts an array of unsigned chars into a bitfield structure 213 | 214 | str2bf() converts a character string of '1's and '0's 215 | 216 | short2bf() converts an array of short integers 217 | 218 | int2bf() converts an array of integers 219 | 220 | long2bf() converts an array of long integers 221 | 222 | ll2bf() converts an array of long long integers 223 | 224 | uint8tobf() converts an array of uint8_t 225 | 226 | uint16tobf() converts an array of uint16_t 227 | 228 | uint32tobf() converts an array of uint32_t 229 | 230 | uint64tobf() converts an array of uint64_t 231 | 232 | "In-place" something-to-bitfield functions are same as above, except that 233 | instead of creating a new bitfield, these functions fill an existing one: 234 | 235 | char2bf_ip() 236 | 237 | str2bf_ip() 238 | 239 | short2bf_ip() 240 | 241 | int2bf_ip() 242 | 243 | long2bf_ip() 244 | 245 | ll2bf_ip() 246 | 247 | uint8tobf_ip() 248 | 249 | uint16tobf_ip() 250 | 251 | uint32tobf_ip() 252 | 253 | uint64tobf_ip() 254 | 255 | Miscellaneous functions: 256 | 257 | bfsize() obtains the number of bits of a bitfield 258 | 259 | Please, see "examples" directory for working examples. 260 | 261 | Versioning 262 | ---------- 263 | 264 | The versioning scheme is MAJOR.MINOR.PATCH, where 265 | * MAJOR version changes with incompatible API/ABI changes, 266 | * MINOR version changes with backwards-compatible changes (like adding new functionality), 267 | * PATCH version changes with backwards-compatible bug fixes. 268 | 269 | Licensing 270 | --------- 271 | 272 | bitfield is free software, and is released under the terms of the GNU General 273 | Public License version 3 or any later version. Please see the file called 274 | LICENSE. 275 | -------------------------------------------------------------------------------- /bitfield-internals.h: -------------------------------------------------------------------------------- 1 | #ifndef CHAR_BIT 2 | #include 3 | #endif 4 | #ifndef SHORT_BIT 5 | #define SHORT_BIT (unsigned int) (sizeof(unsigned short) * CHAR_BIT) 6 | #endif 7 | #ifndef INT_BIT 8 | #define INT_BIT (unsigned int) (sizeof(unsigned int) * CHAR_BIT) 9 | #endif 10 | #ifndef LONG_BIT 11 | #define LONG_BIT (unsigned int) (sizeof(unsigned long) * CHAR_BIT) 12 | #endif 13 | #ifndef LONG_LONG_BIT 14 | #define LONG_LONG_BIT (unsigned int) (sizeof(unsigned long long) * CHAR_BIT) 15 | #endif 16 | #define BITMASK(b) (1UL << ((b) % LONG_BIT)) 17 | #define BITSLOT(b) ((b) / LONG_BIT) 18 | #define BITGET(a, b) (((a)->field[BITSLOT(b)] >> ((b) % LONG_BIT)) & 1UL) 19 | #define BITSET(a, b) ((a)->field[BITSLOT(b)] |= BITMASK(b)) 20 | #define BITCLEAR(a, b) ((a)->field[BITSLOT(b)] &= ~BITMASK(b)) 21 | #define BITTEST(a, b) ((a)->field[BITSLOT(b)] & BITMASK(b)) 22 | #define BITTOGGLE(a, b) ((a)->field[BITSLOT(b)] ^= BITMASK(b)) 23 | #define BITNSLOTS(nb) ((nb + LONG_BIT - 1) / LONG_BIT) 24 | 25 | struct bitfield { /* defines a bitfield */ 26 | unsigned long *field; 27 | unsigned int size; 28 | }; 29 | -------------------------------------------------------------------------------- /bitfield.h: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: bitfield.h 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Copyright 2015, 2016, 2017 8 | **/ 9 | 10 | #include 11 | 12 | #ifndef BITFIELD_H 13 | #define BITFIELD_H 14 | 15 | struct bitfield; 16 | 17 | /* 18 | * Convert integer data types, all unsigned, to bitfield structures, with 19 | * in-place equivalents: 20 | * char as a character (each char storing '0' or '1') 21 | * char as an integer 22 | * int 23 | * long 24 | */ 25 | 26 | struct bitfield *str2bf(const char *input); /* converts a null-terminated character string of ones and zeroes into a bitfield and returns the result in a new bitfield */ 27 | 28 | #define char2bf(X, Y) (struct bitfield *) uint8tobf((const uint8_t *) X, Y) /* write the contents of an array of chars into a bitfield structure */ 29 | 30 | struct bitfield *short2bf(const unsigned short *input, unsigned int size); /* write the contents of an array of short integers into a bitfield structure */ 31 | 32 | #define int2bf(X, Y) (struct bitfield *) uint32tobf((const uint32_t *) X, Y) /* write the contents of an array of integers into a bitfield structure */ 33 | 34 | struct bitfield *long2bf(const unsigned long *input, unsigned int size); /* write the contents of an array of long integers into a bitfield structure */ 35 | 36 | #define ll2bf(x, y) (struct bitfield *) uint64tobf((uint64_t *) x, y) /* write the contents of an array of longlong integers into a bitfield structure */ 37 | 38 | struct bitfield *uint8tobf(const uint8_t * input, unsigned int size); 39 | 40 | struct bitfield *uint16tobf(const uint16_t * input, unsigned int size); 41 | 42 | struct bitfield *uint32tobf(const uint32_t * input, unsigned int size); 43 | 44 | struct bitfield *uint64tobf(const uint64_t * input, unsigned int size); 45 | 46 | void str2bf_ip(const char *input, struct bitfield *output); /* converts a character string of ones and zeroes into a bitfield */ 47 | 48 | #define char2bf_ip(X, Y) uint8tobf_ip((const uint8_t *) X, Y) /* convert an array of chars into a bitfield structure */ 49 | 50 | void short2bf_ip(const unsigned short *intput, struct bitfield *output); /* convert an array of short integers into a bitfield structure */ 51 | 52 | #define int2bf_ip(X, Y) uint32tobf_ip((const uint32_t *) X, Y) /* convert an array of integers into a bitfield structure */ 53 | 54 | void long2bf_ip(const unsigned long *input, struct bitfield *output); /* convert an array of long integers into a bitfield structure */ 55 | 56 | #define ll2bf_ip(x, y) uint64tobf_ip((uint64_t *) x, y); /* convert an array of longlong integers into a bitfield structure */ 57 | 58 | void uint8tobf_ip(const uint8_t * input, struct bitfield *output); 59 | 60 | void uint16tobf_ip(const uint16_t * input, struct bitfield *output); 61 | 62 | void uint32tobf_ip(const uint32_t * input, struct bitfield *output); 63 | 64 | void uint64tobf_ip(const uint64_t * input, struct bitfield *output); 65 | 66 | /* 67 | * Convert bitfield structures to integer data types, all unsigned, with 68 | * in-place equivalents: 69 | * char as a character (each char storing '0' or '1') 70 | * char as an integer 71 | * int 72 | * long 73 | */ 74 | 75 | char *bf2str(const struct bitfield *input); /* converts a bitfield into a character string of ones and zeroes and returns the result in a new character string */ 76 | 77 | #define bf2char(X) (unsigned char *) bftouint8(X) /* return the bitfield as an array of unsigned chars */ 78 | 79 | unsigned short *bf2short(const struct bitfield *input); /* return the bitfield as an array of unsigned short integers */ 80 | 81 | #define bf2int(X) (unsigned int *) bftouint32(X) /* return the bitfield as an array of unsigned integers */ 82 | 83 | unsigned long *bf2long(const struct bitfield *input); /* return the bitfield as an array of unsigned long integers */ 84 | 85 | #define bf2ll(x) (unsigned long long *) bftouint64(x); /* return the bitfield as an array of unsigned longlong integers */ 86 | 87 | uint8_t *bftouint8(const struct bitfield *input); 88 | 89 | uint16_t *bftouint16(const struct bitfield *input); 90 | 91 | uint32_t *bftouint32(const struct bitfield *input); 92 | 93 | uint64_t *bftouint64(const struct bitfield *input); 94 | 95 | void bf2str_ip(const struct bitfield *input, char *output); /* converts a bitfield into a character string of ones and zeroes */ 96 | 97 | #define bf2char_ip(X, Y) bftouint8_ip(X, (uint8_t *) Y) /* converts a bitfield into an array of unsigned chars */ 98 | 99 | void bf2short_ip(const struct bitfield *input, unsigned short *output); /* converts a bitfield into an array of unsigned short integers */ 100 | 101 | #define bf2int_ip(X, Y) bftouint32_ip(X, (uint32_t *) Y) /* converts a bitfield into an array of unsigned integers */ 102 | 103 | void bf2long_ip(const struct bitfield *input, unsigned long *output); /* converts a bitfield into an array of unsigned long integers */ 104 | 105 | #define bf2ll_ip(x, y) bftouint64_ip(x, (uint64_t *) y); /* converts a bitfield into an array of unsigned longlong integers */ 106 | 107 | void bftouint8_ip(const struct bitfield *input, uint8_t * output); 108 | 109 | void bftouint16_ip(const struct bitfield *input, uint16_t * output); 110 | 111 | void bftouint32_ip(const struct bitfield *input, uint32_t * output); 112 | 113 | void bftouint64_ip(const struct bitfield *input, uint64_t * output); 114 | 115 | /* 116 | * Create and delete bitfields 117 | */ 118 | 119 | struct bitfield *bfnew_ones(const unsigned int size); /* creates a bitfield structure, sets all its bits to true with and returns a pointer to it */ 120 | 121 | struct bitfield *bfnew_quick(const unsigned int size); /* creates a bitfield structure and returns a pointer to it */ 122 | 123 | struct bitfield *bfnew(const unsigned int size); /* creates a bitfield structure, sets all its bits to false and returns a pointer to it */ 124 | 125 | void bfdel(struct bitfield *instance); /* destroys a bitfield structure and frees memory */ 126 | 127 | /* 128 | * Operations with single bits 129 | */ 130 | 131 | unsigned int bfgetbit(const struct bitfield *instance, const unsigned int bit); /* checks whether a bit in a bitfield is set */ 132 | 133 | void bfsetbit(struct bitfield *instance, const unsigned int bit); /* sets one bit in a bitfield */ 134 | 135 | void bfclearbit(struct bitfield *instance, const unsigned int bit); /* clears one bit in a bitfield */ 136 | 137 | void bftogglebit(struct bitfield *instance, const unsigned int bit); /* toggles a bit in a bitfield */ 138 | 139 | /* 140 | * Logical operations with bitfields 141 | */ 142 | 143 | struct bitfield *_bfand(unsigned int count, ...); 144 | 145 | #define bfand(...) (struct bitfield *) _bfand( count_arguments(#__VA_ARGS__), __VA_ARGS__) /* performs bitwise AND over a pair of bitfields */ 146 | 147 | struct bitfield *bfnot(const struct bitfield *input); /* reverses all bits in a bitfield and return the result in new bitfield */ 148 | 149 | void bfnot_ip(struct bitfield *instance); /* reverses all bits in a bitfield */ 150 | 151 | struct bitfield *_bfor(unsigned int count, ...); 152 | 153 | #define bfor(...) (struct bitfield *) _bfor( count_arguments(#__VA_ARGS__), __VA_ARGS__) /* performs bitwise inclusive OR over a pair of bitfields */ 154 | 155 | struct bitfield *_bfxor(unsigned int count, ...); 156 | 157 | #define bfxor(...) (struct bitfield *) _bfxor( count_arguments(#__VA_ARGS__), __VA_ARGS__) /* performs bitwise exclusive OR over a pair of bitfields */ 158 | 159 | /* 160 | * Manipulate bitfields 161 | */ 162 | struct bitfield *_bfcat(unsigned int count, ...); 163 | 164 | unsigned int count_arguments(char *s); 165 | 166 | #define bfcat(...) (struct bitfield *) _bfcat( count_arguments(#__VA_ARGS__), __VA_ARGS__) /* concatenates two bitfields into one */ 167 | 168 | void bfclearall(struct bitfield *instance); /* fills a bitfield with zeroes */ 169 | 170 | struct bitfield *bfclone(const struct bitfield *input); /* creates a copy of an existing bitfield */ 171 | 172 | unsigned int bfcmp(const struct bitfield *input1, const struct bitfield *input2, char **errmsg); /* compares two bitfields and returns 0 if same or non-zero and error message if different */ 173 | 174 | unsigned int bfcpy(const struct bitfield *src, struct bitfield *dest); /* copies the contents of a bitfield into another pre-existing bitfield */ 175 | 176 | unsigned int bfhamming(const struct bitfield *input1, const struct bitfield *input2); /* counts the Hamming distance between two bitfields */ 177 | 178 | unsigned int bfisempty(const struct bitfield *instance); /* checks whether all bits of an array are unset */ 179 | 180 | struct bitfield *bfnormalize(const struct bitfield *input); /* treats the bitfield as a closed ring and represents it as a smallest value */ 181 | 182 | unsigned int bfpopcount(const struct bitfield *instance); /* counts the set bits in a bitfield */ 183 | 184 | int bfpos(const struct bitfield *haystack, const struct bitfield *needle); /* check whether an array of bits contains a sub-array */ 185 | 186 | void bfprint_lsb(const struct bitfield *instance); /* prints a bitfield as a series of ones and zeroes, least significant bit first */ 187 | 188 | void bfprint_msb(const struct bitfield *instance); /* prints a bitfield as a series of ones and zeroes, most significant bit first */ 189 | 190 | void bfresize(struct bitfield *instance, const unsigned int new_size); /* resizes an existing bitfield */ 191 | 192 | void bfrev_ip(struct bitfield *instance); /* reverses the order of bits in a bitfield */ 193 | 194 | struct bitfield *bfrev(const struct bitfield *input); /* reverses the order of bits in a bitfield and return result in new bitfield */ 195 | 196 | void bfsetall(struct bitfield *instance); /* fills a bitfield with ones */ 197 | 198 | void bfshift_ip(struct bitfield *input, const int offset); /* circular-shifts the contents of a bitfield */ 199 | 200 | struct bitfield *bfshift(const struct bitfield *input, const int offset); /* circular-shifts the contents of a bitfield and return the result in new bitfield */ 201 | 202 | unsigned int bfsize(const struct bitfield *instance); /* obtains the number of bits of a bitfield */ 203 | 204 | struct bitfield *bfsub(const struct bitfield *input, const unsigned int start, const unsigned int end); /* extracts a slice of a bitfield */ 205 | 206 | #endif 207 | -------------------------------------------------------------------------------- /config.h.in: -------------------------------------------------------------------------------- 1 | /* config.h.in. Generated from configure.ac by autoheader. */ 2 | 3 | /* Define to 1 if you have the header file. */ 4 | #undef HAVE_INTTYPES_H 5 | 6 | /* Define to 1 if you have the `m' library (-lm). */ 7 | #undef HAVE_LIBM 8 | 9 | /* Define to 1 if you have the `rt' library (-lrt). */ 10 | #undef HAVE_LIBRT 11 | 12 | /* Define to 1 if you have the header file. */ 13 | #undef HAVE_LIMITS_H 14 | 15 | /* Define to 1 if your system has a GNU libc compatible `malloc' function, and 16 | to 0 otherwise. */ 17 | #undef HAVE_MALLOC 18 | 19 | /* Define to 1 if you have the header file. */ 20 | #undef HAVE_MEMORY_H 21 | 22 | /* Define to 1 if you have the header file. */ 23 | #undef HAVE_STDINT_H 24 | 25 | /* Define to 1 if you have the header file. */ 26 | #undef HAVE_STDLIB_H 27 | 28 | /* Define to 1 if you have the header file. */ 29 | #undef HAVE_STRINGS_H 30 | 31 | /* Define to 1 if you have the header file. */ 32 | #undef HAVE_STRING_H 33 | 34 | /* Define to 1 if you have the header file. */ 35 | #undef HAVE_SYS_STAT_H 36 | 37 | /* Define to 1 if you have the header file. */ 38 | #undef HAVE_SYS_TYPES_H 39 | 40 | /* Define to 1 if you have the header file. */ 41 | #undef HAVE_UNISTD_H 42 | 43 | /* Define to the address where bug reports for this package should be sent. */ 44 | #undef PACKAGE_BUGREPORT 45 | 46 | /* Define to the full name of this package. */ 47 | #undef PACKAGE_NAME 48 | 49 | /* Define to the full name and version of this package. */ 50 | #undef PACKAGE_STRING 51 | 52 | /* Define to the one symbol short name of this package. */ 53 | #undef PACKAGE_TARNAME 54 | 55 | /* Define to the home page for this package. */ 56 | #undef PACKAGE_URL 57 | 58 | /* Define to the version of this package. */ 59 | #undef PACKAGE_VERSION 60 | 61 | /* Define to 1 if you have the ANSI C header files. */ 62 | #undef STDC_HEADERS 63 | 64 | /* Define to `__inline__' or `__inline' if that's what the C compiler 65 | calls it, or to nothing if 'inline' is not supported under any name. */ 66 | #ifndef __cplusplus 67 | #undef inline 68 | #endif 69 | 70 | /* Define to rpl_malloc if the replacement function should be used. */ 71 | #undef malloc 72 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ([2.68]) 5 | AC_INIT([bitfield], [1.0.1], [vitalie@ciubotaru.tk]) 6 | AC_CONFIG_SRCDIR([bitfield.c]) 7 | AC_CONFIG_HEADERS([config.h]) 8 | AC_CONFIG_FILES([libbitfield.pc]) 9 | 10 | # Checks for programs. 11 | AC_PROG_CC 12 | AC_PROG_LN_S 13 | 14 | # Checks for libraries. 15 | # FIXME: Replace `main' with a function in `-lm': 16 | #AC_CHECK_LIB([m], [main]) 17 | # FIXME: Replace `main' with a function in `-lrt': 18 | #AC_CHECK_LIB([rt], [main]) 19 | 20 | # Checks for header files. 21 | AC_CHECK_HEADERS([limits.h stdlib.h string.h endian.h]) 22 | 23 | # Checks for typedefs, structures, and compiler characteristics. 24 | AC_C_INLINE 25 | 26 | # Checks for library functions. 27 | AC_FUNC_MALLOC 28 | 29 | #AC_CONFIG_FILES([Makefile]) 30 | AC_OUTPUT([Makefile] [tests/Makefile] [man/Makefile] [examples/Makefile]) 31 | -------------------------------------------------------------------------------- /examples/Makefile.in: -------------------------------------------------------------------------------- 1 | EXAMPLES=bfnormalize int2bf random rule30 bfcat bfand bfor bfxor 2 | LIBS=-L.. -I.. 3 | 4 | all: build-examples run-examples 5 | build-examples: $(EXAMPLES) 6 | $(EXAMPLES): %: %.c 7 | $(CC) $(CFLAGS) -Wl,-rpath=$(CURDIR) -o $@ $< $(LIBS) -lbitfield 8 | 9 | run-examples: 10 | for i in $(EXAMPLES); do \ 11 | ./$$i; \ 12 | done 13 | 14 | clean: 15 | rm -rvf $(EXAMPLES) 16 | 17 | distclean: 18 | rm Makefile 19 | 20 | .PHONY: build-tests run-tests clean distclean 21 | -------------------------------------------------------------------------------- /examples/bfand.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | printf("Running logical AND over three bit arrays...\n"); 12 | printf("Inputs:\n"); 13 | struct bitfield *input1 = str2bf("1110111"); 14 | bfprint_lsb(input1); 15 | printf("\n"); 16 | struct bitfield *input2 = str2bf("101010100"); 17 | bfprint_lsb(input2); 18 | printf("\n"); 19 | struct bitfield *input3 = str2bf("10100111111"); 20 | bfprint_lsb(input3); 21 | printf("\n"); 22 | struct bitfield *output = bfand(input1, input2, input3); 23 | printf("Output:\n"); 24 | bfprint_lsb(output); 25 | printf("\n"); 26 | printf("---\n"); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /examples/bfcat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | printf("Joining three bit arrays into one...\n"); 12 | printf("Inputs:\n"); 13 | struct bitfield *input1 = str2bf("1010101"); 14 | bfprint_lsb(input1); 15 | printf("\n"); 16 | struct bitfield *input2 = str2bf("101010100"); 17 | bfprint_lsb(input2); 18 | printf("\n"); 19 | struct bitfield *input3 = str2bf("1010101111111"); 20 | bfprint_lsb(input3); 21 | printf("\n"); 22 | struct bitfield *output = 23 | bfcat(input1, input2, input3, input1, input2, input3); 24 | printf("Output:\n"); 25 | bfprint_lsb(output); 26 | printf("\n"); 27 | printf("---\n"); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /examples/bfnormalize.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: examples/int2bf.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Copyright 2016 8 | **/ 9 | 10 | /* Convert a random non-negative integer number to a bit array */ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #ifdef RAND_MAX 19 | #undef RAND_MAX 20 | #define RAND_MAX UINT_MAX 21 | #endif 22 | 23 | int main() 24 | { 25 | printf 26 | ("Representing a random array of bits as the least binary number...\n"); 27 | srand(time(NULL)); 28 | int len = 80; /* terminal width */ 29 | int len_chars = (len - 2) / 2; /* fit 2 strings and a '->' in one line */ 30 | int int_size = sizeof(unsigned int) * CHAR_BIT; /* equal to the number of bits in an unsigned int */ 31 | int len_ints = (len_chars - 1) / int_size + 1; /* nr of ints to hold one len_str */ 32 | int i, j; 33 | unsigned int *input_ints = malloc(sizeof(unsigned int) * len_ints); 34 | struct bitfield *input, *output; 35 | for (i = 0; i < 50; i++) { 36 | for (j = 0; j < len_ints; j++) 37 | input_ints[j] = rand(); 38 | input = int2bf(input_ints, len_chars); 39 | output = bfnormalize(input); 40 | bfprint_msb(input); 41 | printf("->"); 42 | bfprint_msb(output); 43 | printf("\n"); 44 | } 45 | bfdel(input); 46 | bfdel(output); 47 | printf("---\n"); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /examples/bfor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | printf("Running logical OR over three bit arrays...\n"); 12 | printf("Inputs:\n"); 13 | struct bitfield *input1 = str2bf("1111011"); 14 | bfprint_lsb(input1); 15 | printf("\n"); 16 | struct bitfield *input2 = str2bf("101000100"); 17 | bfprint_lsb(input2); 18 | printf("\n"); 19 | struct bitfield *input3 = str2bf("1010100111"); 20 | bfprint_lsb(input3); 21 | printf("\n"); 22 | struct bitfield *output = bfor(input1, input2, input3); 23 | printf("Output:\n"); 24 | bfprint_lsb(output); 25 | printf("\n"); 26 | printf("---\n"); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /examples/bfxor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | printf("Running logical XOR over three bit arrays...\n"); 12 | printf("Inputs:\n"); 13 | struct bitfield *input1 = str2bf("10111111"); 14 | bfprint_lsb(input1); 15 | printf("\n"); 16 | struct bitfield *input2 = str2bf("10100000"); 17 | bfprint_lsb(input2); 18 | printf("\n"); 19 | struct bitfield *input3 = str2bf("0010101111111"); 20 | bfprint_lsb(input3); 21 | printf("\n"); 22 | struct bitfield *output = bfxor(input1, input2, input3); 23 | printf("Output:\n"); 24 | bfprint_lsb(output); 25 | printf("\n"); 26 | printf("---\n"); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /examples/int2bf.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: examples/int2bf.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Copyright 2016 8 | **/ 9 | 10 | /* Convert a random non-negative integer number to a bit array */ 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #ifdef RAND_MAX 19 | #undef RAND_MAX 20 | #define RAND_MAX UINT_MAX 21 | #endif 22 | 23 | int main() 24 | { 25 | printf 26 | ("Converting a random integer into a bit array of fixed length...\n"); 27 | srand(time(NULL)); 28 | int bitfield_size = sizeof(unsigned int) * CHAR_BIT; /* equal to the number of bits in an unsigned int */ 29 | int i; 30 | unsigned int input; 31 | struct bitfield *output = bfnew(bitfield_size); 32 | for (i = 0; i < 50; i++) { 33 | input = rand(); 34 | /** 35 | * create an array long enough to hold all bits from an unsigned int 36 | * because we have one int, not an array of ints, we pass it by pointer 37 | **/ 38 | output = int2bf(&input, bitfield_size); 39 | printf("%u -> %s\n", input, bf2str(output)); 40 | } 41 | printf("---\n"); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /examples/random.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: examples/random.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 9, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | //#include 14 | //#include 15 | #include 16 | 17 | int main() 18 | { 19 | printf 20 | ("Generating a long random sequence and storing it in a bit array...\n"); 21 | srand(time(NULL)); 22 | int size = 80; 23 | int i; 24 | struct bitfield *input = bfnew(size); 25 | for (i = 0; i < size; i++) { 26 | if (rand() % 2) 27 | bfsetbit(input, i); 28 | } 29 | bfprint_msb(input); 30 | printf("\n"); 31 | printf("---\n"); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /examples/rule30.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: examples/rule30.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 1, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | int main() 16 | { 17 | printf("Wolfram's Rule30 cellular automaton...\n"); 18 | srand(time(NULL)); 19 | int size = 78; 20 | int i; 21 | /* create an array of parent generation cells with 2 extra cells */ 22 | struct bitfield *input = bfnew(size + 2); 23 | /* randomly assign "dead" (0) or "alive" (1) status to parent generation */ 24 | for (i = 0; i < bfsize(input); i++) 25 | if (rand() % 2 == 1) 26 | bfsetbit(input, i); 27 | /* show it */ 28 | bfprint_lsb(input); 29 | printf("\n"); 30 | /* Because the state of every cell depends on its parent and the parent's neighbours from right and left, 31 | * we need three arrays: neighbours from left, parents (center) and neighbours from right. 32 | */ 33 | struct bitfield *left = bfsub(input, 0, bfsize(input) - 2); 34 | struct bitfield *center = bfsub(input, 1, bfsize(input) - 1); 35 | struct bitfield *right = bfsub(input, 2, bfsize(input)); 36 | /* compute the child generation by Rule 30: 37 | * Child(i) = Parent(i-1) XOR ( Parent(i) OR Parent(i+1) ) 38 | */ 39 | struct bitfield *tmp = bfor(center, right); 40 | struct bitfield *output = bfxor(left, tmp); 41 | /* show it */ 42 | printf(" "); 43 | bfprint_lsb(output); 44 | printf("\n"); 45 | printf("---\n"); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /libbitfield.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=@exec_prefix@ 3 | libdir=@libdir@ 4 | includedir=@includedir@ 5 | 6 | Name: @PACKAGE_NAME@ 7 | Description: A bit array manipulation library written in C 8 | URL: https://github.com/ciubotaru/bitfield 9 | Version: @PACKAGE_VERSION@ 10 | Requires: 11 | Libs: -L${libdir} -lbitfield 12 | Cflags: -I${includedir} 13 | -------------------------------------------------------------------------------- /man/Makefile.in: -------------------------------------------------------------------------------- 1 | datarootdir=@datarootdir@ 2 | prefix=@prefix@ 3 | mandir=@mandir@ 4 | man_MANS=bf_converters.3 bfand.3 bfcat.3 bfclone.3 bfcmp.3 bfcpy.3 bfhamming.3 bfisempty.3 bfnew.3 bfnormalize.3 bfpopcount.3 bfpos.3 bfprint.3 bfresize.3 bfrev.3 bfsetall.3 bfsetbit.3 bfshift.3 bfsize.3 bfsub.3 bitfield.3 5 | 6 | all: $(man_MANS) 7 | 8 | $(man_MANS): %: %.in 9 | sed -e 's/%RELEASE_DATE%/$(RELEASE_DATE)/g' -e 's/%VERSION%/$(VERSION)/g' $< > $@ 10 | 11 | install: 12 | mkdir -p $(DESTDIR)$(mandir)/man3 13 | for i in $(man_MANS); do install -m 644 $$i $(DESTDIR)$(mandir)/man3/$$i; done 14 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bf2str.3 15 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bf2char.3 16 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bf2int.3 17 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bf2short.3 18 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bf2long.3 19 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bf2ll.3 20 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bftouint8.3 21 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bftouint16.3 22 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bftouint32.3 23 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bftouint64.3 24 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bf2str_ip.3 25 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bf2char_ip.3 26 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bf2int_ip.3 27 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bf2short_ip.3 28 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bf2long_ip.3 29 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bf2ll_ip.3 30 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bftouint8_ip.3 31 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bftouint16_ip.3 32 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bftouint32_ip.3 33 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/bftouint64_ip.3 34 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/str2bf.3 35 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/char2bf.3 36 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/short2bf.3 37 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/int2bf.3 38 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/long2bf.3 39 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/ll2bf.3 40 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/uint8tobf.3 41 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/uint16tobf.3 42 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/uint32tobf.3 43 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/uint64tobf.3 44 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/str2bf_ip.3 45 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/char2bf_ip.3 46 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/short2bf_ip.3 47 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/int2bf_ip.3 48 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/long2bf_ip.3 49 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/ll2bf_ip.3 50 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/uint8tobf_ip.3 51 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/uint16tobf_ip.3 52 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/uint32tobf_ip.3 53 | ln -s bf_converters.3 $(DESTDIR)$(mandir)/man3/uint64tobf_ip.3 54 | ln -s bfand.3 $(DESTDIR)$(mandir)/man3/bfnot.3 55 | ln -s bfand.3 $(DESTDIR)$(mandir)/man3/bfnot_ip.3 56 | ln -s bfand.3 $(DESTDIR)$(mandir)/man3/bfor.3 57 | ln -s bfand.3 $(DESTDIR)$(mandir)/man3/bfxor.3 58 | ln -s bfnew.3 $(DESTDIR)$(mandir)/man3/bfdel.3 59 | ln -s bfnew.3 $(DESTDIR)$(mandir)/man3/bfnew_ones.3 60 | ln -s bfnew.3 $(DESTDIR)$(mandir)/man3/bfnew_quick.3 61 | ln -s bfprint.3 $(DESTDIR)$(mandir)/man3/bfprint_lsb.3 62 | ln -s bfprint.3 $(DESTDIR)$(mandir)/man3/bfprint_msb.3 63 | ln -s bfrev.3 $(DESTDIR)$(mandir)/man3/bfrev_ip.3 64 | ln -s bfsetall.3 $(DESTDIR)$(mandir)/man3/bfclearall.3 65 | ln -s bfsetbit.3 $(DESTDIR)$(mandir)/man3/bfclearbit.3 66 | ln -s bfsetbit.3 $(DESTDIR)$(mandir)/man3/bfgetbit.3 67 | ln -s bfsetbit.3 $(DESTDIR)$(mandir)/man3/bftogglebit.3 68 | ln -s bfshift.3 $(DESTDIR)$(mandir)/man3/bfshift_ip.3 69 | 70 | uninstall: 71 | for i in bf2str.3 bf2char.3 bf2int.3 bf2short.3 bf2long.3 bf2ll.3 bftouint8.3 bftouint16.3 bftouint32.3 bftouint64.3 bf2str_ip.3 bf2char_ip.3 bf2int_ip.3 bf2short_ip.3 bf2long_ip.3 bf2ll_ip.3 bftouint8_ip.3 bftouint16_ip.3 bftouint32_ip.3 bftouint64_ip.3 str2bf.3 char2bf.3 short2bf.3 int2bf.3 long2bf.3 ll2bf.3 uint8tobf.3 uint16tobf.3 uint32tobf.3 uint64tobf.3 str2bf_ip.3 char2bf_ip.3 short2bf_ip.3 int2bf_ip.3 long2bf_ip.3 ll2bf_ip.3 uint8tobf_ip.3 uint16tobf_ip.3 uint32tobf_ip.3 uint64tobf_ip.3 bfnot.3 bfnot_ip.3 bfor.3 bfxor.3 bfdel.3 bfnew_ones.3 bfnew_quick.3 bfprint_lsb.3 bfprint_msb.3 bfrev_ip.3 bfclearall.3 bfclearbit.3 bfgetbit.3 bftogglebit.3 bfshift_ip.3; do if [ -e $(DESTDIR)$(mandir)/man3/$$i ]; then rm -v $(DESTDIR)$(mandir)/man3/$$i; fi; done 72 | for i in $(man_MANS); do if [ -e $(DESTDIR)$(mandir)/man3/$$i ]; then rm -v $(DESTDIR)$(mandir)/man3/$$i; fi; done 73 | 74 | distclean: 75 | rm Makefile 76 | 77 | clean: 78 | rm -rvf $(man_MANS) 79 | -------------------------------------------------------------------------------- /man/bf_converters.3.in: -------------------------------------------------------------------------------- 1 | .TH bitfield_converters 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bf2str, bf2char, bf2short, bf2int, bf2long, bf2ll, bftouint8, bftouint16, bftouint32, bftouint64 \- convert bit arrays to arrays of standard data types 4 | .sp 5 | bf2str_ip, bf2char_ip, bf2short_ip, bf2int_ip, bf2long_ip, bf2ll_ip, bftouint8_ip, bftouint16_ip, bftouint32_ip, bftouint64_ip \- convert bit arrays to arrays of standard data types "in place" 6 | .sp 7 | str2bf, char2bf, short2bf, int2bf, long2bf, ll2bf, uint8tobf, uint16tobf, uint32tobf, uint64tobf \- convert arrays of standard data types to bit arrays 8 | .sp 9 | str2bf_ip, char2bf_ip, short2bf_ip, int2bf_ip, long2bf_ip, ll2bf_ip, uint8tobf_ip, uint16tobf_ip, uint32tobf_ip, uint64tobf_ip \- convert arrays of standard data types to bit arrays "in place" 10 | .SH SYNOPSIS 11 | .nf 12 | .B "#include 13 | .sp 14 | .BI "char *bf2str(const struct bitfield *"input "); 15 | .BI "char *bf2char(const struct bitfield *"input "); 16 | .BI "unsigned short *bf2short(const struct bitfield *"input "); 17 | .BI "unsigned int *bf2int(const struct bitfield *"input "); 18 | .BI "unsigned long *bf2long(const struct bitfield *"input "); 19 | .BI "unsigned long long *bf2ll(const struct bitfield *"input "); 20 | .BI "uint8_t *bftouint8(const struct bitfield *"input "); 21 | .BI "uint16_t *bftouint16(const struct bitfield *"input "); 22 | .BI "uint32_t *bftouint32(const struct bitfield *"input "); 23 | .BI "uint64_t *bftouint64(const struct bitfield *"input "); 24 | .sp 25 | .BI "void bf2str_ip(const struct bitfield *"input ", char *"output "); 26 | .BI "void bf2char_ip(const struct bitfield *"input ", char *"output "); 27 | .BI "void bf2short_ip(const struct bitfield *"input ", unsigned short *"output "); 28 | .BI "void bf2int_ip(const struct bitfield *"input ", unsigned int *"output "); 29 | .BI "void bf2long_ip(const struct bitfield *"input ", unsigned long *"output "); 30 | .BI "void bf2ll_ip(const struct bitfield *"input ", unsigned long long *"output "); 31 | .BI "void bftouint8_ip(const struct bitfield *"input ", uint8_t *"output "); 32 | .BI "void bftouint16_ip(const struct bitfield *"input ", uint16_t *"output "); 33 | .BI "void bftouint32_ip(const struct bitfield *"input ", uint32_t *"output "); 34 | .BI "void bftouint64_ip(const struct bitfield *"input ", uint64_t *"output "); 35 | .sp 36 | .BI "struct bitfield *str2bf(const char *"input "); 37 | .BI "struct bitfield *char2bf(const char *"input ", unsigned int size); 38 | .BI "struct bitfield *short2bf(const unsigned short *"input ", unsigned int size); 39 | .BI "struct bitfield *int2bf(const unsigned int *"input ", unsigned int size); 40 | .BI "struct bitfield *long2bf(const unsigned long *"input ", unsigned int size); 41 | .BI "struct bitfield *ll2bf(const unsigned long long *"input ", unsigned int size); 42 | .BI "struct bitfield *uint8tobf(const uint8_t *"input ", unsigned int size); 43 | .BI "struct bitfield *uint16tobf(const uint16_t *"input ", unsigned int size); 44 | .BI "struct bitfield *uint32tobf(const uint32_t *"input ", unsigned int size); 45 | .BI "struct bitfield *uint64tobf(const uint64_t *"input ", unsigned int size); 46 | .sp 47 | .BI "void str2bf_ip(const char *"input ", struct bitfield *"output "); 48 | .BI "void char2bf_ip(const char *"input ", struct bitfield *"output "); 49 | .BI "void short2bf_ip(const unsigned short *"input ", struct bitfield *"output "); 50 | .BI "void int2bf_ip(const unsigned int *"input ", struct bitfield *"output "); 51 | .BI "void long2bf_ip(const unsigned long *"input ", struct bitfield *"output "); 52 | .BI "void ll2bf_ip(const unsigned long long *"input ", struct bitfield *"output "); 53 | .BI "void uint8tobf_ip(const uint8_t *"input ", struct bitfield *"output "); 54 | .BI "void uint16tobf_ip(const uint16_t *"input ", struct bitfield *"output "); 55 | .BI "void uint32tobf_ip(const uint32_t *"input ", struct bitfield *"output "); 56 | .BI "void uint64tobf_ip(const uint64_t *"input ", struct bitfield *"output "); 57 | .fi 58 | .SH DESCRIPTION 59 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 60 | .sp 61 | This family of functions convert between bitfield structure and standard data types. Supported data types are: unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, uint8_t, uint16_t, uint32_t and uint64_t. 62 | .sp 63 | The "_ip" suffix stands for "in-place" conversion, i.e. instead of creating a new array, the function fills an existing, properly initialized one. The no-prefix functions return the result of conversion on success or NULL on failure. 64 | .sp 65 | Unsigned chars can be treated in two ways. Functions with "str" in their name treat each char as a storage of one character, '0' or '1'. Functions with "char" in their name treat each char as a storage of several (usually 8) bits. 66 | .sp 67 | .SH "SEE ALSO" 68 | For the full list of bitfield functions and their descriptions, see manual page for 69 | .BR bitfield (3). 70 | .SH AUTHOR 71 | Vitalie CIUBOTARU 72 | 73 | -------------------------------------------------------------------------------- /man/bfand.3.in: -------------------------------------------------------------------------------- 1 | .TH bfand 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfand, bfnot, bfnot_ip, bfor, bfxor \- perform logical operations over bit arrays. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "struct bitfield *bfand(const struct bitfield *"input1 ", const struct bitfield *"input2 ", ...); 9 | .BI "struct bitfield *bfnot(const struct bitfield *"input "); 10 | .BI "void bfnot_ip(const struct bitfield *"instance "); 11 | .BI "struct bitfield *bfor(const struct bitfield *"input1 ", const struct bitfield *"input2 ", ...); 12 | .BI "struct bitfield *bfxor(const struct bitfield *"input1 ", const struct bitfield *"input2 ", ...); 13 | .fi 14 | .SH DESCRIPTION 15 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 16 | .sp 17 | The \fBbfand()\fR function takes pointers to two or more arguments, all of them pointers to bitfields, performs logical AND over each pairs of bits from the first two (and iteratively with each additional argument, if any) and stores the result in a new bitfield. The function returns a pointer to the new bitfield or NULL on failure. 18 | .sp 19 | The following table illustrates inputs and results of logical AND: 20 | .sp 21 | .nf 22 | input1 input2 output 23 | -------------------- 24 | 0 0 0 25 | 0 1 0 26 | 1 0 0 27 | 1 1 1 28 | .fi 29 | .sp 30 | The \fBbfnot()\fR function takes pointer to a bitfield, \fIinput\fR, reverses all its bits and stores the result in a new bitfield, \fIoutput\fR. The function returns a pointer to the new bitfield or NULL on failure. The following table illustrates input and result of logical NOT: 31 | .sp 32 | .nf 33 | input output 34 | ------------ 35 | 0 1 36 | 1 0 37 | .fi 38 | .sp 39 | The \fBbfnot_ip()\fR function is an "in-place" version of the \fBbnot()\fR. 40 | .sp 41 | The \fBbfor()\fR function takes pointers to two or more arguments, all pointers to bitfields, and performs bitwise inclusive OR over pairs of bits from the first two arguments (then iteratively with every additional argument, if any) and stores the result in a new bitfield. The function returns a pointer to the new bitfield or NULL on failure. 42 | .sp 43 | The following table illustrates inputs and results of logical inclusive OR: 44 | .sp 45 | .nf 46 | input1 input2 output 47 | -------------------- 48 | 0 0 0 49 | 0 1 1 50 | 1 0 1 51 | 1 1 1 52 | .fi 53 | .sp 54 | The \fBbfxor()\fR function takes pointers to two arguments, all pointers to bitfields, and performs bitwise eXclusive OR (XOR) over pairs of bits from the first two arguments (then iteratively with every additional argument, if any) and stores the result in a new bitfield. The function returns a pointer to the new bitfield or NULL on failure. 55 | .sp 56 | The following table illustrates inputs and results of logical eXclusive OR (XOR): 57 | .sp 58 | .nf 59 | input1 input2 output 60 | -------------------- 61 | 0 0 0 62 | 0 1 0 63 | 1 0 1 64 | 1 1 0 65 | .fi 66 | .sp 67 | .SH "SEE ALSO" 68 | For the full list of bitfield functions and their descriptions, see manual page for 69 | .BR bitfield (3). 70 | .SH AUTHOR 71 | Vitalie CIUBOTARU 72 | 73 | -------------------------------------------------------------------------------- /man/bfcat.3.in: -------------------------------------------------------------------------------- 1 | .TH bfcat 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfcat \- join several bit arrays into one. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "struct bitfield *bfcat(const struct bitfield *"input1 ", ...); 9 | .fi 10 | .SH DESCRIPTION 11 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 12 | .sp 13 | The \fBbfcat()\fR function takes one or more arguments, all of them pointers to bitfields, and creates a new bitfield that contains all bits from all bitfields, consecutively. The function returns a pointer to the new bitfield or NULL on failure. 14 | .sp 15 | .SH "SEE ALSO" 16 | For the full list of bitfield functions and their descriptions, see manual page for 17 | .BR bitfield (3). 18 | .SH AUTHOR 19 | Vitalie CIUBOTARU 20 | 21 | -------------------------------------------------------------------------------- /man/bfclone.3.in: -------------------------------------------------------------------------------- 1 | .TH bfclone 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfclone \- create a copy of an existing bit array. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "struct bitfield *bfclone(struct bitfield *"input "); 9 | .fi 10 | .SH DESCRIPTION 11 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 12 | .sp 13 | The \fBbfclone()\fR function initializes a bitfield of the same \fIsize\fR as \fIinput\fR, copies the content of \fIinput\fR into the new bitfield and returns a pointer to it or NULL on failure. 14 | .SH EXAMPLES 15 | The following code 16 | .sp 17 | struct bitfield * my_new_bitfield = bfclone(my_old_bitfield); 18 | .sp 19 | creates a bitfield called \fImy_new_bitfield\fR, whose size and contents are identical to \fImy_old_bitfield\fR. 20 | .sp 21 | .SH "SEE ALSO" 22 | For the full list of bitfield functions and their descriptions, see manual page for 23 | .BR bitfield (3). 24 | .SH AUTHOR 25 | Vitalie CIUBOTARU 26 | 27 | -------------------------------------------------------------------------------- /man/bfcmp.3.in: -------------------------------------------------------------------------------- 1 | .TH bfcmp 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfcmp \- compare two bit arrays. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "unsigned int bfcmp(const struct bitfield *"input1 ", const struct bitfield *"input2 ", char **"errmsg "); 9 | .fi 10 | .SH DESCRIPTION 11 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 12 | .sp 13 | The \fBbfcmp()\fR function takes pointers to two bitfields, \fIinput1\fR and \fIinput2\fR, and a pointer to a string \fIerrmsg\fR, and compares them bit-to-bit. The function returns 0 if the two bitfields are identical, 1 if they differ or 2 if they have different size. In case the bitfields are different or have different size, the function also returns an error message \fIerrmsg\fR. 14 | .sp 15 | .SH "SEE ALSO" 16 | For the full list of bitfield functions and their descriptions, see manual page for 17 | .BR bitfield (3). 18 | .SH AUTHOR 19 | Vitalie CIUBOTARU 20 | 21 | -------------------------------------------------------------------------------- /man/bfcpy.3.in: -------------------------------------------------------------------------------- 1 | .TH bfcpy 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfcpy \- copies the contents of a bit array into another bit array. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "unsigned int bfcpy(const struct bitfield *"src ", struct bitfield *"dest "); 9 | .fi 10 | .SH DESCRIPTION 11 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 12 | .sp 13 | The \fBbfcpy()\fR function takes pointers to bitfields \fIsrc\fR and \fIdest\fR, and copies the bits from \fIsrc\fR to a properly initialized \fIdest\fR bit. The function returns 0 on success and 1 otherwise. 14 | .sp 15 | .SH "SEE ALSO" 16 | For the full list of bitfield functions and their descriptions, see manual page for 17 | .BR bitfield (3). 18 | .SH AUTHOR 19 | Vitalie CIUBOTARU 20 | 21 | -------------------------------------------------------------------------------- /man/bfhamming.3.in: -------------------------------------------------------------------------------- 1 | .TH bfhamming 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfhamming \- counts the Humming distance between two bitfields. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "unsigned int bfhamming(const struct bitfield *"input1 ", const struct bitfield *"input2 "); 9 | .fi 10 | .SH DESCRIPTION 11 | A bitfield is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 12 | .sp 13 | The \fBbfhamming()\fR function is an implementation of Hamming distance between two bitfields. It takes pointers to bitfields \fIinput1\fR and \fIinput2\fR, and returns the number of bits that are different between the two bitfields. 14 | .sp 15 | .SH "SEE ALSO" 16 | For the full list of bitfield functions and their descriptions, see manual page for 17 | .BR bitfield (3). 18 | .SH AUTHOR 19 | Vitalie CIUBOTARU 20 | 21 | -------------------------------------------------------------------------------- /man/bfisempty.3.in: -------------------------------------------------------------------------------- 1 | .TH bfisempty 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfisempty \- checks whether all bit of an array are unset. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "unsigned int bfisempty(const struct bitfield *"instance "); 9 | .fi 10 | .SH DESCRIPTION 11 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 12 | .sp 13 | The \fBbfisempty()\fR function takes a pointer to bitfield \fIinstance\fR and returns 0 if all bits are unset or 1 otherwise. 14 | .sp 15 | .SH "SEE ALSO" 16 | For the full list of bitfield functions and their descriptions, see manual page for 17 | .BR bitfield (3). 18 | .SH AUTHOR 19 | Vitalie CIUBOTARU 20 | 21 | -------------------------------------------------------------------------------- /man/bfnew.3.in: -------------------------------------------------------------------------------- 1 | .TH bfnew 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfnew, bfnew_quick, bfnew_ones, bfdel \- create and delete bit arrays. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "struct bitfield *bfnew(const unsigned int "size "); 9 | .BI "struct bitfield *bfnew_quick(const unsigned int "size "); 10 | .BI "struct bitfield *bfnew_ones(const unsigned int "size "); 11 | .BI "void bfdel(struct bitfield *"instance "); 12 | .fi 13 | .SH DESCRIPTION 14 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 15 | .sp 16 | The \fBbfnew()\fR function initializes a bitfield of specified \fIsize\fR (i.e. long enough to host a series of bits of length equal to \fIsize\fR) and returns a pointer to it or NULL on failure. This pointer can later be successfully passed to \fBbfdel()\fR. The bitfield is guaranteed to be empty (i.e. contain only zeroes). 17 | .sp 18 | The \fBbfnew_quick()\fR function does the same thing as \fBbfnew()\fR, except that it does not clear the bits. 19 | .sp 20 | The \fBbfnew_quick()\fR function does the same thing as \fBbfnew()\fR, except that it sets the bits to true (i.e. it contains only ones). 21 | .sp 22 | The \fBbfdel()\fR function frees the memory space pointed to by \fIinstance\fR, which must have been returned by a previous call to \fBbfnew()\fR or another similar function. 23 | .SH EXAMPLES 24 | The following code 25 | .sp 26 | struct bitfield * mybitfield = bfnew(80); 27 | .sp 28 | will create a bitfield called \fImybitfield\fR with \fIsize\fR equal to 3 and a \fIfield\fR, long enough to host 80 bits. The number of longs in \fIfield\fR depends on \fIsize\fR and machine architecture. On machines with 32-bit longs, that will be 3 longs. 29 | .sp 30 | The following code 31 | .sp 32 | bfdel(mybitfield); 33 | .sp 34 | will delete the memory taken by a bitfield called \fImybitfield\fR, effectively deleting the bitfield and making it unavailable for subsequent calls. 35 | .sp 36 | .SH "SEE ALSO" 37 | For the full list of bitfield functions and their descriptions, see manual page for 38 | .BR bitfield (3). 39 | .SH AUTHOR 40 | Vitalie CIUBOTARU 41 | 42 | -------------------------------------------------------------------------------- /man/bfnormalize.3.in: -------------------------------------------------------------------------------- 1 | .TH bfnormalize 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfnormalize \- find the smallest value of a bit array as a ring. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "struct bitfield *bfnormalize(const struct bitfield *"input "); 9 | .fi 10 | .SH DESCRIPTION 11 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 12 | .sp 13 | The \fBbfnormalize()\fR function takes a pointer to a bitfield, \fIinput\fR, and creates a new bitfield, which represents the original array of bits loop-shifted in such a way as to produce the smallest binary number. The function returns a pointer to the new bitfield or NULL on failure. 14 | .sp 15 | .SH EXAMPLE 16 | Suppose we have a bit array with the following content: 17 | .sp 18 | 1001010110001101 19 | .sp 20 | The bfnormalize() function treats it as a loop and shifts it until it represents the smallest possible binary number. In this case, the normalized version will be: 21 | .sp 22 | 0001101100101011 23 | .sp 24 | .SH "SEE ALSO" 25 | For the full list of bitfield functions and their descriptions, see manual page for 26 | .BR bitfield (3). 27 | .SH AUTHOR 28 | Vitalie CIUBOTARU 29 | 30 | -------------------------------------------------------------------------------- /man/bfpopcount.3.in: -------------------------------------------------------------------------------- 1 | .TH bfpopcount 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfpopcount \- counts the set bits in a bit array. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "unsigned int bfpopcount(const struct bitfield *"instance "); 9 | .fi 10 | .SH DESCRIPTION 11 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 12 | .sp 13 | The \fBbfpopcount()\fR function is an implementation of population count (known as the Hamming weight) of a bitfield. It takes pointers to bitfield \fIinstance\fR and returns the number of set bits in it. 14 | .sp 15 | .SH "SEE ALSO" 16 | For the full list of bitfield functions and their descriptions, see manual page for 17 | .BR bitfield (3). 18 | .SH AUTHOR 19 | Vitalie CIUBOTARU 20 | 21 | -------------------------------------------------------------------------------- /man/bfpos.3.in: -------------------------------------------------------------------------------- 1 | .TH bfpos 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfpos \- check whether a bit array contains a sub-array. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "unsigned int bfpos(const struct bitfield *"haystack ", const struct bitfield *"needle "); 9 | .fi 10 | .SH DESCRIPTION 11 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 12 | .sp 13 | The \fBbfpos()\fR function takes pointers to bitfields \fIhaystack\fR and \fIneedle\fR, and searches for the sub-array \fIneedle\fR in the \fIhaystack\fR. The function returns the starting position of the sub-array, -1 if the sub-array was not found, or -2 if the size of \fIneedle\fR is longer than that of \fIhaystack\fR. 14 | .sp 15 | .SH "SEE ALSO" 16 | For the full list of bitfield functions and their descriptions, see manual page for 17 | .BR bitfield (3). 18 | .SH AUTHOR 19 | Vitalie CIUBOTARU 20 | 21 | -------------------------------------------------------------------------------- /man/bfprint.3.in: -------------------------------------------------------------------------------- 1 | .TH bfprint 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfprint_lsb, bfprint_msb \- write the contents of a bitfield to stdout as zeroes and ones. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "void bfprint_lsb(const struct bitfield *"instance "); 9 | .BI "void bfprint_msb(const struct bitfield *"instance "); 10 | .fi 11 | .SH DESCRIPTION 12 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 13 | .sp 14 | The \fBbfprint_lsb()\fR function takes a poiinter to bitfield \fIinstance\fR, and writes its bits to \fIstdout\fR as zeroes and ones, from left to right, least significant bit first. 15 | .sp 16 | The \fBbfprint_msb()\fR function takes a poiinter to bitfield \fIinstance\fR, and writes its bits to \fIstdout\fR as zeroes and ones, from left to right, most significant bit first. 17 | .sp 18 | .SH "SEE ALSO" 19 | For the full list of bitfield functions and their descriptions, see manual page for 20 | .BR bitfield (3). 21 | .SH AUTHOR 22 | Vitalie CIUBOTARU 23 | 24 | -------------------------------------------------------------------------------- /man/bfresize.3.in: -------------------------------------------------------------------------------- 1 | .TH bfresize 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfresize \- resize an existing bitfield. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "void bfresize(struct bitfield *"instance ", const unsigned int "new_size "); 9 | .fi 10 | .SH DESCRIPTION 11 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 12 | .sp 13 | The \fBbfresize()\fR function takes a pointer to bitfield \fIinstance\fR and an integer number \fInew_size\fR, and changes the size of \fIinstance\fR to \fInew_size\fR. If the new size is larger, then the difference is filled with zeroes. 14 | .sp 15 | .SH "SEE ALSO" 16 | For the full list of bitfield functions and their descriptions, see manual page for 17 | .BR bitfield (3). 18 | .SH AUTHOR 19 | Vitalie CIUBOTARU 20 | 21 | -------------------------------------------------------------------------------- /man/bfrev.3.in: -------------------------------------------------------------------------------- 1 | .TH bfrev 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfrev, bifrev_ip \- reverse the order of bits in a bit array. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "struct bitfield *bfrev(const struct bitfield *"instance "); 9 | .BI "void *bfrev_ip(struct bitfield *"input "); 10 | .fi 11 | .SH DESCRIPTION 12 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 13 | .sp 14 | The \fBbfrev()\fR function takes a pointer to bitfield \fIinput\fR and reverses the order of bits in it. The function returns a pointer to the new, reversed bitfield, or NULL on failure. 15 | .sp 16 | The \fBbfrev_ip()\fR function takes a pointer to bitfield \fIinstance\fR and reverses the order of bits in it. The changes are made "in place", in the original bitfield. 17 | .sp 18 | .SH "SEE ALSO" 19 | For the full list of bitfield functions and their descriptions, see manual page for 20 | .BR bitfield (3). 21 | .SH AUTHOR 22 | Vitalie CIUBOTARU 23 | 24 | -------------------------------------------------------------------------------- /man/bfsetall.3.in: -------------------------------------------------------------------------------- 1 | .TH bfsetall 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfsetall, bfclearall \- sets/unsets all bits in a bit array. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "void bfsetall(struct bitfield *"instance "); 9 | .BI "void bfclearall(struct bitfield *"instance "); 10 | .fi 11 | .SH DESCRIPTION 12 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 13 | .sp 14 | The \fBbfsetall()\fR function takes a pointer to bitfield \fIinstance\fR, and sets all its bits (i.e. fills the bitfield with units). 15 | .sp 16 | The \fBbfclearall()\fR function takes a pointer to bitfield \fIinstance\fR, and clears all its bits (i.e. fills the bitfield with zeroes). 17 | .sp 18 | .SH "SEE ALSO" 19 | For the full list of bitfield functions and their descriptions, see manual page for 20 | .BR bitfield (3). 21 | .SH AUTHOR 22 | Vitalie CIUBOTARU 23 | 24 | -------------------------------------------------------------------------------- /man/bfsetbit.3.in: -------------------------------------------------------------------------------- 1 | .TH bfsetbit 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfsetbit, bfclearbit, bfgetbit, bftogglebit \- manipulate one bit in a bit array. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "void bfsetbit(struct bitfield *"instance ", const unsigned int "bit "); 9 | .BI "void bfclearbit(struct bitfield *"instance ", const unsigned int "bit "); 10 | .BI "int bfgetbit(const struct bitfield *"instance ", const unsigned int "bit "); 11 | .BI "void bftogglebit(struct bitfield *"instance ", const unsigned int "bit "); 12 | .fi 13 | .SH DESCRIPTION 14 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 15 | .sp 16 | The \fBbfsetbit()\fR function takes a pointer to bitfield \fIinstance\fR and an integer \fIbit\fR, and sets bit number \fIbit\fR (i.e. fills it with a unit). 17 | .sp 18 | The \fBbfclearbit()\fR function takes a pointer to bitfield \fIinstance\fR and an integer \fIbit\fR, and clears bit number \fIbit\fR (i.e. fills it with a zero). 19 | .sp 20 | The \fBbfgetbit()\fR function takes a pointer to bitfield \fIinstance\fR and an integer \fIbit\fR, and returns the state if bit number \fIbit\fR (i.e. 1 if the bit is set and 0 otherwise). 21 | .sp 22 | The \fBbftogglebit()\fR function takes a pointer to bitfield \fIinstance\fR and an integer \fIbit\fR, and toggles the state if bit number \fIbit\fR (i.e. sets the bit if it was clear and clears it if it was set). 23 | .sp 24 | .SH "SEE ALSO" 25 | For the full list of bitfield functions and their descriptions, see manual page for 26 | .BR bitfield (3). 27 | .SH AUTHOR 28 | Vitalie CIUBOTARU 29 | -------------------------------------------------------------------------------- /man/bfshift.3.in: -------------------------------------------------------------------------------- 1 | .TH bfshift 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfshift, bifshift_ip \- circular-shift the contents of a bit array. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "struct bitfield *bfshift(const struct bitfield *"input ", const unsigned int "offset "); 9 | .BI "void *bfshift_ip(struct bitfield *"input ", const unsigned int "offset "); 10 | .fi 11 | .SH DESCRIPTION 12 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 13 | .sp 14 | The \fBbfshift()\fR function takes a pointer to bitfield \fIinput\fR and an integer \fIoffset\fR, and circular-shifts the bits by \fIoffset\fR positions from the origin. In other words, it "cuts" the array after \fIoffset\fR bits and swaps the parts. It \fIoffset\fR is negative, it is counted from the end. The function returns a pointer to the new bitfield or NULL on failure. 15 | .sp 16 | The \fBbfshift_ip()\fR function does the same thing, but changes are made "in place", in the original bitfield. 17 | .sp 18 | .SH "SEE ALSO" 19 | For the full list of bitfield functions and their descriptions, see manual page for 20 | .BR bitfield (3). 21 | .SH AUTHOR 22 | Vitalie CIUBOTARU 23 | 24 | -------------------------------------------------------------------------------- /man/bfsize.3.in: -------------------------------------------------------------------------------- 1 | .TH bfsize 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfsize \- obtain the number of bits of a bit array. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "unsigned int bfsize(const struct bitfield *"instance "); 9 | .fi 10 | .SH DESCRIPTION 11 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 12 | .sp 13 | The \fBbfsize()\fR function takes pointers to a bitfield \fIinstance\fR and returns its \fIsize\fR (the number of bits). 14 | .sp 15 | .SH "SEE ALSO" 16 | For the full list of bitfield functions and their descriptions, see manual page for 17 | .BR bitfield (3). 18 | .SH AUTHOR 19 | Vitalie CIUBOTARU 20 | 21 | -------------------------------------------------------------------------------- /man/bfsub.3.in: -------------------------------------------------------------------------------- 1 | .TH bfsub 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bfsub \- extract a slice of a bit array. 4 | .SH SYNOPSIS 5 | .nf 6 | .B "#include 7 | .sp 8 | .BI "struct bitfield *bfsub(const struct bitfield *"input ", const unsigned int "start ", const unsigned int "end "); 9 | .fi 10 | .SH DESCRIPTION 11 | A bit array is represented by a "struct bitfield". It has two elements: an array of unsigned long integers \fIfield\fR and an unsigned integer \fIsize\fR. 12 | .sp 13 | The \fBbfsub()\fR function takes a pointer to bitfield \fIinput\fR, the number of the \fIstart\fR bit and \fIend\fR bit, and creates a new bitfield that contains bits between \fIstart\fR (included) bit and \fIend\fR(not included) bit from the \fIinput\fR bitfield. The function returns a pointer to the new bitfield or NULL on failure. 14 | .sp 15 | .SH "SEE ALSO" 16 | For the full list of bitfield functions and their descriptions, see manual page for 17 | .BR bitfield (3). 18 | .SH AUTHOR 19 | Vitalie CIUBOTARU 20 | 21 | -------------------------------------------------------------------------------- /man/bitfield.3.in: -------------------------------------------------------------------------------- 1 | .TH bitfield 3 "%RELEASE_DATE%" "bitfield %VERSION%" "Bit array manipulation library" 2 | .SH NAME 3 | bitfield \- API for manipulating bit arrays. 4 | .SH DESCRIPTION 5 | A library of functions for creating, modifying and destroying bit arrays, i.e. 6 | series of zeroes and ones spread across an array of storage units. 7 | .SH DATA TYPES 8 | .B struct bitfield 9 | is a data structure for storing bit arrays. It has two elements: an array of 10 | unsigned long integers \fIfield\fR for storing the values of bits and an 11 | unsigned integer \fIsize\fR for storing the number of bits in the array. 12 | .SH EXPORTS 13 | For details of each function, refer to its manual page. 14 | .LP 15 | .LP 16 | Creating and deleting bit arrays: 17 | .LP 18 | .B 19 | bfnew() 20 | .br 21 | .RS 22 | creates an empty bitfield structure, and returns a pointer to it 23 | .RE 24 | .LP 25 | .B 26 | bfnew_ones() 27 | .br 28 | .RS 29 | creates a bitfield structure, sets all its bits and returns a pointer to it 30 | .RE 31 | .LP 32 | .B 33 | bfnew_quick() 34 | .br 35 | .RS 36 | creates a bitfield structure and returns a pointer to it 37 | .RE 38 | .LP 39 | .B 40 | bfdel() 41 | .br 42 | .RS 43 | destroys a bitfield structure and frees memory 44 | .RE 45 | .LP 46 | Operations with bit arrays: 47 | .LP 48 | .B 49 | bfresize() 50 | .br 51 | .RS 52 | resizes an existing bitfield 53 | .RE 54 | .LP 55 | .B 56 | bfcat() 57 | .br 58 | .RS 59 | concatenates several bitfields into one 60 | .RE 61 | .LP 62 | .B 63 | bfclearall() 64 | .br 65 | .RS 66 | clears all bits in a bitfield (i.e. fills it with zeroes) 67 | .RE 68 | .LP 69 | .B 70 | bfclone() 71 | .br 72 | .RS 73 | creates a copy of an existing bitfield 74 | .RE 75 | .LP 76 | .B 77 | bfcpy() 78 | .br 79 | .RS 80 | copies the contents of a bitfield into another pre-existing bitfield 81 | .RE 82 | .LP 83 | .B 84 | bfhamming() 85 | .br 86 | .RS 87 | counts the Hamming distance between two bitfields 88 | .RE 89 | .LP 90 | .B 91 | bfisempty() 92 | .br 93 | .RS 94 | checks whether all bit of an array are unset 95 | .RE 96 | .LP 97 | .B 98 | bfnormalize() 99 | .br 100 | .RS 101 | represents a bitfield as a smallest value of a closed ring 102 | .RE 103 | .LP 104 | .B 105 | bfpopcount() 106 | .br 107 | .RS 108 | counts the set bits in a bitfield 109 | .RE 110 | .LP 111 | .B 112 | bfpos() 113 | .br 114 | .RS 115 | checks whether an array of bits contains a sub-array 116 | .RE 117 | .LP 118 | .B 119 | bfrev() 120 | .br 121 | .RS 122 | reverses the order of bits in a bitfield and returns result in new bitfield 123 | .RE 124 | .LP 125 | .B 126 | bfrev_ip() 127 | .br 128 | .RS 129 | reverses the order of bits in an existing bitfield 130 | .RE 131 | .LP 132 | .B 133 | bfsetall() 134 | .br 135 | .RS 136 | sets all bits in a bitfield (i.e. fills it with ones) 137 | .RE 138 | .LP 139 | .B 140 | bfshift() 141 | .br 142 | .RS 143 | circular-shifts the contents of a bitfield and returns the result in new 144 | bitfield 145 | .RE 146 | .LP 147 | .B 148 | bfshift_ip() 149 | .br 150 | .RS 151 | circular-shifts the contents of an existing bitfield 152 | .RE 153 | .LP 154 | .B 155 | bfsub() 156 | .br 157 | .RS 158 | extracts a sub-bitfield in a new bitfield 159 | .RE 160 | .LP 161 | Operations with individual bits: 162 | .LP 163 | .B 164 | bfgetbit() 165 | .br 166 | .RS 167 | checks the state of a bit in a bitfield 168 | .RE 169 | .LP 170 | .B 171 | bfsetbit() 172 | .br 173 | .RS 174 | sets one bit in a bitfield 175 | .RE 176 | .LP 177 | .B 178 | bfclearbit() 179 | .br 180 | .RS 181 | clears one bit in a bitfield 182 | .RE 183 | .LP 184 | .B 185 | bftogglebit() 186 | .br 187 | .RS 188 | toggles (i.e. reverses the state of) a bit in a bitfield 189 | .RE 190 | .LP 191 | Printing bit arrays: 192 | .LP 193 | .B 194 | bfprint_lsb() 195 | .br 196 | .RS 197 | prints a bitfield as a series of ones and zeroes, left to right, the least 198 | significant bit first 199 | .RE 200 | .LP 201 | .B 202 | bfprint_msb() 203 | .br 204 | .RS 205 | prints a bitfield as a series of ones and zeroes, left to right, the most 206 | significant bit first 207 | .RE 208 | .LP 209 | Logical operations with bit arrays: 210 | .LP 211 | .B 212 | bfand() 213 | .br 214 | .RS 215 | performs bitwise AND over a pair of bitfields 216 | .RE 217 | .LP 218 | .B 219 | bfnot() 220 | .br 221 | .RS 222 | reverses all bits in a bitfield and return the result in a new bitfield 223 | .RE 224 | .LP 225 | .B 226 | bfnot_ip() 227 | .br 228 | .RS 229 | reverses all bits in an existing bitfield 230 | .RE 231 | .LP 232 | .B 233 | bfor() 234 | .br 235 | .RS 236 | performs bitwise inclusive OR over a pair of bitfields 237 | .RE 238 | .LP 239 | .B 240 | bfxor() 241 | .br 242 | .RS 243 | performs bitwise exclusive OR over a pair of bitfields 244 | .RE 245 | .LP 246 | .B 247 | bfcmp() 248 | .br 249 | .RS 250 | compares two bitfields and returns 0 if same or non-zero and error 251 | message if different 252 | .RE 253 | .LP 254 | Bitfield-to-something converters: 255 | .LP 256 | .B 257 | bf2char() 258 | .br 259 | .RS 260 | converts a bitfield structure into an array of unsigned chars 261 | .RE 262 | .LP 263 | .B 264 | bf2str() 265 | .br 266 | .RS 267 | converts into a character string of '1's and '0's 268 | .RE 269 | .LP 270 | .B 271 | bf2short() 272 | .br 273 | .RS 274 | converts into an array of short integers 275 | .RE 276 | .LP 277 | .B 278 | bf2int() 279 | .br 280 | .RS 281 | converts into an array of integers 282 | .RE 283 | .LP 284 | .B 285 | bf2long() 286 | .br 287 | .RS 288 | converts into an array of long integers 289 | .RE 290 | .LP 291 | .B 292 | bf2ll() 293 | .br 294 | .RS 295 | converts into an array of long long integers 296 | .RE 297 | .LP 298 | .B 299 | bftouint8() 300 | .br 301 | .RS 302 | converts into an array of uint8_t 303 | .RE 304 | .LP 305 | .B 306 | bftouint16() 307 | .br 308 | .RS 309 | converts into an array of uint16_t 310 | .RE 311 | .LP 312 | .B 313 | bftouint32() 314 | .br 315 | .RS 316 | converts into an array of uint32_t 317 | .RE 318 | .LP 319 | .B 320 | bftouint64() 321 | .br 322 | .RS 323 | converts into an array of uin64_t 324 | .RE 325 | .LP 326 | "In-place" bitfield-to-something converters are same as above, except that 327 | instead of creating a new array, these functions fill an existing one: 328 | .LP 329 | .B 330 | bf2char_ip() 331 | .RE 332 | .LP 333 | .B 334 | bf2str_ip() 335 | .RE 336 | .LP 337 | .B 338 | bf2short_ip() 339 | .RE 340 | .LP 341 | .B 342 | bf2int_ip() 343 | .RE 344 | .LP 345 | .B 346 | bf2long_ip() 347 | .RE 348 | .LP 349 | .B 350 | bf2ll_ip() 351 | .RE 352 | .LP 353 | .B 354 | bftouint8_ip() 355 | .RE 356 | .LP 357 | .B 358 | bftouint16_ip() 359 | .RE 360 | .LP 361 | .B 362 | bftouint32_ip() 363 | .RE 364 | .LP 365 | .B 366 | bftouint64_ip() 367 | .RE 368 | .LP 369 | Something-to-bitfield converters: 370 | .LP 371 | .B 372 | char2bf() 373 | .br 374 | .RS 375 | converts an array of unsigned chars into a bitfield structure 376 | .RE 377 | .LP 378 | .B 379 | str2bf() 380 | .br 381 | .RS 382 | converts a character string of '1's and '0's 383 | .RE 384 | .LP 385 | .B 386 | short2bf() 387 | .br 388 | .RS 389 | converts an array of short integers 390 | .RE 391 | .LP 392 | .B 393 | int2bf() 394 | .br 395 | .RS 396 | converts an array of integers 397 | .RE 398 | .LP 399 | .B 400 | long2bf() 401 | .br 402 | .RS 403 | converts an array of long integers 404 | .RE 405 | .LP 406 | .B 407 | ll2bf() 408 | .br 409 | .RS 410 | converts an array of long long integers 411 | .RE 412 | .LP 413 | .B 414 | uint8tobf() 415 | .br 416 | .RS 417 | converts an array of uint8_t 418 | .RE 419 | .LP 420 | .B 421 | uint16tobf() 422 | .br 423 | .RS 424 | converts an array of uint16_t 425 | .RE 426 | .LP 427 | .B 428 | uint32tobf() 429 | .br 430 | .RS 431 | converts an array of uint32_t 432 | .RE 433 | .LP 434 | .B 435 | uint64tobf() 436 | .br 437 | .RS 438 | converts an array of uint64_t 439 | .RE 440 | .LP 441 | "In-place" something-to-bitfield converters are same as above, except that 442 | instead of creating a new bitfield, these functions fill an existing one: 443 | .LP 444 | .B 445 | char2bf_ip() 446 | .RE 447 | .LP 448 | .B 449 | str2bf_ip() 450 | .RE 451 | .LP 452 | .B 453 | short2bf_ip() 454 | .RE 455 | .LP 456 | .B 457 | int2bf_ip() 458 | .RE 459 | .LP 460 | .B 461 | long2bf_ip() 462 | .RE 463 | .LP 464 | .B 465 | ll2bf_ip() 466 | .RE 467 | .LP 468 | .B 469 | uint8tobf_ip() 470 | .RE 471 | .LP 472 | .B 473 | uint16tobf_ip() 474 | .RE 475 | .LP 476 | .B 477 | uint32tobf_ip() 478 | .RE 479 | .LP 480 | .B 481 | uint64tobf_ip() 482 | .RE 483 | .LP 484 | Miscellaneous: 485 | .LP 486 | .B 487 | bfsize() 488 | .br 489 | .RS 490 | .LP 491 | obtains the number of bits of a bitfield 492 | .RE 493 | .SH AUTHOR 494 | Vitalie CIUBOTARU 495 | -------------------------------------------------------------------------------- /tests/Makefile.in: -------------------------------------------------------------------------------- 1 | TESTFILES=test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 test11 test12 test13 test14 test15 test16 test17 test18 test19 test20 test21 test22 test23 test24 test25 test26 test27 test28 test29 test30 test31 test32 test33 test34 test35 test36 test37 test38 test39 test40 test41 test42 test43 test44 2 | LIBS=-L.. -I.. 3 | 4 | all: build-tests run-tests 5 | 6 | build-tests: $(TESTFILES) 7 | $(TESTFILES): %: %.c 8 | $(CC) $(CFLAGS) -Wl,-rpath=$(CURDIR) -o $@ $< $(LIBS) -lbitfield 9 | 10 | run-tests: $(TESTFILES) 11 | for i in $(TESTFILES); do \ 12 | env LD_LIBRARY_PATH=.. ./$$i; \ 13 | done 14 | 15 | clean: 16 | rm -rvf $(TESTFILES) 17 | 18 | distclean: 19 | rm Makefile 20 | 21 | .PHONY: all build-tests run-tests clean distclean 22 | -------------------------------------------------------------------------------- /tests/test1.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test1.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 1, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bfsub(), bfcat(), bfshift() and bfshift_ip() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | unsigned int i; //counter 23 | unsigned int len = 80; 24 | char *msg = "Testing bfsub(), bfcat(), bfshift() and bfshift_ip()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | struct bitfield *input = bfnew(len); 32 | for (i = 0; i < len; i++) 33 | if (rand() % 2) 34 | BITSET(input, i); 35 | unsigned int point, cmp; 36 | /* try all possible points of division */ 37 | for (point = 1; point < (len - 1); point++) { 38 | /* divide a bitfield into two subfields */ 39 | struct bitfield *chunk1 = bfsub(input, 0, point); 40 | struct bitfield *chunk2 = bfsub(input, point, input->size); 41 | /* concatenate them back into one and compare to the original */ 42 | struct bitfield *output = bfcat(chunk1, chunk2); 43 | cmp = bfcmp(input, output, NULL); 44 | bfdel(output); 45 | if (cmp != 0) { 46 | printf("%s\n", failed); 47 | return 1; 48 | } 49 | /* shift a bitfield using bfshift() */ 50 | struct bitfield *shifted = bfshift(input, point); 51 | /* shift a bitfield by swapping chunks */ 52 | struct bitfield *swapped = bfcat(chunk2, chunk1); 53 | bfdel(chunk1); 54 | bfdel(chunk2); 55 | cmp = bfcmp(shifted, swapped, NULL); 56 | bfdel(swapped); 57 | if (cmp != 0) { 58 | printf("%s\n", failed); 59 | return 1; 60 | } 61 | /* shift it back and compare to the original bitfield */ 62 | bfshift_ip(shifted, -point); 63 | cmp = bfcmp(input, shifted, NULL); 64 | bfdel(shifted); 65 | if (cmp != 0) { 66 | printf("%s\n", failed); 67 | bfdel(input); 68 | return 1; 69 | } 70 | } 71 | printf("%s\n", passed); 72 | bfdel(input); 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /tests/test10.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test10.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: October 20, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include "bitfield.h" 14 | #include "bitfield-internals.h" 15 | 16 | /* Testing bfsize() */ 17 | 18 | int main() 19 | { 20 | unsigned int i; //counter 21 | unsigned int len = 80; 22 | char *msg = "Testing bfsize()"; 23 | char *failed = "[FAIL]"; 24 | char *passed = "[PASS]"; 25 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 26 | printf("%s", msg); 27 | for (i = 0; i < dots; i++) 28 | printf("."); 29 | 30 | for (i = 1; i <= len; i++) { 31 | struct bitfield *input = bfnew_quick(i); 32 | if (bfsize(input) != i) { 33 | printf("%s\n", failed); 34 | bfdel(input); 35 | return 1; 36 | } else 37 | bfdel(input); 38 | } 39 | 40 | printf("%s\n", passed); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /tests/test11.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test11.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: October 20, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bftogglebit() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | int i; //counter 23 | int len = 80; 24 | char *msg = "Testing bftogglebit()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | 32 | struct bitfield *input = bfnew_quick(len); 33 | for (i = 0; i < len; i++) { 34 | if (rand() % 2 == 1) 35 | bfsetbit(input, i); 36 | else 37 | bfclearbit(input, i); 38 | } 39 | 40 | struct bitfield *output = bfclone(input); 41 | for (i = 0; i < len; i++) 42 | bftogglebit(output, i); 43 | 44 | struct bitfield *not = bfnot(output); 45 | bfdel(output); 46 | 47 | int cmp = bfcmp(input, not, NULL); 48 | bfdel(input); 49 | bfdel(not); 50 | 51 | if (cmp != 0) { 52 | printf("%s\n", failed); 53 | return 1; 54 | } 55 | printf("%s\n", passed); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /tests/test12.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test12.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: October 20, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bfpopcount() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | int i; //counter 23 | int len = 80; 24 | char *msg = "Testing bfpopcount()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | 32 | char *input_char = malloc((len + 1) * sizeof(char)); /* allocate space for 80+1 chars */ 33 | for (i = 0; i < len; i++) { 34 | if (rand() % 2) 35 | input_char[i] = '1'; 36 | else 37 | input_char[i] = '0'; 38 | } 39 | input_char[len] = '\0'; 40 | 41 | /* population count in char string */ 42 | int count_s = 0; 43 | for (i = 0; i < len; i++) 44 | if (input_char[i] == '1') 45 | count_s++; 46 | 47 | struct bitfield *input = str2bf(input_char); 48 | free(input_char); 49 | /* population count in bitfield */ 50 | int count_b = bfpopcount(input); 51 | bfdel(input); 52 | 53 | if (count_s != count_b) { 54 | printf("%s\n", failed); 55 | return 1; 56 | } 57 | printf("%s\n", passed); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /tests/test13.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test13.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: October 20, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bfhamming() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | int i; //counter 23 | int len = 80; 24 | char *msg = "Testing bfhamming()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | 32 | char *input_char1 = malloc((len + 1) * sizeof(char)); /* allocate space for 80+1 chars */ 33 | for (i = 0; i < len; i++) { 34 | if (rand() % 2) { 35 | input_char1[i] = '1'; 36 | } else 37 | input_char1[i] = '0'; 38 | } 39 | input_char1[len] = '\0'; 40 | 41 | char *input_char2 = malloc((len + 1) * sizeof(char)); /* allocate space for 80+1 chars */ 42 | for (i = 0; i < len; i++) { 43 | if (rand() % 2) { 44 | input_char2[i] = '1'; 45 | } else 46 | input_char2[i] = '0'; 47 | } 48 | input_char2[len] = '\0'; 49 | 50 | /* Hamming distance counter for char string */ 51 | int hamming_s = 0; 52 | for (i = 0; i < len; i++) { 53 | if (input_char1[i] != input_char2[i]) 54 | hamming_s++; 55 | } 56 | 57 | struct bitfield *input1 = str2bf(input_char1); 58 | free(input_char1); 59 | struct bitfield *input2 = str2bf(input_char2); 60 | free(input_char2); 61 | 62 | /* Hamming distance for bitfield */ 63 | int hamming_b = bfhamming(input1, input2); 64 | bfdel(input1); 65 | bfdel(input2); 66 | 67 | if (hamming_s != hamming_b) { 68 | printf("%s\n", failed); 69 | return 1; 70 | } 71 | printf("%s\n", passed); 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /tests/test14.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test14.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: December 1, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bf2str() and str2bf() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | int i, cmp; //counter 23 | int len = 80; 24 | char *msg = "Testing bf2str() and str2bf()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | struct bitfield *input = bfnew(len); 32 | for (i = 0; i < len; i++) 33 | if (rand() % 2) 34 | BITSET(input, i); 35 | char *input_char = bf2str(input); 36 | struct bitfield *output = str2bf(input_char); 37 | free(input_char); 38 | cmp = bfcmp(input, output, NULL); 39 | bfdel(input); 40 | bfdel(output); 41 | if (cmp != 0) { 42 | printf("%s\n", failed); 43 | return 1; 44 | } 45 | printf("%s\n", passed); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /tests/test15.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test15.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: November 15, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bf2long() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | int i, cmp; //counter 23 | int len = 80; 24 | char *msg = "Testing bf2long()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | struct bitfield *input = bfnew(len); 32 | for (i = 0; i < len; i++) 33 | if (rand() % 2) 34 | BITSET(input, i); 35 | unsigned long *input_long = bf2long(input); 36 | int bitnslots = BITNSLOTS(len); 37 | unsigned long *input_long2 = malloc(bitnslots * sizeof(unsigned long)); 38 | for (i = 0; i < bitnslots; i++) 39 | input_long2[i] = input->field[i]; 40 | cmp = 41 | memcmp(input_long, input_long2, bitnslots * sizeof(unsigned long)); 42 | bfdel(input); 43 | free(input_long); 44 | free(input_long2); 45 | if (cmp) { 46 | printf("%s\n", failed); 47 | return 1; 48 | } 49 | printf("%s\n", passed); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /tests/test16.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test16.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: November 15, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bfisempty() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | int i, j; //counters 23 | int len = 80; 24 | int content; 25 | char *msg = "Testing bfisempty()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | 33 | /* 50 trials */ 34 | for (i = 0; i < 50; i++) { 35 | /* create a bitfield */ 36 | struct bitfield *input = bfnew(len); 37 | 38 | /* choose whether to leave it empty or fill with random ones */ 39 | if (rand() % 2) { 40 | /* fill with ones */ 41 | content = 1; 42 | for (j = 0; j < len; j++) { 43 | if (rand() % 2) { 44 | BITSET(input, j); 45 | } 46 | } 47 | /* make sure there's at least one unit */ 48 | BITSET(input, rand() % len); 49 | } 50 | /* leave empty */ 51 | else 52 | content = 0; 53 | /* check if result of bfisempty corresponds with the value of content */ 54 | int result = bfisempty(input); 55 | if (result != content) { 56 | printf("%s\n", failed); 57 | bfdel(input); 58 | return 1; 59 | } 60 | bfdel(input); 61 | } 62 | printf("%s\n", passed); 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /tests/test17.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test17.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: February 15, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bfpos() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | int i, j, k; //counters 23 | int r; 24 | int len = 80; 25 | int result; 26 | char *msg = "Testing bfpos()"; 27 | char *failed = "[FAIL]"; 28 | char *passed = "[PASS]"; 29 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 30 | printf("%s", msg); 31 | for (i = 0; i < dots; i++) 32 | printf("."); 33 | 34 | /** 35 | * set haystack size to 80 36 | * test different sizes of needle, from 1 to 80 37 | * test different positions of needle, from 0 to where it fits 38 | * all haystacks here contain exactly one instanse of needle, 39 | * so search should return non-negative-values 40 | **/ 41 | for (i = 1; i < len; i++) { // needle size 42 | for (j = 0; j < len - i; j++) { // needle position in the haystack 43 | /* create the needle */ 44 | struct bitfield *needle = bfnew(i); 45 | 46 | /* fill the needle with random values */ 47 | for (k = 0; k < i; k++) { 48 | if (rand() % 2) { 49 | BITSET(needle, k); 50 | } 51 | } 52 | 53 | /* make sure the needle contains at least one set bit, selected randomly */ 54 | r = rand() % i; 55 | BITSET(needle, r); 56 | 57 | /* create the haystack, make sure it contains the needle */ 58 | struct bitfield *haystack = bfclone(needle); 59 | 60 | /* if there is space between the start of the haystack and the sought needle, fill it with zeroes */ 61 | if (j > 0) { 62 | struct bitfield *start = bfnew(j); 63 | struct bitfield *tmp = bfcat(start, haystack); 64 | bfresize(haystack, bfsize(tmp)); 65 | bfcpy(tmp, haystack); 66 | bfdel(start); 67 | bfdel(tmp); 68 | } 69 | 70 | /* if there is space between the sought needle and the end of haystack, fill it with zeroes */ 71 | if (j + i < len) { 72 | struct bitfield *end = bfnew(len - j - i); 73 | struct bitfield *tmp2 = bfcat(haystack, end); 74 | bfresize(haystack, bfsize(tmp2)); 75 | bfcpy(tmp2, haystack); 76 | bfdel(end); 77 | bfdel(tmp2); 78 | } 79 | 80 | /* now let's test */ 81 | result = bfpos(haystack, needle); 82 | bfdel(haystack); 83 | bfdel(needle); 84 | /* the result should be equal to j */ 85 | if (result != j) { 86 | printf("%s\n", failed); 87 | return 1; 88 | } 89 | } 90 | } 91 | printf("%s\n", passed); 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /tests/test18.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test18.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: March 20, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing long2bf() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | unsigned int i, j; //counters 23 | unsigned int len = 80; 24 | char *msg = "Testing long2bf()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | unsigned int bitnslots = BITNSLOTS(len); 32 | unsigned long *input = calloc(1, bitnslots * sizeof(unsigned long)); 33 | for (i = 0; i < bitnslots - 1; i++) { 34 | for (j = 0; j < LONG_BIT; j++) { 35 | if (rand() % 2) 36 | input[i] |= (1UL << j); 37 | } 38 | } 39 | for (i = 0; i < len % LONG_BIT; i++) 40 | if (rand() % 2) 41 | input[bitnslots - 1] |= (1UL << i); 42 | struct bitfield *output = long2bf(input, len); 43 | for (i = 0; i < bitnslots; i++) { 44 | if (output->field[i] != input[i]) { 45 | printf("%s\n", failed); 46 | free(input); 47 | bfdel(output); 48 | return 1; 49 | } 50 | } 51 | printf("%s\n", passed); 52 | free(input); 53 | bfdel(output); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /tests/test19.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test19.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: April 1, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing int2bf() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | unsigned int i, j; //counters 23 | unsigned int len = 80; 24 | char *msg = "Testing int2bf()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | unsigned int bitnslots = (len - 1) / INT_BIT + 1; 32 | unsigned int *input = calloc(1, bitnslots * sizeof(unsigned int)); 33 | for (i = 0; i < bitnslots - 1; i++) { 34 | for (j = 0; j < INT_BIT; j++) { 35 | if (rand() % 2) 36 | input[i] |= (1U << j); 37 | } 38 | } 39 | for (i = 0; i < len % INT_BIT; i++) 40 | if (rand() % 2) 41 | input[bitnslots - 1] |= (1U << i); 42 | struct bitfield *output = int2bf(input, len); 43 | int min_memory_length = 44 | (bitnslots * sizeof(unsigned int) < 45 | BITNSLOTS(len) * sizeof(unsigned long)) ? (bitnslots * 46 | sizeof(unsigned int)) : 47 | BITNSLOTS(len) * sizeof(unsigned long); 48 | if (memcmp(input, output->field, min_memory_length) != 0) { 49 | printf("%s\n", failed); 50 | free(input); 51 | bfdel(output); 52 | return 1; 53 | } 54 | printf("%s\n", passed); 55 | free(input); 56 | bfdel(output); 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /tests/test2.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test2.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 1, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bf2str_ip() and str2bf_ip() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | int i, cmp; //counter 23 | int len = 80; 24 | char *msg = "Testing bf2str_ip() and str2bf_ip()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | struct bitfield *input = bfnew(len); 32 | struct bitfield *output = bfnew(len); 33 | char *input_char = malloc((len + 1) * sizeof(char)); 34 | for (i = 0; i < len; i++) 35 | if (rand() % 2) 36 | BITSET(input, i); 37 | bf2str_ip(input, input_char); 38 | str2bf_ip(input_char, output); 39 | free(input_char); 40 | cmp = bfcmp(input, output, NULL); 41 | bfdel(input); 42 | bfdel(output); 43 | if (cmp != 0) { 44 | printf("%s\n", failed); 45 | return 1; 46 | } 47 | printf("%s\n", passed); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /tests/test20.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test20.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: April 1, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bf2int() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | int i, cmp; //counter 24 | int len = 80; 25 | char *msg = "Testing bf2int()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | struct bitfield *input = bfnew(len); 33 | for (i = 0; i < len; i++) 34 | if (rand() % 2) 35 | BITSET(input, i); 36 | int bitnslots = BITNSLOTS(len); 37 | int ints = (len - 1) / INT_BIT + 1; 38 | unsigned int *output = bf2int(input); 39 | unsigned int *output2 = malloc(bitnslots * sizeof(unsigned long)); 40 | #if __BYTE_ORDER == __BIG_ENDIAN 41 | for (i = 0; i < bitnslots; i++) { 42 | switch (sizeof(unsigned long)) { 43 | case 4: 44 | input->field[i] = htole32(input->field[i]); 45 | break; 46 | case 8: 47 | input->field[i] = htole64(input->field[i]); 48 | break; 49 | } 50 | } 51 | #endif 52 | memcpy(output2, input->field, bitnslots * sizeof(unsigned long)); 53 | #if __BYTE_ORDER == __BIG_ENDIAN 54 | for (i = 0; i < ints; i++) { 55 | switch (sizeof(unsigned int)) { 56 | case 2: 57 | output2[i] = le16toh(output2[i]); 58 | break; 59 | case 4: 60 | output2[i] = le32toh(output2[i]); 61 | break; 62 | } 63 | } 64 | #endif 65 | if (ints * sizeof(unsigned int) != bitnslots * sizeof(unsigned long)) { 66 | output2 = 67 | (unsigned int *)realloc(output2, 68 | ints * sizeof(unsigned int)); 69 | if (output2 == NULL) 70 | free(output2); 71 | } 72 | cmp = memcmp(output, output2, ints * sizeof(unsigned int)); 73 | bfdel(input); 74 | free(output); 75 | free(output2); 76 | if (cmp != 0) { 77 | printf("%s\n", failed); 78 | return 1; 79 | } 80 | printf("%s\n", passed); 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /tests/test21.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test21.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: July 1, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "bitfield.h" 16 | #include "bitfield-internals.h" 17 | 18 | /* Testing bfnormalize() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | int i, cmp; //counter 24 | int len = 80; 25 | char *msg = "Testing bfnormalize()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | 33 | /** 34 | * test contents: 35 | * take a string of ones, insert a substring of zeroes of random length 36 | * and see if bfnormalize will align zeroes at the end 37 | **/ 38 | 39 | /* number of zeroes between 1 and len - 1 */ 40 | int nr_zeroes, nr_ones_head, nr_ones_tail; 41 | struct bitfield *zeroes, *ones_head, *ones_tail, *input, *check, 42 | *output; 43 | 44 | for (i = 0; i < 50; i++) { 45 | nr_zeroes = rand() % (len - 2) + 1; 46 | zeroes = bfnew(nr_zeroes); 47 | nr_ones_head = rand() % (len - nr_zeroes - 1) + 1; 48 | ones_head = bfnew_ones(nr_ones_head); 49 | nr_ones_tail = len - nr_zeroes - nr_ones_head; 50 | ones_tail = bfnew_ones(nr_ones_tail); 51 | 52 | input = bfcat(ones_head, zeroes, ones_tail); 53 | check = bfcat(ones_head, ones_tail, zeroes); 54 | bfdel(zeroes); 55 | bfdel(ones_head); 56 | bfdel(ones_tail); 57 | output = bfnormalize(input); 58 | cmp = bfcmp(output, check, NULL); 59 | bfdel(input); 60 | bfdel(check); 61 | bfdel(output); 62 | if (cmp) { 63 | printf("%s\n", failed); 64 | return 1; 65 | } 66 | } 67 | printf("%s\n", passed); 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /tests/test22.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test22.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: April 1, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bf2long_ip() and long2bf_ip() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | int i, cmp; //counter 23 | int len = 80; 24 | char *msg = "Testing bf2long_ip() and long2bf_ip()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | struct bitfield *input = bfnew(len); 32 | for (i = 0; i < len; i++) 33 | if (rand() % 2) 34 | BITSET(input, i); 35 | int bitnslots = BITNSLOTS(len); 36 | unsigned long *input_long = calloc(bitnslots, sizeof(unsigned long)); 37 | bf2long_ip(input, input_long); 38 | /* check first function */ 39 | for (i = 0; i < bitnslots; i++) { 40 | if (input_long[i] != input->field[i]) { 41 | printf("%s\n", failed); 42 | return 1; 43 | } 44 | } 45 | struct bitfield *output = bfnew(len); 46 | long2bf_ip(input_long, output); 47 | free(input_long); 48 | /* check second function */ 49 | cmp = bfcmp(input, output, NULL); 50 | bfdel(input); 51 | bfdel(output); 52 | if (cmp != 0) { 53 | printf("%s\n", failed); 54 | return 1; 55 | } 56 | printf("%s\n", passed); 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /tests/test23.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test23.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: April 1, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bf2int_ip() and int2bf_ip() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | int i, cmp; //counter 23 | int len = 80; 24 | char *msg = "Testing bf2int_ip() and int2bf_ip()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | struct bitfield *input = bfnew(len); 32 | for (i = 0; i < len; i++) 33 | if (rand() % 2) 34 | BITSET(input, i); 35 | int bitnslots = (len - 1) / INT_BIT + 1; 36 | unsigned int *input_int = calloc(bitnslots, sizeof(unsigned int)); 37 | bf2int_ip(input, input_int); 38 | /* check first function */ 39 | int min_memory_length = 40 | (bitnslots * sizeof(unsigned int) < 41 | BITNSLOTS(len) * sizeof(unsigned long)) ? (bitnslots * 42 | sizeof(unsigned int)) : 43 | BITNSLOTS(len) * sizeof(unsigned long); 44 | if (memcmp(input_int, input->field, min_memory_length) != 0) { 45 | printf("%s\n", failed); 46 | return 1; 47 | } 48 | struct bitfield *output = bfnew(len); 49 | int2bf_ip(input_int, output); 50 | free(input_int); 51 | /* check second function */ 52 | cmp = bfcmp(input, output, NULL); 53 | bfdel(input); 54 | bfdel(output); 55 | if (cmp != 0) { 56 | printf("%s\n", failed); 57 | return 1; 58 | } 59 | printf("%s\n", passed); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /tests/test24.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test24.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: July 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing char2bf() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, j, cmp; //counters 24 | unsigned int len = 80; 25 | char *msg = "Testing char2bf()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | unsigned int chars = (len - 1) / CHAR_BIT + 1; 33 | unsigned char *input = calloc(1, chars); 34 | for (i = 0; i < chars - 1; i++) { 35 | for (j = 0; j < CHAR_BIT; j++) { 36 | if (rand() % 2) 37 | input[i] |= (1U << j); 38 | } 39 | } 40 | for (i = 0; i < (len - 1) % CHAR_BIT + 1; i++) 41 | if (rand() % 2) 42 | input[chars - 1] |= (1U << i); 43 | struct bitfield *output = char2bf(input, len); 44 | struct bitfield *output2 = bfnew(len); 45 | memcpy(output2->field, input, chars); 46 | for (i = 0; i < BITNSLOTS(len); i++) { 47 | switch (sizeof(unsigned long)) { 48 | case 4: 49 | output2->field[i] = 50 | le32toh((uint32_t) output2->field[i]); 51 | break; 52 | case 8: 53 | output2->field[i] = 54 | le64toh((uint64_t) output2->field[i]); 55 | break; 56 | } 57 | } 58 | cmp = 59 | memcmp(output->field, output2->field, 60 | BITNSLOTS(len) * sizeof(unsigned long)); 61 | free(input); 62 | bfdel(output); 63 | bfdel(output2); 64 | if (cmp != 0) { 65 | printf("%s\n", failed); 66 | return 1; 67 | } 68 | printf("%s\n", passed); 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /tests/test25.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test25.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: April 1, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bf2char() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, cmp; //counter 24 | unsigned int len = 80; 25 | char *msg = "Testing bf2char()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | struct bitfield *input = bfnew(len); 33 | for (i = 0; i < len; i++) 34 | if (rand() % 2) 35 | BITSET(input, i); 36 | unsigned int bitnslots = (len - 1) / CHAR_BIT + 1; 37 | unsigned char *input_char = bf2char(input); 38 | unsigned int min_memory_length = 39 | (bitnslots * sizeof(unsigned char) < 40 | BITNSLOTS(len) * sizeof(unsigned long)) ? (bitnslots * 41 | sizeof(unsigned char)) : 42 | BITNSLOTS(len) * sizeof(unsigned long); 43 | for (i = 0; i < BITNSLOTS(len); i++) { 44 | switch (sizeof(unsigned long)) { 45 | case 4: 46 | input->field[i] = 47 | (unsigned long)htole32((uint32_t) input->field[i]); 48 | break; 49 | case 8: 50 | input->field[i] = 51 | (unsigned long)htole64((uint64_t) input->field[i]); 52 | break; 53 | } 54 | } 55 | cmp = memcmp(input_char, input->field, min_memory_length); 56 | bfdel(input); 57 | free(input_char); 58 | if (cmp != 0) { 59 | printf("%s\n", failed); 60 | return 1; 61 | } 62 | printf("%s\n", passed); 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /tests/test26.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test26.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: July 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bf2char_ip() and char2bf_ip() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, cmp; //counter 24 | unsigned int len = 80; 25 | char *msg = "Testing bf2char_ip() and char2bf_ip()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | struct bitfield *input = bfnew(len); 33 | for (i = 0; i < len; i++) 34 | if (rand() % 2) 35 | BITSET(input, i); 36 | unsigned int chars = (len - 1) / CHAR_BIT + 1; 37 | unsigned char *input_char = malloc(chars * sizeof(unsigned char)); 38 | bf2char_ip(input, input_char); 39 | /* check first function */ 40 | struct bitfield *check = bfclone(input); 41 | for (i = 0; i < BITNSLOTS(len); i++) { 42 | switch (sizeof(unsigned long)) { 43 | case 4: 44 | check->field[i] = 45 | (unsigned long)htole32((uint32_t) check->field[i]); 46 | break; 47 | case 8: 48 | check->field[i] = 49 | (unsigned long)htole64((uint64_t) check->field[i]); 50 | break; 51 | } 52 | } 53 | unsigned char *check_char = malloc(chars * sizeof(unsigned char)); 54 | memcpy(check_char, check->field, chars); 55 | bfdel(check); 56 | cmp = memcmp(input_char, check_char, chars); 57 | free(check_char); 58 | if (cmp != 0) { 59 | printf("%s\n", failed); 60 | return 1; 61 | } 62 | struct bitfield *output = bfnew(len); 63 | char2bf_ip(input_char, output); 64 | free(input_char); 65 | /* check second function */ 66 | cmp = bfcmp(input, output, NULL); 67 | bfdel(input); 68 | bfdel(output); 69 | if (cmp != 0) { 70 | printf("%s\n", failed); 71 | return 1; 72 | } 73 | printf("%s\n", passed); 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /tests/test27.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test27.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 1, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing short2bf() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, j, cmp; //counters 24 | unsigned int len = 80; 25 | char *msg = "Testing short2bf()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | unsigned int shorts = (len - 1) / SHORT_BIT + 1; 33 | unsigned short *input = calloc(1, shorts * sizeof(unsigned short)); 34 | for (i = 0; i < shorts - 1; i++) { 35 | for (j = 0; j < SHORT_BIT; j++) { 36 | if (rand() % 2) 37 | input[i] |= (1U << j); 38 | } 39 | } 40 | for (i = 0; i < (len - 1) % SHORT_BIT + 1; i++) 41 | if (rand() % 2) 42 | input[shorts - 1] |= (1U << i); 43 | struct bitfield *output = short2bf(input, len); 44 | unsigned short *input2 = calloc(1, shorts * sizeof(unsigned short)); 45 | for (i = 0; i < shorts; i++) { 46 | switch (sizeof(unsigned short)) { 47 | case 2: 48 | input2[i] = htole16((uint16_t) input[i]); 49 | break; 50 | case 4: 51 | input2[i] = htole32((uint32_t) input[i]); 52 | break; 53 | } 54 | } 55 | struct bitfield *output2 = bfnew(len); 56 | memcpy(output2->field, input2, shorts * sizeof(unsigned short)); 57 | for (i = 0; i < BITNSLOTS(len); i++) { 58 | switch (sizeof(unsigned long)) { 59 | case 4: 60 | output2->field[i] = 61 | le32toh((uint32_t) output2->field[i]); 62 | break; 63 | case 8: 64 | output2->field[i] = 65 | le64toh((uint64_t) output2->field[i]); 66 | break; 67 | } 68 | } 69 | cmp = 70 | memcmp(output->field, output2->field, 71 | BITNSLOTS(len) * sizeof(unsigned long)); 72 | free(input); 73 | bfdel(output); 74 | free(input2); 75 | bfdel(output2); 76 | if (cmp != 0) { 77 | printf("%s\n", failed); 78 | return 1; 79 | } 80 | printf("%s\n", passed); 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /tests/test28.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test28.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 1, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bf2short() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, cmp; //counter 24 | unsigned int len = 80; 25 | char *msg = "Testing bf2short()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | unsigned int shorts = (len - 1) / SHORT_BIT + 1; 33 | struct bitfield *input = bfnew(len); 34 | for (i = 0; i < len; i++) 35 | if (rand() % 2) 36 | BITSET(input, i); 37 | unsigned short *input_short = bf2short(input); 38 | for (i = 0; i < BITNSLOTS(len); i++) { 39 | switch (sizeof(unsigned long)) { 40 | case 4: 41 | input->field[i] = 42 | (unsigned long)htole32((uint32_t) input->field[i]); 43 | break; 44 | case 8: 45 | input->field[i] = 46 | (unsigned long)htole64((uint64_t) input->field[i]); 47 | break; 48 | } 49 | } 50 | // long_htole_ip(input, BITNSLOTS(len)); 51 | unsigned short *check = malloc(shorts * sizeof(unsigned short)); 52 | memcpy(check, input->field, shorts * sizeof(unsigned short)); 53 | bfdel(input); 54 | for (i = 0; i < shorts; i++) { 55 | switch (sizeof(unsigned short)) { 56 | case 2: 57 | check[i] = (unsigned short)le16toh((uint16_t) check[i]); 58 | break; 59 | case 4: 60 | check[i] = (unsigned short)le32toh((uint32_t) check[i]); 61 | break; 62 | } 63 | } 64 | cmp = memcmp(input_short, check, shorts * sizeof(unsigned short)); 65 | free(input_short); 66 | free(check); 67 | if (cmp != 0) { 68 | printf("%s\n", failed); 69 | return 1; 70 | } 71 | printf("%s\n", passed); 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /tests/test29.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test29.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 1, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bf2short_ip() and short2bf_ip() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, cmp; //counter 24 | unsigned int len = 80; 25 | char *msg = "Testing bf2short_ip() and short2bf_ip()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | struct bitfield *input = bfnew(len); 33 | for (i = 0; i < len; i++) 34 | if (rand() % 2) 35 | BITSET(input, i); 36 | unsigned int shorts = (len - 1) / SHORT_BIT + 1; 37 | unsigned short *input_short = malloc(shorts * sizeof(unsigned short)); 38 | bf2short_ip(input, input_short); 39 | /* check first function */ 40 | struct bitfield *check = bfclone(input); 41 | for (i = 0; i < BITNSLOTS(len); i++) { 42 | switch (sizeof(unsigned long)) { 43 | case 4: 44 | check->field[i] = 45 | (unsigned long)htole32((uint32_t) check->field[i]); 46 | break; 47 | case 8: 48 | check->field[i] = 49 | (unsigned long)htole64((uint64_t) check->field[i]); 50 | break; 51 | } 52 | } 53 | unsigned short *check_short = 54 | calloc(1, shorts * sizeof(unsigned short)); 55 | memcpy(check_short, check->field, shorts * sizeof(unsigned short)); 56 | bfdel(check); 57 | for (i = 0; i < shorts; i++) { 58 | switch (sizeof(unsigned short)) { 59 | case 2: 60 | check_short[i] = 61 | (unsigned short)le16toh((uint16_t) check_short[i]); 62 | break; 63 | case 4: 64 | check_short[i] = 65 | (unsigned short)le32toh((uint32_t) check_short[i]); 66 | break; 67 | } 68 | } 69 | cmp = memcmp(input_short, check_short, shorts * sizeof(unsigned short)); 70 | free(check_short); 71 | if (cmp != 0) { 72 | printf("%s\n", failed); 73 | free(input_short); 74 | bfdel(input); 75 | return 1; 76 | } 77 | struct bitfield *output = bfnew(len); 78 | short2bf_ip(input_short, output); 79 | free(input_short); 80 | /* check second function */ 81 | cmp = bfcmp(input, output, NULL); 82 | bfdel(input); 83 | bfdel(output); 84 | if (cmp != 0) { 85 | printf("%s\n", failed); 86 | return 1; 87 | } 88 | printf("%s\n", passed); 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /tests/test3.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test3.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 1, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bfand(), bfnot(), bfor() and bfxor() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | int i, cmp; //counter 23 | int len = 80; 24 | char *msg = "Testing bfand(), bfnot(), bfor() and bfxor()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | struct bitfield *input1 = bfnew(len); 32 | struct bitfield *input2 = bfnew(len); 33 | for (i = 0; i < len; i++) { 34 | if (rand() % 2) 35 | BITSET(input1, i); 36 | if (rand() % 2) 37 | BITSET(input2, i); 38 | } 39 | 40 | struct bitfield *output1 = bfxor(input1, input2); 41 | struct bitfield *and = bfand(input1, input2); 42 | struct bitfield *or = bfor(input1, input2); 43 | bfdel(input1); 44 | bfdel(input2); 45 | struct bitfield *not = bfnot(and); 46 | bfdel(and); 47 | struct bitfield *output2 = bfand(or, not); 48 | bfdel(or); 49 | bfdel(not); 50 | cmp = bfcmp(output1, output2, NULL); 51 | bfdel(output1); 52 | bfdel(output2); 53 | if (cmp != 0) { 54 | printf("%s\n", failed); 55 | return 1; 56 | } 57 | printf("%s\n", passed); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /tests/test30.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test30.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing uint8tobf() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, j, cmp; //counters 24 | unsigned int len = 80; 25 | char *msg = "Testing uint8tobf()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | unsigned int chars = (len - 1) / 8 + 1; 33 | uint8_t *input = calloc(1, chars); 34 | for (i = 0; i < chars - 1; i++) { 35 | for (j = 0; j < 8; j++) { 36 | if (rand() % 2) 37 | input[i] |= (1U << j); 38 | } 39 | } 40 | for (i = 0; i < len % 8; i++) 41 | if (rand() % 2) 42 | input[chars - 1] |= (1U << i); 43 | struct bitfield *output = uint8tobf(input, len); 44 | for (i = 0; i < BITNSLOTS(len); i++) { 45 | switch (sizeof(unsigned long)) { 46 | case 4: 47 | output->field[i] = 48 | (unsigned long)htole32((uint32_t) output->field[i]); 49 | break; 50 | case 8: 51 | output->field[i] = 52 | (unsigned long)htole64((uint64_t) output->field[i]); 53 | break; 54 | } 55 | } 56 | cmp = memcmp(input, output->field, chars); 57 | free(input); 58 | bfdel(output); 59 | if (cmp != 0) { 60 | printf("%s\n", failed); 61 | return 1; 62 | } 63 | printf("%s\n", passed); 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /tests/test31.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test31.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bftouint8() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, cmp; //counter 24 | unsigned int len = 80; 25 | char *msg = "Testing bftouint8()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | struct bitfield *input = bfnew(len); 33 | for (i = 0; i < len; i++) 34 | if (rand() % 2) 35 | BITSET(input, i); 36 | unsigned int chars = (len - 1) / 8 + 1; 37 | uint8_t *input_int = bftouint8(input); 38 | for (i = 0; i < BITNSLOTS(len); i++) { 39 | switch (sizeof(unsigned long)) { 40 | case 4: 41 | input->field[i] = 42 | (unsigned long)htole32((uint32_t) input->field[i]); 43 | break; 44 | case 8: 45 | input->field[i] = 46 | (unsigned long)htole64((uint64_t) input->field[i]); 47 | break; 48 | } 49 | } 50 | cmp = memcmp(input_int, input->field, chars); 51 | free(input_int); 52 | bfdel(input); 53 | if (cmp != 0) { 54 | printf("%s\n", failed); 55 | return 1; 56 | } 57 | printf("%s\n", passed); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /tests/test32.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test32.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing uint16tobf() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, j, cmp; //counters 24 | unsigned int len = 80; 25 | char *msg = "Testing uint16tobf()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | unsigned int shorts = (len - 1) / 16 + 1; 33 | uint16_t *input = calloc(1, shorts * sizeof(uint16_t)); 34 | for (i = 0; i < shorts - 1; i++) { 35 | for (j = 0; j < 16; j++) { 36 | if (rand() % 2) 37 | input[i] |= (1U << j); 38 | } 39 | } 40 | for (i = 0; i < len % 16; i++) 41 | if (rand() % 2) 42 | input[shorts - 1] |= (1U << i); 43 | struct bitfield *output = uint16tobf(input, len); 44 | for (i = 0; i < shorts; i++) { 45 | switch (sizeof(unsigned short)) { 46 | case 2: 47 | input[i] = (unsigned short)htole16((uint16_t) input[i]); 48 | break; 49 | case 4: 50 | input[i] = (unsigned short)htole32((uint32_t) input[i]); 51 | break; 52 | } 53 | } 54 | for (i = 0; i < BITNSLOTS(len); i++) { 55 | switch (sizeof(unsigned long)) { 56 | case 4: 57 | output->field[i] = 58 | (unsigned long)htole32((uint32_t) output->field[i]); 59 | break; 60 | case 8: 61 | output->field[i] = 62 | (unsigned long)htole64((uint64_t) output->field[i]); 63 | break; 64 | } 65 | } 66 | cmp = memcmp(input, output->field, shorts * sizeof(unsigned short)); 67 | free(input); 68 | bfdel(output); 69 | if (cmp != 0) { 70 | printf("%s\n", failed); 71 | return 1; 72 | } 73 | printf("%s\n", passed); 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /tests/test33.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test33.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bftouint16() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, cmp; //counter 24 | unsigned int len = 80; 25 | char *msg = "Testing bftouint16()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | struct bitfield *input = bfnew(len); 33 | for (i = 0; i < len; i++) 34 | if (rand() % 2) 35 | BITSET(input, i); 36 | unsigned int uint16s = (len - 1) / 16 + 1; 37 | uint16_t *output = bftouint16(input); 38 | for (i = 0; i < BITNSLOTS(len); i++) { 39 | switch (sizeof(unsigned long)) { 40 | case 4: 41 | input->field[i] = 42 | (unsigned long)htole32((uint32_t) input->field[i]); 43 | break; 44 | case 8: 45 | input->field[i] = 46 | (unsigned long)htole64((uint64_t) input->field[i]); 47 | break; 48 | } 49 | } 50 | for (i = 0; i < uint16s; i++) 51 | output[i] = htole16(output[i]); 52 | cmp = memcmp(output, input->field, uint16s * sizeof(uint16_t)); 53 | bfdel(input); 54 | free(output); 55 | if (cmp != 0) { 56 | printf("%s\n", failed); 57 | return 1; 58 | } 59 | printf("%s\n", passed); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /tests/test34.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test34.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing uint32tobf() */ 19 | 20 | int main() 21 | { 22 | uint32_t unit = 1; 23 | srand((unsigned)time(NULL)); 24 | unsigned int i, j, cmp; //counters 25 | unsigned int len = 80; 26 | char *msg = "Testing uint32tobf()"; 27 | char *failed = "[FAIL]"; 28 | char *passed = "[PASS]"; 29 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 30 | printf("%s", msg); 31 | for (i = 0; i < dots; i++) 32 | printf("."); 33 | unsigned int int32s = (len - 1) / 32 + 1; 34 | uint32_t *input = calloc(1, int32s * sizeof(uint32_t)); 35 | for (i = 0; i < int32s - 1; i++) { 36 | for (j = 0; j < 32; j++) { 37 | if (rand() % 2) 38 | input[i] |= (unit << j); 39 | } 40 | } 41 | for (i = 0; i < len % 32; i++) 42 | if (rand() % 2) 43 | input[int32s - 1] |= (unit << i); 44 | struct bitfield *output = uint32tobf(input, len); 45 | /* 46 | int min_memory_length = 47 | (bitnslots * sizeof(uint32_t) < 48 | BITNSLOTS(len) * sizeof(unsigned long)) ? (bitnslots * 49 | sizeof(uint32_t)) : 50 | BITNSLOTS(len) * sizeof(unsigned long); 51 | */ 52 | // uint32_t *input2 = uint32_htole(input, bitnslots); 53 | for (i = 0; i < int32s; i++) 54 | input[i] = htole32(input[i]); 55 | // free(input); 56 | for (i = 0; i < BITNSLOTS(len); i++) { 57 | switch (sizeof(unsigned long)) { 58 | case 4: 59 | output->field[i] = 60 | (unsigned long)htole32((uint32_t) output->field[i]); 61 | break; 62 | case 8: 63 | output->field[i] = 64 | (unsigned long)htole64((uint64_t) output->field[i]); 65 | break; 66 | } 67 | } 68 | cmp = memcmp(input, output->field, int32s * sizeof(uint32_t)); 69 | free(input); 70 | bfdel(output); 71 | if (cmp != 0) { 72 | printf("%s\n", failed); 73 | return 1; 74 | } 75 | printf("%s\n", passed); 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /tests/test35.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test35.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bftouint32() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, cmp; //counter 24 | unsigned int len = 80; 25 | char *msg = "Testing bftouint32()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | struct bitfield *input = bfnew(len); 33 | for (i = 0; i < len; i++) 34 | if (rand() % 2) 35 | BITSET(input, i); 36 | unsigned int int32s = (len - 1) / 32 + 1; 37 | uint32_t *input_int = bftouint32(input); 38 | /* 39 | int min_memory_length = 40 | (bitnslots * sizeof(uint32_t) < 41 | BITNSLOTS(len) * sizeof(unsigned long)) ? (bitnslots * 42 | sizeof(uint32_t)) : 43 | BITNSLOTS(len) * sizeof(unsigned long); 44 | */ 45 | for (i = 0; i < BITNSLOTS(len); i++) { 46 | switch (sizeof(unsigned long)) { 47 | case 4: 48 | input->field[i] = 49 | (unsigned long)htole32((uint32_t) input->field[i]); 50 | break; 51 | case 8: 52 | input->field[i] = 53 | (unsigned long)htole64((uint64_t) input->field[i]); 54 | break; 55 | } 56 | } 57 | for (i = 0; i < int32s; i++) 58 | input_int[i] = htole32(input_int[i]); 59 | cmp = memcmp(input_int, input->field, (len - 1) / CHAR_BIT + 1); 60 | bfdel(input); 61 | free(input_int); 62 | if (cmp != 0) { 63 | printf("%s\n", failed); 64 | return 1; 65 | } 66 | printf("%s\n", passed); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /tests/test36.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test36.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing uint64tobf() */ 19 | 20 | int main() 21 | { 22 | uint64_t unit = 1; 23 | srand((unsigned)time(NULL)); 24 | unsigned int i, j, cmp; //counters 25 | unsigned int len = 80; 26 | char *msg = "Testing uint64tobf()"; 27 | char *failed = "[FAIL]"; 28 | char *passed = "[PASS]"; 29 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 30 | printf("%s", msg); 31 | for (i = 0; i < dots; i++) 32 | printf("."); 33 | unsigned int int64s = (len - 1) / 64 + 1; 34 | uint64_t *input = calloc(1, int64s * sizeof(uint64_t)); 35 | for (i = 0; i < int64s - 1; i++) { 36 | for (j = 0; j < 64; j++) { 37 | if (rand() % 2) 38 | input[i] |= (unit << j); 39 | } 40 | } 41 | for (i = 0; i < len % 64; i++) 42 | if (rand() % 2) 43 | input[int64s - 1] |= (unit << i); 44 | struct bitfield *output = uint64tobf(input, len); 45 | for (i = 0; i < int64s; i++) 46 | input[i] = htole64(input[i]); 47 | for (i = 0; i < BITNSLOTS(len); i++) { 48 | switch (sizeof(unsigned long)) { 49 | case 8: 50 | output->field[i] = 51 | (unsigned long)htole64((uint64_t) output->field[i]); 52 | break; 53 | case 4: 54 | output->field[i] = 55 | (unsigned long)htole32((uint32_t) output->field[i]); 56 | break; 57 | } 58 | } 59 | cmp = memcmp(input, output->field, (len - 1) / CHAR_BIT + 1); 60 | bfdel(output); 61 | free(input); 62 | if (cmp != 0) { 63 | printf("%s\n", failed); 64 | return 1; 65 | } 66 | printf("%s\n", passed); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /tests/test37.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test35.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bftouint64() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, cmp; //counter 24 | unsigned int len = 80; 25 | char *msg = "Testing bftouint64()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | struct bitfield *input = bfnew(len); 33 | for (i = 0; i < len; i++) 34 | if (rand() % 2) 35 | BITSET(input, i); 36 | unsigned int int64s = (len - 1) / 64 + 1; 37 | uint64_t *input_int = bftouint64(input); 38 | for (i = 0; i < int64s; i++) 39 | input_int[i] = htole64(input_int[i]); 40 | for (i = 0; i < BITNSLOTS(len); i++) { 41 | switch (sizeof(unsigned long)) { 42 | case 8: 43 | input->field[i] = 44 | (unsigned long)htole64((uint64_t) input->field[i]); 45 | break; 46 | case 4: 47 | input->field[i] = 48 | (unsigned long)htole32((uint32_t) input->field[i]); 49 | break; 50 | } 51 | } 52 | cmp = memcmp(input_int, input->field, (len - 1) / CHAR_BIT + 1); 53 | free(input_int); 54 | bfdel(input); 55 | if (cmp != 0) { 56 | printf("%s\n", failed); 57 | return 1; 58 | } 59 | printf("%s\n", passed); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /tests/test38.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test38.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bftouint8_ip() and uint8tobf_ip() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, cmp; //counter 24 | unsigned int len = 80; 25 | char *msg = "Testing bftouint8_ip() and uint8tobf_ip()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | struct bitfield *input = bfnew(len); 33 | for (i = 0; i < len; i++) 34 | if (rand() % 2) 35 | BITSET(input, i); 36 | unsigned int int8s = (len - 1) / 8 + 1; 37 | uint8_t *input_uint8 = malloc(int8s); 38 | bftouint8_ip(input, input_uint8); 39 | /* check first function */ 40 | struct bitfield *output = bfnew(len); 41 | for (i = 0; i < BITNSLOTS(len); i++) { 42 | switch (sizeof(unsigned long)) { 43 | case 8: 44 | output->field[i] = 45 | (unsigned long)htole64((uint64_t) input->field[i]); 46 | break; 47 | case 4: 48 | output->field[i] = 49 | (unsigned long)htole32((uint32_t) input->field[i]); 50 | break; 51 | } 52 | } 53 | cmp = memcmp(input_uint8, output->field, int8s); 54 | if (cmp != 0) { 55 | printf("%s\n", failed); 56 | free(input_uint8); 57 | bfdel(input); 58 | return 1; 59 | } 60 | uint8tobf_ip(input_uint8, output); 61 | free(input_uint8); 62 | /* check second function */ 63 | cmp = bfcmp(input, output, NULL); 64 | bfdel(input); 65 | bfdel(output); 66 | if (cmp != 0) { 67 | printf("%s\n", failed); 68 | return 1; 69 | } 70 | printf("%s\n", passed); 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /tests/test39.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test39.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bftouint16_ip() and uint16tobf_ip() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, cmp; //counter 24 | unsigned int len = 80; 25 | char *msg = "Testing bftouint16_ip() and uint16tobf_ip()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | struct bitfield *input = bfnew(len); 33 | for (i = 0; i < len; i++) 34 | if (rand() % 2) 35 | BITSET(input, i); 36 | unsigned int int16s = (len - 1) / 16 + 1; 37 | uint16_t *input_uint16 = malloc(int16s * sizeof(uint16_t)); 38 | bftouint16_ip(input, input_uint16); 39 | /* check first function */ 40 | uint16_t *check_uint16 = malloc(int16s * sizeof(uint16_t)); 41 | for (i = 0; i < int16s; i++) 42 | check_uint16[i] = htole16(input_uint16[i]); 43 | struct bitfield *check = bfclone(input); 44 | for (i = 0; i < BITNSLOTS(len); i++) { 45 | switch (sizeof(unsigned long)) { 46 | case 8: 47 | check->field[i] = 48 | (unsigned long)htole64((uint64_t) input->field[i]); 49 | break; 50 | case 4: 51 | check->field[i] = 52 | (unsigned long)htole32((uint32_t) input->field[i]); 53 | break; 54 | } 55 | } 56 | cmp = memcmp(check_uint16, check->field, (len - 1) / CHAR_BIT + 1); 57 | free(check_uint16); 58 | bfdel(check); 59 | if (cmp != 0) { 60 | bfdel(input); 61 | free(input_uint16); 62 | printf("%s\n", failed); 63 | return 1; 64 | } 65 | 66 | struct bitfield *output = bfnew(len); 67 | uint16tobf_ip(input_uint16, output); 68 | free(input_uint16); 69 | /* check second function */ 70 | cmp = bfcmp(input, output, NULL); 71 | bfdel(input); 72 | bfdel(output); 73 | if (cmp != 0) { 74 | printf("%s\n", failed); 75 | return 1; 76 | } 77 | printf("%s\n", passed); 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /tests/test4.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test4.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 20, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include "bitfield.h" 14 | #include "bitfield-internals.h" 15 | 16 | /* Testing bfnew_ones(), bfsetall() and bfclearall() */ 17 | 18 | int main() 19 | { 20 | int i, cmp; //counter 21 | int len = 80; 22 | char *msg = "Testing bfnew_ones(), bfsetall() and bfclearall()"; 23 | char *failed = "[FAIL]"; 24 | char *passed = "[PASS]"; 25 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 26 | printf("%s", msg); 27 | for (i = 0; i < dots; i++) 28 | printf("."); 29 | char *input_char = "11111111111111111111111111111111111111111111111111111111111111111111111111111111"; /* 80 units */ 30 | struct bitfield *output1 = str2bf(input_char); 31 | 32 | struct bitfield *output2 = bfnew_ones(len); 33 | cmp = bfcmp(output1, output2, NULL); 34 | bfdel(output2); 35 | if (cmp != 0) { 36 | printf("%s\n", failed); 37 | return 1; 38 | } 39 | 40 | struct bitfield *output3 = bfnew_quick(len); 41 | bfsetall(output3); 42 | cmp = bfcmp(output1, output3, NULL); 43 | bfdel(output1); 44 | if (cmp != 0) { 45 | printf("%s\n", failed); 46 | return 1; 47 | } 48 | 49 | struct bitfield *output4 = bfnew(len); 50 | bfclearall(output3); 51 | cmp = bfcmp(output3, output4, NULL); 52 | bfdel(output3); 53 | bfdel(output4); 54 | if (cmp != 0) { 55 | printf("%s\n", failed); 56 | return 1; 57 | } 58 | 59 | printf("%s\n", passed); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /tests/test40.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test40.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2032 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bftouint32_ip() and uint32tobf_ip() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, cmp; //counter 24 | unsigned int len = 80; 25 | char *msg = "Testing bftouint32_ip() and uint32tobf_ip()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | struct bitfield *input = bfnew(len); 33 | for (i = 0; i < len; i++) 34 | if (rand() % 2) 35 | BITSET(input, i); 36 | unsigned int int32s = (len - 1) / 32 + 1; 37 | uint32_t *input_uint32 = malloc(int32s * sizeof(uint32_t)); 38 | bftouint32_ip(input, input_uint32); 39 | /* check first function */ 40 | struct bitfield *check = bfclone(input); 41 | uint32_t *check_uint32 = malloc(int32s * sizeof(uint32_t)); 42 | for (i = 0; i < BITNSLOTS(len); i++) { 43 | switch (sizeof(unsigned long)) { 44 | case 8: 45 | check->field[i] = 46 | (unsigned long)htole64((uint64_t) input->field[i]); 47 | break; 48 | case 4: 49 | check->field[i] = 50 | (unsigned long)htole32((uint32_t) input->field[i]); 51 | break; 52 | } 53 | } 54 | for (i = 0; i < int32s; i++) 55 | check_uint32[i] = htole32(input_uint32[i]); 56 | cmp = memcmp(check_uint32, check->field, (len - 1) / CHAR_BIT + 1); 57 | bfdel(check); 58 | free(check_uint32); 59 | if (cmp != 0) { 60 | free(input_uint32); 61 | bfdel(input); 62 | printf("%s\n", failed); 63 | return 1; 64 | } 65 | 66 | struct bitfield *output = bfnew(len); 67 | uint32tobf_ip(input_uint32, output); 68 | free(input_uint32); 69 | /* check second function */ 70 | cmp = bfcmp(input, output, NULL); 71 | bfdel(input); 72 | bfdel(output); 73 | if (cmp != 0) { 74 | printf("%s\n", failed); 75 | return 1; 76 | } 77 | printf("%s\n", passed); 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /tests/test41.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test40.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2064 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bftouint64_ip() and uint64tobf_ip() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, cmp; //counter 24 | unsigned int len = 80; 25 | char *msg = "Testing bftouint64_ip() and uint64tobf_ip()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | struct bitfield *input = bfnew(len); 33 | for (i = 0; i < len; i++) 34 | if (rand() % 2) 35 | BITSET(input, i); 36 | unsigned int int64s = (len - 1) / 64 + 1; 37 | uint64_t *input_uint64 = malloc(int64s * sizeof(uint64_t)); 38 | bftouint64_ip(input, input_uint64); 39 | /* check first function */ 40 | struct bitfield *check = bfclone(input); 41 | uint64_t *check_uint64 = malloc(int64s * sizeof(uint64_t)); 42 | for (i = 0; i < BITNSLOTS(len); i++) { 43 | switch (sizeof(unsigned long)) { 44 | case 8: 45 | check->field[i] = 46 | (unsigned long)htole64((uint64_t) input->field[i]); 47 | break; 48 | case 4: 49 | check->field[i] = 50 | (unsigned long)htole32((uint32_t) input->field[i]); 51 | break; 52 | } 53 | } 54 | for (i = 0; i < int64s; i++) 55 | check_uint64[i] = htole64(input_uint64[i]); 56 | cmp = memcmp(check_uint64, check->field, (len - 1) / CHAR_BIT + 1); 57 | bfdel(check); 58 | free(check_uint64); 59 | if (cmp != 0) { 60 | free(input_uint64); 61 | bfdel(input); 62 | printf("%s\n", failed); 63 | return 1; 64 | } 65 | struct bitfield *output = bfnew(len); 66 | uint64tobf_ip(input_uint64, output); 67 | free(input_uint64); 68 | /* check second function */ 69 | cmp = bfcmp(input, output, NULL); 70 | bfdel(input); 71 | bfdel(output); 72 | if (cmp != 0) { 73 | printf("%s\n", failed); 74 | return 1; 75 | } 76 | printf("%s\n", passed); 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /tests/test42.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test42.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing ll2bf() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, j, cmp; //counters 24 | unsigned int len = 80; 25 | char *msg = "Testing ll2bf()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | unsigned int lls = (len - 1) / LONG_LONG_BIT + 1; 33 | unsigned long long *input = calloc(1, lls * sizeof(unsigned long long)); 34 | for (i = 0; i < lls - 1; i++) { 35 | for (j = 0; j < LONG_LONG_BIT; j++) { 36 | if (rand() % 2) 37 | input[i] |= (1ULL << j); 38 | } 39 | } 40 | for (i = 0; i < len % LONG_LONG_BIT; i++) 41 | if (rand() % 2) 42 | input[lls - 1] |= (1ULL << i); 43 | struct bitfield *output = ll2bf(input, len); 44 | /* assume long long is always 64 bit */ 45 | for (i = 0; i < lls; i++) 46 | input[i] = (unsigned long long)htole64((uint64_t) input[i]); 47 | for (i = 0; i < BITNSLOTS(len); i++) { 48 | switch (sizeof(unsigned long)) { 49 | case 8: 50 | output->field[i] = 51 | (unsigned long)htole64((uint64_t) output->field[i]); 52 | break; 53 | case 4: 54 | output->field[i] = 55 | (unsigned long)htole32((uint32_t) output->field[i]); 56 | break; 57 | } 58 | } 59 | cmp = memcmp(input, output->field, (len - 1) / CHAR_BIT + 1); 60 | free(input); 61 | bfdel(output); 62 | if (cmp != 0) { 63 | printf("%s\n", failed); 64 | return 1; 65 | } 66 | printf("%s\n", passed); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /tests/test43.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test43.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bf2ll() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, cmp; //counter 24 | unsigned int len = 80; 25 | char *msg = "Testing bf2ll()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | struct bitfield *input = bfnew(len); 33 | for (i = 0; i < len; i++) 34 | if (rand() % 2) 35 | BITSET(input, i); 36 | unsigned int lls = (len - 1) / LONG_LONG_BIT + 1; 37 | unsigned long long *output = bf2ll(input); 38 | for (i = 0; i < BITNSLOTS(len); i++) { 39 | switch (sizeof(unsigned long)) { 40 | case 8: 41 | input->field[i] = 42 | (unsigned long)htole64((uint64_t) input->field[i]); 43 | break; 44 | case 4: 45 | input->field[i] = 46 | (unsigned long)htole32((uint32_t) input->field[i]); 47 | break; 48 | } 49 | } 50 | for (i = 0; i < lls; i++) 51 | output[i] = (unsigned long long)htole64((uint64_t) output[i]); 52 | cmp = memcmp(input->field, output, (len - 1) / CHAR_BIT + 1); 53 | free(output); 54 | bfdel(input); 55 | if (cmp != 0) { 56 | printf("%s\n", failed); 57 | return 1; 58 | } 59 | printf("%s\n", passed); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /tests/test44.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test44.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: September 10, 2016 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | #include 17 | 18 | /* Testing bf2ll_ip() and ll2bf_ip() */ 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | unsigned int i, cmp; //counter 24 | unsigned int len = 80; 25 | char *msg = "Testing bf2ll_ip() and ll2bf_ip()"; 26 | char *failed = "[FAIL]"; 27 | char *passed = "[PASS]"; 28 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 29 | printf("%s", msg); 30 | for (i = 0; i < dots; i++) 31 | printf("."); 32 | struct bitfield *input = bfnew(len); 33 | for (i = 0; i < len; i++) 34 | if (rand() % 2) 35 | BITSET(input, i); 36 | unsigned int lls = (len - 1) / LONG_LONG_BIT + 1; 37 | unsigned long long *input_ll = malloc(lls * sizeof(unsigned long long)); 38 | bf2ll_ip(input, input_ll); 39 | /* check first function */ 40 | struct bitfield *check = bfclone(input); 41 | for (i = 0; i < BITNSLOTS(len); i++) { 42 | switch (sizeof(unsigned long)) { 43 | case 8: 44 | check->field[i] = 45 | (unsigned long)htole64((uint64_t) input->field[i]); 46 | break; 47 | case 4: 48 | check->field[i] = 49 | (unsigned long)htole32((uint32_t) input->field[i]); 50 | break; 51 | } 52 | } 53 | unsigned long long *check_ll = malloc(lls * sizeof(unsigned long long)); 54 | for (i = 0; i < lls; i++) 55 | check_ll[i] = 56 | (unsigned long long)htole64((uint64_t) input_ll[i]); 57 | cmp = memcmp(check->field, check_ll, (len - 1) / CHAR_BIT + 1); 58 | bfdel(check); 59 | free(check_ll); 60 | if (cmp != 0) { 61 | bfdel(input); 62 | free(input_ll); 63 | printf("%s\n", failed); 64 | return 1; 65 | } 66 | struct bitfield *output = bfnew(len); 67 | ll2bf_ip(input_ll, output); 68 | free(input_ll); 69 | /* check second function */ 70 | cmp = bfcmp(input, output, NULL); 71 | bfdel(input); 72 | bfdel(output); 73 | if (cmp != 0) { 74 | printf("%s\n", failed); 75 | return 1; 76 | } 77 | printf("%s\n", passed); 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /tests/test5.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test5.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: October 1, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bfresize() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | unsigned int i; //counter 23 | unsigned int len = 80; 24 | char *msg = "Testing bfresize()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | char *input_char = malloc((len + 1) * sizeof(char)); /* allocate space for 80+1 chars */ 32 | for (i = 0; i < len; i++) { 33 | if (rand() % 2) 34 | input_char[i] = '1'; 35 | else 36 | input_char[i] = '0'; 37 | } 38 | input_char[len] = '\0'; 39 | 40 | /* create a small bitfield */ 41 | struct bitfield *input = bfnew_quick(1); 42 | if (input_char[0] == '1') 43 | BITSET(input, 0); 44 | else 45 | BITCLEAR(input, 0); 46 | 47 | /* enlarge the bit field one bit at a time */ 48 | for (i = 1; i < len; i++) { 49 | bfresize(input, i + 1); 50 | if (input->size != (i + 1)) { 51 | printf("%s\n", failed); 52 | free(input_char); 53 | return 1; 54 | } 55 | /* set the newly added bit from input_char */ 56 | if (input_char[i] == '1') 57 | BITSET(input, i); 58 | else 59 | BITCLEAR(input, i); 60 | } 61 | free(input_char); 62 | 63 | /* shrink it back one bit at a time */ 64 | for (i = len; i > 1; i--) { 65 | bfresize(input, i - 1); 66 | if (input->size != (i - 1)) { 67 | printf("%s\n", failed); 68 | return 1; 69 | } 70 | } 71 | printf("%s\n", passed); 72 | bfdel(input); 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /tests/test6.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test6.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: October 1, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include "bitfield.h" 14 | #include "bitfield-internals.h" 15 | 16 | /* Testing bfresize() and bfclearbit() */ 17 | 18 | int main() 19 | { 20 | int i; //counter 21 | int len = 80; 22 | char *msg = "Testing bfsetbit() and bfclearbit()"; 23 | char *failed = "[FAIL]"; 24 | char *passed = "[PASS]"; 25 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 26 | printf("%s", msg); 27 | for (i = 0; i < dots; i++) 28 | printf("."); 29 | struct bitfield *input = bfnew_quick(len); 30 | for (i = 0; i < len; i++) { 31 | bfsetbit(input, i); 32 | if (BITGET(input, i) != 1) { 33 | printf("%s\n", failed); 34 | return 1; 35 | } 36 | } 37 | 38 | for (i = 0; i < len; i++) { 39 | bfclearbit(input, i); 40 | if (BITGET(input, i) != 0) { 41 | printf("%s\n", failed); 42 | bfdel(input); 43 | return 1; 44 | } 45 | } 46 | 47 | printf("%s\n", passed); 48 | bfdel(input); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /tests/test7.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test7.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: October 1, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bfclone() and bfcpy() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | int i, cmp; //counter 23 | int len = 80; 24 | char *msg = "Testing bfclone() and bfcpy()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | char *input_char = malloc((len + 1) * sizeof(char)); /* allocate space for 80+1 chars */ 32 | for (i = 0; i < len; i++) { 33 | if (rand() % 2) 34 | input_char[i] = '1'; 35 | else 36 | input_char[i] = '0'; 37 | } 38 | input_char[len] = '\0'; 39 | struct bitfield *input = str2bf(input_char); 40 | free(input_char); 41 | 42 | struct bitfield *input2 = bfclone(input); 43 | 44 | cmp = bfcmp(input, input2, NULL); 45 | bfdel(input2); 46 | if (cmp != 0) { 47 | printf("zyu1"); 48 | printf("%s\n", failed); 49 | return 1; 50 | } 51 | 52 | struct bitfield *input3 = bfnew_quick(len); 53 | int result = bfcpy(input, input3); 54 | if (result != 0) { 55 | printf("zyu2"); 56 | printf("%s\n", failed); 57 | return 1; 58 | } 59 | 60 | cmp = bfcmp(input, input3, NULL); 61 | bfdel(input); 62 | bfdel(input3); 63 | if (cmp != 0) { 64 | printf("zyu3"); 65 | printf("%s\n", failed); 66 | return 1; 67 | } 68 | 69 | printf("%s\n", passed); 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /tests/test8.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test8.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: October 1, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bfrev() and bfrev_ip */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | int i; //counter 23 | int len = 80; 24 | char *msg = "Testing bfrev() and bfrev_ip()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | char *input_char = malloc((len + 1) * sizeof(char)); /* allocate space for 80+1 chars */ 32 | for (i = 0; i < len; i++) { 33 | if (rand() % 2) 34 | input_char[i] = '1'; 35 | else 36 | input_char[i] = '0'; 37 | } 38 | input_char[len] = '\0'; 39 | char *input_char_rev = malloc((len + 1) * sizeof(char)); 40 | for (i = 0; i < len; i++) { 41 | if (input_char[i] == '1') 42 | input_char_rev[len - i - 1] = '1'; 43 | else 44 | input_char_rev[len - i - 1] = '0'; 45 | } 46 | input_char_rev[len] = '\0'; 47 | 48 | struct bitfield *input1 = str2bf(input_char); 49 | 50 | struct bitfield *input2 = bfrev(input1); 51 | bfdel(input1); 52 | 53 | for (i = 0; i < len; i++) { 54 | if (BITGET(input2, i) != input_char_rev[i] - '0') { 55 | printf("%s\n", failed); 56 | free(input_char); 57 | free(input_char_rev); 58 | return 1; 59 | } 60 | } 61 | 62 | bfrev_ip(input2); 63 | for (i = 0; i < len; i++) { 64 | if (BITGET(input2, i) != input_char[i] - '0') { 65 | printf("%s\n", failed); 66 | free(input_char); 67 | free(input_char_rev); 68 | bfdel(input2); 69 | return 1; 70 | } 71 | } 72 | 73 | printf("%s\n", passed); 74 | free(input_char); 75 | free(input_char_rev); 76 | bfdel(input2); 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /tests/test9.c: -------------------------------------------------------------------------------- 1 | /** 2 | * File name: tests/test9.c 3 | * Project name: bitfield, a bit array manipulation library written in C 4 | * URL: https://github.com/ciubotaru/bitfield 5 | * Author: Vitalie Ciubotaru 6 | * License: General Public License, version 3 or later 7 | * Date: October 1, 2015 8 | **/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "bitfield.h" 15 | #include "bitfield-internals.h" 16 | 17 | /* Testing bfgetbit() */ 18 | 19 | int main() 20 | { 21 | srand((unsigned)time(NULL)); 22 | unsigned int i; //counter 23 | unsigned int len = 80; 24 | char *msg = "Testing bfgetbit()"; 25 | char *failed = "[FAIL]"; 26 | char *passed = "[PASS]"; 27 | unsigned int dots = len - strlen(msg) - 6; /* 6 is the length of pass/fail string */ 28 | printf("%s", msg); 29 | for (i = 0; i < dots; i++) 30 | printf("."); 31 | char *input_char = malloc((len + 1) * sizeof(char)); /* allocate space for 80+1 chars */ 32 | for (i = 0; i < len; i++) { 33 | if (rand() % 2) 34 | input_char[i] = '1'; 35 | else 36 | input_char[i] = '0'; 37 | } 38 | input_char[len] = '\0'; 39 | 40 | struct bitfield *input = str2bf(input_char); 41 | 42 | for (i = 0; i < len; i++) { 43 | if (bfgetbit(input, i) != (unsigned int)input_char[i] - '0') { 44 | printf("%s\n", failed); 45 | free(input_char); 46 | bfdel(input); 47 | return 1; 48 | } 49 | } 50 | 51 | printf("%s\n", passed); 52 | free(input_char); 53 | bfdel(input); 54 | return 0; 55 | } 56 | --------------------------------------------------------------------------------