AutoHotkey Community

It is currently May 27th, 2012, 10:01 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: April 19th, 2005, 3:07 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
If you define a function, it is a good practice to test it with all invalid, extreme and many random normal parameter values. To facilitate this I wrote the simple TEST function, with the first parameter A_LineNumber, and the second parameter the expression I want to evaluate. The function reads the source file, removes the beginning and end of the line containing the expression and shows the expression and its value in a MsgBox, with the ErrorLevel after the expression got evaluated. I usually copy the text "TEST(A_LineNumber,)" into my private clipboard and insert it wherever the test cases appear in the script. Then I write the expression to be tested after the comma. It needs AHK version 1.0.31.05+
Code:
TEST(A_LineNumber,SQRT(2))
TEST(A_LineNumber,SQRT(9))
TEST(A_LineNumber,SQRT(-1))

SQRT(x)
{
   Transform y, sqrt, %x%
   Return y
}

TEST(A,x)   ; Display expression and its value: TEST(A_LineNumber,3*a+2)
{
   E = %ErrorLevel%
   FileReadLine Line, %A_LineFile%, %A%
   StringTrimLeft  Line, Line, 18
   StringTrimRight Line, Line, 1
   MsgBox %Line% = %x%`nErrorLevel = %E%
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 19th, 2005, 3:35 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
I like your idea. That is brilliant. It gives a nice overview of what the fuction can do (it limits).

I moded you code and my suggestion would be something like this:
Code:
ValuesToTest = 2|9|-1
FunctionToTest = SQRT(#)

SQRT(x)
{
   Transform y, sqrt, %x%
   Return y
}

Loop, Parse,ValuesToTest,|
  {
    StringReplace, Function, FunctionToTest, # , %A_LoopField%
    ReturnValue := % Function   ;this doesn't work :(
    MsgBox, Function to test: %FunctionToTest%`nValue given: >%A_LoopField%<`nFunction parsed: %Function%`nReturn value: >%ReturnValue%<`nErrorLevel: %ErrorLevel%
  }


If it worked, this script would be more flexible (for functions with not pipe delimitered parameters), and is gives you the returnvalue not the private variable "x". The problem I have is that I can't call the function via a variable, see comment in code.

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 19th, 2005, 4:21 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
You are right, toralf! If we had indirect function calls, everything would be easier. [Chris, is it planned?] Even now your idea works:
Code:
ValuesToTest = 2|9|-1

Loop Parse, ValuesToTest, |
    MsgBox % "SQRT(" A_LoopField ") = " SQRT(A_LoopField) "`nErrorLevel = " ErrorLevel

SQRT(x)
{
   Transform y, sqrt, %x%
   Return y
}
You have to write the ValuesToTest and to copy the 2 lines of the loop to the place of testing. Replace the 2 function names inside the loop. If there are many test cases with constants, this solution is shorter. However, another level of indirection is necessary if the ValuesToTest are expressions:
Code:
a = 4
ValuesToTest = 2|9|-1|a*a
Expressions are not evaluated dynamically. It would be another desirable feature.
Schöne Idee!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 20th, 2005, 1:43 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Laszlo wrote:
You are right, toralf! If we had indirect function calls, everything would be easier.
It's on the to-do list but it might be a while in coming since it seems far less useful than dynamic variables.

Quote:
Expressions are not evaluated dynamically. It would be another desirable feature.
That feature was omitted for performance and code complexity reasons. It might get added someday but probably only after the previous idea.

Thanks for the ideas.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: sks and 21 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group