Skip to content

Commit 9592d59

Browse files
committed
refactor(test): Slightly nicer C++ interface to tox Random.
1 parent c66e10f commit 9592d59

File tree

3 files changed

+52
-33
lines changed

3 files changed

+52
-33
lines changed

toxcore/crypto_core_test_util.cc

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@
33
#include <cstring>
44
#include <iomanip>
55

6+
Random_Funcs const Random_Class::vtable = {
7+
Method<crypto_random_bytes_cb, Random_Class>::invoke<&Random_Class::random_bytes>,
8+
Method<crypto_random_uniform_cb, Random_Class>::invoke<&Random_Class::random_uniform>,
9+
};
10+
11+
Random_Class::~Random_Class() = default;
12+
13+
void Test_Random::random_bytes(void *obj, uint8_t *bytes, size_t length)
14+
{
15+
std::generate(bytes, &bytes[length], std::ref(lcg));
16+
}
17+
18+
uint32_t Test_Random::random_uniform(void *obj, uint32_t upper_bound)
19+
{
20+
std::uniform_int_distribution<uint32_t> distrib(0, upper_bound);
21+
return distrib(lcg);
22+
}
23+
624
PublicKey random_pk(const Random *rng)
725
{
826
PublicKey pk;
@@ -19,28 +37,3 @@ std::ostream &operator<<(std::ostream &out, PublicKey const &pk)
1937
out << '"';
2038
return out;
2139
}
22-
23-
static void test_random_bytes(void *obj, uint8_t *bytes, size_t length)
24-
{
25-
Test_Random *self = static_cast<Test_Random *>(obj);
26-
std::generate(bytes, &bytes[length], std::ref(self->lcg));
27-
}
28-
29-
static uint32_t test_random_uniform(void *obj, uint32_t upper_bound)
30-
{
31-
Test_Random *self = static_cast<Test_Random *>(obj);
32-
std::uniform_int_distribution<uint32_t> distrib(0, upper_bound);
33-
return distrib(self->lcg);
34-
}
35-
36-
Random_Funcs const Test_Random::vtable = {
37-
test_random_bytes,
38-
test_random_uniform,
39-
};
40-
41-
Test_Random::Test_Random()
42-
: self{&vtable, this}
43-
{
44-
}
45-
46-
Test_Random::operator Random const *() const { return &self; }

toxcore/crypto_core_test_util.hh

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,34 @@
99
#include "crypto_core.h"
1010
#include "test_util.hh"
1111

12+
struct Random_Class {
13+
static Random_Funcs const vtable;
14+
Random const self;
15+
16+
operator Random const *() const { return &self; }
17+
18+
Random_Class(Random_Class const &) = default;
19+
Random_Class()
20+
: self{&vtable, this}
21+
{
22+
}
23+
24+
virtual ~Random_Class();
25+
virtual crypto_random_bytes_cb random_bytes = 0;
26+
virtual crypto_random_uniform_cb random_uniform = 0;
27+
};
28+
1229
/**
1330
* A very simple, fast, and deterministic PRNG just for testing.
1431
*
1532
* We generally don't want to use system_random(), since it's a
1633
* cryptographically secure PRNG and we don't need that in unit tests.
1734
*/
18-
class Test_Random {
19-
static Random_Funcs const vtable;
20-
Random const self;
21-
22-
public:
23-
Test_Random();
24-
operator Random const *() const;
25-
35+
class Test_Random : public Random_Class {
2636
std::minstd_rand lcg;
37+
38+
void random_bytes(void *obj, uint8_t *bytes, size_t length) override;
39+
uint32_t random_uniform(void *obj, uint32_t upper_bound) override;
2740
};
2841

2942
struct PublicKey : private std::array<uint8_t, CRYPTO_PUBLIC_KEY_SIZE> {

toxcore/test_util.hh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,25 @@ struct Function_Deleter {
1111
void operator()(T *ptr) const { Delete(ptr); }
1212
};
1313

14+
// No default deleter, because we want to catch when we forget to specialise this one.
1415
template <typename T>
1516
struct Deleter;
1617

1718
template <typename T>
1819
using Ptr = std::unique_ptr<T, Deleter<T>>;
1920

21+
template <typename Func, typename Class>
22+
struct Method;
23+
24+
template <typename R, typename Class, typename... Args>
25+
struct Method<R(void *, Args...), Class> {
26+
template <R (Class::*M)(void *, Args...)>
27+
static R invoke(void *self, Args... args)
28+
{
29+
return (static_cast<Class *>(self)->*M)(self, args...);
30+
}
31+
};
32+
2033
template <typename T, std::size_t N>
2134
std::array<T, N> to_array(T const (&arr)[N])
2235
{

0 commit comments

Comments
 (0)