Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Simplifications.
[simgrid.git] / src / xbt / config_test.cpp
1 /* Copyright (c) 2004-2019. 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
29   SECTION("Alloc and free a config set")
30   {
31     auto temp = simgrid_config;
32     make_set();
33     INFO("Alloc and free a config set");
34     simgrid::config::set_parse("peername:veloce user:bidule");
35     simgrid::config::finalize();
36     simgrid_config = temp;
37   }
38
39   SECTION("Data retrieving tests")
40   {
41     auto temp = simgrid_config;
42     make_set();
43
44     INFO("Get a single value");
45     /* get_single_value */
46     simgrid::config::set_parse("peername:toto:42 speed:42");
47     int ival = simgrid::config::get_value<int>("speed");
48     REQUIRE(ival == 42); // Unexpected value for speed
49
50     INFO("Access to a non-existant entry");
51
52     REQUIRE_THROWS_MATCHES(
53         simgrid::config::set_parse("color:blue"), xbt_ex,
54         Catch::Matchers::Predicate<xbt_ex>([](xbt_ex const& e) { return e.category == not_found_error; },
55                                            "category not_found_error"));
56
57     simgrid::config::finalize();
58     simgrid_config = temp;
59   }
60
61   SECTION("C++ flags")
62   {
63     auto temp = simgrid_config;
64     make_set();
65     INFO("C++ declaration of flags");
66
67     simgrid::config::Flag<int> int_flag("int", "", 0);
68     simgrid::config::Flag<std::string> string_flag("string", "", "foo");
69     simgrid::config::Flag<double> double_flag("double", "", 0.32);
70     simgrid::config::Flag<bool> bool_flag1("bool1", "", false);
71     simgrid::config::Flag<bool> bool_flag2("bool2", "", true);
72
73     INFO("Parse values");
74     simgrid::config::set_parse("int:42 string:bar double:8.0 bool1:true bool2:false");
75     REQUIRE(int_flag == 42);       // Check int flag
76     REQUIRE(string_flag == "bar"); // Check string flag
77     REQUIRE(double_flag == 8.0);   // Check double flag
78     REQUIRE(bool_flag1);           // Check bool1 flag
79     REQUIRE(not bool_flag2);       // Check bool2 flag
80
81     simgrid::config::finalize();
82     simgrid_config = temp;
83   }
84 }