Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2006, 3:31 PM   #1
JawaKing00
Newbie
 
Join Date: Sep 2004
Posts: 29
Rep Power: 0 JawaKing00 is on a distinguished road
Multiple conditions in an #ifdef statement

I'm trying to add some test code to a previously written program, and I wanted it to be seperated from the rest of the code using #ifdefs so I can easily disable it. There are multiple test cases, but some of the test code is used for multiple tests.

Basically what I have is this:
#ifdef TEST_1
// do stuff
#endif

#ifdef TEST_2
// do stuff
#endif

#ifdef TEST_3
// do stuff
#endif

And what I want is
#ifdef TEST_1 OR TEST_2 OR TEST_3
// do stuff
#endif

Is that possible to do?

Thanks
JawaKing00 is offline   Reply With Quote
Old Apr 26th, 2006, 3:40 PM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
That's going to do the same as if you only had TEST_1 or TEST2 or TEST3 though. Why on earth would you want to handle only one case for 3 test cases?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 26th, 2006, 3:47 PM   #3
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
You can have three #indef's and inside each one, set a flag. After the program finishes evaluating the three conditions, if the flag is on, do stuff. Don't forget to set the flag to false before the #ifdef's
OpenLoop is offline   Reply With Quote
Old Apr 26th, 2006, 4:00 PM   #4
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 4 Kaja Fumei is on a distinguished road
Or you could just do this:

#if defined(TEST_1) || defined(TEST_2) || defined(TEST_3)
  // do stuff
#endif
Kaja Fumei is offline   Reply With Quote
Old Apr 26th, 2006, 4:05 PM   #5
JawaKing00
Newbie
 
Join Date: Sep 2004
Posts: 29
Rep Power: 0 JawaKing00 is on a distinguished road
Quote:
Originally Posted by nnxion
That's going to do the same as if you only had TEST_1 or TEST2 or TEST3 though. Why on earth would you want to handle only one case for 3 test cases?
Well what it's for is in multiple places I set a test bit and that's in a different location for each test, but the bit gets cleared in the same place for most tests.


As for the flag, that should work. Thanks.
JawaKing00 is offline   Reply With Quote
Old Apr 26th, 2006, 4:06 PM   #6
JawaKing00
Newbie
 
Join Date: Sep 2004
Posts: 29
Rep Power: 0 JawaKing00 is on a distinguished road
Quote:
Originally Posted by Kaja Fumei
Or you could just do this:

#if defined(TEST_1) || defined(TEST_2) || defined(TEST_3)
  // do stuff
#endif

Ok, thanks. This is more what I was looking for but I didn't know if that was possible.
JawaKing00 is offline   Reply With Quote
Old Apr 26th, 2006, 4:32 PM   #7
Lich
Professional Programmer
 
Lich's Avatar
 
Join Date: May 2005
Location: Detroit
Posts: 254
Rep Power: 4 Lich is on a distinguished road
Send a message via AIM to Lich Send a message via MSN to Lich
Question

Quote:
Originally Posted by JawaKing00
Ok, thanks. This is more what I was looking for but I didn't know if that was possible.
you may have to put the conditions in () to make it work, not sure though
__________________
--John Cruz
Web Developer
www.cruzweb.net
Lich is offline   Reply With Quote
Old Apr 26th, 2006, 5:05 PM   #8
JawaKing00
Newbie
 
Join Date: Sep 2004
Posts: 29
Rep Power: 0 JawaKing00 is on a distinguished road
I just tested it and it appears to work exactly the way you typed it.

Thanks again.
JawaKing00 is offline   Reply With Quote
Old Apr 26th, 2006, 5:11 PM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by Lich
you may have to put the conditions in () to make it work, not sure though
That is not necessary. It is fine as kaja had it.

Keep in mind that some coding guidelines discourage (or disallow depending on the intent of the guidelines) use of the preprocessor for anything other than include guards in header files.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC