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 }
This class implements a config file-like interface over a memory-stored string list.
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:3827
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:282
mrpt::config::CConfigFile CConfigFile
const GLubyte * c
Definition: glext.h:6313
GLubyte GLubyte b
Definition: glext.h:6279
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
GLsizei const GLchar ** string
Definition: glext.h:4101
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
void setContent(const std::vector< std::string > &stringList)
Changes the contents of the virtual "config file".
TEST(CConfigFileMemory, readwrite)
mrpt::config::CConfigFileMemory CConfigFileMemory
const std::string sampleCfgTxt
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
const std::string expectedStr



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020