MRPT  2.0.1
CConfigFileMemory_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, 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>
13 #include <mrpt/system/filesystem.h>
14 #include <cstdlib>
15 #include <fstream>
16 
18 {
19  const std::string a = "check", b = "test", c = "final //comments";
21  first.write(a, b, c);
22  EXPECT_STREQ("final", first.read_string(a, b, b).c_str());
23 }
24 
26 {
27  std::vector<std::string> sections;
29  second.write("one", "name", "val");
30  second.write("two", "names", "value");
31  second.getAllSections(sections);
32  EXPECT_EQ(2U, sections.size());
33  if (sections.size() == 2)
34  { // avoid potential crash if fails
35  EXPECT_STREQ("one", sections[0].c_str());
36  EXPECT_STREQ("two", sections[1].c_str());
37  }
38 }
39 
41 {
42  std::vector<std::string> names;
44  third.write("sec", "name", "val");
45  third.write("sec", "names", "value");
46  third.getAllKeys("sec", names);
47  EXPECT_EQ(2U, names.size());
48  if (names.size() == 2)
49  { // avoid potential crash if fails
50  EXPECT_STREQ("name", names[0].c_str());
51  EXPECT_STREQ("names", names[1].c_str());
52  }
53 
54  EXPECT_TRUE(third.sectionExists("sec"));
55  EXPECT_TRUE(third.sectionExists("SEC"));
56  EXPECT_FALSE(third.sectionExists("SECXX"));
57 
58  EXPECT_TRUE(third.keyExists("sec", "name"));
59  EXPECT_TRUE(third.keyExists("SEC", "NAME"));
60  EXPECT_FALSE(third.keyExists("SEC", "NAMEX"));
61 }
62 
63 TEST(CConfigFileMemory, setFromString)
64 {
65  const std::string sampleCfgTxt =
66  "# example config file from std::string\n"
67  "[test]\n"
68  "key_num = 4\n"
69  "key_str = pepe\n";
70 
73 
74  EXPECT_EQ(cfg.read_int("test", "key_num", 0), 4);
75  EXPECT_EQ(cfg.read_string("test", "key_str", ""), std::string("pepe"));
76 }
77 
78 // Being able of read
79 const std::string sampleCfgTxt =
80  "[test]\n"
81  "key_str = this is a \\\n"
82  "long value that can be \\\n"
83  "split into several lines \\\n"
84  "but read as a single line. \n";
85 ;
86 const std::string expectedStr = std::string(
87  "this is a long value that can be split into several lines but read as a "
88  "single line.");
89 
90 TEST(CConfigFileMemory, readMultiLineStrings)
91 {
94 
95  const std::string readStr = cfg.read_string("test", "key_str", "");
96  EXPECT_EQ(readStr, expectedStr);
97 }
98 
99 TEST(CConfigFile, readMultiLineStrings)
100 {
101  const std::string tmpFile = mrpt::system::getTempFileName();
102  {
103  std::ofstream f;
104  f.open(tmpFile.c_str(), std::ofstream::out);
105  EXPECT_TRUE(f.is_open());
106  f << sampleCfgTxt;
107  f.close();
108  }
109 
110  mrpt::config::CConfigFile cfg(tmpFile);
111 
112  const std::string readStr = cfg.read_string("test", "key_str", "");
113  EXPECT_EQ(readStr, expectedStr);
114 }
115 
116 TEST(CConfigFileMemory, parseVariables)
117 {
118 #ifdef _MSC_VER
119  _putenv_s("ENV_VAR_MULTIPLIER", "2");
120 #else
121  ::setenv("ENV_VAR_MULTIPLIER", "2", 1);
122 #endif
123 
124  const std::string sampleCfgTxt2 =
125  "@define MAXSPEED 10\n"
126  "@define MAXOMEGA -30 \n"
127  "[test]\n"
128  "var1=5\n"
129  "var2=${MAXSPEED}\n"
130  "var3=${MAXOMEGA}\n"
131  "var4=$eval{5*MAXSPEED+MAXOMEGA}\n"
132  "var5 = $eval{ MAXSPEED - MAXOMEGA } \n"
133  "var6=$env{ENV_VAR_MULTIPLIER}\n"
134  "varstr1=MAXSPEED\n";
135  ;
137  cfg.setContent(sampleCfgTxt2);
138 
139  EXPECT_EQ(cfg.read_int("test", "var1", 0), 5);
140  EXPECT_EQ(cfg.read_int("test", "var2", 0), 10);
141  EXPECT_EQ(cfg.read_int("test", "var3", 0), -30);
142  EXPECT_NEAR(cfg.read_double("test", "var4", .0), 20.0, 1e-6);
143  EXPECT_NEAR(cfg.read_double("test", "var5", .0), 40.0, 1e-6);
144  EXPECT_NEAR(cfg.read_double("test", "var6", .0), 2.0, 1e-6);
145  EXPECT_EQ(cfg.read_string("test", "varstr1", ""), std::string("MAXSPEED"));
146 }
This class implements a config file-like interface over a memory-stored string list.
EXPECT_TRUE(mrpt::system::fileExists(ini_fil))
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
This class allows loading and storing values and vectors of different types from ".ini" files easily.
void getAllKeys(const std::string &section, std::vector< std::string > &keys) const override
Returs a list with all the keys into a section.
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
std::string getTempFileName()
Returns the name of a proposed temporary file name.
Definition: filesystem.cpp:283
mrpt::config::CConfigFile CConfigFile
bool sectionExists(const std::string &section_name) const
Checks if a given section exists (name is case insensitive)
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
bool keyExists(const std::string &section, const std::string &key) const
Checks if a given key exists inside a section (case insensitive)
void getAllSections(std::vector< std::string > &sections) const override
Returns a list with all the section names.
void write(const std::string &section, const std::string &name, enum_t value, const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment=std::string())
std::vector< string > names
mrpt::vision::TStereoCalibResults out
void setContent(const std::vector< std::string > &stringList)
Changes the contents of the virtual "config file".
EXPECT_EQ(out.image_pair_was_used.size(), NUM_IMGS)
EXPECT_NEAR(out.cam_params.rightCameraPose.x, 0.1194, 0.005)
TEST(CConfigFileMemory, readwrite)
mrpt::config::CConfigFileMemory CConfigFileMemory
const std::string sampleCfgTxt
const std::string expectedStr



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020