Debian Codegraph @

libs/config/src/CConfigFileMemory_unittest.cpp

raw
1 /* +------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | https://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2026, Individual contributors, see AUTHORS file |
6 | See: https://www.mrpt.org/Authors - All rights reserved. |
7 | Released under BSD License. See: https://www.mrpt.org/License |
8 +------------------------------------------------------------------------+ */
9
10 #include <gtest/gtest.h>
11 #include <mrpt/config/CConfigFile.h>
12 #include <mrpt/config/CConfigFileMemory.h>
13 #include <mrpt/system/filesystem.h>
14
15 #include <cstdlib>
16 #include <fstream>
17
18 TEST(CConfigFileMemory, readwrite)
19 {
20 const std::string a = "check", b = "test", c = "final //comments";
21 mrpt::config::CConfigFileMemory first;
22 first.write(a, b, c);
23 EXPECT_STREQ("final", first.read_string(a, b, b).c_str());
24 }
25
26 TEST(CConfigFileMemory, Sections)
27 {
28 mrpt::config::CConfigFileMemory second;
29 second.write("one", "name", "val");
30 second.write("two", "names", "value");
31
32 const std::vector<std::string> sections = second.sections();
33 EXPECT_EQ(2U, sections.size());
34 if (sections.size() == 2)
35 { // avoid potential crash if fails
36 EXPECT_STREQ("one", sections[0].c_str());
37 EXPECT_STREQ("two", sections[1].c_str());
38 }
39 }
40
41 TEST(CConfigFileMemory, Names)
42 {
43 mrpt::config::CConfigFileMemory third;
44 third.write("sec", "name", "val");
45 third.write("sec", "names", "value");
46
47 const std::vector<std::string> names = third.keys("sec");
48 EXPECT_EQ(2U, names.size());
49 if (names.size() == 2)
50 { // avoid potential crash if fails
51 EXPECT_STREQ("name", names[0].c_str());
52 EXPECT_STREQ("names", names[1].c_str());
53 }
54
55 EXPECT_TRUE(third.sectionExists("sec"));
56 EXPECT_TRUE(third.sectionExists("SEC"));
57 EXPECT_FALSE(third.sectionExists("SECXX"));
58
59 EXPECT_TRUE(third.keyExists("sec", "name"));
60 EXPECT_TRUE(third.keyExists("SEC", "NAME"));
61 EXPECT_FALSE(third.keyExists("SEC", "NAMEX"));
62 }
63
64 TEST(CConfigFileMemory, setFromString)
65 {
66 const std::string sampleCfgTxt =
67 "# example config file from std::string\n"
68 "[test]\n"
69 "key_num = 4\n"
70 "key_str = pepe\n";
71
72 mrpt::config::CConfigFileMemory cfg;
73 cfg.setContent(sampleCfgTxt);
74
75 EXPECT_EQ(cfg.read_int("test", "key_num", 0), 4);
76 EXPECT_EQ(cfg.read_string("test", "key_str", ""), std::string("pepe"));
77 }
78
79 // Being able of read
80 const std::string sampleCfgTxt =
81 "[test]\n"
82 "key_str = this is a \\\n"
83 "long value that can be \\\n"
84 "split into several lines \\\n"
85 "but read as a single line. \n";
86 ;
87 const std::string expectedStr = std::string(
88 "this is a long value that can be split into several lines but read as a "
89 "single line.");
90
91 TEST(CConfigFileMemory, readMultiLineStrings)
92 {
93 mrpt::config::CConfigFileMemory cfg;
94 cfg.setContent(sampleCfgTxt);
95
96 const std::string readStr = cfg.read_string("test", "key_str", "");
97 EXPECT_EQ(readStr, expectedStr);
98 }
99
100 TEST(CConfigFile, readMultiLineStrings)
101 {
102 const std::string tmpFile = mrpt::system::getTempFileName();
103 {
104 std::ofstream f;
105 f.open(tmpFile.c_str(), std::ofstream::out);
106 EXPECT_TRUE(f.is_open());
107 f << sampleCfgTxt;
108 f.close();
109 }
110
111 mrpt::config::CConfigFile cfg(tmpFile);
112
113 const std::string readStr = cfg.read_string("test", "key_str", "");
114 EXPECT_EQ(readStr, expectedStr);
115 }
116
117 TEST(CConfigFileMemory, parseVariables)
118 {
119 #ifdef _MSC_VER
120 _putenv_s("ENV_VAR_MULTIPLIER", "2");
121 #else
122 ::setenv("ENV_VAR_MULTIPLIER", "2", 1);
123 #endif
124
125 const std::string sampleCfgTxt2 =
126 "@define MAXSPEED 10\n"
127 "@define MAXOMEGA -30 \n"
128 "[test]\n"
129 "var1=5\n"
130 "var2=${MAXSPEED}\n"
131 "var3=${MAXOMEGA}\n"
132 "var4=$eval{5*MAXSPEED+MAXOMEGA}\n"
133 "var5 = $eval{ MAXSPEED - MAXOMEGA } \n"
134 "var6=$env{ENV_VAR_MULTIPLIER}\n"
135 "varstr1=MAXSPEED\n";
136 ;
137 mrpt::config::CConfigFileMemory cfg;
138 cfg.setContent(sampleCfgTxt2);
139
140 EXPECT_EQ(cfg.read_int("test", "var1", 0), 5);
141 EXPECT_EQ(cfg.read_int("test", "var2", 0), 10);
142 EXPECT_EQ(cfg.read_int("test", "var3", 0), -30);
143 EXPECT_NEAR(cfg.read_double("test", "var4", .0), 20.0, 1e-6);
144 EXPECT_NEAR(cfg.read_double("test", "var5", .0), 40.0, 1e-6);
145 EXPECT_NEAR(cfg.read_double("test", "var6", .0), 2.0, 1e-6);
146 EXPECT_EQ(cfg.read_string("test", "varstr1", ""), std::string("MAXSPEED"));
147 }