/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 | 12.9k | { |
32 | 12.9k | return (static_cast<Class *>(self)->*M)(self, args...); |
33 | 12.9k | } _ZN6MethodIFvPvPhjE12Random_ClassE6invokeIXadL_ZNS3_12random_bytesES0_S1_jEEEEvS0_S1_j 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 | 5.37k | { | 32 | 5.37k | return (static_cast<Class *>(self)->*M)(self, args...); | 33 | 5.37k | } |
_ZN6MethodIFPvS0_S0_jE12Memory_ClassE6invokeIXadL_ZNS2_7reallocES0_S0_jEEEES0_S0_S0_j Line | Count | Source | 31 | 18 | { | 32 | 18 | return (static_cast<Class *>(self)->*M)(self, args...); | 33 | 18 | } |
_ZN6MethodIFvPvS0_E12Memory_ClassE6invokeIXadL_ZNS2_7deallocES0_S0_EEEEvS0_S0_ Line | Count | Source | 31 | 5.39k | { | 32 | 5.39k | return (static_cast<Class *>(self)->*M)(self, args...); | 33 | 5.39k | } |
Unexecuted instantiation: _ZN6MethodIFiPv6SocketE13Network_ClassE6invokeIXadL_ZNS3_5closeES0_S1_EEEEiS0_S1_ Unexecuted instantiation: _ZN6MethodIF6SocketPvS0_E13Network_ClassE6invokeIXadL_ZNS3_6acceptES1_S0_EEEES0_S1_S0_ Unexecuted instantiation: _ZN6MethodIFiPv6SocketPK12Network_AddrE13Network_ClassE6invokeIXadL_ZNS6_4bindES0_S1_S4_EEEEiS0_S1_S4_ Unexecuted instantiation: _ZN6MethodIFiPv6SocketiE13Network_ClassE6invokeIXadL_ZNS3_6listenES0_S1_iEEEEiS0_S1_i Unexecuted instantiation: _ZN6MethodIFiPv6SocketPK12Network_AddrE13Network_ClassE6invokeIXadL_ZNS6_7connectES0_S1_S4_EEEEiS0_S1_S4_ Unexecuted instantiation: _ZN6MethodIFiPv6SocketE13Network_ClassE6invokeIXadL_ZNS3_7recvbufES0_S1_EEEEiS0_S1_ Unexecuted instantiation: _ZN6MethodIFiPv6SocketPhmE13Network_ClassE6invokeIXadL_ZNS4_4recvES0_S1_S2_mEEEEiS0_S1_S2_m Unexecuted instantiation: _ZN6MethodIFiPv6SocketPhmP12Network_AddrE13Network_ClassE6invokeIXadL_ZNS6_8recvfromES0_S1_S2_mS4_EEEEiS0_S1_S2_mS4_ Unexecuted instantiation: _ZN6MethodIFiPv6SocketPKhmE13Network_ClassE6invokeIXadL_ZNS5_4sendES0_S1_S3_mEEEEiS0_S1_S3_m Unexecuted instantiation: _ZN6MethodIFiPv6SocketPKhmPK12Network_AddrE13Network_ClassE6invokeIXadL_ZNS8_6sendtoES0_S1_S3_mS6_EEEEiS0_S1_S3_mS6_ Unexecuted instantiation: _ZN6MethodIF6SocketPviiiE13Network_ClassE6invokeIXadL_ZNS3_6socketES1_iiiEEEES0_S1_iii Unexecuted instantiation: _ZN6MethodIFiPv6SocketbE13Network_ClassE6invokeIXadL_ZNS3_15socket_nonblockES0_S1_bEEEEiS0_S1_b Unexecuted instantiation: _ZN6MethodIFiPv6SocketiiS0_PmE13Network_ClassE6invokeIXadL_ZNS4_10getsockoptES0_S1_iiS0_S2_EEEEiS0_S1_iiS0_S2_ Unexecuted instantiation: _ZN6MethodIFiPv6SocketiiPKvmE13Network_ClassE6invokeIXadL_ZNS5_10setsockoptES0_S1_iiS3_mEEEEiS0_S1_iiS3_m Unexecuted instantiation: _ZN6MethodIFiPvPK10Tox_MemoryPKciiPP12Network_AddrE13Network_ClassE6invokeIXadL_ZNSA_11getaddrinfoES0_S3_S5_iiS8_EEEEiS0_S3_S5_iiS8_ Unexecuted instantiation: _ZN6MethodIFiPvPK10Tox_MemoryP12Network_AddrE13Network_ClassE6invokeIXadL_ZNS7_12freeaddrinfoES0_S3_S5_EEEEiS0_S3_S5_ |
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 | 398 | { |
74 | 398 | 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 | 398 | return ptr; |
79 | 398 | } |
80 | | |
81 | | #define REQUIRE_NOT_NULL(ptr) require_not_null(__FILE__, __LINE__, ptr) |
82 | | |
83 | | #endif // C_TOXCORE_TOXCORE_TEST_UTIL_H |