10 #include <mva/interface/Weightfile.h>
11 #include <mva/interface/Options.h>
12 #include <framework/utilities/TestHelpers.h>
14 #include <framework/database/Configuration.h>
15 #include <framework/database/Database.h>
16 #include <boost/filesystem/operations.hpp>
22 #include <gtest/gtest.h>
31 TestOptions(
const std::string& _x,
const std::string& _y) : x(_x), y(_y) { }
32 void load(
const boost::property_tree::ptree& pt)
override { y = pt.get<std::string>(x); }
33 void save(boost::property_tree::ptree& pt)
const override { pt.put(x, y); }
34 po::options_description getDescription()
override
36 po::options_description description(
"General options");
37 description.add_options()
38 (
"help",
"print this message");
46 TEST(WeightfileTest, Options)
48 TestOptions options1(
"Test1",
"a");
49 TestOptions options2(
"Test2",
"b");
54 EXPECT_EQ(weightfile.
getElement<std::string>(
"Test1"),
"a");
55 EXPECT_EQ(weightfile.
getElement<std::string>(
"Test2"),
"b");
57 TestOptions options3(
"Test2",
"c");
59 EXPECT_EQ(options3.y,
"b");
62 TEST(WeightfileTest, FeatureImportance)
64 std::map<std::string, float> importance;
65 importance[
"a"] = 1.0;
66 importance[
"b"] = 2.0;
67 importance[
"c"] = 3.0;
71 EXPECT_EQ(weightfile.
getElement<
unsigned int>(
"number_of_importance_vars"), 3);
72 EXPECT_EQ(weightfile.
getElement<std::string>(
"importance_key0"),
"a");
73 EXPECT_EQ(weightfile.
getElement<
float>(
"importance_value0"), 1.0);
77 EXPECT_EQ(importance2.size(), 3);
78 EXPECT_EQ(importance2[
"a"], 1.0);
79 EXPECT_EQ(importance2[
"b"], 2.0);
80 EXPECT_EQ(importance2[
"c"], 3.0);
84 TEST(WeightfileTest, SignalFraction)
93 TEST(WeightfileTest, Element)
98 EXPECT_EQ(weightfile.
getElement<
int>(
"Test"), 1);
102 TEST(WeightfileTest, Stream)
106 std::stringstream sstream(
"MyStream");
108 EXPECT_EQ(weightfile.
getStream(
"Test"),
"MyStream");
116 std::ofstream ofile(
"file.txt");
121 weightfile.
addFile(
"Test",
"file.txt");
123 weightfile.
getFile(
"Test",
"file2.txt");
125 std::ifstream ifile(
"file2.txt");
129 EXPECT_EQ(content,
"MyFile");
133 TEST(WeightfileTest, StaticSaveLoadDatabase)
139 conf.overrideGlobalTags();
140 conf.prependTestingPayloadLocation(
"localdb/database.txt");
147 EXPECT_EQ(loaded.getElement<std::string>(
"Test"),
"a");
152 EXPECT_EQ(loaded.getElement<std::string>(
"Test"),
"a");
156 boost::filesystem::remove_all(
"testPayloads");
161 TEST(WeightfileTest, StaticSaveLoadXML)
171 EXPECT_EQ(loaded.getElement<std::string>(
"Test"),
"a");
174 EXPECT_EQ(loaded.getElement<std::string>(
"Test"),
"a");
177 EXPECT_EQ(loaded.getElement<std::string>(
"Test"),
"a");
183 TEST(WeightfileTest, StaticSaveLoadROOT)
193 EXPECT_EQ(loaded.getElement<std::string>(
"Test"),
"a");
196 EXPECT_EQ(loaded.getElement<std::string>(
"Test"),
"a");
199 EXPECT_EQ(loaded.getElement<std::string>(
"Test"),
"a");
205 std::fstream file(
"INVALID.root");
211 TEST(WeightfileTest, StaticDatabase)
216 conf.overrideGlobalTags();
217 conf.prependTestingPayloadLocation(
"localdb/database.txt");
226 EXPECT_EQ(loaded.getElement<std::string>(
"Test"),
"a");
228 boost::filesystem::remove_all(
"testPayloads");
233 TEST(WeightfileTest, StaticDatabaseBadSymbols)
238 conf.overrideGlobalTags();
239 conf.prependTestingPayloadLocation(
"localdb/database.txt");
244 std::string evilIdentifier =
"==> *+:";
249 EXPECT_EQ(loaded.getElement<std::string>(
"Test"),
"a");
251 boost::filesystem::remove_all(
"testPayloads");
256 TEST(WeightfileTest, StaticXMLFile)
267 EXPECT_EQ(loaded.getElement<std::string>(
"Test"),
"a");
271 TEST(WeightfileTest, StaticROOTFile)
282 EXPECT_EQ(loaded.getElement<std::string>(
"Test"),
"a");
284 TFile file(
"invalid_weightfile.root",
"RECREATE");
290 TEST(WeightfileTest, StaticStream)
297 std::ofstream ofile(
"file.txt");
301 std::ifstream ifile(
"file.txt");
303 EXPECT_EQ(loaded.getElement<std::string>(
"Test"),
"a");
307 TEST(WeightfileTest, GetFileName)
312 unsigned int length =
filename.size();
313 EXPECT_TRUE(
filename.substr(length - 4, length) ==
".xml");
321 std::ofstream a(filename);
323 EXPECT_TRUE(boost::filesystem::exists(filename));
325 EXPECT_FALSE(boost::filesystem::exists(filename));
333 std::ofstream a(filename);
335 EXPECT_TRUE(boost::filesystem::exists(filename));
337 EXPECT_TRUE(boost::filesystem::exists(filename));
338 boost::filesystem::remove_all(boost::filesystem::path(filename).parent_path());
339 EXPECT_FALSE(boost::filesystem::exists(boost::filesystem::path(filename).parent_path()));
341 char* directory_template = strdup(
"/tmp/Basf2Sub.XXXXXX");
342 auto tempdir = std::string(mkdtemp(directory_template));
347 EXPECT_EQ(
filename.substr(0, tempdir.size()), tempdir);
349 free(directory_template);
350 boost::filesystem::remove_all(tempdir);
351 EXPECT_FALSE(boost::filesystem::exists(tempdir));