test/unit: Add some tests to verify my ideas of how stuff works.
[ca] / test / unit
CommitLineData
bf1840b4 1#! /usr/bin/tclsh
71669a0a
MW
2### -*-tcl-*-
3
4source [file join [file dirname $argv0] "../lib/func.tcl"]
5
6set RUN 0
7set FAIL 0
8
9proc test {name testargs testbody tests} {
10 global RC RUN FAIL
11
12 eval proc testproc [list $testargs] [list $testbody]
13 set run 0
14 set ok true
15 puts -nonewline "$name: "
16 foreach test $tests {
17 puts -nonewline "."
18 flush stdout
19 incr run
20 set rc [catch {eval testproc $test} out]
21 switch -exact $rc {
22 0 { }
23 1 {
24 puts ""
25 puts "FAILED: $out"
26 puts -nonewline "$name: [string repeat . $run]"
27 set ok false
28 }
29 default { return -code $code $rc }
30 }
31 }
32 if {$ok} {
33 puts " ok"
34 } else {
35 puts " FAILED"
36 incr FAIL
37 }
38 incr RUN
39}
40
41test next-matching-date {pat ref want} {
42 set t_ref [clock scan $ref -format "%Y-%m-%d %H:%M:%S"]
43 set t_want [clock scan $want -format "%Y-%m-%d %H:%M:%S"]
44 set t_found [next-matching-date $pat $t_ref]
45 if {$t_found != $t_want} {
46 set found [clock format $t_found -format "%Y-%m-%d %H:%M:%S"]
47 error "mismatch: <$pat> <$ref> -> <$found> /= <$want>"
48 }
49} {
50 {"*-*-* 03:00:00" "2011-09-03 02:32:45" "2011-09-03 03:00:00"}
51 {"*-*-* 03:00:00" "2011-09-03 18:32:45" "2011-09-04 03:00:00"}
52 {"*-*-* 03:00:00" "2011-02-28 18:32:45" "2011-03-01 03:00:00"}
53 {"*-*-* 03:00:00" "2012-02-28 18:32:45" "2012-02-29 03:00:00"}
54 {"*-*-* 03:00:00" "2011-03-30 18:32:45" "2011-03-31 03:00:00"}
55 {"*-*-* 03:00:00" "2012-04-30 18:32:45" "2012-05-01 03:00:00"}
56 {"*-*-* 03:00:00" "2012-12-31 18:32:45" "2013-01-01 03:00:00"}
57 {"*-*-* 00:00:00" "2012-12-04 00:25:01" "2012-12-05 00:00:00"}
58 {"*-*-* 10:20:30" "2012-12-06 10:21:30" "2012-12-07 10:20:30"}
59 {"*-*-* *:*:05" "2012-12-04 00:00:00" "2012-12-04 00:00:05"}
60 {"*-*-* *:*:05" "2012-12-04 00:00:04" "2012-12-04 00:00:05"}
61 {"*-*-* *:*:05" "2012-12-04 00:00:05" "2012-12-04 00:00:05"}
62 {"*-*-* *:*:05" "2012-12-04 00:00:06" "2012-12-04 00:01:05"}
63 {"*-*-* *:*:05" "2012-12-04 23:59:06" "2012-12-05 00:00:05"}
64 {"*-*-* *:19:05" "2012-12-04 00:00:00" "2012-12-04 00:19:05"}
65 {"*-*-* *:19:05" "2012-12-04 00:20:04" "2012-12-04 01:19:05"}
66 {"*-*-* *:19:05" "2012-12-04 00:18:06" "2012-12-04 00:19:05"}
67 {"*-*-* *:19:05" "2012-12-04 00:19:06" "2012-12-04 01:19:05"}
68 {"*-*-31 01:02:03" "2012-11-03 04:05:06" "2012-12-31 01:02:03"}
69 {"*/2-*/3-*/5 */7:*/11:*/13" "2011-12-04 00:00:04" "2012-03-05 00:00:00"}
4b6fce84
MW
70 {"*-03-01 00:00:00" "2020-05-14 13:44:09" "2021-03-01 00:00:00"}
71 {"*-03-01 00:00:00" "2021-03-01 00:25:04" "2022-03-01 00:00:00"}
71669a0a
MW
72}
73
74
75set tests [expr {$RUN == 1 ? "test" : "tests"}]
76if {!$FAIL} {
77 puts "All $RUN $tests PASSED"
78 set rc 0
79} else {
80 puts "FAILED $FAIL of $RUN $tests"
81}
82exit $RC