/* * This file Copyright (C) 2010-2014 Mnemosyne LLC * * It may be used under the GNU GPL versions 2 or 3 * or any future license endorsed by Mnemosyne LLC. * */ #include /* strlen () */ #include "transmission.h" #include "crypto-utils.h" #include "bitfield.h" #include "utils.h" /* tr_free */ #include "libtransmission-test.h" static int test_bitfield_count_range (void) { int i; int n; int begin; int end; int count1; int count2; const int bitCount = 100 + tr_rand_int_weak (1000); tr_bitfield bf; /* generate a random bitfield */ tr_bitfieldConstruct (&bf, bitCount); for (i=0, n=tr_rand_int_weak (bitCount); i= 21))); /* test tr_bitfieldRemRange on the boundaries */ tr_bitfieldAddRange (&field, 0, 64); tr_bitfieldRemRange (&field, 8, 24); for (i=0; i<64; i++) check (tr_bitfieldHas (&field, i) == ((i < 8) || (i >= 24))); /* test tr_bitfieldRemRange when begin & end is on the same word */ tr_bitfieldAddRange (&field, 0, 64); tr_bitfieldRemRange (&field, 4, 5); for (i=0; i<64; i++) check (tr_bitfieldHas (&field, i) == ((i < 4) || (i >= 5))); /* test tr_bitfieldAddRange */ tr_bitfieldRemRange (&field, 0, 64); tr_bitfieldAddRange (&field, 4, 21); for (i=0; i<64; i++) check (tr_bitfieldHas (&field, i) == ((4 <= i) && (i < 21))); /* test tr_bitfieldAddRange on the boundaries */ tr_bitfieldRemRange (&field, 0, 64); tr_bitfieldAddRange (&field, 8, 24); for (i=0; i<64; i++) check (tr_bitfieldHas (&field, i) == ((8 <= i) && (i < 24))); /* test tr_bitfieldAddRange when begin & end is on the same word */ tr_bitfieldRemRange (&field, 0, 64); tr_bitfieldAddRange (&field, 4, 5); for (i=0; i<64; i++) check (tr_bitfieldHas (&field, i) == ((4 <= i) && (i < 5))); tr_bitfieldDestruct (&field); return 0; } static int test_bitfield_has_all_none (void) { tr_bitfield field; tr_bitfieldConstruct (&field, 3); check (!tr_bitfieldHasAll (&field)); check (tr_bitfieldHasNone (&field)); tr_bitfieldAdd (&field, 0); check (!tr_bitfieldHasAll (&field)); check (!tr_bitfieldHasNone (&field)); tr_bitfieldRem (&field, 0); tr_bitfieldAdd (&field, 1); check (!tr_bitfieldHasAll (&field)); check (!tr_bitfieldHasNone (&field)); tr_bitfieldRem (&field, 1); tr_bitfieldAdd (&field, 2); check (!tr_bitfieldHasAll (&field)); check (!tr_bitfieldHasNone (&field)); tr_bitfieldAdd (&field, 0); tr_bitfieldAdd (&field, 1); check (tr_bitfieldHasAll (&field)); check (!tr_bitfieldHasNone (&field)); tr_bitfieldSetHasNone (&field); check (!tr_bitfieldHasAll (&field)); check (tr_bitfieldHasNone (&field)); tr_bitfieldSetHasAll (&field); check (tr_bitfieldHasAll (&field)); check (!tr_bitfieldHasNone (&field)); tr_bitfieldDestruct (&field); tr_bitfieldConstruct (&field, 0); check (!tr_bitfieldHasAll (&field)); check (!tr_bitfieldHasNone (&field)); tr_bitfieldSetHasNone (&field); check (!tr_bitfieldHasAll (&field)); check (tr_bitfieldHasNone (&field)); tr_bitfieldSetHasAll (&field); check (tr_bitfieldHasAll (&field)); check (!tr_bitfieldHasNone (&field)); tr_bitfieldDestruct (&field); return 0; } int main (void) { int l; int ret; const testFunc tests[] = { test_bitfields, test_bitfield_has_all_none }; if ((ret = runTests (tests, NUM_TESTS (tests)))) return ret; /* bitfield count range */ for (l=0; l<10000; ++l) if ((ret = test_bitfield_count_range ())) return ret; return 0; }