Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems
 
Testing new algorithms in PHP

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
H311 - Testing your PHP
  [3959] Testing code coverage (have I tested everything?) in PHP - (2012-12-21)
  [3958] Testing classes for your PHP website with PHPUnit - (2012-12-20)
  [3623] Some TestWise examples - helping use Ruby code to check your web site operation - (2012-02-24)
  [3426] Automed web site testing scripted in Ruby using watir-webdriver - (2011-09-09)

Q110 - Object Orientation and General technical topics - Programming Algorithms
  [4707] Some gems from an introduction to Python - (2016-10-29)
  [4656] Identifying the first and last records in a sequence - (2016-02-26)
  [4410] A good example of recursion - a real use in Python - (2015-02-01)
  [4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
  [4401] Selecting RECENT and POPULAR news and trends for your web site users - (2015-01-19)
  [4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
  [3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
  [3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
  [3451] Why would you want to use a Perl hash? - (2011-09-20)
  [3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
  [3093] How many toilet rolls - hotel inventory and useage - (2010-12-18)
  [3072] Finding elements common to many lists / arrays - (2010-11-26)
  [3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
  [2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
  [2951] Lots of way of converting 3 letter month abbreviations to numbers - (2010-09-10)
  [2894] Sorting people by their names - (2010-07-29)
  [2617] Comparing floating point numbers - a word of caution and a solution - (2010-02-01)
  [2586] And and Or illustrated by locks - (2010-01-17)
  [2509] A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21)
  [2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
  [2189] Matching disparate referencing systems (MediaWiki, PHP, also Tcl) - (2009-05-19)
  [1949] Nuclear Physics comes to our web site - (2008-12-17)
  [1840] Validating Credit Card Numbers - (2008-10-14)
  [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
  [1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
  [1157] Speed Networking - a great evening and how we arranged it - (2007-04-21)
  [642] How similar are two words - (2006-03-11)
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
  [202] Searching for numbers - (2005-02-04)

Q456 - Object Orientation and General technical topics - Test Driven Development and Behaviour Driven Development
  [4634] Regression testing - via a very short C testing framework - (2016-01-29)
  [4542] The principle of mocking - and the Python Mock package - (2015-10-17)
  [4457] Test framework for TCL - Tcltest - some examples - (2015-03-11)
  [4387] Regression Testing my website - Cucumber and Watir - (2015-01-07)
  [4380] Behaviour Driven Development / Ruby and Cucumber - (2015-01-02)
  [4374] Test driven development, and class design, from first principles (using C++) - (2014-12-30)
  [4346] A behaviour driven example of writing a Java program - (2014-12-09)
  [4336] Test Driven Development - a first example of principle in C - (2014-12-01)
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)


Back to
Pressure selling in the fire safety business
Previous and next
or
Horse's mouth home
Forward to
Coats of arms - towns and authorities in Wiltshire
Some other Articles
Image indexer / thumbnail display scripts in PHP
Getting to the Royal United Hospital - the Hopper and the alternatives
Coats of arms - towns and authorities in Wiltshire
Testing new algorithms in PHP
Pressure selling in the fire safety business
Why populate object with values as you construct them?
Object and Static methods - what is the difference; example in Python 3
Why is bus funding a much hotter topic than a new set of traffic lights?
On the problems of a printed train timetable
4750 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4652_Tes ... n-PHP.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat