Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[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 };
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
23   if (behavior == Behavior::ASSERT) {
24     MC_assert(x != 3 || y != 4);
25   } else if (behavior == Behavior::PRINTF) {
26     if (x == 3 && y == 4)
27       XBT_ERROR("Error reached");
28   } else { // behavior == Behavior::ABORT
29     abort();
30   }
31 }
32
33 /** Main function */
34 int main(int argc, char* argv[])
35 {
36   simgrid::s4u::Engine e(&argc, argv);
37   xbt_assert(argc == 3, "Usage: random-bug raise|assert <platformfile>");
38   if (strcmp(argv[1], "abort") == 0) {
39     XBT_INFO("Behavior: abort");
40     behavior = Behavior::ABORT;
41   } else if (strcmp(argv[1], "assert") == 0) {
42     XBT_INFO("Behavior: assert");
43     behavior = Behavior::ASSERT;
44   } else if (strcmp(argv[1], "printf") == 0) {
45     XBT_INFO("Behavior: printf");
46     behavior = Behavior::PRINTF;
47   } else {
48     xbt_die("Please use either 'abort', 'assert' or 'printf' as first parameter, to specify what to do when the error "
49             "is found.");
50   }
51
52   e.load_platform(argv[2]);
53
54   simgrid::s4u::Actor::create("app", simgrid::s4u::Host::by_name("Fafard"), &app);
55   e.run();
56 }