codex
Loading...
Searching...
No Matches
common.hpp
Go to the documentation of this file.
1#ifndef CODEX_BASE64_TEST_COMMON_H
2#define CODEX_BASE64_TEST_COMMON_H
3
4#include <functional>
5#include <random>
6#include <string_view>
7#include <vector>
8#include <tuple>
9
10#include "cmd.hpp"
11
12template<typename T>
13std::vector<char> rnd_vec(T *gen, std::size_t n) {
14 std::vector<char> ret(round_to_block_size<3>(n));
15 std::generate(
16 begin(ret), begin(ret) + static_cast<std::ptrdiff_t>(n),
17 std::bind_front(std::uniform_int_distribution<char>{}, *gen));
18 return ret;
19}
20
21template<typename T>
22auto gen_input(T *gen) {
23 QFETCH(std::size_t, dec_size);
24 dec_size = round_to_block_size<3>(dec_size);
25 const auto enc_size = output_size<4, 3>(dec_size);
26 return std::tuple(dec_size, enc_size, rnd_vec(gen, dec_size));
27}
28
29template<typename T, typename C, bool B>
30void encode_test(auto *gen) {
31 const auto [dec_size, enc_size, input_v] = gen_input(gen);
32 const std::string_view input = {input_v.data(), dec_size};
33 auto output = std::vector<char>(enc_size);
34 if constexpr(B)
35 QBENCHMARK { T::encode(input, output.data()); }
36 else
37 T::encode(input, output.data());
38 check_encode<T::encode>(input, output);
39}
40
41template<typename T, typename C, bool B>
42void decode_test(auto *gen) {
43 const auto [dec_size, enc_size, input_v] = gen_input(gen);
44 const std::string_view input = {input_v.data(), dec_size};
45 auto encoded = std::vector<char>(enc_size);
46 T::encode(input, encoded.data());
47 auto output = std::vector<char>(input_v.size());
48 if constexpr(B)
49 QBENCHMARK { T::decode({encoded.data(), enc_size}, output.data()); }
50 else
51 T::decode({encoded.data(), enc_size}, output.data());
52 check_decode<T::decode>(input, {encoded.data(), encoded.size()}, output);
53}
54
55template<auto F>
56void check_encode(std::string_view input, const std::vector<char> &output) {
57 const auto n = output.size();
58 auto expected = std::vector<char>(n);
59 F(input, expected.data());
60 QCOMPARE(
61 std::string_view(output.data(), n),
62 std::string_view(expected.data(), n));
63}
64
65template<auto F>
67 std::string_view input, std::string_view encoded,
68 const std::vector<char> &output
69) {
70 const auto n = input.size();
71 auto expected = std::vector<char>(n);
72 QCOMPARE(
73 static_cast<const void*>(expected.data() + n),
74 static_cast<const void*>(F(encoded, expected.data())));
75 QCOMPARE(
76 std::string_view(output.data(), n),
77 std::string_view(expected.data(), n));
78}
79
80#endif
constexpr std::size_t output_size(std::size_t n)
Calculates the size of the buffer required for the output, rounding up.
Definition cmd.hpp:48
constexpr T round_to_block_size(T n)
Rounds an integral value to a given block size, always towards infinity.
Definition cmd.hpp:44
std::vector< char > rnd_vec(T *gen, std::size_t n)
Definition common.hpp:13
void check_decode(std::string_view input, std::string_view encoded, const std::vector< char > &output)
Definition common.hpp:66
void encode_test(auto *gen)
Definition common.hpp:30
auto gen_input(T *gen)
Definition common.hpp:22
void check_encode(std::string_view input, const std::vector< char > &output)
Definition common.hpp:56
void decode_test(auto *gen)
Definition common.hpp:42
void * B
Definition ooc.c:153
codex::refl::field_tuple_t< E > T
Definition soa.cpp:9