Prevent NaN and Inf values in webrtc_perf_test
That is, cause a fatal error when a test produces such a result.
Bug: webrtc:9767
Change-Id: I588a34aa1e7e34b3036d5661e894676b21072862
Reviewed-on: https://webrtc-review.googlesource.com/c/101320
Commit-Queue: Oleh Prypin <oprypin@webrtc.org>
Reviewed-by: Yves Gerey <yvesg@webrtc.org>
Reviewed-by: Patrik Höglund <phoglund@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24950}
diff --git a/test/BUILD.gn b/test/BUILD.gn
index 0daae3f..c7f7383 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -127,6 +127,7 @@
deps = [
"..:webrtc_common",
"../api:array_view",
+ "../rtc_base:checks",
"../rtc_base:rtc_base_approved",
]
}
diff --git a/test/testsupport/perf_test.cc b/test/testsupport/perf_test.cc
index c59c071..bd810e3 100644
--- a/test/testsupport/perf_test.cc
+++ b/test/testsupport/perf_test.cc
@@ -9,9 +9,11 @@
*/
#include "test/testsupport/perf_test.h"
+#include "rtc_base/checks.h"
#include "rtc_base/criticalsection.h"
#include <stdio.h>
+#include <cmath>
#include <fstream>
#include <map>
#include <sstream>
@@ -44,6 +46,8 @@
const double value,
const std::string& units,
const bool important) {
+ RTC_CHECK(std::isfinite(value));
+
std::ostringstream value_stream;
value_stream.precision(8);
value_stream << value;
@@ -64,6 +68,9 @@
const double error,
const std::string& units,
const bool important) {
+ RTC_CHECK(std::isfinite(mean));
+ RTC_CHECK(std::isfinite(error));
+
std::ostringstream value_stream;
value_stream.precision(8);
value_stream << '{' << mean << ',' << error << '}';
@@ -84,6 +91,10 @@
const rtc::ArrayView<const double> values,
const std::string& units,
const bool important) {
+ for (double v : values) {
+ RTC_CHECK(std::isfinite(v));
+ }
+
std::ostringstream value_stream;
value_stream.precision(8);
value_stream << '[';
diff --git a/test/testsupport/perf_test_unittest.cc b/test/testsupport/perf_test_unittest.cc
index ac2b8b9..2c8a93c 100644
--- a/test/testsupport/perf_test_unittest.cc
+++ b/test/testsupport/perf_test_unittest.cc
@@ -10,6 +10,7 @@
#include "test/testsupport/perf_test.h"
+#include <limits>
#include <string>
#include "test/gtest.h"
@@ -104,5 +105,25 @@
EXPECT_EQ(R"({"format_version":"1.0","charts":{}})", GetPerfResultsJSON());
}
+#if GTEST_HAS_DEATH_TEST
+using PerfDeathTest = PerfTest;
+
+TEST_F(PerfDeathTest, TestFiniteResultError) {
+ const double kNan = std::numeric_limits<double>::quiet_NaN();
+ const double kInf = std::numeric_limits<double>::infinity();
+
+ EXPECT_DEATH(PrintResult("a", "b", "c", kNan, "d", false), "finit");
+ EXPECT_DEATH(PrintResult("a", "b", "c", kInf, "d", false), "finit");
+
+ EXPECT_DEATH(PrintResultMeanAndError("a", "b", "c", kNan, 1, "d", false), "");
+ EXPECT_DEATH(PrintResultMeanAndError("a", "b", "c", 1, kInf, "d", false), "");
+
+ const double kNanList[] = {kNan, kNan};
+ EXPECT_DEATH(PrintResultList("a", "b", "c", kNanList, "d", false), "");
+ const double kInfList[] = {0, kInf};
+ EXPECT_DEATH(PrintResultList("a", "b", "c", kInfList, "d", false), "");
+}
+#endif
+
} // namespace test
} // namespace webrtc