[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: PATCH: getting rid of typeof(x)
From:       Stefan Westerfeld <stefan () space ! twc ! de>
Date:       2001-02-02 18:52:08
[Download RAW message or body]

Hi!

On Fri, Feb 02, 2001 at 01:57:38PM +0000, Luis Coelho wrote:
> On Quinta,  1 de Fevereiro de 2001 19:41, Stefan Westerfeld wrote:
> > typeof(x) is a gcc extension, which works like this:
> >
> > 	int a;
> > 	typeof(a) b;		/* expands to int b; */
> >
> > I use that in kdelibs/arts/tests/test.h (only compiled when using make
> > check), to prevent that the testEquals macro evaluates it's arguments
> > twice.
> >
> > Since it is a gcc extension I probably shouldn't use it. So here is a patch
> > that replaces it by a template based construct. I am not really proud of
> > the new code, but it should do the same thing that the old code did.
> 
> I think you could do it with just a function:
> 
> template <typename T1, typename T2>
> inline void checkEqualsFunction(const T1& v1, const T2& v2, const char* file, 
> int line, const char* expr1, const char* expr2)
> {
> 	if ( !(v1 == static_cast<T1>(v2)) ) {
> 		fprintf (stderr,"\n  %s::%s (%s:%d)\n",
> 			currentClass, currentTest, file, line);
> 		fprintf (stderr,"   => test assertion failed: (%s == %s)\n",expr1, expr2);
> 		cerr << "   => expected '" << v1 << "'" << " got "
> 		 	 << "'" << v2 << "'." << endl;
> 		exit(1);
> 	}
> }
> 
> #define testEquals(expr1,expr2) \
> 	testsEqualsFunction((expr1),(expr2),__FILE__,__LINE__, #expr1,#expr2);

Oh yes, you are right, that looks much better. And it seems to work, too.
The only thing I changed is to cast to the "got" type rather than the
"expected" type, as otherwise:

	string x = "Hello";
	testEquals("Hello",x);

will fail (you shouldn't cast a string to (const char *)).

> Why does it print to both stderr using fprintf and to cerr?

It's copypasted from testCaption(). But you are right, having both in one
function looks strange. So for the next patch, I made it cerr-only. However,
I will not change all other instances of fprintf to iostreams or vice versa.
Better only change lines of code you really need to that short before the
release.

I attached a new patch.

   Cu... Stefan
-- 
  -* Stefan Westerfeld, stefan@space.twc.de (PGP!), Hamburg/Germany
     KDE Developer, project infos at http://space.twc.de/~stefan/kde *-         

["20010202-no-typeof.diff" (text/plain)]

Index: test.h
===================================================================
RCS file: /home/kde/kdelibs/arts/tests/test.h,v
retrieving revision 1.3
diff -u -r1.3 test.h
--- test.h	2000/11/18 12:12:14	1.3
+++ test.h	2001/02/02 18:42:24
@@ -41,20 +41,28 @@
 		exit(1); \
 	 }
 
-#define testEquals(expr, expr2) 										\
-	{																	\
-		typeof(expr2) val1 = expr, val2 = expr2;						\
-		if(!(val1 == val2))												\
-		{																\
-	 		testCaption();												\
-	 		fprintf (stderr,"   => test assertion failed: (%s == %s)\n",\
-														#expr, #expr2); \
-	 		cerr << "   => expected '" << (val1) << "'" << " got "		\
-			 	 << "'" << (val2) << "'." << endl;						\
-			exit(1);													\
-		}																\
+template<typename T1, typename T2>
+inline void testEqualsCheck(const T1& expected, const T2& got,
+		 const char *file, int line, const char *expr, const char *expr2)
+{
+	if(! (static_cast<T2>(expected) == got) ) {
+ 		cerr << endl
+
+			 << "  " << currentClass << "::" << currentTest
+			 << " (" << file << ":" << line << ")" << endl
+
+ 			 << "   => test assertion failed:"
+			 << " (" << expr << " == " << expr2 << ")" << endl
+
+ 			 << "   => expected '" << expected << "'" << " got "
+		 	 << "'" << got << "'." << endl;
+		exit(1);
 	}
+}
 
+#define testEquals(expr, expr2) 										\
+	testEqualsCheck(expr, expr2, __FILE__, __LINE__, #expr, #expr2)
+	
 #define TEST(func)													\
 	struct t ## func : TestClass 									\
 	{																\


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic