[libc-commits] [PATCH] D73668: [libc] Add [EXPECT|ASSERT]_[TRUE|FALSE] unittest macros.

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Jan 29 15:38:42 PST 2020


sivachandra created this revision.
sivachandra added a reviewer: gchatelet.
Herald added subscribers: libc-commits, tschuett, MaskRay.
Herald added a project: libc-project.

These macros can only be used for values of type `bool`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73668

Files:
  libc/utils/CPP/TypeTraits.h
  libc/utils/UnitTest/Test.h


Index: libc/utils/UnitTest/Test.h
===================================================================
--- libc/utils/UnitTest/Test.h
+++ libc/utils/UnitTest/Test.h
@@ -82,6 +82,15 @@
                           (unsigned long long)RHS, LHSStr, RHSStr, File, Line);
   }
 
+  // Restricting the ValType to bool ensures that only values of type bool
+  // are used with [EXPECT|ASSERT]_[TRUE|FALSE].
+  template <typename ValType,
+            cpp::EnableIfType<cpp::IsBool<ValType>::Value, ValType> = true>
+  static bool testTrue(RunContext &Ctx, ValType Val, const char *ValStr,
+                       const char *File, unsigned long Line) {
+    return Val;
+  }
+
   static bool testStrEq(RunContext &Ctx, const char *LHS, const char *RHS,
                         const char *LHSStr, const char *RHSStr,
                         const char *File, unsigned long Line);
@@ -176,3 +185,17 @@
 #define ASSERT_STRNE(LHS, RHS)                                                 \
   if (!EXPECT_STRNE(LHS, RHS))                                                 \
   return
+
+#define EXPECT_TRUE(VAL)                                                       \
+  __llvm_libc::testing::Test::testTrue(Ctx, (VAL), #VAL, __FILE__, __LINE__)
+
+#define ASSERT_TRUE(VAL)                                                       \
+  if (!EXPECT_TRUE(VAL))                                                       \
+  return
+
+#define EXPECT_FALSE(VAL)                                                      \
+  __llvm_libc::testing::Test::testTrue(Ctx, !(VAL), #VAL, __FILE__, __LINE__)
+
+#define ASSERT_FALSE(VAL)                                                      \
+  if (!EXPECT_FALSE(VAL))                                                      \
+  return
Index: libc/utils/CPP/TypeTraits.h
===================================================================
--- libc/utils/CPP/TypeTraits.h
+++ libc/utils/CPP/TypeTraits.h
@@ -40,6 +40,9 @@
 template <typename Type> struct IsIntegralNotBool : public IsIntegral<Type> {};
 template <> struct IsIntegralNotBool<bool> : public FalseValue {};
 
+template <typename Type> struct IsBool : public FalseValue {};
+template <> struct IsBool<bool> : public TrueValue {};
+
 template <typename T> struct IsPointerType : public FalseValue {};
 template <typename T> struct IsPointerType<T *> : public TrueValue {};
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73668.241305.patch
Type: text/x-patch
Size: 2344 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20200129/eac521c8/attachment.bin>


More information about the libc-commits mailing list