lokinet/test/test_util.hpp

115 lines
1.8 KiB
C++
Raw Permalink Normal View History

2019-01-15 00:42:50 +00:00
#ifndef TEST_UTIL_HPP
#define TEST_UTIL_HPP
2023-01-09 17:47:41 +00:00
#include <llarp/util/fs.hpp>
#include <llarp/util/types.hpp>
2019-02-27 21:46:23 +00:00
#include <bitset>
2019-03-25 23:16:28 +00:00
#include <vector>
2019-02-27 21:46:23 +00:00
2019-01-15 00:42:50 +00:00
namespace llarp
{
namespace test
{
std::string
randFilename();
2019-01-22 23:50:26 +00:00
template < typename Buf >
Buf
makeBuf(byte_t val)
{
Buf b;
b.Fill(val);
return b;
}
2019-01-15 00:42:50 +00:00
struct FileGuard
{
2019-12-09 19:29:33 +00:00
const fs::path p;
2019-01-15 00:42:50 +00:00
2019-12-09 19:29:33 +00:00
FileGuard(const fs::path &_p) : p(_p)
2019-01-15 00:42:50 +00:00
{
}
~FileGuard()
{
if(fs::exists(fs::status(p)))
{
fs::remove_all(p);
2019-01-15 00:42:50 +00:00
}
}
};
2019-05-28 19:45:09 +00:00
inline void
randbytes_impl(byte_t *ptr, size_t sz)
{
std::fill_n(ptr, sz, 0xAA);
}
template < typename T >
inline void
keygen_val(T &val, byte_t x)
{
val.Fill(x);
}
template < typename T >
inline void
keygen(T &val)
{
keygen_val(val, 0xAA);
}
2019-02-27 21:46:23 +00:00
template < typename T >
struct CombinationIterator
{
std::vector< T > toCombine;
std::vector< T > currentCombo;
int bits;
int maxBits;
void
createCombo()
{
currentCombo.clear();
for(size_t i = 0; i < toCombine.size(); ++i)
{
if(bits & (1 << i))
{
currentCombo.push_back(toCombine[i]);
}
}
}
CombinationIterator(const std::vector< T > &values)
: toCombine(values), bits(0), maxBits((1 << values.size()) - 1)
{
currentCombo.reserve(values.size());
createCombo();
}
bool
next()
{
if(bits >= maxBits)
{
return false;
}
++bits;
createCombo();
return true;
}
bool
includesElement(size_t index)
{
return bits & (1 << index);
}
};
2019-01-15 00:42:50 +00:00
} // namespace test
} // namespace llarp
#endif