Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Enum class in teshsuite/mc/random-bug/.
[simgrid.git] / teshsuite / mc / random-bug / random-bug.cpp
1 /* Copyright (c) 2014-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <cstring>
7 #include <simgrid/modelchecker.h>
8 #include <simgrid/s4u.hpp>
9 #include <xbt/log.h>
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(random_bug, "For this example");
12
13 enum class Behavior { ABORT, ASSERT, PRINTF } behavior;
14
15 /** A fake application with a bug occurring for some random values */
16 static void app()
17 {
18   int x = MC_random(0, 5);
19   int y = MC_random(0, 5);
20
21   if (behavior == Behavior::ASSERT) {
22     MC_assert(x != 3 || y != 4);
23   } else if (behavior == Behavior::PRINTF) {
24     if (x == 3 && y == 4)
25       XBT_ERROR("Error reached");
26   } else { // behavior == Behavior::ABORT
27     abort();
28   }
29 }
30
31 /** Main function */
32 int main(int argc, char* argv[])
33 {
34   simgrid::s4u::Engine e(&argc, argv);
35   xbt_assert(argc == 3, "Usage: random-bug raise|assert <platformfile>");
36   if (strcmp(argv[1], "abort") == 0) {
37     XBT_INFO("Behavior: abort");
38     behavior = Behavior::ABORT;
39   } else if (strcmp(argv[1], "assert") == 0) {
40     XBT_INFO("Behavior: assert");
41     behavior = Behavior::ASSERT;
42   } else if (strcmp(argv[1], "printf") == 0) {
43     XBT_INFO("Behavior: printf");
44     behavior = Behavior::PRINTF;
45   } else {
46     xbt_die("Please use either 'abort', 'assert' or 'printf' as first parameter, to specify what to do when the error "
47             "is found.");
48   }
49
50   e.load_platform(argv[2]);
51
52   simgrid::s4u::Actor::create("app", simgrid::s4u::Host::by_name("Fafard"), &app);
53   e.run();
54 }