1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
7 #include <iostream>
8 #include <sstream>
9 
10 #include "alps/testing/unique_file.hpp"
11 #include <alps/hdf5/archive.hpp>
12 
13 #include "alps/params.hpp"
14 #include "gtest/gtest.h"
15 
16 // FIXME: rewrite the test using convenience test classes from "param_generators.hpp"
17 //        and test ALL supported types.
18 
19 // FIXME: add test for saving the options as individual H5 datafields.
20 
TEST(param,Archive)21 TEST(param, Archive)
22 {
23     typedef std::vector<double> dblvec_type;
24 
25     // Prepare parameters
26     const char* argv[]={ "", "--param1=111", "--flag" };
27     const int argc=sizeof(argv)/sizeof(*argv);
28     alps::params p(argc,argv);
29 
30     p.description("Archiving test").
31         define("flag","trigger param").
32         define<int>("param1","integer 1").
33         define<double>("param2",22.25,"double").
34         define<dblvec_type>("vparam","vector of doubles");
35 
36     p["param3"]=333;
37     p["nosuchparam"]; // FIXME: test that it did not spring into existance.
38     dblvec_type dv(3);
39     dv[0]=1.25;
40     dv[1]=2.50;
41     dv[2]=3.75;
42     p["vparam"]=dv;
43 
44     EXPECT_FALSE(p.is_restored());
45     EXPECT_THROW(p.get_archive_name(), alps::params::not_restored);
46 
47 
48     // Save to archive
49     std::string filename(alps::testing::temporary_filename("hdf5_file.h5."));
50     {
51         alps::hdf5::archive oar(filename, "w");
52         p.save(oar);
53     }
54 
55     // Load from archive
56     alps::params p2;
57     {
58         alps::hdf5::archive iar(filename, "r");
59         p2.load(iar);
60     }
61     EXPECT_FALSE(p2["help"].as<bool>());
62     EXPECT_TRUE(p2["flag"].as<bool>());
63     EXPECT_EQ(111, p2["param1"]);
64     EXPECT_EQ(22.25, p2["param2"]);
65     EXPECT_EQ(333, p2["param3"]);
66     EXPECT_EQ(dv.size(), p2["vparam"].as<dblvec_type>().size());
67     EXPECT_EQ(dv, p2["vparam"].as<dblvec_type>());
68 
69     EXPECT_FALSE(p2.is_restored());
70     EXPECT_THROW(p2.get_archive_name(), alps::params::not_restored);
71 
72     // Create from archive
73     alps::hdf5::archive iar3(filename, "r");
74     alps::params p3(iar3, "/");
75     EXPECT_EQ(111, p3["param1"]);
76     EXPECT_EQ(22.25, p3["param2"]);
77     EXPECT_EQ(333, p3["param3"]);
78     EXPECT_EQ(dv.size(), p2["vparam"].as<dblvec_type>().size());
79     EXPECT_EQ(dv, p2["vparam"].as<dblvec_type>());
80 
81     EXPECT_FALSE(p3.is_restored());
82     EXPECT_THROW(p3.get_archive_name(), alps::params::not_restored);
83 
84 
85     // Create from commandline containing an archive
86     const char* argv2[]={ "", filename.c_str(), "--param1=999" };
87     const int argc2=sizeof(argv2)/sizeof(*argv2);
88     alps::params p4(argc2,argv2,"/");
89     EXPECT_EQ(999, p4["param1"]); // note: not 111; cmdline is re-processed!
90     EXPECT_EQ(22.25, p4["param2"]);
91     EXPECT_EQ(333, p4["param3"]);
92     EXPECT_EQ(dv.size(), p2["vparam"].as<dblvec_type>().size());
93     EXPECT_EQ(dv, p2["vparam"].as<dblvec_type>());
94 
95     EXPECT_TRUE(p4.is_restored());
96     EXPECT_EQ(filename, p4.get_archive_name());
97 }
98 
99