diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-06-23 12:32:41 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-06-24 20:17:53 +0000 |
commit | 29b2dd6315ffe29cd212c4d5a846479f56b33d52 (patch) | |
tree | 881ba80e88fc461a688c0416abb10956fe14e247 /testing | |
parent | 5c2600d87bd000b32b2a37c5a74275912ddd5328 (diff) |
Use clang-format for C++ code.
`clang-format -style='{BasedOnStyle: Google, ColumnLimit: 100}'`
Diffstat (limited to 'testing')
-rw-r--r-- | testing/random_testing.cc | 77 |
1 files changed, 39 insertions, 38 deletions
diff --git a/testing/random_testing.cc b/testing/random_testing.cc index ecad07d3..b896e694 100644 --- a/testing/random_testing.cc +++ b/testing/random_testing.cc | |||
@@ -15,7 +15,7 @@ | |||
15 | namespace { | 15 | namespace { |
16 | 16 | ||
17 | // Whether to write log messages when handling callbacks. | 17 | // Whether to write log messages when handling callbacks. |
18 | constexpr bool LOG_CALLBACKS = 0; | 18 | constexpr bool LOG_CALLBACKS = false; |
19 | 19 | ||
20 | // Number of participants in the test run. | 20 | // Number of participants in the test run. |
21 | constexpr uint32_t NUM_TOXES = 10; | 21 | constexpr uint32_t NUM_TOXES = 10; |
@@ -79,7 +79,7 @@ struct Action { | |||
79 | uint32_t weight; | 79 | uint32_t weight; |
80 | char const *title; | 80 | char const *title; |
81 | bool (*can)(Local_State const &state); | 81 | bool (*can)(Local_State const &state); |
82 | void (*run)(Local_State &state, Random &rnd, std::mt19937 &rng); | 82 | void (*run)(Local_State *state, Random *rnd, std::mt19937 *rng); |
83 | }; | 83 | }; |
84 | 84 | ||
85 | std::vector<size_t> get_action_weights(std::vector<Action> const &actions) { | 85 | std::vector<size_t> get_action_weights(std::vector<Action> const &actions) { |
@@ -107,7 +107,7 @@ struct Global_State : std::vector<Local_State> { | |||
107 | : actions_(actions), rnd_(actions), action_counter_(actions.size()) {} | 107 | : actions_(actions), rnd_(actions), action_counter_(actions.size()) {} |
108 | 108 | ||
109 | Action const &action(size_t id) const { return actions_.at(id); } | 109 | Action const &action(size_t id) const { return actions_.at(id); } |
110 | Random &rnd() { return rnd_; } | 110 | Random *rnd() { return &rnd_; } |
111 | std::vector<unsigned> &action_counter() { return action_counter_; } | 111 | std::vector<unsigned> &action_counter() { return action_counter_; } |
112 | 112 | ||
113 | private: | 113 | private: |
@@ -147,7 +147,8 @@ void handle_conference_message(Tox *tox, uint32_t conference_number, uint32_t pe | |||
147 | Local_State *state = static_cast<Local_State *>(user_data); | 147 | Local_State *state = static_cast<Local_State *>(user_data); |
148 | 148 | ||
149 | if (LOG_CALLBACKS) { | 149 | if (LOG_CALLBACKS) { |
150 | std::printf("Tox #%u received a message of length %u\n", state->id(), (unsigned)length); | 150 | std::printf("Tox #%u received a message of length %u\n", state->id(), |
151 | static_cast<unsigned>(length)); | ||
151 | } | 152 | } |
152 | } | 153 | } |
153 | 154 | ||
@@ -227,18 +228,18 @@ bool all_connected(Global_State const &toxes) { | |||
227 | }); | 228 | }); |
228 | } | 229 | } |
229 | 230 | ||
230 | bool bootstrap_toxes(Global_State &toxes) { | 231 | bool bootstrap_toxes(Global_State *toxes) { |
231 | std::printf("Waiting for %u iterations for all friends to come online\n", | 232 | std::printf("Waiting for %u iterations for all friends to come online\n", |
232 | MAX_BOOTSTRAP_ITERATIONS); | 233 | MAX_BOOTSTRAP_ITERATIONS); |
233 | 234 | ||
234 | for (uint32_t i = 0; i < MAX_BOOTSTRAP_ITERATIONS; i++) { | 235 | for (uint32_t i = 0; i < MAX_BOOTSTRAP_ITERATIONS; i++) { |
235 | c_sleep(tox_iteration_interval(toxes.front().tox())); | 236 | c_sleep(tox_iteration_interval(toxes->front().tox())); |
236 | 237 | ||
237 | for (Local_State &state : toxes) { | 238 | for (Local_State &state : *toxes) { |
238 | tox_iterate(state.tox(), &state); | 239 | tox_iterate(state.tox(), &state); |
239 | } | 240 | } |
240 | 241 | ||
241 | if (all_connected(toxes)) { | 242 | if (all_connected(*toxes)) { |
242 | std::printf("Took %u iterations\n", i); | 243 | std::printf("Took %u iterations\n", i); |
243 | return true; | 244 | return true; |
244 | } | 245 | } |
@@ -247,25 +248,25 @@ bool bootstrap_toxes(Global_State &toxes) { | |||
247 | return false; | 248 | return false; |
248 | } | 249 | } |
249 | 250 | ||
250 | bool execute_random_action(Global_State &toxes, std::mt19937 &rng) { | 251 | bool execute_random_action(Global_State *toxes, std::mt19937 *rng) { |
251 | // First, choose a random actor. | 252 | // First, choose a random actor. |
252 | Local_State &actor = toxes.at(toxes.rnd().tox_selector(rng)); | 253 | Local_State &actor = toxes->at(toxes->rnd()->tox_selector(*rng)); |
253 | size_t const action_id = toxes.rnd().action_selector(rng); | 254 | size_t const action_id = toxes->rnd()->action_selector(*rng); |
254 | Action const &action = toxes.action(action_id); | 255 | Action const &action = toxes->action(action_id); |
255 | if (!action.can(actor)) { | 256 | if (!action.can(actor)) { |
256 | return false; | 257 | return false; |
257 | } | 258 | } |
258 | 259 | ||
259 | std::printf("Tox #%u %s", actor.id(), action.title); | 260 | std::printf("Tox #%u %s", actor.id(), action.title); |
260 | action.run(actor, toxes.rnd(), rng); | 261 | action.run(&actor, toxes->rnd(), rng); |
261 | std::printf("\n"); | 262 | std::printf("\n"); |
262 | 263 | ||
263 | toxes.action_counter().at(action_id)++; | 264 | toxes->action_counter().at(action_id)++; |
264 | 265 | ||
265 | return true; | 266 | return true; |
266 | } | 267 | } |
267 | 268 | ||
268 | bool attempt_action(Global_State &toxes, std::mt19937 &rng) { | 269 | bool attempt_action(Global_State *toxes, std::mt19937 *rng) { |
269 | for (uint32_t i = 0; i < MAX_ACTION_ATTEMPTS; i++) { | 270 | for (uint32_t i = 0; i < MAX_ACTION_ATTEMPTS; i++) { |
270 | if (execute_random_action(toxes, rng)) { | 271 | if (execute_random_action(toxes, rng)) { |
271 | return true; | 272 | return true; |
@@ -284,9 +285,9 @@ int main() { | |||
284 | [](Local_State const &state) { | 285 | [](Local_State const &state) { |
285 | return tox_conference_get_chatlist_size(state.tox()) < MAX_CONFERENCES_PER_USER; | 286 | return tox_conference_get_chatlist_size(state.tox()) < MAX_CONFERENCES_PER_USER; |
286 | }, | 287 | }, |
287 | [](Local_State &state, Random &rnd, std::mt19937 &rng) { | 288 | [](Local_State *state, Random *rnd, std::mt19937 *rng) { |
288 | TOX_ERR_CONFERENCE_NEW err; | 289 | TOX_ERR_CONFERENCE_NEW err; |
289 | tox_conference_new(state.tox(), &err); | 290 | tox_conference_new(state->tox(), &err); |
290 | assert(err == TOX_ERR_CONFERENCE_NEW_OK); | 291 | assert(err == TOX_ERR_CONFERENCE_NEW_OK); |
291 | }, | 292 | }, |
292 | }, | 293 | }, |
@@ -295,12 +296,12 @@ int main() { | |||
295 | [](Local_State const &state) { | 296 | [](Local_State const &state) { |
296 | return tox_conference_get_chatlist_size(state.tox()) != 0; | 297 | return tox_conference_get_chatlist_size(state.tox()) != 0; |
297 | }, | 298 | }, |
298 | [](Local_State &state, Random &rnd, std::mt19937 &rng) { | 299 | [](Local_State *state, Random *rnd, std::mt19937 *rng) { |
299 | TOX_ERR_CONFERENCE_INVITE err; | 300 | TOX_ERR_CONFERENCE_INVITE err; |
300 | tox_conference_invite(state.tox(), rnd.friend_selector(rng), | 301 | tox_conference_invite( |
301 | state.next_invite % tox_conference_get_chatlist_size(state.tox()), | 302 | state->tox(), rnd->friend_selector(*rng), |
302 | &err); | 303 | state->next_invite % tox_conference_get_chatlist_size(state->tox()), &err); |
303 | state.next_invite++; | 304 | state->next_invite++; |
304 | assert(err == TOX_ERR_CONFERENCE_INVITE_OK); | 305 | assert(err == TOX_ERR_CONFERENCE_INVITE_OK); |
305 | }, | 306 | }, |
306 | }, | 307 | }, |
@@ -309,9 +310,9 @@ int main() { | |||
309 | [](Local_State const &state) { | 310 | [](Local_State const &state) { |
310 | return tox_conference_get_chatlist_size(state.tox()) != 0; | 311 | return tox_conference_get_chatlist_size(state.tox()) != 0; |
311 | }, | 312 | }, |
312 | [](Local_State &state, Random &rnd, std::mt19937 &rng) { | 313 | [](Local_State *state, Random *rnd, std::mt19937 *rng) { |
313 | TOX_ERR_CONFERENCE_DELETE err; | 314 | TOX_ERR_CONFERENCE_DELETE err; |
314 | tox_conference_delete(state.tox(), tox_conference_get_chatlist_size(state.tox()) - 1, | 315 | tox_conference_delete(state->tox(), tox_conference_get_chatlist_size(state->tox()) - 1, |
315 | &err); | 316 | &err); |
316 | assert(err == TOX_ERR_CONFERENCE_DELETE_OK); | 317 | assert(err == TOX_ERR_CONFERENCE_DELETE_OK); |
317 | }, | 318 | }, |
@@ -321,18 +322,18 @@ int main() { | |||
321 | [](Local_State const &state) { | 322 | [](Local_State const &state) { |
322 | return tox_conference_get_chatlist_size(state.tox()) != 0; | 323 | return tox_conference_get_chatlist_size(state.tox()) != 0; |
323 | }, | 324 | }, |
324 | [](Local_State &state, Random &rnd, std::mt19937 &rng) { | 325 | [](Local_State *state, Random *rnd, std::mt19937 *rng) { |
325 | std::vector<uint8_t> message(rnd.message_length_selector(rng)); | 326 | std::vector<uint8_t> message(rnd->message_length_selector(*rng)); |
326 | for (uint8_t &byte : message) { | 327 | for (uint8_t &byte : message) { |
327 | byte = rnd.byte_selector(rng); | 328 | byte = rnd->byte_selector(*rng); |
328 | } | 329 | } |
329 | 330 | ||
330 | TOX_ERR_CONFERENCE_SEND_MESSAGE err; | 331 | TOX_ERR_CONFERENCE_SEND_MESSAGE err; |
331 | tox_conference_send_message( | 332 | tox_conference_send_message( |
332 | state.tox(), tox_conference_get_chatlist_size(state.tox()) - 1, | 333 | state->tox(), tox_conference_get_chatlist_size(state->tox()) - 1, |
333 | TOX_MESSAGE_TYPE_NORMAL, message.data(), message.size(), &err); | 334 | TOX_MESSAGE_TYPE_NORMAL, message.data(), message.size(), &err); |
334 | if (err == TOX_ERR_CONFERENCE_SEND_MESSAGE_OK) { | 335 | if (err == TOX_ERR_CONFERENCE_SEND_MESSAGE_OK) { |
335 | printf(" (OK, length = %u)", (unsigned)message.size()); | 336 | printf(" (OK, length = %u)", static_cast<unsigned>(message.size())); |
336 | } else { | 337 | } else { |
337 | printf(" (FAILED: %u)", err); | 338 | printf(" (FAILED: %u)", err); |
338 | } | 339 | } |
@@ -340,24 +341,24 @@ int main() { | |||
340 | }, | 341 | }, |
341 | { | 342 | { |
342 | 10, "changes their name", [](Local_State const &state) { return true; }, | 343 | 10, "changes their name", [](Local_State const &state) { return true; }, |
343 | [](Local_State &state, Random &rnd, std::mt19937 &rng) { | 344 | [](Local_State *state, Random *rnd, std::mt19937 *rng) { |
344 | std::vector<uint8_t> name(rnd.name_length_selector(rng)); | 345 | std::vector<uint8_t> name(rnd->name_length_selector(*rng)); |
345 | for (uint8_t &byte : name) { | 346 | for (uint8_t &byte : name) { |
346 | byte = rnd.byte_selector(rng); | 347 | byte = rnd->byte_selector(*rng); |
347 | } | 348 | } |
348 | 349 | ||
349 | TOX_ERR_SET_INFO err; | 350 | TOX_ERR_SET_INFO err; |
350 | tox_self_set_name(state.tox(), name.data(), name.size(), &err); | 351 | tox_self_set_name(state->tox(), name.data(), name.size(), &err); |
351 | assert(err == TOX_ERR_SET_INFO_OK); | 352 | assert(err == TOX_ERR_SET_INFO_OK); |
352 | 353 | ||
353 | printf(" (length = %u)", (unsigned)name.size()); | 354 | printf(" (length = %u)", static_cast<unsigned>(name.size())); |
354 | }, | 355 | }, |
355 | }, | 356 | }, |
356 | { | 357 | { |
357 | 10, "sets their name to empty", [](Local_State const &state) { return true; }, | 358 | 10, "sets their name to empty", [](Local_State const &state) { return true; }, |
358 | [](Local_State &state, Random &rnd, std::mt19937 &rng) { | 359 | [](Local_State *state, Random *rnd, std::mt19937 *rng) { |
359 | TOX_ERR_SET_INFO err; | 360 | TOX_ERR_SET_INFO err; |
360 | tox_self_set_name(state.tox(), nullptr, 0, &err); | 361 | tox_self_set_name(state->tox(), nullptr, 0, &err); |
361 | assert(err == TOX_ERR_SET_INFO_OK); | 362 | assert(err == TOX_ERR_SET_INFO_OK); |
362 | }, | 363 | }, |
363 | }, | 364 | }, |
@@ -368,12 +369,12 @@ int main() { | |||
368 | std::mt19937 rng; | 369 | std::mt19937 rng; |
369 | uint32_t action_number; | 370 | uint32_t action_number; |
370 | for (action_number = 0; action_number < MAX_ACTIONS; action_number++) { | 371 | for (action_number = 0; action_number < MAX_ACTIONS; action_number++) { |
371 | if (!all_connected(toxes) && !bootstrap_toxes(toxes)) { | 372 | if (!all_connected(toxes) && !bootstrap_toxes(&toxes)) { |
372 | std::printf("Bootstrapping took too long; %u actions performed\n", action_number); | 373 | std::printf("Bootstrapping took too long; %u actions performed\n", action_number); |
373 | return EXIT_FAILURE; | 374 | return EXIT_FAILURE; |
374 | } | 375 | } |
375 | 376 | ||
376 | if (!attempt_action(toxes, rng)) { | 377 | if (!attempt_action(&toxes, &rng)) { |
377 | std::printf( | 378 | std::printf( |
378 | "System is stuck after %u actions: none of the toxes can perform an action anymore\n", | 379 | "System is stuck after %u actions: none of the toxes can perform an action anymore\n", |
379 | action_number); | 380 | action_number); |