During 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 (written 2016-02-20, updated 2016-02-21)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q456 - Object Orientation and General technical topics - Test Driven Development and Behaviour Driven Development [4326] Learning to program - comments, documentation and test code - (2014-11-22)
[4336] Test Driven Development - a first example of principle in C - (2014-12-01)
[4346] A behaviour driven example of writing a Java program - (2014-12-09)
[4374] Test driven development, and class design, from first principles (using C++) - (2014-12-30)
[4380] Behaviour Driven Development / Ruby and Cucumber - (2015-01-02)
[4387] Regression Testing my website - Cucumber and Watir - (2015-01-07)
[4457] Test framework for TCL - Tcltest - some examples - (2015-03-11)
[4542] The principle of mocking - and the Python Mock package - (2015-10-17)
[4634] Regression testing - via a very short C testing framework - (2016-01-29)
Q110 - Object Orientation and General technical topics - Programming Algorithms [202] Searching for numbers - (2005-02-04)
[227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[642] How similar are two words - (2006-03-11)
[1157] Speed Networking - a great evening and how we arranged it - (2007-04-21)
[1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1840] Validating Credit Card Numbers - (2008-10-14)
[1949] Nuclear Physics comes to our web site - (2008-12-17)
[2189] Matching disparate referencing systems (MediaWiki, PHP, also Tcl) - (2009-05-19)
[2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
[2509] A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21)
[2586] And and Or illustrated by locks - (2010-01-17)
[2617] Comparing floating point numbers - a word of caution and a solution - (2010-02-01)
[2894] Sorting people by their names - (2010-07-29)
[2951] Lots of way of converting 3 letter month abbreviations to numbers - (2010-09-10)
[2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
[3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3093] How many toilet rolls - hotel inventory and useage - (2010-12-18)
[3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
[3451] Why would you want to use a Perl hash? - (2011-09-20)
[3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
[3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
[4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
[4401] Selecting RECENT and POPULAR news and trends for your web site users - (2015-01-19)
[4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
[4410] A good example of recursion - a real use in Python - (2015-02-01)
[4656] Identifying the first and last records in a sequence - (2016-02-26)
[4707] Some gems from an introduction to Python - (2016-10-29)
H311 - Testing your PHP [3426] Automed web site testing scripted in Ruby using watir-webdriver - (2011-09-09)
[3623] Some TestWise examples - helping use Ruby code to check your web site operation - (2012-02-24)
[3958] Testing classes for your PHP website with PHPUnit - (2012-12-20)
[3959] Testing code coverage (have I tested everything?) in PHP - (2012-12-21)
Some other Articles
Image indexer / thumbnail display scripts in PHPGetting to the Royal United Hospital - the Hopper and the alternativesCoats of arms - towns and authorities in WiltshireTesting new algorithms in PHPPressure selling in the fire safety businessWhy populate object with values as you construct them?Object and Static methods - what is the difference; example in Python 3Why is bus funding a much hotter topic than a new set of traffic lights?On the problems of a printed train timetable