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-2018, 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 
12 #include <mrpt/system/filesystem.h>
13 #include <gtest/gtest.h>
14 #include <fstream>
15 #include <cstdlib>
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 
109 TEST(CConfigFileMemory, parseVariables)
110 {
111 #ifdef _MSC_VER
112  _putenv_s("ENV_VAR_MULTIPLIER","2");
113 #else
114  ::setenv("ENV_VAR_MULTIPLIER","2", 1);
115 #endif
116 
117  const std::string sampleCfgTxt2 =
118  "@define MAXSPEED 10\n"
119  "@define MAXOMEGA -30 \n"
120  "[test]\n"
121  "var1=5\n"
122  "var2=${MAXSPEED}\n"
123  "var3=${MAXOMEGA}\n"
124  "var4=$eval{5*MAXSPEED+MAXOMEGA}\n"
125  "var5 = $eval{ MAXSPEED - MAXOMEGA } \n"
126  "var6=$env{ENV_VAR_MULTIPLIER}\n"
127  "varstr1=MAXSPEED\n";
128  ;
130  cfg.setContent(sampleCfgTxt2);
131 
132  EXPECT_EQ(cfg.read_int("test", "var1", 0), 5);
133  EXPECT_EQ(cfg.read_int("test", "var2", 0), 10);
134  EXPECT_EQ(cfg.read_int("test", "var3", 0), -30);
135  EXPECT_NEAR(cfg.read_double("test", "var4", .0), 20.0, 1e-6);
136  EXPECT_NEAR(cfg.read_double("test", "var5", .0), 40.0, 1e-6);
137  EXPECT_NEAR(cfg.read_double("test", "var6", .0), 2.0, 1e-6);
138  EXPECT_EQ(cfg.read_string("test", "varstr1", ""), std::string("MAXSPEED"));
139 }
mrpt::utils::CConfigFileMemory
mrpt::config::CConfigFileMemory CConfigFileMemory
Definition: utils/CConfigFileMemory.h:7
filesystem.h
mrpt::config::CConfigFileMemory::getAllSections
void getAllSections(std::vector< std::string > &sections) const override
Returns a list with all the section names.
Definition: CConfigFileMemory.cpp:116
CConfigFileMemory.h
mrpt::config::CConfigFileBase::write
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())
Definition: config/CConfigFileBase.h:85
mrpt::config::CConfigFileMemory::getAllKeys
void getAllKeys(const std::string &section, std::vector< std::string > &keys) const override
Returs a list with all the keys into a section.
Definition: CConfigFileMemory.cpp:128
c
const GLubyte * c
Definition: glext.h:6313
mrpt::config::CConfigFileBase::read_double
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
Definition: CConfigFileBase.cpp:101
CConfigFile.h
mrpt::utils::CConfigFile
mrpt::config::CConfigFile CConfigFile
Definition: utils/CConfigFile.h:7
mrpt::system::getTempFileName
std::string getTempFileName()
Returns the name of a proposed temporary file name.
Definition: filesystem.cpp:282
mrpt::config::CConfigFileBase::read_string
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
Definition: CConfigFileBase.cpp:169
sampleCfgTxt
const std::string sampleCfgTxt
Definition: CConfigFileMemory_unittest.cpp:71
mrpt::config::CConfigFileBase::read_int
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
Definition: CConfigFileBase.cpp:125
mrpt::config::CConfigFileMemory::setContent
void setContent(const std::vector< std::string > &stringList)
Changes the contents of the virtual "config file".
Definition: CConfigFileMemory.cpp:53
b
GLubyte GLubyte b
Definition: glext.h:6279
names
std::vector< string > names
Definition: vision_stereo_rectify/test.cpp:23
expectedStr
const std::string expectedStr
Definition: CConfigFileMemory_unittest.cpp:78
mrpt::config::CConfigFile
This class allows loading and storing values and vectors of different types from "....
Definition: config/CConfigFile.h:33
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::config::CConfigFileMemory
This class implements a config file-like interface over a memory-stored string list.
Definition: config/CConfigFileMemory.h:30
first
GLint * first
Definition: glext.h:3827
TEST
TEST(CConfigFileMemory, readwrite)
Definition: CConfigFileMemory_unittest.cpp:17
a
GLubyte GLubyte GLubyte a
Definition: glext.h:6279



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST