0

Consider I have hot function with a loop and there is gtest assertion in it:

for (i = 0; i < BIG_NUMBER; i++)
{
    EXPECT_TRUE(a[i] > 0.) << "a[i] = " << a[i];
    c[i] = a[i] + b[i];
}

I want to have 2 different build types for the program:

  1. With all assertions enabled (debug type)
  2. With all assertions disabled (release type)

Is it possible?

Maybe is it possible to re-define the macro EXPECT_TRUE?

Ilya Palachev
  • 274
  • 1
  • 2
  • 16
  • Do you mean having that assert enabled when you compile in debug mode and disabled in release mode? What's wrong with #ifdefs? You could of course change gtest's sources but #ifdefs would be a cleaner way to do it – Marco A. Oct 09 '14 at 16:57
  • I haven't used google test assertions but looking at the code you have, you have to not only disable the tests but you'll also have to define it to something which supports the overloaded `<<` operator. – R Sahu Oct 09 '14 at 16:57
  • @Marco A., yes of course, I meant to use `#ifdef`s to re-define the macro. – Ilya Palachev Oct 09 '14 at 17:08
  • @R Sahu, yes, maybe this: http://stackoverflow.com/questions/760301/implementing-a-no-op-stdostream – Ilya Palachev Oct 09 '14 at 17:11
  • But I wonder whether there is some commonly-used techique for that. Becase it look strange that disabling debug output needs some additional implementation. – Ilya Palachev Oct 09 '14 at 17:12
  • @IlyaPalachev `EXPECT_TRUE` is _not_ debug output. It is a unit test. To disable asserts, just compile with `NDEBUG` defined. – James Kanze Oct 09 '14 at 17:34

1 Answers1

0

First, I can't imagine wanting to do this, except locally, to make the tests run faster to get to some more exotic cases; EXPECT_TRUE et al. are only useful in the Google test environment, and should only appear in unit tests, not in the body of your code.

Locally, I'd use a separate macro (so that someone reading the code knows immediatly that it is a conditional test), say COND_EXPECT_TRUE (for conditional EXPECT_TRUE), defined something like:

#ifdef ALL_TESTS
#define COND_EXPECT_TRUE EXPECT_TRUE
#else
#define COND_EXPECT_TRUE dummyOutput
#endif

, where dummyOutput is an unopened std::ofstream somewhere. (Or if you really want to be sure, you can define a nullstream class, which outputs into thin air. But in this case, and conversions in the output will still occur; in an unopened std::ofstream, the fact that it is in an error state inhibits the conversions.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329