Useful True, False and Assert macros for inline expressions

Share your advanced PureBasic knowledge/code with the community.
User avatar
mback2k
Enthusiast
Enthusiast
Posts: 257
Joined: Sun Dec 02, 2007 12:11 pm
Location: Germany

Useful True, False and Assert macros for inline expressions

Post by mback2k »

Code: Select all

Macro Assert(A)
  ((Not (A))!#True)
EndMacro

Macro True(A)
  (Not (A)=#False)
EndMacro

Macro False(A)
  (Not (A)<>#False)
EndMacro

Procedure.s Test(A)
  ProcedureReturn Str(A)
EndProcedure

If OpenConsole()
  Define A = $F0
  Define B = $0F
  
  PrintN("0=" + Test(Assert(A & B)))
  PrintN("0=" + Test(True(A & B)))
  PrintN("1=" + Test(False(A & B)))
  
  PrintN("---")
  
  PrintN("1=" + Test(Assert(A | B)))
  PrintN("1=" + Test(True(A | B)))
  PrintN("0=" + Test(False(A | B)))
  
  PrintN("---")
  
  PrintN("1=" + Test(Assert(A ! B)))
  PrintN("1=" + Test(True(A ! B)))
  PrintN("0=" + Test(False(A ! B)))
  
  PrintN("---")
  
  PrintN("1=" + Test(Assert(A << B)))
  PrintN("1=" + Test(True(A << B)))
  PrintN("0=" + Test(False(A << B)))
  
  PrintN("---")
  
  PrintN("0=" + Test(Assert(A >> B)))
  PrintN("0=" + Test(True(A >> B)))
  PrintN("1=" + Test(False(A >> B)))
  
  PrintN("---")
  
  PrintN("0=" + Test(Assert(A = B)))
  PrintN("1=" + Test(Assert(A <> B)))
  PrintN("0=" + Test(Assert(A < B)))
  PrintN("1=" + Test(Assert(A > B)))
  
  Input()
EndIf
Assert works with any binary expression, including comparisions.
True and False work with any binary expression, except comparisions.

All 3 create a little overhead in CPU cycle consumption, especially the useless "AND x, x" lines, but IMHO thats still better than jumping to a special function which compares parameters.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Just a note to remind people that Assert is a debug and testing "tool", production code does not have assert compiled into them.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

if you say...

... I would have to read Wiki first to get an idea what "assert" could be.
first time I hear this word these days. only knew dessert so far...
oh... and have a nice day.
User avatar
mback2k
Enthusiast
Enthusiast
Posts: 257
Joined: Sun Dec 02, 2007 12:11 pm
Location: Germany

Post by mback2k »

Rescator wrote:Just a note to remind people that Assert is a debug and testing "tool", production code does not have assert compiled into them.
Maybe I should rename my macro to Eval/Expr then, it just allows you to turn any expression into 1 or 0 depending on it's result.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Rescator wrote:Just a note to remind people that Assert is a debug and testing "tool", production code does not have assert compiled into them.
GTK is shipped by distros with asserts enabled. And also it's mighty slow, it could be a connection there.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

@Trond
If that is what they do by default then I'm not surprised it's "slow", there could be quite some overhead with some of those Asserts.

If one need input or data validation it's better (and usually easier) to code that directly instead. Besides, PureBasic got a error library that is way more powerful than Assert and much more useful to the end user.
But during development Asserts are kinda cool.

Then again, didn't the PureBasic team mention working on some Unit Testing stuff some time ago? Which is even cooler. :P
Post Reply