Testing new algorithms in PHP
Archive - Originally posted on "The Horse's Mouth" - 2016-02-20 20:29:15 - Graham EllisDuring algorithm development, code testing, experiments and spike solutions often account for far more code than the final working code - a classic example of that today where I was writing / testing a loop for Lisa.
Scenario - our booking database holds flags for each of our five hotel bedrooms for each day indication if they're booked and confirmed, booked but only provisionally, or open / available (that last being the absence of a flag). There's a requirement to check a new booking as it's being entered, given a starting date and a duration, to see if it conflicts with an existing booking.
The actual algorithm (I'm using PHP) I used turns out to be short:
$conflict = 0;
for ($k=0; $k<$user["number of nights"]; $k++) {
if ($data[$user["room"]][$k+$offset] != "") $conflict += 1;
}
But there's a whole number of setup elemsnts in there too.
I had to set up user data for the tests:
$user = array();
$user["day number"] = 710028;
$user["room"] = "Bedroom 2";
$user["number of nights"] = 6;
I had to provide dummy (mock) database data to check against:
$data = array();
$data["Bedroom 1"] = array("","","","","","","","","","","","");
$data["Bedroom 2"] = array("","confirmed","","confirmed","confirmed","confirmed","","","","","","");
$data["Bedroom 3"] = array("","","","","","prov","prov","prov","","","","");
$data["Bedroom 4"] = array("","","","","","","confirmed","confirmed","","","","");
$data["Bedroom 5"] = array("","","","","","","","","","","","");
$offset = /* $user["day number"] */ + 2;
I added tracing reports within the algorithm loop to check what was happeneing:
print ($user["room"] . "/ day " . ($k+$offset+$user["day number"]) . " --- ");
print ($data[$user["room"]][$k+$offset]. "\n");
and I had to output my success or failure too:
if ($conflict == 0) {
print ("That's fine\n");
} else {
print ("Problem for $conflict night(s)\n");
}
In order to test multiple cases (and not just one), I've also added a loop to provide somewhat more tests than just th eone, but even that's limited and I could probably do much, much better. Loop is:
foreach (array("Bedroom 1","Bedroom 2","Bedroom 3","Bedroom 4","Bedroom 5") as $user["room"]) {
through to
}
and, yes, I should be using nested loops rather than repeating the word "Bedroom" all through!!
Code [here] for the complete example, together with results
The example above is only one element of the whole specifcation, development, testing or algorithm, implementation, and continued testing at future code changes / enhancements that'e needed for production-quality code. I would strongly recommend:
a) Algorithems be wrapped into named blocks of code (functions, subroutines, methods) with inputs passed in, results resturned and an absolute minimum of globals
b) A suite of tests be built up so that as the application grows they can all be run / rerun at a single call to encourage an easy "let's check I haven't upset anything" whenever a code change is made
c) The suite of tests be setup to check (assert) that a particular results is returned, so that a test management program can easily and efficiently flag that all tests have worked, or can highlight where problems exist in just one or two places in hundreds or perhaps thousands of tests
d) Addition of new tests where a bug or suspected bug is found, even before the bug is fixed. That was, the tests will show "yes, we have a problem", then when it's fixed they'll show the problem has been cured and that no other problem has been introduced that causes some other test that was working to now fail