Page 1 of 1

Useful True, False and Assert macros for inline expressions

Posted: Tue Jun 16, 2009 7:50 pm
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.

Posted: Thu Jun 18, 2009 6:27 am
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.

Posted: Thu Jun 18, 2009 9:48 am
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...

Posted: Thu Jun 18, 2009 1:10 pm
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.

Posted: Thu Jun 18, 2009 2:43 pm
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.

Posted: Sat Jun 20, 2009 4:09 pm
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