Merge branch 'master' of git://git.distorted.org.uk/~mdw/ca
[ca] / test / unit
diff --git a/test/unit b/test/unit
new file mode 100755 (executable)
index 0000000..73c01ae
--- /dev/null
+++ b/test/unit
@@ -0,0 +1,80 @@
+#! /usr/bin/tclsh8.5
+### -*-tcl-*-
+
+source [file join [file dirname $argv0] "../lib/func.tcl"]
+
+set RUN 0
+set FAIL 0
+
+proc test {name testargs testbody tests} {
+  global RC RUN FAIL
+
+  eval proc testproc [list $testargs] [list $testbody]
+  set run 0
+  set ok true
+  puts -nonewline "$name: "
+  foreach test $tests {
+    puts -nonewline "."
+    flush stdout
+    incr run
+    set rc [catch {eval testproc $test} out]
+    switch -exact $rc {
+      0 { }
+      1 {
+       puts ""
+       puts "FAILED: $out"
+       puts -nonewline "$name: [string repeat . $run]"
+       set ok false
+      }
+      default { return -code $code $rc }
+    }
+  }
+  if {$ok} {
+    puts " ok"
+  } else {
+    puts " FAILED"
+    incr FAIL
+  }
+  incr RUN
+}
+
+test next-matching-date {pat ref want} {
+  set t_ref [clock scan $ref -format "%Y-%m-%d %H:%M:%S"]
+  set t_want [clock scan $want -format "%Y-%m-%d %H:%M:%S"]
+  set t_found [next-matching-date $pat $t_ref]
+  if {$t_found != $t_want} {
+    set found [clock format $t_found -format "%Y-%m-%d %H:%M:%S"]
+    error "mismatch: <$pat> <$ref> -> <$found> /= <$want>"
+  }
+} {
+  {"*-*-* 03:00:00" "2011-09-03 02:32:45" "2011-09-03 03:00:00"}
+  {"*-*-* 03:00:00" "2011-09-03 18:32:45" "2011-09-04 03:00:00"}
+  {"*-*-* 03:00:00" "2011-02-28 18:32:45" "2011-03-01 03:00:00"}
+  {"*-*-* 03:00:00" "2012-02-28 18:32:45" "2012-02-29 03:00:00"}
+  {"*-*-* 03:00:00" "2011-03-30 18:32:45" "2011-03-31 03:00:00"}
+  {"*-*-* 03:00:00" "2012-04-30 18:32:45" "2012-05-01 03:00:00"}
+  {"*-*-* 03:00:00" "2012-12-31 18:32:45" "2013-01-01 03:00:00"}
+  {"*-*-* 00:00:00" "2012-12-04 00:25:01" "2012-12-05 00:00:00"}
+  {"*-*-* 10:20:30" "2012-12-06 10:21:30" "2012-12-07 10:20:30"}
+  {"*-*-* *:*:05" "2012-12-04 00:00:00" "2012-12-04 00:00:05"}
+  {"*-*-* *:*:05" "2012-12-04 00:00:04" "2012-12-04 00:00:05"}
+  {"*-*-* *:*:05" "2012-12-04 00:00:05" "2012-12-04 00:00:05"}
+  {"*-*-* *:*:05" "2012-12-04 00:00:06" "2012-12-04 00:01:05"}
+  {"*-*-* *:*:05" "2012-12-04 23:59:06" "2012-12-05 00:00:05"}
+  {"*-*-* *:19:05" "2012-12-04 00:00:00" "2012-12-04 00:19:05"}
+  {"*-*-* *:19:05" "2012-12-04 00:20:04" "2012-12-04 01:19:05"}
+  {"*-*-* *:19:05" "2012-12-04 00:18:06" "2012-12-04 00:19:05"}
+  {"*-*-* *:19:05" "2012-12-04 00:19:06" "2012-12-04 01:19:05"}
+  {"*-*-31 01:02:03" "2012-11-03 04:05:06" "2012-12-31 01:02:03"}
+  {"*/2-*/3-*/5 */7:*/11:*/13" "2011-12-04 00:00:04" "2012-03-05 00:00:00"}
+}
+
+
+set tests [expr {$RUN == 1 ? "test" : "tests"}]
+if {!$FAIL} {
+  puts "All $RUN $tests PASSED"
+  set rc 0
+} else {
+  puts "FAILED $FAIL of $RUN $tests"
+}
+exit $RC