2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright (C) 2010-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-11-25 20:30:13 +00:00
|
|
|
#include <algorithm>
|
2021-10-17 15:34:36 +00:00
|
|
|
#include <array>
|
2021-11-25 20:30:13 +00:00
|
|
|
#include <limits>
|
|
|
|
#include <vector>
|
2021-10-17 15:34:36 +00:00
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/crypto-utils.h>
|
|
|
|
#include <libtransmission/bitfield.h>
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2021-11-25 20:30:13 +00:00
|
|
|
TEST(Bitfield, count)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
auto constexpr IterCount = int{ 10000 };
|
|
|
|
|
|
|
|
for (auto i = 0; i < IterCount; ++i)
|
|
|
|
{
|
2022-12-21 15:58:32 +00:00
|
|
|
auto const bit_count = 100U + tr_rand_int(1000U);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// generate a random bitfield
|
2021-10-24 20:43:36 +00:00
|
|
|
tr_bitfield bf(bit_count);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-12-21 15:58:32 +00:00
|
|
|
for (size_t j = 0, n = tr_rand_int(bit_count); j < n; ++j)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2022-12-14 18:21:56 +00:00
|
|
|
bf.set(tr_rand_int(bit_count));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 18:21:56 +00:00
|
|
|
int begin = tr_rand_int(bit_count);
|
2022-11-14 20:16:29 +00:00
|
|
|
int end = 0;
|
2020-08-11 18:11:55 +00:00
|
|
|
do
|
|
|
|
{
|
2022-12-14 18:21:56 +00:00
|
|
|
end = tr_rand_int(bit_count);
|
2021-08-15 09:41:48 +00:00
|
|
|
} while (end == begin);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
// ensure end <= begin
|
|
|
|
if (end < begin)
|
|
|
|
{
|
|
|
|
int const tmp = begin;
|
|
|
|
begin = end;
|
|
|
|
end = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// test the bitfield
|
|
|
|
unsigned long count1 = {};
|
|
|
|
for (auto j = begin; j < end; ++j)
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
if (bf.test(j))
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
++count1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
auto const count2 = bf.count(begin, end);
|
2020-08-11 18:11:55 +00:00
|
|
|
EXPECT_EQ(count1, count2);
|
|
|
|
}
|
2021-11-25 20:30:13 +00:00
|
|
|
|
|
|
|
auto bf = tr_bitfield{ 0 };
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(0U, bf.count(0, 0));
|
|
|
|
EXPECT_EQ(0U, bf.count(0, 1));
|
2021-11-25 20:30:13 +00:00
|
|
|
|
|
|
|
bf = tr_bitfield{ 100 };
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(0U, bf.count(0, 0));
|
|
|
|
EXPECT_EQ(0U, bf.count(0, 100));
|
2021-11-25 20:30:13 +00:00
|
|
|
bf.setHasAll();
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(0U, bf.count(0, 0));
|
|
|
|
EXPECT_EQ(1U, bf.count(0, 1));
|
|
|
|
EXPECT_EQ(100U, bf.count(0, 100));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-10-17 15:34:36 +00:00
|
|
|
TEST(Bitfield, ctorFromFlagArray)
|
|
|
|
{
|
|
|
|
auto constexpr Tests = std::array<std::array<bool, 10>, 3>{ {
|
|
|
|
{ false, true, false, true, false, false, true, false, false, true }, // mixed
|
|
|
|
{ true, true, true, true, true, true, true, true, true, true }, // have all
|
|
|
|
{ false, false, false, false, false, false, false, false, false, false }, // have none
|
|
|
|
} };
|
|
|
|
|
|
|
|
for (auto const& flags : Tests)
|
|
|
|
{
|
|
|
|
size_t const true_count = std::count(std::begin(flags), std::end(flags), true);
|
|
|
|
size_t const n = std::size(flags);
|
|
|
|
bool const have_all = true_count == n;
|
|
|
|
bool const have_none = true_count == 0;
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
auto bf = tr_bitfield(n);
|
|
|
|
bf.setFromBools(std::data(flags), std::size(flags));
|
2021-10-17 15:34:36 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
EXPECT_EQ(n, bf.size());
|
2021-10-17 15:34:36 +00:00
|
|
|
EXPECT_EQ(have_all, bf.hasAll());
|
|
|
|
EXPECT_EQ(have_none, bf.hasNone());
|
2021-10-24 20:43:36 +00:00
|
|
|
EXPECT_EQ(true_count, bf.count());
|
2021-10-17 15:34:36 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < std::size(flags); ++i)
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
EXPECT_EQ(flags[i], bf.test(i));
|
2021-10-17 15:34:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
TEST(Bitfield, setRaw)
|
|
|
|
{
|
|
|
|
auto constexpr TestByte = uint8_t{ 10 };
|
|
|
|
auto constexpr TestByteTrueBits = 2;
|
|
|
|
|
2021-11-25 20:30:13 +00:00
|
|
|
auto raw = std::vector<uint8_t>(100, TestByte);
|
2021-10-24 20:43:36 +00:00
|
|
|
|
|
|
|
auto bf = tr_bitfield(std::size(raw) * 8);
|
2021-11-08 00:25:45 +00:00
|
|
|
bf.setRaw(std::data(raw), std::size(raw));
|
2021-10-24 20:43:36 +00:00
|
|
|
EXPECT_EQ(TestByteTrueBits * std::size(raw), bf.count());
|
|
|
|
|
|
|
|
// The first byte of the bitfield corresponds to indices 0 - 7
|
|
|
|
// from high bit to low bit, respectively. The next one 8-15, etc.
|
|
|
|
// Spare bits at the end are set to zero.
|
|
|
|
auto test = uint8_t{};
|
|
|
|
for (int i = 0; i < 8; ++i)
|
|
|
|
{
|
|
|
|
if (bf.test(i))
|
|
|
|
{
|
|
|
|
test |= (1 << (7 - i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPECT_EQ(TestByte, test);
|
|
|
|
EXPECT_EQ(raw, bf.raw());
|
2021-11-25 20:30:13 +00:00
|
|
|
|
|
|
|
// check that has-all bitfield gets all-true
|
|
|
|
bf = tr_bitfield(std::size(raw) * 8);
|
|
|
|
bf.setHasAll();
|
|
|
|
raw = bf.raw();
|
|
|
|
EXPECT_EQ(std::size(bf) / 8, std::size(raw));
|
|
|
|
EXPECT_EQ(std::numeric_limits<unsigned char>::max(), raw[0]);
|
|
|
|
|
|
|
|
// check that the spare bits t the end are zero
|
|
|
|
bf = tr_bitfield{ 1 };
|
2022-07-27 14:03:13 +00:00
|
|
|
uint8_t const by = std::numeric_limits<uint8_t>::max();
|
2021-11-25 20:30:13 +00:00
|
|
|
bf.setRaw(&by, 1);
|
|
|
|
EXPECT_TRUE(bf.hasAll());
|
|
|
|
EXPECT_FALSE(bf.hasNone());
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(1U, bf.count());
|
2021-11-25 20:30:13 +00:00
|
|
|
raw = bf.raw();
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(1U, std::size(raw));
|
2021-11-25 20:30:13 +00:00
|
|
|
EXPECT_EQ(1 << 7, raw[0]);
|
2021-10-24 20:43:36 +00:00
|
|
|
}
|
|
|
|
|
2021-10-17 15:34:36 +00:00
|
|
|
TEST(Bitfield, bitfields)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2022-07-27 14:03:13 +00:00
|
|
|
unsigned int const bitcount = 500;
|
2021-10-24 20:43:36 +00:00
|
|
|
tr_bitfield field(bitcount);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
// test tr_bitfield::set()
|
2020-08-11 18:11:55 +00:00
|
|
|
for (unsigned int i = 0; i < bitcount; i++)
|
|
|
|
{
|
|
|
|
if (i % 7 == 0)
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
field.set(i);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < bitcount; i++)
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
EXPECT_EQ(field.test(i), (i % 7 == 0));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
/* test tr_bitfield::setSpan */
|
|
|
|
field.setSpan(0, bitcount);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < bitcount; i++)
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
EXPECT_TRUE(field.test(i));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
/* test tr_bitfield::clearBit */
|
2020-08-11 18:11:55 +00:00
|
|
|
for (unsigned int i = 0; i < bitcount; i++)
|
|
|
|
{
|
|
|
|
if (i % 7 != 0)
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
field.unset(i);
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < bitcount; i++)
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
EXPECT_EQ(field.test(i), (i % 7 == 0));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
/* test tr_bitfield::clearBitRange in the middle of a boundary */
|
2021-11-25 18:26:51 +00:00
|
|
|
field.setSpan(0, 64);
|
|
|
|
field.unsetSpan(4, 21);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < 64; i++)
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
EXPECT_EQ(field.test(i), (i < 4 || i >= 21));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
/* test tr_bitfield::clearBitRange on the boundaries */
|
2021-11-25 18:26:51 +00:00
|
|
|
field.setSpan(0, 64);
|
|
|
|
field.unsetSpan(8, 24);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < 64; i++)
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
EXPECT_EQ(field.test(i), (i < 8 || i >= 24));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
/* test tr_bitfield::clearBitRange when begin & end is on the same word */
|
2021-11-25 18:26:51 +00:00
|
|
|
field.setSpan(0, 64);
|
|
|
|
field.unsetSpan(4, 5);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < 64; i++)
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
EXPECT_EQ(field.test(i), (i < 4 || i >= 5));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
/* test tr_bitfield::setSpan */
|
|
|
|
field.unsetSpan(0, 64);
|
|
|
|
field.setSpan(4, 21);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < 64; i++)
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
EXPECT_EQ(field.test(i), (4 <= i && i < 21));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
/* test tr_bitfield::setSpan on the boundaries */
|
|
|
|
field.unsetSpan(0, 64);
|
|
|
|
field.setSpan(8, 24);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < 64; i++)
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
EXPECT_EQ(field.test(i), (8 <= i && i < 24));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
/* test tr_bitfield::setSpan when begin & end is on the same word */
|
|
|
|
field.unsetSpan(0, 64);
|
|
|
|
field.setSpan(4, 5);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < 64; i++)
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
EXPECT_EQ(field.test(i), (4 <= i && i < 5));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
2021-11-25 20:30:13 +00:00
|
|
|
|
|
|
|
/* test tr_bitfield::setSpan when end runs beyond the end of the bitfield */
|
|
|
|
field.setHasNone();
|
|
|
|
field.setSpan(100, 1000);
|
|
|
|
EXPECT_FALSE(field.hasNone());
|
|
|
|
EXPECT_FALSE(field.hasAll());
|
|
|
|
EXPECT_EQ(std::size(field) - 100, field.count());
|
|
|
|
|
|
|
|
/* test tr_bitfield::unsetSpan when it changes nothing */
|
|
|
|
field.setHasNone();
|
|
|
|
field.unsetSpan(0, 100);
|
|
|
|
EXPECT_TRUE(field.hasNone());
|
|
|
|
EXPECT_FALSE(field.hasAll());
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(0U, field.count());
|
2021-11-25 20:30:13 +00:00
|
|
|
|
|
|
|
/* test tr_bitfield::setSpan when it changes nothing */
|
|
|
|
field.setHasAll();
|
|
|
|
field.setSpan(0, 100);
|
|
|
|
EXPECT_FALSE(field.hasNone());
|
|
|
|
EXPECT_TRUE(field.hasAll());
|
|
|
|
EXPECT_EQ(std::size(field), field.count());
|
|
|
|
|
|
|
|
/* test tr_bitfield::setSpan with an invalid span doesn't crash */
|
|
|
|
field.setHasAll();
|
|
|
|
field.setSpan(0, 0);
|
|
|
|
EXPECT_TRUE(field.hasAll());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
2021-10-17 15:34:36 +00:00
|
|
|
TEST(Bitfield, hasAllNone)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
2021-10-11 17:29:14 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
tr_bitfield field(3);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
EXPECT_TRUE(!field.hasAll());
|
|
|
|
EXPECT_TRUE(field.hasNone());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
field.set(0);
|
2021-10-11 17:29:14 +00:00
|
|
|
EXPECT_TRUE(!field.hasAll());
|
|
|
|
EXPECT_TRUE(!field.hasNone());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
field.unset(0);
|
|
|
|
field.set(1);
|
2021-10-11 17:29:14 +00:00
|
|
|
EXPECT_TRUE(!field.hasAll());
|
|
|
|
EXPECT_TRUE(!field.hasNone());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
field.unset(1);
|
|
|
|
field.set(2);
|
2021-10-11 17:29:14 +00:00
|
|
|
EXPECT_TRUE(!field.hasAll());
|
|
|
|
EXPECT_TRUE(!field.hasNone());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
field.set(0);
|
|
|
|
field.set(1);
|
2021-10-11 17:29:14 +00:00
|
|
|
EXPECT_TRUE(field.hasAll());
|
|
|
|
EXPECT_TRUE(!field.hasNone());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
field.setHasNone();
|
2021-10-11 17:29:14 +00:00
|
|
|
EXPECT_TRUE(!field.hasAll());
|
|
|
|
EXPECT_TRUE(field.hasNone());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
field.setHasAll();
|
2021-10-11 17:29:14 +00:00
|
|
|
EXPECT_TRUE(field.hasAll());
|
|
|
|
EXPECT_TRUE(!field.hasNone());
|
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
tr_bitfield field(0);
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
EXPECT_TRUE(!field.hasAll());
|
|
|
|
EXPECT_TRUE(!field.hasNone());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
field.setHasNone();
|
2021-10-11 17:29:14 +00:00
|
|
|
EXPECT_TRUE(!field.hasAll());
|
|
|
|
EXPECT_TRUE(field.hasNone());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
field.setHasAll();
|
2021-10-11 17:29:14 +00:00
|
|
|
EXPECT_TRUE(field.hasAll());
|
|
|
|
EXPECT_TRUE(!field.hasNone());
|
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
2022-11-23 05:26:10 +00:00
|
|
|
|
|
|
|
TEST(Bitfield, percent)
|
|
|
|
{
|
|
|
|
auto field = tr_bitfield{ 100 };
|
|
|
|
field.setHasAll();
|
|
|
|
EXPECT_NEAR(1.0F, field.percent(), 0.01);
|
|
|
|
|
|
|
|
field.setHasNone();
|
|
|
|
EXPECT_NEAR(0.0F, field.percent(), 0.01);
|
|
|
|
|
|
|
|
field.setSpan(0, std::size(field) / 2U);
|
|
|
|
EXPECT_NEAR(0.5F, field.percent(), 0.01);
|
|
|
|
|
|
|
|
field.setHasNone();
|
|
|
|
field.setSpan(0, std::size(field) / 4U);
|
|
|
|
EXPECT_NEAR(0.25F, field.percent(), 0.01);
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:47:04 +00:00
|
|
|
TEST(Bitfield, bitwiseOr)
|
2022-11-23 05:26:10 +00:00
|
|
|
{
|
|
|
|
auto a = tr_bitfield{ 100 };
|
|
|
|
auto b = tr_bitfield{ 100 };
|
|
|
|
|
|
|
|
a.setHasAll();
|
|
|
|
b.setHasNone();
|
|
|
|
a |= b;
|
|
|
|
EXPECT_TRUE(a.hasAll());
|
|
|
|
|
|
|
|
a.setHasNone();
|
|
|
|
b.setHasAll();
|
|
|
|
a |= b;
|
|
|
|
EXPECT_TRUE(a.hasAll());
|
|
|
|
|
|
|
|
a.setHasNone();
|
|
|
|
b.setHasNone();
|
|
|
|
a |= b;
|
|
|
|
EXPECT_TRUE(a.hasNone());
|
|
|
|
|
|
|
|
a.setHasNone();
|
|
|
|
b.setHasNone();
|
|
|
|
a.setSpan(0, std::size(a) / 2U);
|
|
|
|
b.setSpan(std::size(a) / 2U, std::size(a));
|
|
|
|
EXPECT_EQ(0.5, a.percent());
|
|
|
|
EXPECT_EQ(0.5, b.percent());
|
|
|
|
a |= b;
|
|
|
|
EXPECT_EQ(1.0, a.percent());
|
|
|
|
EXPECT_TRUE(a.hasAll());
|
|
|
|
|
|
|
|
a.setHasNone();
|
|
|
|
b.setHasNone();
|
|
|
|
for (size_t i = 0; i < std::size(a); ++i)
|
|
|
|
{
|
|
|
|
if ((i % 2U) != 0U)
|
|
|
|
{
|
|
|
|
a.set(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b.set(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPECT_NEAR(0.5F, a.percent(), 0.01);
|
|
|
|
EXPECT_NEAR(0.5F, b.percent(), 0.01);
|
|
|
|
a |= b;
|
|
|
|
EXPECT_TRUE(a.hasAll());
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:47:04 +00:00
|
|
|
TEST(Bitfield, bitwiseAnd)
|
2022-11-23 05:26:10 +00:00
|
|
|
{
|
|
|
|
auto a = tr_bitfield{ 100 };
|
|
|
|
auto b = tr_bitfield{ 100 };
|
|
|
|
|
|
|
|
a.setHasAll();
|
|
|
|
b.setHasNone();
|
|
|
|
a &= b;
|
|
|
|
EXPECT_TRUE(a.hasNone());
|
|
|
|
|
|
|
|
a.setHasNone();
|
|
|
|
b.setHasAll();
|
|
|
|
a &= b;
|
|
|
|
EXPECT_TRUE(a.hasNone());
|
|
|
|
|
|
|
|
a.setHasAll();
|
|
|
|
b.setHasAll();
|
|
|
|
a &= b;
|
|
|
|
EXPECT_TRUE(a.hasAll());
|
|
|
|
|
|
|
|
a.setHasNone();
|
|
|
|
b.setHasNone();
|
|
|
|
a.setSpan(0, std::size(a) / 2U);
|
|
|
|
b.setSpan(std::size(a) / 2U, std::size(a));
|
|
|
|
EXPECT_EQ(0.5, a.percent());
|
|
|
|
EXPECT_EQ(0.5, b.percent());
|
|
|
|
a &= b;
|
|
|
|
EXPECT_TRUE(a.hasNone());
|
|
|
|
|
|
|
|
a.setHasNone();
|
|
|
|
b.setHasNone();
|
|
|
|
for (size_t i = 0; i < std::size(a); ++i)
|
|
|
|
{
|
|
|
|
if ((i % 2U) != 0U)
|
|
|
|
{
|
|
|
|
a.set(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b.set(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a &= b;
|
|
|
|
EXPECT_TRUE(a.hasNone());
|
|
|
|
|
|
|
|
a.setHasNone();
|
|
|
|
a.setSpan(0U, std::size(a) / 10U);
|
|
|
|
b.setHasNone();
|
|
|
|
b.setSpan(0U, std::size(a) / 20U);
|
|
|
|
a &= b;
|
|
|
|
EXPECT_NEAR(0.05F, a.percent(), 0.01);
|
|
|
|
|
|
|
|
a.setHasNone();
|
|
|
|
a.setSpan(0U, std::size(a) / 10U);
|
|
|
|
b.setHasNone();
|
|
|
|
b.setSpan(0U, std::size(a) / 20U);
|
|
|
|
b &= a;
|
|
|
|
EXPECT_NEAR(0.1F, a.percent(), 0.01);
|
|
|
|
}
|
2023-04-14 23:45:46 +00:00
|
|
|
|
|
|
|
TEST(Bitfield, intersects)
|
|
|
|
{
|
|
|
|
auto a = tr_bitfield{ 100 };
|
|
|
|
auto b = tr_bitfield{ 100 };
|
|
|
|
|
|
|
|
a.setHasAll();
|
|
|
|
b.setHasNone();
|
|
|
|
EXPECT_FALSE(a.intersects(b));
|
|
|
|
EXPECT_FALSE(b.intersects(a));
|
|
|
|
|
|
|
|
a.setHasAll();
|
|
|
|
b.setHasAll();
|
|
|
|
EXPECT_TRUE(a.intersects(b));
|
|
|
|
EXPECT_TRUE(b.intersects(a));
|
|
|
|
|
|
|
|
a.setHasNone();
|
|
|
|
b.setHasNone();
|
|
|
|
EXPECT_FALSE(a.intersects(b));
|
|
|
|
EXPECT_FALSE(b.intersects(a));
|
|
|
|
|
|
|
|
a.setHasNone();
|
|
|
|
b.setHasNone();
|
|
|
|
a.setSpan(0, std::size(a) / 2U);
|
|
|
|
b.setSpan(std::size(a) / 2U, std::size(a));
|
|
|
|
EXPECT_EQ(0.5, a.percent());
|
|
|
|
EXPECT_EQ(0.5, b.percent());
|
|
|
|
EXPECT_FALSE(a.intersects(b));
|
|
|
|
EXPECT_FALSE(b.intersects(a));
|
|
|
|
|
|
|
|
a.setHasNone();
|
|
|
|
b.setHasNone();
|
|
|
|
for (size_t i = 0; i < std::size(a); ++i)
|
|
|
|
{
|
|
|
|
if ((i % 2U) != 0U)
|
|
|
|
{
|
|
|
|
a.set(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b.set(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPECT_FALSE(a.intersects(b));
|
|
|
|
EXPECT_FALSE(b.intersects(a));
|
|
|
|
|
|
|
|
a.setHasNone();
|
|
|
|
a.setSpan(0U, std::size(a) / 10U);
|
|
|
|
b.setHasNone();
|
|
|
|
b.setSpan(0U, std::size(a) / 20U);
|
|
|
|
EXPECT_TRUE(a.intersects(b));
|
|
|
|
EXPECT_TRUE(b.intersects(a));
|
|
|
|
|
|
|
|
a.setHasNone();
|
|
|
|
a.setSpan(0U, std::size(a) / 10U);
|
|
|
|
b.setHasNone();
|
|
|
|
b.setSpan(0U, std::size(a) / 20U);
|
|
|
|
EXPECT_TRUE(a.intersects(b));
|
|
|
|
EXPECT_TRUE(b.intersects(a));
|
|
|
|
}
|