Share your advanced PureBasic knowledge/code with the community.
mback2k
Enthusiast
Posts: 257 Joined: Sun Dec 02, 2007 12:11 pm
Location: Germany
Post
by mback2k » Tue Jun 16, 2009 7:50 pm
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.
Rescator
Addict
Posts: 1769 Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway
Post
by Rescator » Thu Jun 18, 2009 6:27 am
Just a note to remind people that Assert is a debug and testing "tool", production code does not have assert compiled into them.
Kaeru Gaman
Addict
Posts: 4826 Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany
Post
by Kaeru Gaman » Thu Jun 18, 2009 9:48 am
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.
mback2k
Enthusiast
Posts: 257 Joined: Sun Dec 02, 2007 12:11 pm
Location: Germany
Post
by mback2k » Thu Jun 18, 2009 1:10 pm
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
Posts: 7446 Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway
Post
by Trond » Thu Jun 18, 2009 2:43 pm
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.
Rescator
Addict
Posts: 1769 Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway
Post
by Rescator » Sat Jun 20, 2009 4:09 pm
@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.