RU beehive logo ITEC dept promo banner
ITEC 325
2019spring
ibarland

arrays
and serving via web

Due 2019.Feb.12 (Tue) in class
Submit on D2L, and hardcopy: utils.php, utils-test.php, silly.php. (You can also provide a css file, if separate.) The file silly.php should also be viewable on the web, as discussed below.

  1. Copy your hw01/ to a new folder hw02/; we will add on to our existing files. (cp -r will recursively copy an entire directory.) You can append your work for this homework to your (copy of) the existing files, as appropriate.
    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.php so that, instead of starting with one single “<?php” and being mostly explicit echo statements, it appears as if it’s mostly html with occasional <?php ?> sections (as shown in notes about php-strings).
    (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.
    Don’t Panic:

    As of Feb.08, I am suspecting that most of the class has not had their accoun configured so that php.radford.edu can see their H: drive. I've filed a ticket w/ Radford IT.

    SO: make sure your permissions are correct, and that your silly.php runs from rucs’s command line, but DON’T PANIC if you can’t view the files via the web. (For grading, if it’s not fixed, I’ll write a script which simply looks at the file-permissions of your silly.php and each enclosing folder.)

  6. (15pts) Write a function asRow which takes in an array of strings, and returns a single, long string: the HTML for one table-row, where each of the provided strings is one column in that row. The very first item (if any) should be in a th element; any remaining items will be tds.
    Note: We are not creating HTML for an entire table — merely one row. (Though, we might make a bunch of calls to this function, and splice the results between the string “<table>” and “</table>” … and it might even be a another function with its own loop, that is making the calls to asRow.)

    (2pts extra credit) Add an optional second-parameter “firstItemIsAHeader”; if false then the resulting tr won’t contain a leading th. (The parameter’s default value should be true, for compatibility with the above when no second argument is provided. You don’t need to use that exact parameter-name.)

  7. (15pts) Write a function asAttrs which takes an array of strings (where each the key associated with each string-value is also a string) and returns one long string that is those key/value pairs separated by “=”, with the value in quotes. Note that this string is suitable for splicing into an html-file, as a tag’s attributes-and-values2.

    For example, given an array with "id" associated with "main-point" and "class" associated with "unimportant", asAttrs would return3 "id='main-point' class='unimportant'".

    We mentioned in lecture how PHP arrays are hash tables. We didn’t mention, though, how to loop over keys&values together. See the 2nd half of the video-in-lecture notes “arrays in php” (actual loop starts at 10m30s).

  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.4

    (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.


1 Why 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.)      
2 Well, if the key is not an empty-string, then it might be valid HTML. We could add a pre-condition that the keys must be non-empty-strings, but on the other hand there’s no real reason for us to do that; well leave the burden on the caller to provide an empty-string-key if, and only if, they want nothing immediately-preceding the “=” for that pair.      
3 It’s fine if you want your result to contain double-quote characters instead of single-quotes characters. However, I recommend having generating html-which-contains-single-quotes, as it ends up making php code more convenient.      
4 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.)      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.