Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b480f5b1115dbcb4be8841b140a1c4915a878494
[simgrid.git] / src / xbt / config_test.cpp
1 /* Copyright (c) 2004-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 <xbt/config.hpp>
7
8 #include "simgrid/Exception.hpp"
9 #include <string>
10 #include <xbt/log.h>
11
12 #include "catch.hpp"
13
14 XBT_PUBLIC_DATA simgrid::config::Config* simgrid_config;
15
16 static void make_set()
17 {
18   XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
19   xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
20   simgrid_config = nullptr;
21   simgrid::config::declare_flag<int>("speed", "description", 0);
22   simgrid::config::declare_flag<std::string>("peername", "description", "");
23   simgrid::config::declare_flag<std::string>("user", "description", "");
24 }
25
26 TEST_CASE("xbt::config: Configuration support", "config")
27 {
28   SECTION("Alloc and free a config set")
29   {
30     auto temp = simgrid_config;
31     make_set();
32     INFO("Alloc and free a config set");
33     simgrid::config::set_parse("peername:veloce user:bidule");
34     simgrid::config::finalize();
35     simgrid_config = temp;
36   }
37
38   SECTION("Data retrieving tests")
39   {
40     auto temp = simgrid_config;
41     make_set();
42
43     INFO("Get a single value");
44     /* get_single_value */
45     simgrid::config::set_parse("peername:toto:42 speed:42");
46     int ival = simgrid::config::get_value<int>("speed");
47     REQUIRE(ival == 42); // Unexpected value for speed
48
49     INFO("Access to a non-existent entry");
50
51     REQUIRE_THROWS_AS(simgrid::config::set_parse("color:blue"), std::out_of_range);
52
53     simgrid::config::finalize();
54     simgrid_config = temp;
55   }
56
57   SECTION("C++ flags")
58   {
59     auto temp = simgrid_config;
60     make_set();
61     INFO("C++ declaration of flags");
62
63     simgrid::config::Flag<int> int_flag("int", "", 0);
64     simgrid::config::Flag<std::string> string_flag("string", "", "foo");
65     simgrid::config::Flag<double> double_flag("double", "", 0.32);
66     simgrid::config::Flag<bool> bool_flag1("bool1", "", false);
67     simgrid::config::Flag<bool> bool_flag2("bool2", "", true);
68
69     INFO("Parse values");
70     simgrid::config::set_parse("int:42 string:bar double:8.0 bool1:true bool2:false");
71     REQUIRE(int_flag == 42);       // Check int flag
72     REQUIRE(string_flag == "bar"); // Check string flag
73     REQUIRE(double_flag == 8.0);   // Check double flag
74     REQUIRE(bool_flag1);           // Check bool1 flag
75     REQUIRE(not bool_flag2);       // Check bool2 flag
76
77     simgrid::config::finalize();
78     simgrid_config = temp;
79   }
80 }