2021-03-01 21:07:32 +00:00
|
|
|
#include <catch2/catch.hpp>
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2023-01-09 17:47:41 +00:00
|
|
|
#include <llarp/util/aligned.hpp>
|
2018-12-20 16:03:55 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
using TestSizes = std::tuple<
|
|
|
|
std::integral_constant<std::size_t, 8>,
|
|
|
|
std::integral_constant<std::size_t, 12>,
|
|
|
|
std::integral_constant<std::size_t, 16>,
|
|
|
|
std::integral_constant<std::size_t, 32>,
|
|
|
|
std::integral_constant<std::size_t, 64>,
|
|
|
|
std::integral_constant<std::size_t, 77>,
|
|
|
|
std::integral_constant<std::size_t, 1024>,
|
|
|
|
std::integral_constant<std::size_t, 3333>>;
|
|
|
|
|
|
|
|
TEMPLATE_LIST_TEST_CASE("AlignedBuffer", "[AlignedBuffer]", TestSizes)
|
2018-12-20 16:03:55 +00:00
|
|
|
{
|
2021-03-01 21:07:32 +00:00
|
|
|
using Buffer = llarp::AlignedBuffer<TestType::value>;
|
2018-12-20 16:03:55 +00:00
|
|
|
|
|
|
|
Buffer b;
|
2021-03-01 21:07:32 +00:00
|
|
|
CHECK(b.IsZero());
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
SECTION("Constructor")
|
|
|
|
{
|
|
|
|
CHECK(b.size() == TestType::value);
|
|
|
|
}
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
SECTION("CopyConstructor")
|
|
|
|
{
|
|
|
|
Buffer c = b;
|
|
|
|
CHECK(c.IsZero());
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
c.Fill(1);
|
|
|
|
CHECK_FALSE(c.IsZero());
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
Buffer d = c;
|
|
|
|
CHECK_FALSE(d.IsZero());
|
|
|
|
}
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
SECTION("AltConstructors")
|
|
|
|
{
|
|
|
|
b.Fill(2);
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
Buffer c(b.as_array());
|
|
|
|
CHECK_FALSE(c.IsZero());
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
Buffer d(c.data());
|
|
|
|
CHECK_FALSE(d.IsZero());
|
|
|
|
}
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
SECTION("Assignment")
|
|
|
|
{
|
|
|
|
Buffer c;
|
|
|
|
c = b;
|
|
|
|
CHECK(c.IsZero());
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
c.Fill(1);
|
|
|
|
CHECK_FALSE(c.IsZero());
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
Buffer d;
|
|
|
|
d = c;
|
|
|
|
CHECK_FALSE(d.IsZero());
|
|
|
|
}
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2022-07-18 15:59:13 +00:00
|
|
|
SECTION("FmtOut")
|
2021-03-01 21:07:32 +00:00
|
|
|
{
|
2022-07-18 15:59:13 +00:00
|
|
|
std::string out;
|
|
|
|
out = fmt::format("{}", b);
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2022-07-18 15:59:13 +00:00
|
|
|
CHECK(out == std::string(TestType::value * 2, '0'));
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
b.Fill(255);
|
2022-07-18 15:59:13 +00:00
|
|
|
out = fmt::format("{}", b);
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2022-07-18 15:59:13 +00:00
|
|
|
CHECK(out == std::string(TestType::value * 2, 'f'));
|
2021-03-01 21:07:32 +00:00
|
|
|
}
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
SECTION("BitwiseNot")
|
|
|
|
{
|
|
|
|
Buffer c = ~b;
|
|
|
|
CHECK_FALSE(c.IsZero());
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
for (auto val : c.as_array())
|
|
|
|
{
|
|
|
|
CHECK(255 == val);
|
|
|
|
}
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
Buffer d = ~c;
|
|
|
|
CHECK(d.IsZero());
|
|
|
|
}
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
SECTION("Operators")
|
2018-12-20 16:03:55 +00:00
|
|
|
{
|
2021-03-01 21:07:32 +00:00
|
|
|
Buffer c = b;
|
|
|
|
CHECK(b == c);
|
|
|
|
CHECK(b >= c);
|
|
|
|
CHECK(b <= c);
|
|
|
|
CHECK(c >= b);
|
|
|
|
CHECK(c <= b);
|
|
|
|
|
|
|
|
c.Fill(1);
|
|
|
|
CHECK(b != c);
|
|
|
|
CHECK(b < c);
|
|
|
|
CHECK(c > b);
|
2018-12-20 16:03:55 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
SECTION("Xor")
|
|
|
|
{
|
|
|
|
Buffer c;
|
|
|
|
b.Fill(255);
|
|
|
|
c.Fill(255);
|
|
|
|
CHECK_FALSE(b.IsZero());
|
|
|
|
CHECK_FALSE(c.IsZero());
|
|
|
|
|
|
|
|
Buffer d = b ^ c;
|
|
|
|
// 1 ^ 1 = 0
|
|
|
|
CHECK(d.IsZero());
|
|
|
|
// Verify unchanged
|
|
|
|
CHECK_FALSE(b.IsZero());
|
|
|
|
CHECK_FALSE(c.IsZero());
|
|
|
|
|
|
|
|
Buffer e, f;
|
|
|
|
e.Fill(255);
|
|
|
|
Buffer g = e ^ f;
|
|
|
|
// 1 ^ 0 = 1
|
|
|
|
CHECK_FALSE(g.IsZero());
|
|
|
|
|
|
|
|
Buffer h, i;
|
|
|
|
i.Fill(255);
|
|
|
|
Buffer j = h ^ i;
|
|
|
|
// 0 ^ 1 = 1
|
|
|
|
CHECK_FALSE(j.IsZero());
|
|
|
|
}
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
SECTION("XorAssign")
|
|
|
|
{
|
|
|
|
Buffer c;
|
|
|
|
b.Fill(255);
|
|
|
|
c.Fill(255);
|
|
|
|
CHECK_FALSE(b.IsZero());
|
|
|
|
CHECK_FALSE(c.IsZero());
|
|
|
|
|
|
|
|
b ^= c;
|
|
|
|
CHECK(b.IsZero());
|
|
|
|
}
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
SECTION("Zero")
|
|
|
|
{
|
|
|
|
b.Fill(127);
|
|
|
|
CHECK_FALSE(b.IsZero());
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
b.Zero();
|
|
|
|
CHECK(b.IsZero());
|
|
|
|
}
|
2018-12-20 16:03:55 +00:00
|
|
|
|
2021-03-01 21:07:32 +00:00
|
|
|
SECTION("TestHash")
|
|
|
|
{
|
2021-03-09 18:39:40 +00:00
|
|
|
using Map_t = std::unordered_map<Buffer, int>;
|
2021-03-01 21:07:32 +00:00
|
|
|
|
|
|
|
Buffer k, other_k;
|
|
|
|
k.Randomize();
|
|
|
|
other_k.Randomize();
|
|
|
|
Map_t m;
|
|
|
|
CHECK(m.empty());
|
|
|
|
CHECK(m.emplace(k, 1).second);
|
|
|
|
CHECK(m.find(k) != m.end());
|
|
|
|
CHECK(m[k] == 1);
|
|
|
|
CHECK_FALSE(m.find(other_k) != m.end());
|
|
|
|
CHECK(m.size() == 1);
|
|
|
|
Buffer k_copy = k;
|
|
|
|
CHECK_FALSE(m.emplace(k_copy, 2).second);
|
|
|
|
CHECK_FALSE(m[k_copy] == 2);
|
|
|
|
CHECK(m[k_copy] == 1);
|
|
|
|
}
|
2019-04-25 23:21:19 +00:00
|
|
|
}
|