summaryrefslogtreecommitdiff
path: root/base/include/android-base/test_utils.h
diff options
context:
space:
mode:
authorJosh Gao <jmgao@google.com>2019-11-05 13:46:14 -0800
committerJosh Gao <jmgao@google.com>2019-12-12 14:25:45 -0800
commit81e316b3a2bb55c88cd4143bacee8440254d35e4 (patch)
treef73cf0ec4640abf5737170a11ea60d7d885f23fc /base/include/android-base/test_utils.h
parent40a8b07e4dd786a445648deded7a4ecac5acc81e (diff)
base: avoid evaluating macro argument multiple times.
Previously, in the regex test helpers, we would evaluate the haystack expression again to generate the error message, which leads to nonsensical errors if the expression returns a different value on the second call (e.g. functions like dlerror which return null on subsequent calls). Test: bionic-unit-tests with a failure Test: treehugger Change-Id: I2126cefeb45e26638194af8a82d0f2a9d7196edf
Diffstat (limited to 'base/include/android-base/test_utils.h')
-rw-r--r--base/include/android-base/test_utils.h44
1 files changed, 24 insertions, 20 deletions
diff --git a/base/include/android-base/test_utils.h b/base/include/android-base/test_utils.h
index b20f278462..f3d7cb031d 100644
--- a/base/include/android-base/test_utils.h
+++ b/base/include/android-base/test_utils.h
@@ -53,30 +53,34 @@ class CapturedStdout : public CapturedStdFd {
CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {}
};
-#define ASSERT_MATCH(str, pattern) \
- do { \
- if (!std::regex_search((str), std::regex((pattern)))) { \
- FAIL() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \
- } \
+#define ASSERT_MATCH(str, pattern) \
+ do { \
+ auto __s = (str); \
+ if (!std::regex_search(__s, std::regex((pattern)))) { \
+ FAIL() << "regex mismatch: expected " << (pattern) << " in:\n" << __s; \
+ } \
} while (0)
-#define ASSERT_NOT_MATCH(str, pattern) \
- do { \
- if (std::regex_search((str), std::regex((pattern)))) { \
- FAIL() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \
- } \
+#define ASSERT_NOT_MATCH(str, pattern) \
+ do { \
+ auto __s = (str); \
+ if (std::regex_search(__s, std::regex((pattern)))) { \
+ FAIL() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << __s; \
+ } \
} while (0)
-#define EXPECT_MATCH(str, pattern) \
- do { \
- if (!std::regex_search((str), std::regex((pattern)))) { \
- ADD_FAILURE() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \
- } \
+#define EXPECT_MATCH(str, pattern) \
+ do { \
+ auto __s = (str); \
+ if (!std::regex_search(__s, std::regex((pattern)))) { \
+ ADD_FAILURE() << "regex mismatch: expected " << (pattern) << " in:\n" << __s; \
+ } \
} while (0)
-#define EXPECT_NOT_MATCH(str, pattern) \
- do { \
- if (std::regex_search((str), std::regex((pattern)))) { \
- ADD_FAILURE() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \
- } \
+#define EXPECT_NOT_MATCH(str, pattern) \
+ do { \
+ auto __s = (str); \
+ if (std::regex_search(__s, std::regex((pattern)))) { \
+ ADD_FAILURE() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << __s; \
+ } \
} while (0)