lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [5979] Add some new macros, with a unit test that fails to


From: Greg Chicares
Subject: [lmi-commits] [5979] Add some new macros, with a unit test that fails to compile
Date: Fri, 10 Oct 2014 17:13:31 +0000

Revision: 5979
          http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=5979
Author:   chicares
Date:     2014-10-10 17:13:30 +0000 (Fri, 10 Oct 2014)
Log Message:
-----------
Add some new macros, with a unit test that fails to compile

Modified Paths:
--------------
    lmi/trunk/ChangeLog
    lmi/trunk/assert_lmi.hpp
    lmi/trunk/assert_lmi_test.cpp

Modified: lmi/trunk/ChangeLog
===================================================================
--- lmi/trunk/ChangeLog 2014-10-10 15:51:19 UTC (rev 5978)
+++ lmi/trunk/ChangeLog 2014-10-10 17:13:30 UTC (rev 5979)
@@ -34286,3 +34286,10 @@
 the lmi epoch: messagebox title swapped with message. See:
   http://lists.nongnu.org/archive/html/lmi/2014-10/msg00032.html
 
+20141010T1713Z <address@hidden> [533]
+
+  assert_lmi.hpp
+  assert_lmi_test.cpp
+Add some new macros, with a unit test that fails to compile. See:
+  http://lists.nongnu.org/archive/html/lmi/2014-10/msg00023.html
+

Modified: lmi/trunk/assert_lmi.hpp
===================================================================
--- lmi/trunk/assert_lmi.hpp    2014-10-10 15:51:19 UTC (rev 5978)
+++ lmi/trunk/assert_lmi.hpp    2014-10-10 17:13:30 UTC (rev 5979)
@@ -59,5 +59,36 @@
         }                                                       \
     while(0)
 
+/// This LMI_ASSERT variant displays an extra message on failure.
+///
+/// The 'message' parameter is deliberately not token-pasted, so
+/// that it can include streaming operators, e.g.:
+///   "value is " << value
+
+#define LMI_ASSERT_WITH_MSG(condition,message)                 \
+    do                                                         \
+        {                                                      \
+        if(!(condition))                                       \
+            {                                                  \
+            std::ostringstream oss;                            \
+            oss                                                \
+                << "Assertion '" << (#condition) << "' failed" \
+                << "\n(" << message << ")."                    \
+                << "\n[file " << __FILE__                      \
+                << ", line " << __LINE__ << "]\n"              \
+                ;                                              \
+            throw std::runtime_error(oss.str());               \
+            }                                                  \
+        }                                                      \
+    while(0)
+
+/// This LMI_ASSERT variant displays both its parameters if unequal.
+
+#define LMI_ASSERT_EQUAL(observed,expected)                     \
+    LMI_ASSERT_WITH_MSG                                         \
+        (observed == expected                                   \
+        ,"expected " << expected << " vs observed " << observed \
+        )
+
 #endif // assert_lmi_hpp
 

Modified: lmi/trunk/assert_lmi_test.cpp
===================================================================
--- lmi/trunk/assert_lmi_test.cpp       2014-10-10 15:51:19 UTC (rev 5978)
+++ lmi/trunk/assert_lmi_test.cpp       2014-10-10 17:13:30 UTC (rev 5979)
@@ -30,6 +30,12 @@
 
 #include "test_tools.hpp"
 
+#define SINGLY_STRINGIFY(Z) #Z
+#define DOUBLY_STRINGIFY(Z) SINGLY_STRINGIFY(Z)
+
+#define LMI_LOCATION \
+    "\n[file " __FILE__ ", line " DOUBLY_STRINGIFY(__LINE__) "]\n"
+
 int test_main(int, char*[])
 {
     LMI_ASSERT(true);
@@ -48,6 +54,26 @@
     else
         LMI_ASSERT(!not_true);
 
+    // This use-case demonstrates why LMI_ASSERT_WITH_MSG's second
+    // parameter is not token-pasted. This is a deliberate tradeoff,
+    // with the consequence that this:
+    //   LMI_ASSERT_WITH_MSG(1&1,1&1);
+    // fails to compile.
+    BOOST_TEST_THROW
+        (LMI_ASSERT_WITH_MSG(not_true,"<" << not_true << ">")
+        ,std::runtime_error
+        ,"Assertion 'not_true' failed\n(<0>)." LMI_LOCATION
+        );
+
+    BOOST_TEST_THROW
+        (LMI_ASSERT_EQUAL(not_true,true)
+        ,std::runtime_error
+        ,"Assertion 'not_true == true' failed\n(expected 1 vs observed 0)." 
LMI_LOCATION
+        );
+
+    // It does seem wrong that this fails to compile:
+    LMI_ASSERT_EQUAL(1&1,1&1);
+
     return 0;
 }
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]