Main MRPT website > C++ reference for MRPT 1.9.9
CConfigFileMemory_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
11 #include <mrpt/utils/CConfigFile.h>
12 #include <mrpt/system/filesystem.h>
13 #include <gtest/gtest.h>
14 #include <fstream>
15 
16 using namespace mrpt;
17 using namespace mrpt::utils;
18 using namespace std;
19 
21 {
22  const std::string a = "check", b = "test", c = "final //comments";
24  first.write(a, b, c);
25  EXPECT_STREQ("final", first.read_string(a, b, b).c_str());
26 }
27 
29 {
30  vector_string sections;
31  CConfigFileMemory second;
32  second.write("one", "name", "val");
33  second.write("two", "names", "value");
34  second.getAllSections(sections);
35  EXPECT_EQ(2U, sections.size());
36  if (sections.size() == 2)
37  { // avoid potential crash if fails
38  EXPECT_STREQ("one", sections[0].c_str());
39  EXPECT_STREQ("two", sections[1].c_str());
40  }
41 }
42 
44 {
45  vector_string names;
46  CConfigFileMemory third;
47  third.write("sec", "name", "val");
48  third.write("sec", "names", "value");
49  third.getAllKeys("sec", names);
50  EXPECT_EQ(2U, names.size());
51  if (names.size() == 2)
52  { // avoid potential crash if fails
53  EXPECT_STREQ("name", names[0].c_str());
54  EXPECT_STREQ("names", names[1].c_str());
55  }
56 }
57 
58 TEST(CConfigFileMemory, setFromString)
59 {
61  "# example config file from std::string\n"
62  "[test]\n"
63  "key_num = 4\n"
64  "key_str = pepe\n";
65 
68 
69  EXPECT_EQ(cfg.read_int("test", "key_num", 0), 4);
70  EXPECT_EQ(cfg.read_string("test", "key_str", ""), std::string("pepe"));
71 }
72 
73 // Being able of read
75  "[test]\n"
76  "key_str = this is a \\\n"
77  "long value that can be \\\n"
78  "split into several lines \\\n"
79  "but read as a single line. \n";
80 ;
82  "this is a long value that can be split into several lines but read as a "
83  "single line.");
84 
85 TEST(CConfigFileMemory, readMultiLineStrings)
86 {
89 
90  const std::string readStr = cfg.read_string("test", "key_str", "");
91  EXPECT_EQ(readStr, expectedStr);
92 }
93 
94 TEST(CConfigFile, readMultiLineStrings)
95 {
97  {
98  std::ofstream f;
99  f.open(tmpFile.c_str(), std::ofstream::out);
100  EXPECT_TRUE(f.is_open());
101  f << sampleCfgTxt;
102  f.close();
103  }
104 
105  CConfigFile cfg(tmpFile);
106 
107  const std::string readStr = cfg.read_string("test", "key_str", "");
108  EXPECT_EQ(readStr, expectedStr);
109 }
110 
111 TEST(CConfigFileMemory, parseVariables)
112 {
113  const std::string sampleCfgTxt2 =
114  "@define MAXSPEED 10\n"
115  "@define MAXOMEGA -30 \n"
116  "[test]\n"
117  "var1=5\n"
118  "var2=${MAXSPEED}\n"
119  "var3=${MAXOMEGA}\n"
120  "var4=$eval{5*MAXSPEED+MAXOMEGA}\n"
121  "var5 = $eval{ MAXSPEED - MAXOMEGA } \n"
122  "varstr1=MAXSPEED\n";
123  ;
124  CConfigFileMemory cfg;
125  cfg.setContent(sampleCfgTxt2);
126 
127  EXPECT_EQ(cfg.read_int("test", "var1", 0), 5);
128  EXPECT_EQ(cfg.read_int("test", "var2", 0), 10);
129  EXPECT_EQ(cfg.read_int("test", "var3", 0), -30);
130  EXPECT_NEAR(cfg.read_double("test", "var4", .0), 20.0, 1e-6);
131  EXPECT_NEAR(cfg.read_double("test", "var5", .0), 40.0, 1e-6);
132  EXPECT_EQ(cfg.read_string("test", "varstr1", ""), std::string("MAXSPEED"));
133 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
GLint * first
Definition: glext.h:3827
This class allows loading and storing values and vectors of different types from ".ini" files easily.
Definition: CConfigFile.h:35
void getAllSections(vector_string &sections) const override
Returns a list with all the section names.
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
STL namespace.
This class implements a config file-like interface over a memory-stored string list.
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
std::vector< std::string > vector_string
A type for passing a vector of strings.
Definition: types_simple.h:33
std::string getTempFileName()
Returns the name of a proposed temporary file name.
Definition: filesystem.cpp:281
const GLubyte * c
Definition: glext.h:6313
GLubyte GLubyte b
Definition: glext.h:6279
GLsizei const GLchar ** string
Definition: glext.h:4101
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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())
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
TEST(CConfigFileMemory, readwrite)
void getAllKeys(const std::string &section, vector_string &keys) const override
Returs a list with all the keys into a section.
const std::string sampleCfgTxt
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
void setContent(const utils::CStringList &stringList)
Changes the contents of the virtual "config file".
const std::string expectedStr



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019