mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
refactor(test): Slightly nicer C++ interface to tox Random.
This commit is contained in:
parent
c66e10fb7a
commit
9592d590cf
|
@ -3,6 +3,24 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
|
||||||
|
Random_Funcs const Random_Class::vtable = {
|
||||||
|
Method<crypto_random_bytes_cb, Random_Class>::invoke<&Random_Class::random_bytes>,
|
||||||
|
Method<crypto_random_uniform_cb, Random_Class>::invoke<&Random_Class::random_uniform>,
|
||||||
|
};
|
||||||
|
|
||||||
|
Random_Class::~Random_Class() = default;
|
||||||
|
|
||||||
|
void Test_Random::random_bytes(void *obj, uint8_t *bytes, size_t length)
|
||||||
|
{
|
||||||
|
std::generate(bytes, &bytes[length], std::ref(lcg));
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t Test_Random::random_uniform(void *obj, uint32_t upper_bound)
|
||||||
|
{
|
||||||
|
std::uniform_int_distribution<uint32_t> distrib(0, upper_bound);
|
||||||
|
return distrib(lcg);
|
||||||
|
}
|
||||||
|
|
||||||
PublicKey random_pk(const Random *rng)
|
PublicKey random_pk(const Random *rng)
|
||||||
{
|
{
|
||||||
PublicKey pk;
|
PublicKey pk;
|
||||||
|
@ -19,28 +37,3 @@ std::ostream &operator<<(std::ostream &out, PublicKey const &pk)
|
||||||
out << '"';
|
out << '"';
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_random_bytes(void *obj, uint8_t *bytes, size_t length)
|
|
||||||
{
|
|
||||||
Test_Random *self = static_cast<Test_Random *>(obj);
|
|
||||||
std::generate(bytes, &bytes[length], std::ref(self->lcg));
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint32_t test_random_uniform(void *obj, uint32_t upper_bound)
|
|
||||||
{
|
|
||||||
Test_Random *self = static_cast<Test_Random *>(obj);
|
|
||||||
std::uniform_int_distribution<uint32_t> distrib(0, upper_bound);
|
|
||||||
return distrib(self->lcg);
|
|
||||||
}
|
|
||||||
|
|
||||||
Random_Funcs const Test_Random::vtable = {
|
|
||||||
test_random_bytes,
|
|
||||||
test_random_uniform,
|
|
||||||
};
|
|
||||||
|
|
||||||
Test_Random::Test_Random()
|
|
||||||
: self{&vtable, this}
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
Test_Random::operator Random const *() const { return &self; }
|
|
||||||
|
|
|
@ -9,21 +9,34 @@
|
||||||
#include "crypto_core.h"
|
#include "crypto_core.h"
|
||||||
#include "test_util.hh"
|
#include "test_util.hh"
|
||||||
|
|
||||||
|
struct Random_Class {
|
||||||
|
static Random_Funcs const vtable;
|
||||||
|
Random const self;
|
||||||
|
|
||||||
|
operator Random const *() const { return &self; }
|
||||||
|
|
||||||
|
Random_Class(Random_Class const &) = default;
|
||||||
|
Random_Class()
|
||||||
|
: self{&vtable, this}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~Random_Class();
|
||||||
|
virtual crypto_random_bytes_cb random_bytes = 0;
|
||||||
|
virtual crypto_random_uniform_cb random_uniform = 0;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A very simple, fast, and deterministic PRNG just for testing.
|
* A very simple, fast, and deterministic PRNG just for testing.
|
||||||
*
|
*
|
||||||
* We generally don't want to use system_random(), since it's a
|
* We generally don't want to use system_random(), since it's a
|
||||||
* cryptographically secure PRNG and we don't need that in unit tests.
|
* cryptographically secure PRNG and we don't need that in unit tests.
|
||||||
*/
|
*/
|
||||||
class Test_Random {
|
class Test_Random : public Random_Class {
|
||||||
static Random_Funcs const vtable;
|
|
||||||
Random const self;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Test_Random();
|
|
||||||
operator Random const *() const;
|
|
||||||
|
|
||||||
std::minstd_rand lcg;
|
std::minstd_rand lcg;
|
||||||
|
|
||||||
|
void random_bytes(void *obj, uint8_t *bytes, size_t length) override;
|
||||||
|
uint32_t random_uniform(void *obj, uint32_t upper_bound) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PublicKey : private std::array<uint8_t, CRYPTO_PUBLIC_KEY_SIZE> {
|
struct PublicKey : private std::array<uint8_t, CRYPTO_PUBLIC_KEY_SIZE> {
|
||||||
|
|
|
@ -11,12 +11,25 @@ struct Function_Deleter {
|
||||||
void operator()(T *ptr) const { Delete(ptr); }
|
void operator()(T *ptr) const { Delete(ptr); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// No default deleter, because we want to catch when we forget to specialise this one.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct Deleter;
|
struct Deleter;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
using Ptr = std::unique_ptr<T, Deleter<T>>;
|
using Ptr = std::unique_ptr<T, Deleter<T>>;
|
||||||
|
|
||||||
|
template <typename Func, typename Class>
|
||||||
|
struct Method;
|
||||||
|
|
||||||
|
template <typename R, typename Class, typename... Args>
|
||||||
|
struct Method<R(void *, Args...), Class> {
|
||||||
|
template <R (Class::*M)(void *, Args...)>
|
||||||
|
static R invoke(void *self, Args... args)
|
||||||
|
{
|
||||||
|
return (static_cast<Class *>(self)->*M)(self, args...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template <typename T, std::size_t N>
|
template <typename T, std::size_t N>
|
||||||
std::array<T, N> to_array(T const (&arr)[N])
|
std::array<T, N> to_array(T const (&arr)[N])
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user