Skip to content

Commit bc20c34

Browse files
committed
Adds a check to make sure AgradRev is used for all tests in test/unit/math/rev
1 parent 490c8b5 commit bc20c34

9 files changed

Lines changed: 106 additions & 52 deletions

runChecks.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,47 @@ def check_non_test_files_in_test():
142142
return errors
143143

144144

145+
def check_rev_test_fixtures():
146+
test_files = [
147+
x for x in files_in_folder("test/unit/math/rev")
148+
if os.path.isfile(x) and x.endswith(testsfx)
149+
]
150+
errors = []
151+
for filepath in test_files:
152+
line_num = 0
153+
multi_line_comment = False
154+
old_state_multi_line_comment = False
155+
with open(filepath, "r") as f:
156+
for line in f:
157+
line_num += 1
158+
if multi_line_comment:
159+
if re.search("\*/", line):
160+
multi_line_comment = False
161+
else:
162+
if re.search("/\*", line):
163+
multi_line_comment = True
164+
if not multi_line_comment or (
165+
multi_line_comment and not old_state_multi_line_comment
166+
):
167+
if (
168+
not re.search(r".*\bTEST\(.*\*/.*", line)
169+
and not re.search(r".*/\*.*\bTEST\(", line)
170+
and not re.search(r".*//.*\bTEST\(", line)
171+
and re.search(r"\bTEST\(", line)
172+
):
173+
errors.append(
174+
filepath
175+
+ " at line "
176+
+ str(line_num)
177+
+ ":\n\t[rev-tests] Reverse-mode tests in "
178+
+ "test/unit/math/rev must use a cleanup fixture. "
179+
+ "Replace raw TEST(...) with TEST_F(AgradRev, ...) "
180+
+ "or another approved fixture-based form."
181+
)
182+
old_state_multi_line_comment = multi_line_comment
183+
return errors
184+
185+
145186
def main():
146187
errors = []
147188
# Check for files inside stan/math/prim that contain stan/math/rev or stan/math/fwd
@@ -242,6 +283,7 @@ def main():
242283
)
243284

244285
errors.extend(check_non_test_files_in_test())
286+
errors.extend(check_rev_test_fixtures())
245287

246288
errors.extend(check_non_unique_test_names())
247289

test/unit/math/rev/core/precomputed_gradients_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ TEST_F(AgradRev, StanAgradRevInternal_precomputed_gradients_containers) {
131131
stan::math::recover_memory();
132132
}
133133

134-
TEST(StanAgradRevInternal,
135-
precomputed_gradients_containers_direct_construction) {
134+
TEST_F(AgradRev,
135+
StanAgradRevInternal_precomputed_gradients_containers_direct_construction) {
136136
double value = 1;
137137
std::vector<stan::math::var> vars;
138138
std::vector<double> gradients;

test/unit/math/rev/functor/integrate_1d_impl_test.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,9 @@ TEST_F(AgradRev, StanMath_integrate_1d_impl_rev_TestDerivatives_zero_crossing) {
278278
-19.06340613646808, 21.41380852375568);
279279
}
280280

281-
TEST(StanMath_integrate_1d_impl_rev,
282-
TestDerivatives_var_right_endpoint_var_params) {
281+
TEST_F(
282+
AgradRev,
283+
StanMath_integrate_1d_impl_rev_TestDerivatives_var_right_endpoint_var_params) {
283284
// Zero crossing integral + test x_r + vars at right endpoint
284285
using stan::math::var;
285286
test_derivatives<double, var, var>(
@@ -289,8 +290,9 @@ TEST(StanMath_integrate_1d_impl_rev,
289290
{5 * pow(0.5, 1.5), 12 * 1.75 * 1.75, 4.0}, 0.0, 21.41380852375568);
290291
}
291292

292-
TEST(StanMath_integrate_1d_impl_rev,
293-
TestDerivatives_var_left_endpoint_var_params) {
293+
TEST_F(
294+
AgradRev,
295+
StanMath_integrate_1d_impl_rev_TestDerivatives_var_left_endpoint_var_params) {
294296
// Zero crossing integral + test x_r + var at left endpoint
295297
using stan::math::var;
296298
test_derivatives<var, double, var>(
@@ -416,8 +418,8 @@ TEST_F(AgradRev, StanMath_integrate_1d_impl_rev_TestDerivatives_gaussian) {
416418
{0.0, 0.0});
417419
}
418420

419-
TEST(StanMath_integrate_1d_impl_rev,
420-
TestDerivativesSameVarAtEndpointAndInParams) {
421+
TEST_F(AgradRev,
422+
StanMath_integrate_1d_impl_rev_TestDerivativesSameVarAtEndpointAndInParams) {
421423
using stan::math::var;
422424

423425
var a = 2.0;

test/unit/math/rev/prob/categorical_logit_glm_lpmf_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ categorical_logit_glm_simple_lpmf(const std::vector<int>& y, const T_x& x,
3030
return lpmf;
3131
}
3232

33-
TEST(ProbDistributionsCategoricalLogitGLM,
34-
glm_matches_categorical_logit_doubles) {
33+
TEST_F(
34+
AgradRev,
35+
ProbDistributionsCategoricalLogitGLM_glm_matches_categorical_logit_doubles) {
3536
using Eigen::Dynamic;
3637
using Eigen::Matrix;
3738
using Eigen::MatrixXd;

test/unit/math/rev/prob/gamma_lccdf_test.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include <stan/math/rev.hpp>
2+
#include <test/unit/math/rev/util.hpp>
23
#include <gtest/gtest.h>
34
#include <vector>
45
#include <limits>
56

6-
TEST(ProbDistributionsGamma, lccdf_values) {
7+
TEST_F(AgradRev, ProbDistributionsGamma_lccdf_values) {
78
using stan::math::gamma_lccdf;
89
using stan::math::var;
910

@@ -22,7 +23,7 @@ TEST(ProbDistributionsGamma, lccdf_values) {
2223
EXPECT_LE(lccdf_var.val(), 0.0);
2324
}
2425

25-
TEST(ProbDistributionsGamma, lccdf_derivatives_y) {
26+
TEST_F(AgradRev, ProbDistributionsGamma_lccdf_derivatives_y) {
2627
using stan::math::gamma_lccdf;
2728
using stan::math::var;
2829

@@ -47,7 +48,7 @@ TEST(ProbDistributionsGamma, lccdf_derivatives_y) {
4748
EXPECT_TRUE(std::isfinite(grads[0]));
4849
}
4950

50-
TEST(ProbDistributionsGamma, lccdf_derivatives_alpha) {
51+
TEST_F(AgradRev, ProbDistributionsGamma_lccdf_derivatives_alpha) {
5152
using stan::math::gamma_lccdf;
5253
using stan::math::var;
5354

@@ -70,7 +71,7 @@ TEST(ProbDistributionsGamma, lccdf_derivatives_alpha) {
7071
EXPECT_TRUE(std::isfinite(grads[0]));
7172
}
7273

73-
TEST(ProbDistributionsGamma, lccdf_derivatives_beta) {
74+
TEST_F(AgradRev, ProbDistributionsGamma_lccdf_derivatives_beta) {
7475
using stan::math::gamma_lccdf;
7576
using stan::math::var;
7677

@@ -93,7 +94,7 @@ TEST(ProbDistributionsGamma, lccdf_derivatives_beta) {
9394
EXPECT_TRUE(std::isfinite(grads[0]));
9495
}
9596

96-
TEST(ProbDistributionsGamma, lccdf_derivatives_all_params) {
97+
TEST_F(AgradRev, ProbDistributionsGamma_lccdf_derivatives_all_params) {
9798
using stan::math::gamma_lccdf;
9899
using stan::math::var;
99100

@@ -123,7 +124,7 @@ TEST(ProbDistributionsGamma, lccdf_derivatives_all_params) {
123124
EXPECT_LT(grads[0], 0.0) << "d/dy should be negative";
124125
}
125126

126-
TEST(ProbDistributionsGamma, lccdf_finite_diff_y) {
127+
TEST_F(AgradRev, ProbDistributionsGamma_lccdf_finite_diff_y) {
127128
using stan::math::gamma_lccdf;
128129
using stan::math::var;
129130

@@ -149,7 +150,7 @@ TEST(ProbDistributionsGamma, lccdf_finite_diff_y) {
149150
EXPECT_NEAR(grad_autodiff, grad_findiff, 1e-5);
150151
}
151152

152-
TEST(ProbDistributionsGamma, lccdf_finite_diff_alpha) {
153+
TEST_F(AgradRev, ProbDistributionsGamma_lccdf_finite_diff_alpha) {
153154
using stan::math::gamma_lccdf;
154155
using stan::math::var;
155156

@@ -176,7 +177,7 @@ TEST(ProbDistributionsGamma, lccdf_finite_diff_alpha) {
176177
EXPECT_NEAR(grad_autodiff, grad_findiff, 1e-3);
177178
}
178179

179-
TEST(ProbDistributionsGamma, lccdf_finite_diff_beta) {
180+
TEST_F(AgradRev, ProbDistributionsGamma_lccdf_finite_diff_beta) {
180181
using stan::math::gamma_lccdf;
181182
using stan::math::var;
182183

@@ -202,7 +203,7 @@ TEST(ProbDistributionsGamma, lccdf_finite_diff_beta) {
202203
EXPECT_NEAR(grad_autodiff, grad_findiff, 1e-5);
203204
}
204205

205-
TEST(ProbDistributionsGamma, lccdf_extreme_values_small) {
206+
TEST_F(AgradRev, ProbDistributionsGamma_lccdf_extreme_values_small) {
206207
using stan::math::gamma_lccdf;
207208
using stan::math::var;
208209

@@ -230,7 +231,7 @@ TEST(ProbDistributionsGamma, lccdf_extreme_values_small) {
230231
}
231232
}
232233

233-
TEST(ProbDistributionsGamma, lccdf_extreme_values_large) {
234+
TEST_F(AgradRev, ProbDistributionsGamma_lccdf_extreme_values_large) {
234235
using stan::math::gamma_lccdf;
235236
using stan::math::var;
236237

@@ -258,7 +259,7 @@ TEST(ProbDistributionsGamma, lccdf_extreme_values_large) {
258259
}
259260
}
260261

261-
TEST(ProbDistributionsGamma, lccdf_alpha_one_derivatives) {
262+
TEST_F(AgradRev, ProbDistributionsGamma_lccdf_alpha_one_derivatives) {
262263
using stan::math::gamma_lccdf;
263264
using stan::math::var;
264265

@@ -287,7 +288,7 @@ TEST(ProbDistributionsGamma, lccdf_alpha_one_derivatives) {
287288
EXPECT_NEAR(grads[2], -y_d, 1e-10);
288289
}
289290

290-
TEST(ProbDistributionsGamma, lccdf_various_parameter_combinations) {
291+
TEST_F(AgradRev, ProbDistributionsGamma_lccdf_various_parameter_combinations) {
291292
using stan::math::gamma_lccdf;
292293
using stan::math::var;
293294

@@ -323,7 +324,7 @@ TEST(ProbDistributionsGamma, lccdf_various_parameter_combinations) {
323324
}
324325
}
325326

326-
TEST(ProbDistributionsGamma, lccdf_second_derivative_y) {
327+
TEST_F(AgradRev, ProbDistributionsGamma_lccdf_second_derivative_y) {
327328
using stan::math::gamma_lccdf;
328329
using stan::math::var;
329330

@@ -345,7 +346,8 @@ TEST(ProbDistributionsGamma, lccdf_second_derivative_y) {
345346
EXPECT_TRUE(std::isfinite(grads[0]));
346347
}
347348

348-
TEST(ProbDistributionsGamma, lccdf_numerically_challenging_derivatives) {
349+
TEST_F(AgradRev,
350+
ProbDistributionsGamma_lccdf_numerically_challenging_derivatives) {
349351
using stan::math::gamma_lccdf;
350352
using stan::math::var;
351353

test/unit/math/rev/prob/gamma_lcdf_test.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include <stan/math/rev.hpp>
2+
#include <test/unit/math/rev/util.hpp>
23
#include <gtest/gtest.h>
34
#include <vector>
45
#include <limits>
56

6-
TEST(ProbDistributionsGamma, lcdf_values) {
7+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_values) {
78
using stan::math::gamma_lcdf;
89
using stan::math::var;
910

@@ -22,7 +23,7 @@ TEST(ProbDistributionsGamma, lcdf_values) {
2223
EXPECT_LE(lcdf_var.val(), 0.0); // log of probability
2324
}
2425

25-
TEST(ProbDistributionsGamma, lcdf_derivatives_y) {
26+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_derivatives_y) {
2627
using stan::math::gamma_lcdf;
2728
using stan::math::var;
2829

@@ -47,7 +48,7 @@ TEST(ProbDistributionsGamma, lcdf_derivatives_y) {
4748
EXPECT_TRUE(std::isfinite(grads[0]));
4849
}
4950

50-
TEST(ProbDistributionsGamma, lcdf_derivatives_alpha) {
51+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_derivatives_alpha) {
5152
using stan::math::gamma_lcdf;
5253
using stan::math::var;
5354

@@ -70,7 +71,7 @@ TEST(ProbDistributionsGamma, lcdf_derivatives_alpha) {
7071
EXPECT_TRUE(std::isfinite(grads[0]));
7172
}
7273

73-
TEST(ProbDistributionsGamma, lcdf_derivatives_beta) {
74+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_derivatives_beta) {
7475
using stan::math::gamma_lcdf;
7576
using stan::math::var;
7677

@@ -93,7 +94,7 @@ TEST(ProbDistributionsGamma, lcdf_derivatives_beta) {
9394
EXPECT_TRUE(std::isfinite(grads[0]));
9495
}
9596

96-
TEST(ProbDistributionsGamma, lcdf_derivatives_all_params) {
97+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_derivatives_all_params) {
9798
using stan::math::gamma_lcdf;
9899
using stan::math::var;
99100

@@ -123,7 +124,7 @@ TEST(ProbDistributionsGamma, lcdf_derivatives_all_params) {
123124
EXPECT_GT(grads[0], 0.0) << "d/dy should be positive";
124125
}
125126

126-
TEST(ProbDistributionsGamma, lcdf_finite_diff_y) {
127+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_finite_diff_y) {
127128
using stan::math::gamma_lcdf;
128129
using stan::math::var;
129130

@@ -149,7 +150,7 @@ TEST(ProbDistributionsGamma, lcdf_finite_diff_y) {
149150
EXPECT_NEAR(grad_autodiff, grad_findiff, 1e-5);
150151
}
151152

152-
TEST(ProbDistributionsGamma, lcdf_finite_diff_alpha) {
153+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_finite_diff_alpha) {
153154
using stan::math::gamma_lcdf;
154155
using stan::math::var;
155156

@@ -176,7 +177,7 @@ TEST(ProbDistributionsGamma, lcdf_finite_diff_alpha) {
176177
EXPECT_NEAR(grad_autodiff, grad_findiff, 1e-3);
177178
}
178179

179-
TEST(ProbDistributionsGamma, lcdf_finite_diff_beta) {
180+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_finite_diff_beta) {
180181
using stan::math::gamma_lcdf;
181182
using stan::math::var;
182183

@@ -202,7 +203,7 @@ TEST(ProbDistributionsGamma, lcdf_finite_diff_beta) {
202203
EXPECT_NEAR(grad_autodiff, grad_findiff, 1e-5);
203204
}
204205

205-
TEST(ProbDistributionsGamma, lcdf_extreme_values_small) {
206+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_extreme_values_small) {
206207
using stan::math::gamma_lcdf;
207208
using stan::math::var;
208209

@@ -230,7 +231,7 @@ TEST(ProbDistributionsGamma, lcdf_extreme_values_small) {
230231
}
231232
}
232233

233-
TEST(ProbDistributionsGamma, lcdf_extreme_values_large) {
234+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_extreme_values_large) {
234235
using stan::math::gamma_lcdf;
235236
using stan::math::var;
236237

@@ -258,7 +259,7 @@ TEST(ProbDistributionsGamma, lcdf_extreme_values_large) {
258259
}
259260
}
260261

261-
TEST(ProbDistributionsGamma, lcdf_alpha_one_derivatives) {
262+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_alpha_one_derivatives) {
262263
using stan::math::gamma_lcdf;
263264
using stan::math::var;
264265

@@ -286,7 +287,7 @@ TEST(ProbDistributionsGamma, lcdf_alpha_one_derivatives) {
286287
EXPECT_NEAR(grads[0], expected_dy, 1e-10);
287288
}
288289

289-
TEST(ProbDistributionsGamma, lcdf_various_parameter_combinations) {
290+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_various_parameter_combinations) {
290291
using stan::math::gamma_lcdf;
291292
using stan::math::var;
292293

@@ -322,7 +323,7 @@ TEST(ProbDistributionsGamma, lcdf_various_parameter_combinations) {
322323
}
323324
}
324325

325-
TEST(ProbDistributionsGamma, lcdf_consistency_with_lccdf) {
326+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_consistency_with_lccdf) {
326327
using stan::math::gamma_lccdf;
327328
using stan::math::gamma_lcdf;
328329
using stan::math::var;
@@ -357,7 +358,7 @@ TEST(ProbDistributionsGamma, lcdf_consistency_with_lccdf) {
357358
EXPECT_LT(grads_lccdf[0], 0.0);
358359
}
359360

360-
TEST(ProbDistributionsGamma, lcdf_second_derivative_y) {
361+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_second_derivative_y) {
361362
using stan::math::gamma_lcdf;
362363
using stan::math::var;
363364

@@ -379,7 +380,8 @@ TEST(ProbDistributionsGamma, lcdf_second_derivative_y) {
379380
EXPECT_TRUE(std::isfinite(grads[0]));
380381
}
381382

382-
TEST(ProbDistributionsGamma, lcdf_numerically_challenging_derivatives) {
383+
TEST_F(AgradRev,
384+
ProbDistributionsGamma_lcdf_numerically_challenging_derivatives) {
383385
using stan::math::gamma_lcdf;
384386
using stan::math::var;
385387

@@ -416,7 +418,7 @@ TEST(ProbDistributionsGamma, lcdf_numerically_challenging_derivatives) {
416418
}
417419
}
418420

419-
TEST(ProbDistributionsGamma, lcdf_monotonic_derivative) {
421+
TEST_F(AgradRev, ProbDistributionsGamma_lcdf_monotonic_derivative) {
420422
using stan::math::gamma_lcdf;
421423
using stan::math::var;
422424

0 commit comments

Comments
 (0)