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>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <cstddef> // size_t
|
|
|
|
#include <cstdint> // uint8_t
|
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/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));
|
2023-04-23 01:25:55 +00:00
|
|
|
bf.set_has_all();
|
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);
|
2023-04-23 01:25:55 +00:00
|
|
|
bf.set_from_bools(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());
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_EQ(have_all, bf.has_all());
|
|
|
|
EXPECT_EQ(have_none, bf.has_none());
|
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);
|
2023-04-23 01:25:55 +00:00
|
|
|
bf.set_raw(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);
|
2023-04-23 01:25:55 +00:00
|
|
|
bf.set_has_all();
|
2021-11-25 20:30:13 +00:00
|
|
|
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();
|
2023-04-23 01:25:55 +00:00
|
|
|
bf.set_raw(&by, 1);
|
|
|
|
EXPECT_TRUE(bf.has_all());
|
|
|
|
EXPECT_FALSE(bf.has_none());
|
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 */
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_span(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 */
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_span(0, 64);
|
|
|
|
field.unset_span(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 */
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_span(0, 64);
|
|
|
|
field.unset_span(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 */
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_span(0, 64);
|
|
|
|
field.unset_span(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 */
|
2023-04-23 01:25:55 +00:00
|
|
|
field.unset_span(0, 64);
|
|
|
|
field.set_span(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 */
|
2023-04-23 01:25:55 +00:00
|
|
|
field.unset_span(0, 64);
|
|
|
|
field.set_span(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 */
|
2023-04-23 01:25:55 +00:00
|
|
|
field.unset_span(0, 64);
|
|
|
|
field.set_span(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 */
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_has_none();
|
|
|
|
field.set_span(100, 1000);
|
|
|
|
EXPECT_FALSE(field.has_none());
|
|
|
|
EXPECT_FALSE(field.has_all());
|
2021-11-25 20:30:13 +00:00
|
|
|
EXPECT_EQ(std::size(field) - 100, field.count());
|
|
|
|
|
|
|
|
/* test tr_bitfield::unsetSpan when it changes nothing */
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_has_none();
|
|
|
|
field.unset_span(0, 100);
|
|
|
|
EXPECT_TRUE(field.has_none());
|
|
|
|
EXPECT_FALSE(field.has_all());
|
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 */
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_has_all();
|
|
|
|
field.set_span(0, 100);
|
|
|
|
EXPECT_FALSE(field.has_none());
|
|
|
|
EXPECT_TRUE(field.has_all());
|
2021-11-25 20:30:13 +00:00
|
|
|
EXPECT_EQ(std::size(field), field.count());
|
|
|
|
|
|
|
|
/* test tr_bitfield::setSpan with an invalid span doesn't crash */
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_has_all();
|
|
|
|
field.set_span(0, 0);
|
|
|
|
EXPECT_TRUE(field.has_all());
|
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
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(!field.has_all());
|
|
|
|
EXPECT_TRUE(field.has_none());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
field.set(0);
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(!field.has_all());
|
|
|
|
EXPECT_TRUE(!field.has_none());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
field.unset(0);
|
|
|
|
field.set(1);
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(!field.has_all());
|
|
|
|
EXPECT_TRUE(!field.has_none());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
field.unset(1);
|
|
|
|
field.set(2);
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(!field.has_all());
|
|
|
|
EXPECT_TRUE(!field.has_none());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
field.set(0);
|
|
|
|
field.set(1);
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(field.has_all());
|
|
|
|
EXPECT_TRUE(!field.has_none());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_has_none();
|
|
|
|
EXPECT_TRUE(!field.has_all());
|
|
|
|
EXPECT_TRUE(field.has_none());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_has_all();
|
|
|
|
EXPECT_TRUE(field.has_all());
|
|
|
|
EXPECT_TRUE(!field.has_none());
|
2021-10-11 17:29:14 +00:00
|
|
|
}
|
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
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(!field.has_all());
|
|
|
|
EXPECT_TRUE(!field.has_none());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_has_none();
|
|
|
|
EXPECT_TRUE(!field.has_all());
|
|
|
|
EXPECT_TRUE(field.has_none());
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_has_all();
|
|
|
|
EXPECT_TRUE(field.has_all());
|
|
|
|
EXPECT_TRUE(!field.has_none());
|
2021-10-11 17:29:14 +00:00
|
|
|
}
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
2022-11-23 05:26:10 +00:00
|
|
|
|
|
|
|
TEST(Bitfield, percent)
|
|
|
|
{
|
|
|
|
auto field = tr_bitfield{ 100 };
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_has_all();
|
2022-11-23 05:26:10 +00:00
|
|
|
EXPECT_NEAR(1.0F, field.percent(), 0.01);
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_has_none();
|
2022-11-23 05:26:10 +00:00
|
|
|
EXPECT_NEAR(0.0F, field.percent(), 0.01);
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_span(0, std::size(field) / 2U);
|
2022-11-23 05:26:10 +00:00
|
|
|
EXPECT_NEAR(0.5F, field.percent(), 0.01);
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
field.set_has_none();
|
|
|
|
field.set_span(0, std::size(field) / 4U);
|
2022-11-23 05:26:10 +00:00
|
|
|
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 };
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_all();
|
|
|
|
b.set_has_none();
|
2022-11-23 05:26:10 +00:00
|
|
|
a |= b;
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(a.has_all());
|
2022-11-23 05:26:10 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_none();
|
|
|
|
b.set_has_all();
|
2022-11-23 05:26:10 +00:00
|
|
|
a |= b;
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(a.has_all());
|
2022-11-23 05:26:10 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_none();
|
|
|
|
b.set_has_none();
|
2022-11-23 05:26:10 +00:00
|
|
|
a |= b;
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(a.has_none());
|
2022-11-23 05:26:10 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_none();
|
|
|
|
b.set_has_none();
|
|
|
|
a.set_span(0, std::size(a) / 2U);
|
|
|
|
b.set_span(std::size(a) / 2U, std::size(a));
|
2022-11-23 05:26:10 +00:00
|
|
|
EXPECT_EQ(0.5, a.percent());
|
|
|
|
EXPECT_EQ(0.5, b.percent());
|
|
|
|
a |= b;
|
|
|
|
EXPECT_EQ(1.0, a.percent());
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(a.has_all());
|
2022-11-23 05:26:10 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_none();
|
|
|
|
b.set_has_none();
|
2022-11-23 05:26:10 +00:00
|
|
|
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;
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(a.has_all());
|
2022-11-23 05:26:10 +00:00
|
|
|
}
|
|
|
|
|
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 };
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_all();
|
|
|
|
b.set_has_none();
|
2022-11-23 05:26:10 +00:00
|
|
|
a &= b;
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(a.has_none());
|
2022-11-23 05:26:10 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_none();
|
|
|
|
b.set_has_all();
|
2022-11-23 05:26:10 +00:00
|
|
|
a &= b;
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(a.has_none());
|
2022-11-23 05:26:10 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_all();
|
|
|
|
b.set_has_all();
|
2022-11-23 05:26:10 +00:00
|
|
|
a &= b;
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(a.has_all());
|
2022-11-23 05:26:10 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_none();
|
|
|
|
b.set_has_none();
|
|
|
|
a.set_span(0, std::size(a) / 2U);
|
|
|
|
b.set_span(std::size(a) / 2U, std::size(a));
|
2022-11-23 05:26:10 +00:00
|
|
|
EXPECT_EQ(0.5, a.percent());
|
|
|
|
EXPECT_EQ(0.5, b.percent());
|
|
|
|
a &= b;
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(a.has_none());
|
2022-11-23 05:26:10 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_none();
|
|
|
|
b.set_has_none();
|
2022-11-23 05:26:10 +00:00
|
|
|
for (size_t i = 0; i < std::size(a); ++i)
|
|
|
|
{
|
|
|
|
if ((i % 2U) != 0U)
|
|
|
|
{
|
|
|
|
a.set(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b.set(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a &= b;
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(a.has_none());
|
2022-11-23 05:26:10 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_none();
|
|
|
|
a.set_span(0U, std::size(a) / 10U);
|
|
|
|
b.set_has_none();
|
|
|
|
b.set_span(0U, std::size(a) / 20U);
|
2022-11-23 05:26:10 +00:00
|
|
|
a &= b;
|
|
|
|
EXPECT_NEAR(0.05F, a.percent(), 0.01);
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_none();
|
|
|
|
a.set_span(0U, std::size(a) / 10U);
|
|
|
|
b.set_has_none();
|
|
|
|
b.set_span(0U, std::size(a) / 20U);
|
2022-11-23 05:26:10 +00:00
|
|
|
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 };
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_all();
|
|
|
|
b.set_has_none();
|
2023-04-14 23:45:46 +00:00
|
|
|
EXPECT_FALSE(a.intersects(b));
|
|
|
|
EXPECT_FALSE(b.intersects(a));
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_all();
|
|
|
|
b.set_has_all();
|
2023-04-14 23:45:46 +00:00
|
|
|
EXPECT_TRUE(a.intersects(b));
|
|
|
|
EXPECT_TRUE(b.intersects(a));
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_none();
|
|
|
|
b.set_has_none();
|
2023-04-14 23:45:46 +00:00
|
|
|
EXPECT_FALSE(a.intersects(b));
|
|
|
|
EXPECT_FALSE(b.intersects(a));
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_none();
|
|
|
|
b.set_has_none();
|
|
|
|
a.set_span(0, std::size(a) / 2U);
|
|
|
|
b.set_span(std::size(a) / 2U, std::size(a));
|
2023-04-14 23:45:46 +00:00
|
|
|
EXPECT_EQ(0.5, a.percent());
|
|
|
|
EXPECT_EQ(0.5, b.percent());
|
|
|
|
EXPECT_FALSE(a.intersects(b));
|
|
|
|
EXPECT_FALSE(b.intersects(a));
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_none();
|
|
|
|
b.set_has_none();
|
2023-04-14 23:45:46 +00:00
|
|
|
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));
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_none();
|
|
|
|
a.set_span(0U, std::size(a) / 10U);
|
|
|
|
b.set_has_none();
|
|
|
|
b.set_span(0U, std::size(a) / 20U);
|
2023-04-14 23:45:46 +00:00
|
|
|
EXPECT_TRUE(a.intersects(b));
|
|
|
|
EXPECT_TRUE(b.intersects(a));
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
a.set_has_none();
|
|
|
|
a.set_span(0U, std::size(a) / 10U);
|
|
|
|
b.set_has_none();
|
|
|
|
b.set_span(0U, std::size(a) / 20U);
|
2023-04-14 23:45:46 +00:00
|
|
|
EXPECT_TRUE(a.intersects(b));
|
|
|
|
EXPECT_TRUE(b.intersects(a));
|
|
|
|
}
|