Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-03-2010, 04:55 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
QuickCheck

A library for automated specification-based testing. Inspired by QuickCheck, but pretty different because of how different Haskell is from GS2.

To use QuickCheck, you define a function which returns true or false based on if the defined test succeeds:
PHP Code:
  temp.= function (temp.c) {
    return 
temp.20// Is the passed value less than 20?
  
}; 
The example here would only return true if the passed value is less than 20.


Now, here comes the QuickCheck magic:
PHP Code:
  this.quickCheck(
    
choose(419), // create a generator which chooses a random number between 4 and 19
    
temp.// pass our test function
  
); 
What happens here is that we call choose(lower, upper) which returns a "function that generates a random value" (a.k.a. generator). The random value would be between 4 and 19 in our case.
Then we pass it to quickCheck(domain, testFunction), which will run this.bound many trials using a different arbitrary value each time.


All together now:
PHP Code:
this.join("utility_quickcheck");
//#CLIENTSIDE
function onCreated() {

  
// Make the test function
  
temp.= function (temp.c) {
    return 
temp.20// Is the passed value less than 20?
  
};
  
this.quickCheck(
    
choose(419), // create a generator which chooses a random number between 4 and 19.
    
temp.// pass our test function
  
);
  
this.quickCheck(
    
choose(625), // create a generator which chooses a random number between 6 and 25.
    
temp.// pass our test function
  
); 
I also added another quickCheck (which will supposedly fail since it can generate a number greater than 20).

Here is the output:
NPC Code:
Succeeded 100 times.
Failed on 2:24



First check worked, second one failed on the second trial (which used the number 24, and was indeed out of bounds).
Looks like it works.







Here is another more advanced example:
PHP Code:
this.join("utility_quickcheck");
this.join("algorithm_introsort");
//#CLIENTSIDE
function onCreated() {

  
temp.= function (temp.c) {
  
    
temp.introsort(temp.c); // We want to test our example function "introsort" which sorts a passed in list.
    
    // In order to succeed, a random element in the list must be smaller than any random element after that inital element.
    
temp.r1 random(0temp.c.size() - 1);
    return 
temp.c[temp.r1] <= temp.c[temp.r1 random(1temp.c.size() - temp.r1)];
  };
  
this.quickCheck(
    
vectorOf(20choose(10100)), // create a 20 element list, filled with random numbers between 10 and 100.
    
temp.// pass our test function
  
);

This time we generate a 20-element list filled with random numbers between 10 and 100.

It then gets passed through a function we want to test (in this case, introsort). If the condition evaluates to true, that means the sort worked!

Output:
NPC Code:
Succeeded 100 times.



Looks like our introsort function is working fine.




Those were two simple examples, you could get more complicated, and I may write an article about doing real-world test cases.

I may also update this script at some point to make it more powerful.


Only works in v6, sorry!

For this to work you also need utility_bind.
Attached Files
File Type: txt utility_quickcheck.txt (3.4 KB, 311 views)
Reply With Quote
Reply


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


All times are GMT +2. The time now is 02:30 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.