RPS: Some fixes
[oweals/gnunet.git] / src / rps / rps-test_util.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C)
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @file rps/rps-test_util.h
23  * @brief Some utils faciliating the view into the internals for the sampler
24  *        needed for evaluation
25  * @author Julius Bünger
26  */
27
28 #ifndef RPS_TEST_UTIL_H
29 #define RPS_TEST_UTIL_H
30
31 #define TO_FILE 0
32
33
34 char *
35 auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key);
36
37 struct GNUNET_CRYPTO_AuthKey
38 string_to_auth_key (const char *str);
39
40
41 /**
42  * @brief Get file handle
43  *
44  * If necessary, create file handle and store it with the other file handles.
45  *
46  * @param name Name of the file
47  *
48  * @return File handle
49  */
50 struct GNUNET_DISK_FileHandle *
51 get_file_handle (const char *name);
52
53 /**
54  * @brief Close all files that were opened with #get_file_handle
55  *
56  * @return Success of iterating over files
57  */
58 int
59 close_all_files ();
60
61 /**
62  * This function is used to facilitate writing important information to disk
63  */
64 #ifdef TO_FILE
65 #define to_file(file_name, ...) do { \
66     char tmp_buf[512] = "";\
67     int size;\
68     if (NULL == file_name) break; \
69     size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
70     if (0 > size)\
71     {\
72       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
73            "Failed to create tmp_buf\n");\
74       break;\
75     }\
76     (void) strncat(tmp_buf,"\n",512);\
77     GNUNET_DISK_file_write (get_file_handle (file_name),\
78                             tmp_buf,\
79                             strnlen (tmp_buf, 512));\
80   } while (0);
81
82
83 #define to_file_w_len(file_name, len, ...) do {char tmp_buf[len];\
84     int size;\
85     memset (tmp_buf, 0, len);\
86     size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
87     if (0 > size)\
88     {\
89       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
90            "Failed to create tmp_buf\n");\
91       break;\
92     }\
93     (void) strncat(tmp_buf,"\n",len);\
94     GNUNET_DISK_file_write (get_file_handle (file_name),\
95                             tmp_buf,\
96                             strnlen (tmp_buf, len));\
97   } while (0);
98 #else /* TO_FILE */
99 #  define to_file(file_name, ...)
100 #  define to_file_w_len(file_name, len, ...)
101 #endif /* TO_FILE */
102
103 char *
104 store_prefix_file_name (const struct GNUNET_PeerIdentity *peer,
105                         const char *prefix);
106
107 void
108 to_file_raw (const char *file_name, const char *buf, size_t size_buf);
109
110 void
111 to_file_raw_unaligned (const char *file_name,
112                        const char *buf,
113                        size_t size_buf,
114                        unsigned bits_needed);
115
116
117 /**
118  * @brief Factorial
119  *
120  * @param x Number of which to compute the factorial
121  *
122  * @return Factorial of @a x
123  */
124 uint32_t fac (uint32_t x);
125
126
127 /**
128  * @brief Binomial coefficient (n choose k)
129  *
130  * @param n
131  * @param k
132  *
133  * @return Binomial coefficient of @a n and @a k
134  */
135 uint32_t binom (uint32_t n, uint32_t k);
136
137 #endif /* RPS_TEST_UTIL_H */
138 /* end of gnunet-service-rps.c */