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.f = function (temp.c) {
return temp.c < 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(4, 19), // create a generator which chooses a random number between 4 and 19
temp.f // 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.f = function (temp.c) {
return temp.c < 20; // Is the passed value less than 20?
};
this.quickCheck(
choose(4, 19), // create a generator which chooses a random number between 4 and 19.
temp.f // pass our test function
);
this.quickCheck(
choose(6, 25), // create a generator which chooses a random number between 6 and 25.
temp.f // 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.f = function (temp.c) {
temp.c = 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(0, temp.c.size() - 1);
return temp.c[temp.r1] <= temp.c[temp.r1 + random(1, temp.c.size() - 1 - temp.r1)];
};
this.quickCheck(
vectorOf(20, choose(10, 100)), // create a 20 element list, filled with random numbers between 10 and 100.
temp.f // 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.