Discussion:
[gem5-dev] Change in gem5/gem5[master]: base: IniFile dumping to ostream
(too old to reply)
Giacomo Travaglini (Gerrit)
2018-11-27 13:40:22 UTC
Permalink
Giacomo Travaglini has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/14616


Change subject: base: IniFile dumping to ostream
......................................................................

base: IniFile dumping to ostream

Change-Id: Icd3ca463b628066df0debf01d36e8b629cce7702
Signed-off-by: Giacomo Travaglini <***@arm.com>
---
M src/base/inifile.cc
M src/base/inifile.hh
M src/base/inifiletest.cc
3 files changed, 59 insertions(+), 26 deletions(-)



diff --git a/src/base/inifile.cc b/src/base/inifile.cc
index b0ece2e..7ef2c0e 100644
--- a/src/base/inifile.cc
+++ b/src/base/inifile.cc
@@ -331,20 +331,20 @@


void
-IniFile::Section::dump(const string &sectionName)
+IniFile::Section::dump(const string &sectionName, std::ostream &out)
{
for (EntryTable::iterator ei = table.begin();
ei != table.end(); ++ei) {
- cout << sectionName << ": " << (*ei).first << " => "
+ out << sectionName << ": " << (*ei).first << " => "
<< (*ei).second->getValue() << "\n";
}
}

void
-IniFile::dump()
+IniFile::dump(std::ostream &out)
{
for (SectionTable::iterator i = table.begin();
i != table.end(); ++i) {
- i->second->dump(i->first);
+ i->second->dump(i->first, out);
}
}
diff --git a/src/base/inifile.hh b/src/base/inifile.hh
index 447ec7c..4139aaf 100644
--- a/src/base/inifile.hh
+++ b/src/base/inifile.hh
@@ -33,6 +33,7 @@
#define __INIFILE_HH__

#include <fstream>
+#include <iostream>
#include <list>
#include <string>
#include <unordered_map>
@@ -134,7 +135,7 @@
bool printUnreferenced(const std::string &sectionName);

/// Print the contents of this section to cout (for debugging).
- void dump(const std::string &sectionName);
+ void dump(const std::string &sectionName, std::ostream &out);
};

/// SectionTable type. Map of strings to Section object pointers.
@@ -205,7 +206,7 @@
bool printUnreferenced();

/// Dump contents to cout. For debugging.
- void dump();
+ void dump(std::ostream &out = std::cout);
};

#endif // __INIFILE_HH__
diff --git a/src/base/inifiletest.cc b/src/base/inifiletest.cc
index d331905..15e6c6a 100644
--- a/src/base/inifiletest.cc
+++ b/src/base/inifiletest.cc
@@ -35,6 +35,7 @@
#include <fstream>
#include <iostream>
#include <string>
+#include <sstream>
#include <vector>

#include <gtest/gtest.h>
@@ -43,34 +44,45 @@

using namespace std;

-namespace {
+class IniFileTest : public testing::Test
+{
+ public:
+ void SetUp() override
+ {
+ inifileText.str(R"ini_file(
+ [General]
+ Test1=BARasdf
+ Test2=bar

-std::istringstream iniFile(R"ini_file(
-[General]
- Test1=BARasdf
- Test2=bar
+ [Junk]
+ Test3=yo
+ Test4=mama

-[Junk]
-Test3=yo
-Test4=mama
+ [Foo]
+ Foo1=89
+ Foo2=384

-[Foo]
-Foo1=89
-Foo2=384
+ [General]
+ Test3=89

-[General]
-Test3=89
+ [Junk]
+ Test4+=mia
+ )ini_file");
+ }

-[Junk]
-Test4+=mia
-)ini_file");
+ void TearDown() override
+ {
+ inifileText.str(std::string());
+ inifileText.clear();
+ }

+ std::istringstream inifileText;
};

-TEST(Initest, MatchFound)
+TEST_F(IniFileTest, MatchFound)
{
IniFile simConfigDB;
- simConfigDB.load(iniFile);
+ simConfigDB.load(inifileText);

std::string value;

@@ -93,12 +105,13 @@
ret = simConfigDB.find("General", "Test3", value);
ASSERT_TRUE(ret);
ASSERT_STREQ(value.c_str(), "89");
+
}

-TEST(Initest, MatchNotFound)
+TEST_F(IniFileTest, MatchNotFound)
{
IniFile simConfigDB;
- simConfigDB.load(iniFile);
+ simConfigDB.load(inifileText);

std::string value;

@@ -108,3 +121,22 @@
ret = simConfigDB.find("Junk", "test4", value);
ASSERT_FALSE(ret);
}
+
+TEST_F(IniFileTest, Dump)
+{
+ std::istringstream inifile_text(R"ini_file(
+ [General]
+ Test1=BARasdf
+ Test2=bar)ini_file");
+
+ std::string expect_dump(R"(
+ General: Test1 => BARasdf
+ General: Test2 => bar)");
+
+ IniFile simConfigDB;
+ simConfigDB.load(inifile_text);
+
+ std::ostringstream out;
+ simConfigDB.dump(out);
+ ASSERT_EQ(out.str(), expect_dump);
+}
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/14616
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: master
Gerrit-Change-Id: Icd3ca463b628066df0debf01d36e8b629cce7702
Gerrit-Change-Number: 14616
Gerrit-PatchSet: 1
Gerrit-Owner: Giacomo Travaglini <***@arm.com>
Gerrit-MessageType: newchange
Continue reading on narkive:
Loading...