Main MRPT website > C++ reference for MRPT 1.5.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) { // avoid potential crash if fails
37  EXPECT_STREQ("one", sections[0].c_str());
38  EXPECT_STREQ("two", sections[1].c_str());
39  }
40 }
41 
43 {
44  vector_string names;
45  CConfigFileMemory third;
46  third.write("sec","name","val");
47  third.write("sec","names","value");
48  third.getAllKeys("sec", names);
49  EXPECT_EQ(2U, names.size());
50  if (names.size() == 2) { // avoid potential crash if fails
51  EXPECT_STREQ("name", names[0].c_str());
52  EXPECT_STREQ("names", names[1].c_str());
53  }
54 }
55 
56 TEST(CConfigFileMemory, setFromString)
57 {
59  "# example config file from std::string\n"
60  "[test]\n"
61  "key_num = 4\n"
62  "key_str = pepe\n"
63  ;
64 
67 
68  EXPECT_EQ(cfg.read_int("test", "key_num", 0), 4);
69  EXPECT_EQ(cfg.read_string("test","key_str",""),std::string("pepe"));
70 }
71 
72 // Being able of read
74 "[test]\n"
75 "key_str = this is a \\\n"
76 "long value that can be \\\n"
77 "split into several lines \\\n"
78 "but read as a single line. \n";
79 ;
80 const std::string expectedStr = std::string("this is a long value that can be split into several lines but read as a 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  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  const std::string sampleCfgTxt2 =
111  "@define MAXSPEED 10\n"
112  "@define MAXOMEGA -30 \n"
113  "[test]\n"
114  "var1=5\n"
115  "var2=${MAXSPEED}\n"
116  "var3=${MAXOMEGA}\n"
117  "var4=$eval{5*MAXSPEED+MAXOMEGA}\n"
118  "var5 = $eval{ MAXSPEED - MAXOMEGA } \n"
119  "varstr1=MAXSPEED\n";
120  ;
121  CConfigFileMemory cfg;
122  cfg.setContent(sampleCfgTxt2);
123 
124  EXPECT_EQ(cfg.read_int("test", "var1", 0), 5);
125  EXPECT_EQ(cfg.read_int("test", "var2", 0), 10);
126  EXPECT_EQ(cfg.read_int("test", "var3", 0), -30);
127  EXPECT_NEAR(cfg.read_double("test", "var4", .0), 20.0, 1e-6);
128  EXPECT_NEAR(cfg.read_double("test", "var5", .0), 40.0,1e-6);
129  EXPECT_EQ(cfg.read_string("test", "varstr1", ""), std::string("MAXSPEED"));
130 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
GLint * first
Definition: glext.h:3703
This class allows loading and storing values and vectors of different types from ".ini" files easily.
Definition: CConfigFile.h:30
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
STL namespace.
void getAllSections(vector_string &sections) const MRPT_OVERRIDE
Returns a list with all the section names.
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:30
std::string BASE_IMPEXP getTempFileName()
Returns the name of a proposed temporary file name.
Definition: filesystem.cpp:273
const GLubyte * c
Definition: glext.h:5590
GLubyte GLubyte b
Definition: glext.h:5575
GLsizei const GLchar ** string
Definition: glext.h:3919
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void write(const std::string &section, const std::string &name, const data_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
void getAllKeys(const std::string &section, vector_string &keys) const MRPT_OVERRIDE
Returs a list with all the keys into a section.
TEST(CConfigFileMemory, readwrite)
const std::string sampleCfgTxt
GLubyte GLubyte GLubyte a
Definition: glext.h:5575
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.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020