2014-01-19 01:09:44 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2010-2014 Mnemosyne LLC
|
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <string.h> /* strlen() */
|
2012-12-09 05:32:52 +00:00
|
|
|
#include "transmission.h"
|
2014-12-04 11:27:38 +00:00
|
|
|
#include "crypto-utils.h"
|
2012-12-09 05:32:52 +00:00
|
|
|
#include "bitfield.h"
|
|
|
|
#include "utils.h" /* tr_free */
|
|
|
|
|
|
|
|
#include "libtransmission-test.h"
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_bitfield_count_range(void)
|
2012-12-09 05:32:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int begin;
|
|
|
|
int end;
|
|
|
|
int count1;
|
|
|
|
int count2;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const bitCount = 100 + tr_rand_int_weak(1000);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfield bf;
|
|
|
|
|
|
|
|
/* generate a random bitfield */
|
|
|
|
tr_bitfieldConstruct(&bf, bitCount);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0, n = tr_rand_int_weak(bitCount); i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_bitfieldAdd(&bf, tr_rand_int_weak(bitCount));
|
|
|
|
}
|
|
|
|
|
|
|
|
begin = tr_rand_int_weak(bitCount);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
end = tr_rand_int_weak(bitCount);
|
|
|
|
}
|
|
|
|
while (end == begin);
|
|
|
|
|
|
|
|
/* ensure end <= begin */
|
|
|
|
if (end < begin)
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const tmp = begin;
|
2017-04-19 12:04:45 +00:00
|
|
|
begin = end;
|
|
|
|
end = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test the bitfield */
|
|
|
|
count1 = 0;
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = begin; i < end; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (tr_bitfieldHas(&bf, i))
|
|
|
|
{
|
|
|
|
++count1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count2 = tr_bitfieldCountRange(&bf, begin, end);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(count1, ==, count2);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_bitfieldDestruct(&bf);
|
|
|
|
return 0;
|
2012-12-09 05:32:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_bitfields(void)
|
2012-12-09 05:32:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
unsigned int bitcount = 500;
|
|
|
|
tr_bitfield field;
|
|
|
|
|
|
|
|
tr_bitfieldConstruct(&field, bitcount);
|
|
|
|
|
|
|
|
/* test tr_bitfieldAdd */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < bitcount; i++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (i % 7 == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_bitfieldAdd(&field, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < bitcount; i++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-30 17:56:12 +00:00
|
|
|
check_bool(tr_bitfieldHas(&field, i), ==, (i % 7 == 0));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* test tr_bitfieldAddRange */
|
|
|
|
tr_bitfieldAddRange(&field, 0, bitcount);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < bitcount; i++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
check(tr_bitfieldHas(&field, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test tr_bitfieldRem */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < bitcount; i++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (i % 7 != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_bitfieldRem(&field, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < bitcount; i++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-30 17:56:12 +00:00
|
|
|
check_bool(tr_bitfieldHas(&field, i), ==, (i % 7 == 0));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* test tr_bitfieldRemRange in the middle of a boundary */
|
|
|
|
tr_bitfieldAddRange(&field, 0, 64);
|
|
|
|
tr_bitfieldRemRange(&field, 4, 21);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < 64; i++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-30 17:56:12 +00:00
|
|
|
check_bool(tr_bitfieldHas(&field, i), ==, (i < 4 || i >= 21));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* test tr_bitfieldRemRange on the boundaries */
|
|
|
|
tr_bitfieldAddRange(&field, 0, 64);
|
|
|
|
tr_bitfieldRemRange(&field, 8, 24);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < 64; i++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-30 17:56:12 +00:00
|
|
|
check_bool(tr_bitfieldHas(&field, i), ==, (i < 8 || i >= 24));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* test tr_bitfieldRemRange when begin & end is on the same word */
|
|
|
|
tr_bitfieldAddRange(&field, 0, 64);
|
|
|
|
tr_bitfieldRemRange(&field, 4, 5);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < 64; i++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-30 17:56:12 +00:00
|
|
|
check_bool(tr_bitfieldHas(&field, i), ==, (i < 4 || i >= 5));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* test tr_bitfieldAddRange */
|
|
|
|
tr_bitfieldRemRange(&field, 0, 64);
|
|
|
|
tr_bitfieldAddRange(&field, 4, 21);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < 64; i++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-30 17:56:12 +00:00
|
|
|
check_bool(tr_bitfieldHas(&field, i), ==, (4 <= i && i < 21));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* test tr_bitfieldAddRange on the boundaries */
|
|
|
|
tr_bitfieldRemRange(&field, 0, 64);
|
|
|
|
tr_bitfieldAddRange(&field, 8, 24);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < 64; i++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-30 17:56:12 +00:00
|
|
|
check_bool(tr_bitfieldHas(&field, i), ==, (8 <= i && i < 24));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* test tr_bitfieldAddRange when begin & end is on the same word */
|
|
|
|
tr_bitfieldRemRange(&field, 0, 64);
|
|
|
|
tr_bitfieldAddRange(&field, 4, 5);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (unsigned int i = 0; i < 64; i++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-30 17:56:12 +00:00
|
|
|
check_bool(tr_bitfieldHas(&field, i), ==, (4 <= i && i < 5));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_bitfieldDestruct(&field);
|
|
|
|
return 0;
|
2012-12-09 05:32:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_bitfield_has_all_none(void)
|
2016-01-08 11:12:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfield field;
|
2016-01-08 11:12:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldConstruct(&field, 3);
|
2016-01-08 11:12:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
check(!tr_bitfieldHasAll(&field));
|
|
|
|
check(tr_bitfieldHasNone(&field));
|
2016-01-08 11:12:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldAdd(&field, 0);
|
|
|
|
check(!tr_bitfieldHasAll(&field));
|
|
|
|
check(!tr_bitfieldHasNone(&field));
|
2016-01-08 11:12:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldRem(&field, 0);
|
|
|
|
tr_bitfieldAdd(&field, 1);
|
|
|
|
check(!tr_bitfieldHasAll(&field));
|
|
|
|
check(!tr_bitfieldHasNone(&field));
|
2016-01-08 11:12:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldRem(&field, 1);
|
|
|
|
tr_bitfieldAdd(&field, 2);
|
|
|
|
check(!tr_bitfieldHasAll(&field));
|
|
|
|
check(!tr_bitfieldHasNone(&field));
|
2016-01-08 11:12:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldAdd(&field, 0);
|
|
|
|
tr_bitfieldAdd(&field, 1);
|
|
|
|
check(tr_bitfieldHasAll(&field));
|
|
|
|
check(!tr_bitfieldHasNone(&field));
|
2016-01-08 11:12:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldSetHasNone(&field);
|
|
|
|
check(!tr_bitfieldHasAll(&field));
|
|
|
|
check(tr_bitfieldHasNone(&field));
|
2016-01-08 11:12:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldSetHasAll(&field);
|
|
|
|
check(tr_bitfieldHasAll(&field));
|
|
|
|
check(!tr_bitfieldHasNone(&field));
|
2016-01-08 11:12:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldDestruct(&field);
|
|
|
|
tr_bitfieldConstruct(&field, 0);
|
2016-01-08 11:12:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
check(!tr_bitfieldHasAll(&field));
|
|
|
|
check(!tr_bitfieldHasNone(&field));
|
2016-01-08 11:12:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldSetHasNone(&field);
|
|
|
|
check(!tr_bitfieldHasAll(&field));
|
|
|
|
check(tr_bitfieldHasNone(&field));
|
2016-01-08 11:12:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldSetHasAll(&field);
|
|
|
|
check(tr_bitfieldHasAll(&field));
|
|
|
|
check(!tr_bitfieldHasNone(&field));
|
2016-01-08 11:12:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_bitfieldDestruct(&field);
|
|
|
|
return 0;
|
2016-01-08 11:12:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int main(void)
|
2012-12-09 05:32:52 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
testFunc const tests[] =
|
2016-01-08 11:12:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
test_bitfields,
|
|
|
|
test_bitfield_has_all_none
|
2016-01-08 11:12:22 +00:00
|
|
|
};
|
2012-12-09 05:32:52 +00:00
|
|
|
|
2017-05-29 17:05:24 +00:00
|
|
|
int ret = runTests(tests, NUM_TESTS(tests));
|
2012-12-09 05:32:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* bitfield count range */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int l = 0; l < 10000; ++l)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-29 17:05:24 +00:00
|
|
|
if (test_bitfield_count_range() != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-29 17:05:24 +00:00
|
|
|
++ret;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-09 05:32:52 +00:00
|
|
|
|
2017-05-29 17:05:24 +00:00
|
|
|
return ret;
|
2012-12-09 05:32:52 +00:00
|
|
|
}
|