Logo AND Algorithmique Numérique Distribuée

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