Fix all self-tests.

This commit is contained in:
Dominik Pantůček 2023-03-11 08:58:20 +01:00
parent dd2716c1c7
commit e5045f893d

View file

@ -26,6 +26,7 @@
(chicken file posix) (chicken file posix)
(chicken io) (chicken io)
(chicken string) (chicken string)
(chicken format)
(chicken process-context)) (chicken process-context))
@ -50,7 +51,7 @@
(syntax-rules () (syntax-rules ()
((_ name condition) ((_ name condition)
(if (with-handler (lambda (x) #f) (if (with-handler (lambda (x) #f)
(lambda () condition)) condition)
(display ".") (display ".")
(error 'unit-test name))))) (error 'unit-test name)))))
@ -131,28 +132,28 @@
(car m) (car m)
(cdr m) (cdr m)
(cadr m) (cadr m)
(nil? (cddr m)) (null? (cddr m))
(integer? (car m)) (integer? (car m))
(integer? (cadr m)) (integer? (cadr m))
(>= (car m 1000)) (>= (car m) 1000)
(<= (car m 9999)) (<= (car m) 9999)
(>= (cadr m 1)) (>= (cadr m) 1)
(<= (cadr m 12)))) (<= (cadr m) 12)))
;; Converts string in a format YYYY-MM to valid month. Returns #f if ;; Converts string in a format YYYY-MM to valid month. Returns #f if
;; the conversion fails. ;; the conversion fails.
(define (string->month s) (define (string->month s)
(let ((l (string-split s "-"))) (let ((l (string-split s "-")))
(if (or (not l) (if (or (not l)
(nil? l) (null? l)
(nil? (cdr l)) (null? (cdr l))
(not (nil? (cddr l)))) (not (null? (cddr l))))
#f #f
(let ((y (string->number (car l))) (let ((y (string->number (car l)))
(m (string->number (cadr l)))) (m (string->number (cadr l))))
(if (and y m) (if (and y m)
(let ((M (list y m))) (let ((M (list y m)))
(if (valid-month? M) (if (month-valid? M)
M M
#f)) #f))
#f))))) #f)))))
@ -160,18 +161,18 @@
;; Formats (valid) month as YYYY-MM string ;; Formats (valid) month as YYYY-MM string
(define (month->string M) (define (month->string M)
(if (month-valid? M) (if (month-valid? M)
(let ((y (car s)) (let ((y (car M))
(m (cadr s))) (m (cadr M)))
(sprintf "~A-~A~A" (sprintf "~A-~A~A"
y y
(if (< m 10) " " "") (if (< m 10) "0" "")
m)) m))
(error 'string->month "Invalid month" M))) (error 'string->month "Invalid month" M)))
;; Returns true if both arguments are a valid month and are equal ;; Returns true if both arguments are a valid month and are equal
(define (month=? m n) (define (month=? m n)
(and (valid-month? m) (and (month-valid? m)
(valid-month? n) (month-valid? n)
(equal? m n))) (equal? m n)))
;; Returns true if the first argument is a month in the past of the ;; Returns true if the first argument is a month in the past of the
@ -183,16 +184,23 @@
(and (= (car m) (car n)) (and (= (car m) (car n))
(< (cadr m) (cadr n)))))) (< (cadr m) (cadr n))))))
(define (month-diff m n) ;; Returns the number of months between from f and to t. The first
; Exclusive ;; month is included in the count, the last month is not.
1) (define (month-diff f t)
(if (month-valid? f)
(if (month-valid? t)
(let ((F (+ (* (car f) 12) (cadr f)))
(T (+ (* (car t) 12) (cadr t))))
(- T F))
(error 'month-diff "Second argument is not a valid month" t))
(error 'month-diff "First argument is not a valid month" f)))
(define (month-tests!) (define (month-tests!)
(display "[test] month ") (display "[test] month ")
(unit-test 'month-valid? (month-valid? '(2023 5))) (unit-test 'month-valid? (month-valid? '(2023 5)))
(unit-test 'month-valid?-bad-year (not (month-valid? '(999 8)))) (unit-test 'month-valid?-bad-year (not (month-valid? '(999 8))))
(unit-test 'month-valid?-bad-month (not (month-valid? '(2023 -5)))) (unit-test 'month-valid?-bad-month (not (month-valid? '(2023 -5))))
(unit-test 'string->month (equal? (string->month "2023-01" '(2023 1)))) (unit-test 'string->month (equal? (string->month "2023-01") '(2023 1)))
(unit-test 'string->month-bad-month (not (string->month "2023-13"))) (unit-test 'string->month-bad-month (not (string->month "2023-13")))
(unit-test 'string->month-nonumber-year (not (string->month "YYYY-01"))) (unit-test 'string->month-nonumber-year (not (string->month "YYYY-01")))
(unit-test 'string->month-nonumber-month (not (string->month "2023-MMM"))) (unit-test 'string->month-nonumber-month (not (string->month "2023-MMM")))
@ -201,11 +209,13 @@
(unit-test 'month->string-bad-month (with-handler (lambda (x) #t) (month->string '(2023 13)) #f)) (unit-test 'month->string-bad-month (with-handler (lambda (x) #t) (month->string '(2023 13)) #f))
(unit-test 'month<? (month<? '(2023 5) '(2023 6))) (unit-test 'month<? (month<? '(2023 5) '(2023 6)))
(unit-test 'month<?-cross-year (month<? '(2022 12) '(2023 1))) (unit-test 'month<?-cross-year (month<? '(2022 12) '(2023 1)))
(unit-test 'month<?-equal (not (month<? '(2023 1) '(2023 1)))) (unit-test 'month<?-is-equal (not (month<? '(2023 1) '(2023 1))))
(unit-test 'month<?-greater (not (month<? '(2023 1) '(2023 2)))) (unit-test 'month<?-greater (not (month<? '(2023 1) '(2023 1))))
(unit-test 'month-equal? (month-equal? '(2023 4) '(2023 4))) (unit-test 'month-equal? (month=? '(2023 4) '(2023 4)))
(unit-test 'month-equal? (not (month-equal? '(2023 4) '(2023 5)))) (unit-test 'month-equal?-not (not (month=? '(2023 4) '(2023 5))))
;; Difference - exclusive end (unit-test 'month-diff-1 (= (month-diff '(2023 1) '(2023 2)) 1))
(unit-test 'month-diff-11 (eq? (month-diff '(2023 1) '(2023 12)) 11))
(unit-test 'month-diff (= (month-diff '(2023 1) '(2022 2)) -11))
(print " ok.")) (print " ok."))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;