/work/toxcore/test_util.hh
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef C_TOXCORE_TOXCORE_TEST_UTIL_H |
2 | | #define C_TOXCORE_TOXCORE_TEST_UTIL_H |
3 | | |
4 | | #include <algorithm> |
5 | | #include <array> |
6 | | #include <cstdio> |
7 | | #include <cstdlib> |
8 | | #include <memory> |
9 | | #include <type_traits> |
10 | | #include <vector> |
11 | | |
12 | | template <typename T, void (*Delete)(T *)> |
13 | | struct Function_Deleter { |
14 | | void operator()(T *ptr) const { Delete(ptr); } |
15 | | }; |
16 | | |
17 | | // No default deleter, because we want to catch when we forget to specialise this one. |
18 | | template <typename T> |
19 | | struct Deleter; |
20 | | |
21 | | template <typename T> |
22 | | using Ptr = std::unique_ptr<T, Deleter<T>>; |
23 | | |
24 | | template <typename Func, typename Class> |
25 | | struct Method; |
26 | | |
27 | | template <typename R, typename Class, typename... Args> |
28 | | struct Method<R(void *, Args...), Class> { |
29 | | template <R (Class::*M)(void *, Args...)> |
30 | | static R invoke(void *self, Args... args) |
31 | 3.23k | { |
32 | 3.23k | return (static_cast<Class *>(self)->*M)(self, args...); |
33 | 3.23k | } _ZN6MethodIFvPvPhmE12Random_ClassE6invokeIXadL_ZNS3_12random_bytesES0_S1_mEEEEvS0_S1_m Line | Count | Source | 31 | 2.16k | { | 32 | 2.16k | return (static_cast<Class *>(self)->*M)(self, args...); | 33 | 2.16k | } |
Unexecuted instantiation: _ZN6MethodIFjPvjE12Random_ClassE6invokeIXadL_ZNS2_14random_uniformES0_jEEEEjS0_j _ZN6MethodIFPvS0_jE12Memory_ClassE6invokeIXadL_ZNS2_6mallocES0_jEEEES0_S0_j Line | Count | Source | 31 | 9 | { | 32 | 9 | return (static_cast<Class *>(self)->*M)(self, args...); | 33 | 9 | } |
_ZN6MethodIFPvS0_jjE12Memory_ClassE6invokeIXadL_ZNS2_6callocES0_jjEEEES0_S0_jj Line | Count | Source | 31 | 471 | { | 32 | 471 | return (static_cast<Class *>(self)->*M)(self, args...); | 33 | 471 | } |
_ZN6MethodIFPvS0_S0_jE12Memory_ClassE6invokeIXadL_ZNS2_7reallocES0_S0_jEEEES0_S0_S0_j Line | Count | Source | 31 | 2 | { | 32 | 2 | return (static_cast<Class *>(self)->*M)(self, args...); | 33 | 2 | } |
_ZN6MethodIFvPvS0_E12Memory_ClassE6invokeIXadL_ZNS2_4freeES0_S0_EEEEvS0_S0_ Line | Count | Source | 31 | 586 | { | 32 | 586 | return (static_cast<Class *>(self)->*M)(self, args...); | 33 | 586 | } |
Unexecuted instantiation: _ZN6MethodIFiPviE13Network_ClassE6invokeIXadL_ZNS2_5closeES0_iEEEEiS0_i Unexecuted instantiation: _ZN6MethodIFiPviE13Network_ClassE6invokeIXadL_ZNS2_6acceptES0_iEEEEiS0_i Unexecuted instantiation: _ZN6MethodIFiPviPK12Network_AddrE13Network_ClassE6invokeIXadL_ZNS5_4bindES0_iS3_EEEEiS0_iS3_ Unexecuted instantiation: _ZN6MethodIFiPviiE13Network_ClassE6invokeIXadL_ZNS2_6listenES0_iiEEEEiS0_ii Unexecuted instantiation: _ZN6MethodIFiPviE13Network_ClassE6invokeIXadL_ZNS2_7recvbufES0_iEEEEiS0_i Unexecuted instantiation: _ZN6MethodIFiPviPhmE13Network_ClassE6invokeIXadL_ZNS3_4recvES0_iS1_mEEEEiS0_iS1_m Unexecuted instantiation: _ZN6MethodIFiPviPhmP12Network_AddrE13Network_ClassE6invokeIXadL_ZNS5_8recvfromES0_iS1_mS3_EEEEiS0_iS1_mS3_ Unexecuted instantiation: _ZN6MethodIFiPviPKhmE13Network_ClassE6invokeIXadL_ZNS4_4sendES0_iS2_mEEEEiS0_iS2_m Unexecuted instantiation: _ZN6MethodIFiPviPKhmPK12Network_AddrE13Network_ClassE6invokeIXadL_ZNS7_6sendtoES0_iS2_mS5_EEEEiS0_iS2_mS5_ Unexecuted instantiation: _ZN6MethodIFiPviiiE13Network_ClassE6invokeIXadL_ZNS2_6socketES0_iiiEEEEiS0_iii Unexecuted instantiation: _ZN6MethodIFiPvibE13Network_ClassE6invokeIXadL_ZNS2_15socket_nonblockES0_ibEEEEiS0_ib Unexecuted instantiation: _ZN6MethodIFiPviiiS0_PmE13Network_ClassE6invokeIXadL_ZNS3_10getsockoptES0_iiiS0_S1_EEEEiS0_iiiS0_S1_ Unexecuted instantiation: _ZN6MethodIFiPviiiPKvmE13Network_ClassE6invokeIXadL_ZNS4_10setsockoptES0_iiiS2_mEEEEiS0_iiiS2_m Unexecuted instantiation: _ZN6MethodIFiPviPP12Network_AddrE13Network_ClassE6invokeIXadL_ZNS5_11getaddrinfoES0_iS3_EEEEiS0_iS3_ Unexecuted instantiation: _ZN6MethodIFiPvP12Network_AddrE13Network_ClassE6invokeIXadL_ZNS4_12freeaddrinfoES0_S2_EEEEiS0_S2_ |
34 | | }; |
35 | | |
36 | | template <typename T, std::size_t N> |
37 | | std::array<T, N> to_array(T const (&arr)[N]) |
38 | 10 | { |
39 | 10 | std::array<T, N> stdarr; |
40 | 10 | std::copy(arr, arr + N, stdarr.begin()); |
41 | 10 | return stdarr; |
42 | 10 | } |
43 | | |
44 | | template <std::size_t N, typename T, typename... Args> |
45 | | auto array_of(T &&make, Args... args) |
46 | | { |
47 | | std::array<std::invoke_result_t<T, Args...>, N> arr; |
48 | | for (auto &elem : arr) { |
49 | | elem = make(args...); |
50 | | } |
51 | | return arr; |
52 | | } |
53 | | |
54 | | template <typename T, typename... Args> |
55 | | auto vector_of(std::size_t n, T &&make, Args... args) |
56 | | { |
57 | | std::vector<std::invoke_result_t<T, Args...>> vec; |
58 | | for (std::size_t i = 0; i < n; ++i) { |
59 | | vec.push_back(make(args...)); |
60 | | } |
61 | | return vec; |
62 | | } |
63 | | |
64 | | template <typename Container, typename Less> |
65 | | Container sorted(Container arr, Less less) |
66 | | { |
67 | | std::sort(arr.begin(), arr.end(), less); |
68 | | return arr; |
69 | | } |
70 | | |
71 | | template <typename T> |
72 | | T *require_not_null(const char *file, int line, T *ptr) |
73 | 247 | { |
74 | 247 | if (ptr == nullptr) { |
75 | 0 | std::fprintf(stderr, "unexpected null pointer at %s:%d\n", file, line); |
76 | 0 | std::exit(7); |
77 | 0 | } |
78 | 247 | return ptr; |
79 | 247 | } |
80 | | |
81 | | #define REQUIRE_NOT_NULL(ptr) require_not_null(__FILE__, __LINE__, ptr) |
82 | | |
83 | | #endif // C_TOXCORE_TOXCORE_TEST_UTIL_H |