[llvm] [NFC][Support] Add FormatVariadic sub-test for validation (PR #106578)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 29 09:04:12 PDT 2024


https://github.com/jurahul created https://github.com/llvm/llvm-project/pull/106578

- Add validation subtest that tests assert failures in assert enabled builds, 
  and that validation is disabled in assert disabled builds.

>From 87a3330452b5ec9c6565118ede8716947a0c37d5 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Thu, 29 Aug 2024 09:00:52 -0700
Subject: [PATCH] [NFC][Support] Add FormatVariadic sub-test for validation

- Add validation subtest that tests assert failures in assert enabled
  builds, and that validation is disabled in assert disabled builds.
---
 llvm/unittests/Support/FormatVariadicTest.cpp | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/llvm/unittests/Support/FormatVariadicTest.cpp b/llvm/unittests/Support/FormatVariadicTest.cpp
index 6ee0d924867419..7f1e09b1857dde 100644
--- a/llvm/unittests/Support/FormatVariadicTest.cpp
+++ b/llvm/unittests/Support/FormatVariadicTest.cpp
@@ -710,6 +710,23 @@ TEST(FormatVariadicTest, FormatFilterRange) {
   EXPECT_EQ("1, 2, 3", formatv("{0}", Range).str());
 }
 
+TEST(FormatVariadicTest, Validate) {
+#ifndef NDEBUG
+#if GTEST_HAS_DEATH_TEST
+  // If asserts are enabled, verify that invalid formatv calls cause assertions.
+  EXPECT_DEATH(formatv("{0}", 1, 2).str(), "Expected 1 Args, but got 2");
+  EXPECT_DEATH(formatv("{0} {2}", 1, 2, 3).str(),
+               "Replacement field indices cannot have holes");
+#else  // GTEST_HAS_DEATH_TEST
+  GTEST_SKIP() << "No support for EXPECT_DEATH";
+#endif // GTEST_HAS_DEATH_TEST
+#else  // NDEBUG
+  // If asserts are disabled, verify that validation is disabled.
+  EXPECT_EQ(formatv("{0}", 1, 2).str(), "1");
+  EXPECT_EQ(formatv("{0} {2}", 1, 2, 3).str(), "1 3");
+#endif // NDEBUG
+}
+
 namespace {
 
 enum class Base { First };



More information about the llvm-commits mailing list