fixed_param_test.cpp
Go to the documentation of this file.
2 #include <gtest/gtest.h>
4 #include <test/test-models/good/optimization/rosenbrock.hpp>
6 #include <iostream>
7 
8 
9 class ServicesSamplesFixedParam : public testing::Test {
10 public:
12  : model(context, &model_log) {}
13 
14  std::stringstream model_log;
18  stan_model model;
19 };
20 
21 
23  unsigned int seed = 0;
24  unsigned int chain = 1;
25  double init_radius = 0;
26  int num_iterations = 10;
27 
28  int refresh = 0;
30  EXPECT_EQ(interrupt.call_count(), 0);
31 
33  seed, chain, init_radius,
34  num_iterations,
35  1,
36  refresh,
37  interrupt,
38  logger,
39  init,
40  parameter,
41  diagnostic);
42  EXPECT_EQ(0, return_code);
43 
44  // Expecatations of message call counts
45  EXPECT_EQ(num_iterations, interrupt.call_count());
46  EXPECT_EQ(1, parameter.call_count("vector_string"));
47  EXPECT_EQ(num_iterations, parameter.call_count("vector_double"));
48  EXPECT_EQ(1, diagnostic.call_count("vector_string"));
49  EXPECT_EQ(num_iterations, diagnostic.call_count("vector_double"));
50 }
51 
52 
54  unsigned int seed = 0;
55  unsigned int chain = 1;
56  double init_radius = 0;
57  int num_iterations = 10;
58 
59  int refresh = 0;
61  EXPECT_EQ(interrupt.call_count(), 0);
62 
64  seed, chain, init_radius,
65  num_iterations,
66  1,
67  refresh,
68  interrupt,
69  logger,
70  init,
71  parameter,
72  diagnostic);
73 
74  std::vector<std::vector<std::string> > parameter_names;
75  parameter_names = parameter.vector_string_values();
76  std::vector<std::vector<double> > parameter_values;
77  parameter_values = parameter.vector_double_values();
78  std::vector<std::vector<std::string> > diagnostic_names;
79  diagnostic_names = diagnostic.vector_string_values();
80  std::vector<std::vector<double> > diagnostic_values;
81  diagnostic_values = diagnostic.vector_double_values();
82 
83  // Expectations of parameter parameter names.
84  ASSERT_EQ(4, parameter_names[0].size());
85  EXPECT_EQ("lp__", parameter_names[0][0]);
86  EXPECT_EQ("accept_stat__", parameter_names[0][1]);
87  EXPECT_EQ("x", parameter_names[0][2]);
88  EXPECT_EQ("y", parameter_names[0][3]);
89 
90  // Expect one name per parameter value.
91  EXPECT_EQ(parameter_names[0].size(), parameter_values[0].size());
92  EXPECT_EQ(diagnostic_names[0].size(), diagnostic_values[0].size());
93 
94  // Expect one vector of parameter values per iterations
95  EXPECT_EQ(num_iterations, parameter_values.size());
96 
97  // Expect one call to set parameter names, and one set of output per
98  // iteration.
99  EXPECT_EQ("lp__", diagnostic_names[0][0]);
100  EXPECT_EQ("accept_stat__", diagnostic_names[0][1]);
101 }
102 
103 
104 TEST_F(ServicesSamplesFixedParam, parameter_checks) {
105  unsigned int seed = 0;
106  unsigned int chain = 1;
107  double init_radius = 0;
108  int num_iterations = 10;
109 
110  int refresh = 0;
112  EXPECT_EQ(interrupt.call_count(), 0);
113 
115  seed, chain, init_radius,
116  num_iterations,
117  1,
118  refresh,
119  interrupt,
120  logger,
121  init,
122  parameter,
123  diagnostic);
124 
125  std::vector<std::vector<std::string> > parameter_names;
126  parameter_names = parameter.vector_string_values();
127  std::vector<std::vector<double> > parameter_values;
128  parameter_values = parameter.vector_double_values();
129  std::vector<std::vector<std::string> > diagnostic_names;
130  diagnostic_names = diagnostic.vector_string_values();
131  std::vector<std::vector<double> > diagnostic_values;
132  diagnostic_values = diagnostic.vector_double_values();
133 
134  // Expect parameter values to stay at zero.
135  EXPECT_DOUBLE_EQ(0.0, parameter_values.front()[1])
136  << "initial memory_writer should be (0, 0)";
137  EXPECT_DOUBLE_EQ(0.0, parameter_values.front()[2])
138  << "initial memory_writer should be (0, 0)";
139  EXPECT_DOUBLE_EQ(0.0, parameter_values.back()[1])
140  << "final memory_writer should be (0, 0)";
141  EXPECT_DOUBLE_EQ(0.0, parameter_values.back()[2])
142  << "final memory_writer should be (0, 0)";
143  EXPECT_EQ(return_code, 0);
144 }
145 
146 TEST_F(ServicesSamplesFixedParam, output_regression) {
147  unsigned int seed = 0;
148  unsigned int chain = 1;
149  double init_radius = 0;
150  int num_iterations = 10;
151 
152  int refresh = 0;
154  EXPECT_EQ(interrupt.call_count(), 0);
155 
157  seed, chain, init_radius,
158  num_iterations,
159  1,
160  refresh,
161  interrupt,
162  logger,
163  init,
164  parameter,
165  diagnostic);
166 
167  std::vector<std::vector<std::string> > parameter_names;
168  parameter_names = parameter.vector_string_values();
169  std::vector<std::vector<double> > parameter_values;
170  parameter_values = parameter.vector_double_values();
171  std::vector<std::vector<std::string> > diagnostic_names;
172  diagnostic_names = diagnostic.vector_string_values();
173  std::vector<std::vector<double> > diagnostic_values;
174  diagnostic_values = diagnostic.vector_double_values();
175  std::vector<std::string> init_values;
176  init_values = init.string_values();
177 
178  EXPECT_EQ(1, logger.find_info("Elapsed Time:"));
179  EXPECT_EQ(1, logger.find_info("seconds (Warm-up)"));
180  EXPECT_EQ(1, logger.find_info("seconds (Sampling)"));
181  EXPECT_EQ(1, logger.find_info("seconds (Total)"));
182  EXPECT_EQ(0, logger.call_count_error());
183 
184  EXPECT_EQ(0, init_values.size());
185 }
std::vector< std::vector< std::string > > vector_string_values()
stan::io::empty_var_context context
int fixed_param(Model &model, stan::io::var_context &init, unsigned int random_seed, unsigned int chain, double init_radius, int num_samples, int num_thin, int refresh, callbacks::interrupt &interrupt, callbacks::logger &logger, callbacks::writer &init_writer, callbacks::writer &sample_writer, callbacks::writer &diagnostic_writer)
Definition: fixed_param.hpp:44
stan::test::unit::instrumented_writer diagnostic
TEST_F(ServicesSamplesFixedParam, call_count)
stan::test::unit::instrumented_writer parameter
unsigned int seed
Definition: runWimpSim.h:102
stan::test::unit::instrumented_writer init
chain
Check that an output directory exists.
const XML_Char * context
Definition: expat.h:434
std::vector< std::vector< double > > vector_double_values()
unsigned int find_info(const std::string &msg)
const XML_Char XML_Content * model
Definition: expat.h:151
void refresh()
Definition: show_event.C:21
stan::test::unit::instrumented_logger logger