2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright (C) 2010-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// 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
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "crypto-utils.h"
|
|
|
|
#include "bitfield.h"
|
|
|
|
#include "utils.h" /* tr_free */
|
|
|
|
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
int const bit_count = 100 + tr_rand_int_weak(1000);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
for (int j = 0, n = tr_rand_int_weak(bit_count); j < n; ++j)
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
bf.set(tr_rand_int_weak(bit_count));
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int begin = tr_rand_int_weak(bit_count);
|
|
|
|
int end;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
end = tr_rand_int_weak(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-04-02 14:06:02 +00:00
|
|
|
uint8_t 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
|
|
|
{
|
|
|
|
unsigned int 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
|
|
|
}
|