Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / teshsuite / mc / random-bug / random-bug.cpp
1 /* Copyright (c) 2014-2021. 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, SEGV };
14
15 Behavior behavior;
16
17 /** A fake application with a bug occurring for some random values */
18 static void app()
19 {
20   int x = MC_random(0, 5);
21   int y = MC_random(0, 5);
22   XBT_DEBUG("got %d %d", x, y);
23
24   if (behavior == Behavior::ASSERT) {
25     MC_assert(x != 3 || y != 4);
26   } else if (behavior == Behavior::PRINTF) {
27     if (x == 3 && y == 4)
28       XBT_ERROR("Error reached");
29   } else if (behavior == Behavior::ABORT) {
30     if (x == 3 && y == 4)
31       abort();
32   } else if (behavior == Behavior::SEGV) {
33     int* A = 0;
34     if (x == 3 && y == 4)
35       *A = 1;
36   } else {
37     DIE_IMPOSSIBLE;
38   }
39 }
40
41 /** Main function */
42 int main(int argc, char* argv[])
43 {
44   simgrid::s4u::Engine e(&argc, argv);
45   xbt_assert(argc == 3, "Usage: random-bug abort|assert|printf|segv <platformfile>");
46   if (strcmp(argv[1], "abort") == 0) {
47     XBT_INFO("Behavior: abort");
48     behavior = Behavior::ABORT;
49   } else if (strcmp(argv[1], "assert") == 0) {
50     XBT_INFO("Behavior: assert");
51     behavior = Behavior::ASSERT;
52   } else if (strcmp(argv[1], "printf") == 0) {
53     XBT_INFO("Behavior: printf");
54     behavior = Behavior::PRINTF;
55   } else if (strcmp(argv[1], "segv") == 0) {
56     XBT_INFO("Behavior: segv");
57     behavior = Behavior::SEGV;
58   } else {
59     xbt_die("Please use either 'abort', 'assert', 'printf', or 'segv' as first parameter,"
60             " to specify what to do when the error is found.");
61   }
62
63   e.load_platform(argv[2]);
64
65   simgrid::s4u::Actor::create("app", simgrid::s4u::Host::by_name("Fafard"), &app);
66   e.run();
67 }