-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathvar_context_loading_test.cpp
More file actions
174 lines (152 loc) · 6.58 KB
/
Copy pathvar_context_loading_test.cpp
File metadata and controls
174 lines (152 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <cmdstan/command_helper.hpp>
#include <gtest/gtest.h>
#include <test/utility.hpp>
#include <string>
#include <vector>
class VarContextLoading : public testing::Test {
public:
void SetUp() {
using cmdstan::test::convert_model_path;
init_good = convert_model_path(
std::vector{"src", "test", "test-models", "bern_init.json"});
init_template_good = convert_model_path(
std::vector{"src", "test", "test-models", "bern_init2.json"});
init_template_good_member = convert_model_path(
std::vector{"src", "test", "test-models", "bern_init2_2.json"});
init_template_missing_member = convert_model_path(
std::vector{"src", "test", "test-models", "bern_init2_6.json"});
init_template_R_good = convert_model_path(
std::vector{"src", "test", "test-models", "bern_init2.R"});
init_template_R_good_member = convert_model_path(
std::vector{"src", "test", "test-models", "bern_init2_2.R"});
}
std::string init_good;
std::string init_template_good;
std::string init_template_good_member;
std::string init_template_missing_member;
std::string init_template_R_good;
std::string init_template_R_good_member;
};
// --------------- single chain ---------------
TEST_F(VarContextLoading, one_chain_good) {
auto var_context = cmdstan::get_vec_var_context(init_good, 1, 1);
EXPECT_EQ(1, var_context.size());
EXPECT_FLOAT_EQ(0.1, var_context[0]->vals_r("theta")[0]);
}
TEST_F(VarContextLoading, one_chain_bad_extension) {
EXPECT_THROW_MSG(
cmdstan::get_vec_var_context(init_good + "foo", 1, 1),
std::invalid_argument,
"User specified files must end in .json or .R. Found: .jsonfoo");
}
TEST_F(VarContextLoading, one_chain_missing_file) {
EXPECT_THROW_MSG(
cmdstan::get_vec_var_context(init_template_missing_member, 1, 1),
std::invalid_argument, "Cannot open specified file");
}
TEST_F(VarContextLoading, one_chain_warning_R_extension) {
testing::internal::CaptureStderr();
auto var_context
= cmdstan::get_vec_var_context(init_template_R_good_member, 1, 1);
std::string output = testing::internal::GetCapturedStderr();
EXPECT_IN_STRING(
"This format is deprecated and will not receive new features.", output);
EXPECT_EQ(1, var_context.size());
EXPECT_FLOAT_EQ(0.2, var_context[0]->vals_r("theta")[0]);
}
TEST_F(VarContextLoading, one_chain_empty) {
auto var_context = cmdstan::get_vec_var_context("", 1, 1);
EXPECT_EQ(1, var_context.size());
std::vector<std::string> names;
var_context[0]->names_r(names);
EXPECT_EQ(0, names.size());
}
// --------------- multi-chain, legacy 'template' format ---------------
TEST_F(VarContextLoading, multi_template_good) {
auto var_context = cmdstan::get_vec_var_context(init_template_good, 4, 1);
EXPECT_EQ(4, var_context.size());
EXPECT_FLOAT_EQ(0.1, var_context[0]->vals_r("theta")[0]);
EXPECT_FLOAT_EQ(0.2, var_context[1]->vals_r("theta")[0]);
}
TEST_F(VarContextLoading, multi_template_fallback) {
testing::internal::CaptureStderr();
auto var_context = cmdstan::get_vec_var_context(init_good, 4, 1);
std::string output = testing::internal::GetCapturedStderr();
EXPECT_IN_STRING("is being used to initialize all 4 chains", output);
EXPECT_EQ(4, var_context.size());
// all the same
for (size_t i = 0; i < 4; ++i) {
EXPECT_FLOAT_EQ(0.1, var_context[i]->vals_r("theta")[0]);
}
}
TEST_F(VarContextLoading, multi_template_bad_extension) {
EXPECT_THROW_MSG(
cmdstan::get_vec_var_context(init_template_good + "foo", 4, 1),
std::invalid_argument,
"User specified files must end in .json or .R. Found: .jsonfoo");
}
TEST_F(VarContextLoading, multi_template_missing_file) {
EXPECT_THROW_MSG(
// will end up asking for _5, etc.
cmdstan::get_vec_var_context(init_template_good, 4, 3),
std::invalid_argument, "Cannot open some of the requested files: [");
// a bit awkward to test twice, but can't match full string due to OS-paths
EXPECT_THROW_MSG(cmdstan::get_vec_var_context(init_template_good, 4, 3),
std::invalid_argument, "Also failed to find base file");
}
TEST_F(VarContextLoading, multi_template_warning_R_extension) {
testing::internal::CaptureStderr();
auto var_context = cmdstan::get_vec_var_context(init_template_R_good, 4, 1);
std::string output = testing::internal::GetCapturedStderr();
EXPECT_IN_STRING(
"This format is deprecated and will not receive new features.", output);
EXPECT_EQ(4, var_context.size());
EXPECT_FLOAT_EQ(0.1, var_context[0]->vals_r("theta")[0]);
EXPECT_FLOAT_EQ(0.2, var_context[1]->vals_r("theta")[0]);
}
TEST_F(VarContextLoading, multi_empty) {
auto var_context = cmdstan::get_vec_var_context("", 4, 1);
EXPECT_EQ(4, var_context.size());
std::vector<std::string> names;
for (size_t i = 0; i < 4; ++i) {
var_context[i]->names_r(names);
EXPECT_EQ(0, names.size());
}
}
// --------------- multi-chain, comma-separated ---------------
TEST_F(VarContextLoading, multi_comma_good) {
auto var_context = cmdstan::get_vec_var_context(
init_good + "," + init_template_good_member, 2, 1);
EXPECT_EQ(2, var_context.size());
EXPECT_FLOAT_EQ(0.1, var_context[0]->vals_r("theta")[0]);
EXPECT_FLOAT_EQ(0.2, var_context[1]->vals_r("theta")[0]);
}
TEST_F(VarContextLoading, multi_comma_wrong_number){
EXPECT_THROW_MSG(cmdstan::get_vec_var_context(
init_good + "," + init_template_good_member, 4, 1),
std::invalid_argument,
"Number of filenames does not match number of chains: got "
"comma-separated list '")}
TEST_F(VarContextLoading, multi_comma_bad_extension) {
EXPECT_THROW_MSG(
cmdstan::get_vec_var_context(init_good + "," + init_good + "foo", 2, 1),
std::invalid_argument,
"User specified files must end in .json or .R. Found: .jsonfoo");
}
TEST_F(VarContextLoading, multi_comma_missing_file) {
EXPECT_THROW_MSG(cmdstan::get_vec_var_context(
init_good + "," + init_template_missing_member, 2, 3),
std::invalid_argument,
"Cannot open some of the requested files: ");
}
TEST_F(VarContextLoading, multi_comma_warning_R_extension) {
testing::internal::CaptureStderr();
auto var_context = cmdstan::get_vec_var_context(
init_good + "," + init_template_R_good_member, 2, 1);
std::string output = testing::internal::GetCapturedStderr();
EXPECT_IN_STRING(
"This format is deprecated and will not receive new features.", output);
EXPECT_EQ(2, var_context.size());
EXPECT_FLOAT_EQ(0.1, var_context[0]->vals_r("theta")[0]);
EXPECT_FLOAT_EQ(0.2, var_context[1]->vals_r("theta")[0]);
}