Skip to content

Commit f30c584

Browse files
authored
Merge pull request #3293 from stan-dev/fix/force_ad_cleanup_rev
Adds a check to make sure AgradRev is used for all tests in test/unit/math/rev
2 parents 4dca116 + d1c7701 commit f30c584

15 files changed

Lines changed: 146 additions & 95 deletions

make/tests

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ endif
3838

3939
##
4040
# Adding a test for multiple translation units. If this fails,
41-
# a new function is probably missing an inline
41+
# a public umbrella header likely exposes a new non-inline external definition.
4242
##
4343

4444
ifneq ($(OS),Windows_NT)

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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ 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, precomputed_gradients_containers_direct_construction) {
136135
double value = 1;
137136
std::vector<stan::math::var> vars;
138137
std::vector<double> gradients;

0 commit comments

Comments
 (0)