MRPT  1.9.9
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-2019, 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 
55 TEST(CConfigFileMemory, setFromString)
56 {
58  "# example config file from std::string\n"
59  "[test]\n"
60  "key_num = 4\n"
61  "key_str = pepe\n";
62 
65 
66  EXPECT_EQ(cfg.read_int("test", "key_num", 0), 4);
67  EXPECT_EQ(cfg.read_string("test", "key_str", ""), std::string("pepe"));
68 }
69 
70 // Being able of read
72  "[test]\n"
73  "key_str = this is a \\\n"
74  "long value that can be \\\n"
75  "split into several lines \\\n"
76  "but read as a single line. \n";
77 ;
79  "this is a long value that can be split into several lines but read as a "
80  "single line.");
81 
82 TEST(CConfigFileMemory, readMultiLineStrings)
83 {
86 
87  const std::string readStr = cfg.read_string("test", "key_str", "");
88  EXPECT_EQ(readStr, expectedStr);
89 }
90 
91 TEST(CConfigFile, readMultiLineStrings)
92 {
94  {
95  std::ofstream f;
96  f.open(tmpFile.c_str(), std::ofstream::out);
97  EXPECT_TRUE(f.is_open());
98  f << sampleCfgTxt;
99  f.close();
100  }
101 
102  mrpt::config::CConfigFile cfg(tmpFile);
103 
104  const std::string readStr = cfg.read_string("test", "key_str", "");
105  EXPECT_EQ(readStr, expectedStr);
106 }
107 
108 TEST(CConfigFileMemory, parseVariables)
109 {
110 #ifdef _MSC_VER
111  _putenv_s("ENV_VAR_MULTIPLIER", "2");
112 #else
113  ::setenv("ENV_VAR_MULTIPLIER", "2", 1);
114 #endif
115 
116  const std::string sampleCfgTxt2 =
117  "@define MAXSPEED 10\n"
118  "@define MAXOMEGA -30 \n"
119  "[test]\n"
120  "var1=5\n"
121  "var2=${MAXSPEED}\n"
122  "var3=${MAXOMEGA}\n"
123  "var4=$eval{5*MAXSPEED+MAXOMEGA}\n"
124  "var5 = $eval{ MAXSPEED - MAXOMEGA } \n"
125  "var6=$env{ENV_VAR_MULTIPLIER}\n"
126  "varstr1=MAXSPEED\n";
127  ;
129  cfg.setContent(sampleCfgTxt2);
130 
131  EXPECT_EQ(cfg.read_int("test", "var1", 0), 5);
132  EXPECT_EQ(cfg.read_int("test", "var2", 0), 10);
133  EXPECT_EQ(cfg.read_int("test", "var3", 0), -30);
134  EXPECT_NEAR(cfg.read_double("test", "var4", .0), 20.0, 1e-6);
135  EXPECT_NEAR(cfg.read_double("test", "var5", .0), 40.0, 1e-6);
136  EXPECT_NEAR(cfg.read_double("test", "var6", .0), 2.0, 1e-6);
137  EXPECT_EQ(cfg.read_string("test", "varstr1", ""), std::string("MAXSPEED"));
138 }
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
GLint * first
Definition: glext.h:3833
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
const GLubyte * c
Definition: glext.h:6406
GLubyte GLubyte b
Definition: glext.h:6372
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
GLsizei const GLchar ** string
Definition: glext.h:4116
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
GLubyte GLubyte GLubyte a
Definition: glext.h:6372
const std::string expectedStr



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ce444d842 Fri Dec 6 19:35:10 2019 +0100 at vie dic 6 19:45:12 CET 2019