RU beehive logo ITEC dept promo banner
ITEC 325
2017fall
ibarland

homelectshws
D2Lbreeze (snow day)

serve-and-arrays
arrays
and serving via web

Due 2017.Sep.2225 (Mon) 11:00
Submit on D2L, and hardcopy: utils.php, utils-test.php, silly.php. (You can also provide a css file, if separate.) (Bring hardcopy to Monday's class, or Friday's if you've finished it.)

  1. Copy your hw01 to a new folder; we will add on to our existing files. (cp -r will recursively copy an entire directory.)
    As per syllabus, you can use anything I post on the web page, include hw01-soln; just cite the URL.
  2. (5pts) Write a function test : ANY, ANY → void which determines if the first value (an actual test-result) is equal to the second value (the desired test-result). If the two are not equal, it should print a message saying that a test failed. If the two are equal, it should merely print.”. (Note that this is the only functions we'll write all semester, whose purpose is to print something rather than return a value.)

    We'll use this function to make our testing more convenient. For example, your tests from hw01 could be replaced with lines like “test( pluralize(4,"cow"), "4 cows" );”. This way, only tests that fail get printed, and we don't need to visually scan through lots of output about successful tests — if you see “......” you know you're good. (And if any test failed, you'll get an error message blaring at you, that you can't miss.)

    Optional (for slight extra credit): These further features make test even more useful:

    1. (2pts)recommended When testing functions which return html, it's a bit annoying if a test were to fail due only to whitespace (e.g. our function returned two spaces where the expected-output only had one space, or had a newline). SO: before comparing strings, collapse all groups of one-or-more whitespace characters to a single space1. (Hint: use preg_replace('/\s+/', ' ', ), but only call it your if your two inputs is_strings.) You may want to also trim whitespace from the start and end of each string.

      Note that rectifying whitespace is particularly helpful when you are comparing test-cases-written-on-a-Windows-system (where a newline uses two bytes), and then running the code on php.radford.edu (a UNIX machine, where any newlines are one byte). (Alternately, you can also run dos2unix on files that were originally created in Windows.)

    2. (2pts) In the UNIX philosophy, it's nice to only see printed messages when something goes wrong. Add a “switch”, so that you can get this behavior if you like: a third, optional argument to test. Then, by providing (say) false, successful-tests cause no output nothing, otherwise they print “.”. (Be sure that test always prints something when a test fails, though!)
    3. (2pts) Add an optional third parameter: a boolean to decide if you really want to “normalize” string whitespace, or not. Its default value should be false.
    4. (2pts) You may add further features if you like, such as keep a counter of how many tests have been made, and then any failing-tests can include the test-number as well as other details. (Myself, I made an additional function testResults, which prints those statistics.)
    (You don't need to have test-cases for test itself; since it's a void function that prints, it's not suitable for unit-testing. This means you need to be extra careful to make sure it really does work as you expect.)

    Put this function into utils.php -- it is a utility function-definition. It doesn't go in utils-test.php. because it is not itself testing anything (it's a function that your unit-tests will call to help them do their job!)

  3. Re-write your utils-test.php to use test.
  4. Modify silly-page.php so that, instead of starting with one single “<?php” and being mostly echo statements, it appears as if it’s mostly html with occasional <?php ?> sections.
    (We understand, though that really it is one solid php program, filled with implicit echo statements, which happens to be print valid html.)
    Of course, you'll make sure it's still runnable via the command-line.
  5. (5pts) Make sure silly.php is runnable via the web at https://php.radford.edu/~yourUserName/itec325/hw02/silly.php. You'll also be doing this for most all the future homeworks.
  6. (20pts) Write a function which takes in an array of strings, and returns a single, long string: a comma-separated list of all the individual strings, with “and” before the last element (if more than one)2. The resulting string should be suitable for splicing into the middle of a paragraph of prose, and should be proper, grammatical English. (This function has nothing to do with html.) Include at least three test cases, each testing a different situation.
  7. (20pts) Write a function toc (“table of contents”): It takes an array of strings, and returns a string that is HTML for an un-ordered list of links to anchors within the same document.

    For example, given an array with "thumbnail-problem", "commaList-problem", and "due-date", it would return HTML that would render as:

    (Try clicking on those items, and feel free to view-source, to understand what it's doing. Of course, this function is only helpful if the strings in the array are also used as id attributes elsewhere in the page.)

    For full credit, this function should involve a call to hyperlink.

  8. If (while making test cases and thinking about any odd inputs) you want to either document what odd-but-legal inputs do, or you want to document a pre-condition about what you consider a requirement for a legal input, please do so.

Standard Homework Requirements

For all assignments in this class (and all your classes, as appropriate):

  1. Your primary file (utils.php in this case) must start with a comment containing: Your name; the class/semester; the URL of this assignment page.
  2. Every function should be commented with a short (1-sentence) description, (which mentions every parameter by name), and should indicate the types of the parameters and the return-type.
  3. You do not need to check for invalid-inputs to your functions. E.g.: if I ask for a function taking in (say) a number and a string, and you repeat in your function-comment that your function handles a number and a string, then you don't need to check for being given incorrect inputs — that's the caller's problem, not yours. Similarly for more refined types like "non-empty-string" or "integer in the range [1,12]": if it's in the comments as a pre-condition, that'll suffice.3

    (We'll talk about validating input received from the (external, untrusted) user later.)

  4. Any HTML/XML for this class should follow the following common guidelines: We'll require these even where it's not strictly needed for (mere) HTML5 compliance.


1Why not collapse whitespace to nothing at all? This is tempting, since an actual&desired output of "<ul></ul>" and "<ul>\n</ul>" would then be considered okay. However, this allows false-negatives: getting "hit here" when expecting "hi there" wouldn't register as an error. (… I guess the best of both worlds might be, after collapsing whitespace, make another substitution which removes any space right before/after any "<" or ">", as well as on either side of the the "=" between an attribute and its value? That still has allows some false-negatives, but not as many. It's much harder to this exactly get right though, if just using regular expressions.)      
2When having a list of more than 2 items, I personally recommend using the Oxford comma (or, see wikipedia) but I will leave it to your preference.      
3 While the defensive-programming can be helpful in bigger projects (esp. in untyped languages like php), it's never interesting code. If you DO want to do the checks anyway, that's fine, but do all the type-validation right away (before you enter the 'real' function-logic), and throw an error if something goes wrong (don't continue and return some sort of odd, undocumented answer). For example:
function foo( $someNum ) {
  // optional error-check:
  if !(is_numeric($someNum)) { throw new InvalidArgumentException( 'foo: expected number, given ' . $someNum ); }

  // ...now put your real code, uncluttered by further type-checks.
}
(Ideally, you'd include the type-requirements in comments, and then a tool would automatically generate the above boilerplate error-checking for you. That's what Java's type-system gives you, for example.)      

homelectshws
D2Lbreeze (snow day)


©2017, Ian Barland, Radford University
Last modified 2017.Sep.22 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.