JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added ordinalize() and tests for it
authorJason Woofenden <jason@jasonwoof.com>
Thu, 1 Apr 2010 11:43:49 +0000 (07:43 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Thu, 1 Apr 2010 11:43:49 +0000 (07:43 -0400)
misc.php
unit_tests.php
unit_tests/misc.php [new file with mode: 0644]

index 8f46014..1045d45 100644 (file)
--- a/misc.php
+++ b/misc.php
 #  You should have received a copy of the GNU General Public License
 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+# pass an integer, returns the suffix
+function ordinal_suffix($i) {
+       # teens are all __th
+       if(strlen($i) > 1 && substr($i, -2, 1) === '1') {
+               return 'th';
+       }
+       switch(substr($i, -1)) {
+               case '':
+                       return '';
+               case '1':
+                       return 'st';
+               case '2':
+                       return 'nd';
+               case '3':
+                       return 'rd';
+               default;
+                       return 'th';
+       }
+}
+
+# ordinalize(1) => "1st"
+# ordinalize(2) => "2nd"
+# ordinalize(3) => "3rd"
+# ordinalize(111) => "111th"
+# ordinalize("62") => "62nd"
+function ordinalize($i) {
+       return $i . ordinal_suffix($i);
+}
+
 # returns an array containing just the elements of $pipes that are readable (without blocking)
 # timeout 0 means don't wait, timeout NULL means wait indefinitely
 function readable_sockets($pipes, $timeout = 0){
index 0808fa4..97df911 100644 (file)
@@ -49,7 +49,7 @@ function run_unit_tests($directory, $basenames) {
 # Call this to unit test wfpl. By default it tests everything, or you can pass
 # a space-separated list of the basenames of the files in code/wfpl/unit_tests/
 # that you'd like to run.
-function unit_test_wfpl($basenames = 'format db') {
+function unit_test_wfpl($basenames = 'format db misc') {
        tem_load('code/wfpl/unit_tests/template.html');
        run_unit_tests('code/wfpl/unit_tests', $basenames);
        display_messages();
diff --git a/unit_tests/misc.php b/unit_tests/misc.php
new file mode 100644 (file)
index 0000000..0582649
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+#  Copyright (C) 2009 Jason Woofenden
+#
+#  This program is free software: you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation, either version 3 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+# This file contains tests for functions in code/wfpl/misc.php
+#
+# See code/wfpl/unit_tests.php for details on how to run or create tests
+
+
+require_once('code/wfpl/misc.php');
+
+# returns number of errors
+function test_ordinalize() {
+       $errors = 0;
+
+       $tests = array(
+               '', '',
+               0, "0th",
+               1, "1st",
+               2, "2nd",
+               3, "3rd",
+               111, "111th",
+               "62", "62nd");
+       for($i = 0; $i < count($tests); $i += 2) {
+               $arg = $tests[$i];
+               $correct = $tests[$i + 1];
+               $ret = ordinalize($arg);
+               if($ret !== $correct) {
+                       message("ordinalize($arg) returned \"$ret\" instead of \"$correct\"");
+                       $errors += 1;
+               }
+       }
+
+       return $errors;
+}
+
+# returns number of errors
+function misc_unit_tests_main() {
+       $errors = 0;
+       $errors += test_ordinalize();
+       return $errors;
+}