;; ----------------------------------------------------------------------
;;  SRFI 128 ...
;; ----------------------------------------------------------------------

(define (vector-cdr vec)
    (let* ((len (vector-length vec))
           (result (make-vector (- len 1))))
      (let loop ((n 1))
        (cond
          ((= n len) result)
          (else (vector-set! result (- n 1) (vector-ref vec n))
                (loop (+ n 1)))))))

(test "vector-cdr"   '#(2 3 4) (vector-cdr '#(1 2 3 4)))
(test "vector-cdr 2" '#() (vector-cdr '#(1)))

(define default-comparator (make-default-comparator))
(define real-comparator (make-comparator real? = < number-hash))
(define degenerate-comparator (make-comparator (lambda (x) #t) equal? #f #f))
(define boolean-comparator
  (make-comparator boolean? eq? (lambda (x y) (and (not x) y)) boolean-hash))
(define bool-pair-comparator (make-pair-comparator boolean-comparator boolean-comparator))
(define num-list-comparator
  (make-list-comparator real-comparator list? null? car cdr))
(define num-vector-comparator
  (make-vector-comparator real-comparator vector? vector-length vector-ref))
(define vector-qua-list-comparator
  (make-list-comparator
   real-comparator
   vector?
   (lambda (vec) (= 0 (vector-length vec)))
   (lambda (vec) (vector-ref vec 0))
   vector-cdr))
(define list-qua-vector-comparator
  (make-vector-comparator default-comparator list? length list-ref))
(define eq-comparator (make-eq-comparator))
(define eqv-comparator (make-eqv-comparator))
(define equal-comparator (make-equal-comparator))
(define symbol-comparator
  (make-comparator
   symbol?
   eq?
   (lambda (a b) (string<? (symbol->string a) (symbol->string b)))
   symbol-hash))

;; comparators/predicates
(test "comparator? 1" #t (comparator? real-comparator))
(test "comparator? 2" #t (not (comparator? =)))
(test "ordered?" #t (comparator-ordered? real-comparator))
(test "hashtable?" #t (comparator-hashable? real-comparator))
(test "ordered? 2" #t (not (comparator-ordered? degenerate-comparator)))
(test "hashtable? 2" #t (not (comparator-hashable? degenerate-comparator)))

;; comparators/constructors

(test "1" #t (=? boolean-comparator #t #t))
(test "2" #t (not (=? boolean-comparator #t #f)))
(test "3" #t (<? boolean-comparator #f #t))
(test "4" #t (not (<? boolean-comparator #t #t)))
(test "5" #t (not (<? boolean-comparator #t #f)))

(test "6" #t (comparator-test-type bool-pair-comparator '(#t . #f)))
(test "7" #t (not (comparator-test-type bool-pair-comparator 32)))
(test "8" #t (not (comparator-test-type bool-pair-comparator '(32 . #f))))
(test "9" #t (not (comparator-test-type bool-pair-comparator '(#t . 32))))
(test "10" #t (not (comparator-test-type bool-pair-comparator '(32 . 34))))
(test "11" #t (=? bool-pair-comparator '(#t . #t) '(#t . #t)))
(test "12" #t (not (=? bool-pair-comparator '(#t . #t) '(#f . #t))))
(test "13" #t (not (=? bool-pair-comparator '(#t . #t) '(#t . #f))))
(test "14" #t (<? bool-pair-comparator '(#f . #t) '(#t . #t)))
(test "15" #t (<? bool-pair-comparator '(#t . #f) '(#t . #t)))
(test "16" #t (not (<? bool-pair-comparator '(#t . #t) '(#t . #t))))
(test "17" #t (not (<? bool-pair-comparator '(#t . #t) '(#f . #t))))
(test "18" #t (not (<? bool-pair-comparator '(#f . #t) '(#f . #f))))

(test "19" #t (comparator-test-type num-vector-comparator '#(1 2 3)))
(test "20" #t (comparator-test-type num-vector-comparator '#()))
(test "21" #t (not (comparator-test-type num-vector-comparator 1)))
(test "22" #t (not (comparator-test-type num-vector-comparator '#(a 2 3))))
(test "23" #t (not (comparator-test-type num-vector-comparator '#(1 b 3))))
(test "24" #t (not (comparator-test-type num-vector-comparator '#(1 2 c))))
(test "25" #t (=? num-vector-comparator '#(1 2 3) '#(1 2 3)))
(test "26" #t (not (=? num-vector-comparator '#(1 2 3) '#(4 5 6))))
(test "27" #t (not (=? num-vector-comparator '#(1 2 3) '#(1 5 6))))
(test "28" #t (not (=? num-vector-comparator '#(1 2 3) '#(1 2 6))))
(test "29" #t (<? num-vector-comparator '#(1 2) '#(1 2 3)))
(test "30" #t (<? num-vector-comparator '#(1 2 3) '#(2 3 4)))
(test "31" #t (<? num-vector-comparator '#(1 2 3) '#(1 3 4)))
(test "32" #t (<? num-vector-comparator '#(1 2 3) '#(1 2 4)))
(test "33" #t (<? num-vector-comparator '#(3 4) '#(1 2 3)))
(test "34" #t (not (<? num-vector-comparator '#(1 2 3) '#(1 2 3))))
(test "35" #t (not (<? num-vector-comparator '#(1 2 3) '#(1 2))))
(test "36" #t (not (<? num-vector-comparator '#(1 2 3) '#(0 2 3))))
(test "37" #t (not (<? num-vector-comparator '#(1 2 3) '#(1 1 3))))

(test "38" #t (not (<? vector-qua-list-comparator '#(3 4) '#(1 2 3))))
(test "39" #t (<? list-qua-vector-comparator '(3 4) '(1 2 3)))

(define bool-pair (cons #t #f))
(define bool-pair-2 (cons #t #f))
(define reverse-bool-pair (cons #f #t))

(test "40" #t (=? eq-comparator #t #t))
(test "41" #t (not (=? eq-comparator #f #t)))
(test "42" #t (=? eqv-comparator bool-pair bool-pair))
(test "43" #t (not (=? eqv-comparator bool-pair bool-pair-2)))
(test "44" #t (=? equal-comparator bool-pair bool-pair-2))
(test "45" #t (not (=? equal-comparator bool-pair reverse-bool-pair)))

;; comparators/hash
(test "46" #t (exact-integer? (boolean-hash #f)))
(test "47" #t (not (negative? (boolean-hash #t))))
(test "48" #t (exact-integer? (char-hash #\a)))
(test "49" #t (not (negative? (char-hash #\b))))
(test "50" #t (exact-integer? (char-ci-hash #\a)))
(test "51" #t (not (negative? (char-ci-hash #\b))))
(test "52" #t (= (char-ci-hash #\a) (char-ci-hash #\A)))
(test "53" #t (exact-integer? (string-hash "f")))
(test "54" #t (not (negative? (string-hash "g"))))
(test "55" #t (exact-integer? (string-ci-hash "f")))
(test "56" #t (not (negative? (string-ci-hash "g"))))
(test "57" #t (= (string-ci-hash "f") (string-ci-hash "F")))
(test "58" #t (exact-integer? (symbol-hash 'f)))
(test "59" #t (not (negative? (symbol-hash 't))))
(test "60" #t (exact-integer? (number-hash 3)))
(test "61" #t (not (negative? (number-hash 3))))
(test "62" #t (exact-integer? (number-hash -3)))
(test "63" #t (not (negative? (number-hash -3))))
(test "64" #t (exact-integer? (number-hash 3.0)))
(test "65" #t (not (negative? (number-hash 3.0))))

;; comparators/default
(test "66" #t (<? default-comparator '() '(a)))
(test "67" #t (not (=? default-comparator '() '(a))))
(test "68" #t (=? default-comparator #t #t))
(test "69" #t (not (=? default-comparator #t #f)))
(test "70" #t (<? default-comparator #f #t))
(test "71" #t (not (<? default-comparator #t #t)))
(test "72" #t (=? default-comparator #\a #\a))
(test "73" #t (<? default-comparator #\a #\b))

(test "74" #t (comparator-test-type default-comparator '()))
(test "75" #t (comparator-test-type default-comparator #t))
(test "76" #t (comparator-test-type default-comparator #\t))
(test "77" #t (comparator-test-type default-comparator '(a)))
(test "78" #t (comparator-test-type default-comparator 'a))
(test "79" #t (comparator-test-type default-comparator (make-bytevector 10)))
(test "80" #t (comparator-test-type default-comparator 10))
(test "81" #t (comparator-test-type default-comparator 10.0))
(test "82" #t (comparator-test-type default-comparator "10.0"))
(test "83" #t (comparator-test-type default-comparator '#(10)))

(test "84" #t (=? default-comparator '(#t . #t) '(#t . #t)))
(test "85" #t (not (=? default-comparator '(#t . #t) '(#f . #t))))
(test "86" #t (not (=? default-comparator '(#t . #t) '(#t . #f))))
(test "87" #t (<? default-comparator '(#f . #t) '(#t . #t)))
(test "88" #t (<? default-comparator '(#t . #f) '(#t . #t)))
(test "89" #t (not (<? default-comparator '(#t . #t) '(#t . #t))))
(test "90" #t (not (<? default-comparator '(#t . #t) '(#f . #t))))
(test "91" #t (not (<? default-comparator '#(#f #t) '#(#f #f))))

(test "92" #t (=? default-comparator '#(#t #t) '#(#t #t)))
(test "93" #t (not (=? default-comparator '#(#t #t) '#(#f #t))))
(test "94" #t (not (=? default-comparator '#(#t #t) '#(#t #f))))
(test "95" #t (<? default-comparator '#(#f #t) '#(#t #t)))
(test "96" #t (<? default-comparator '#(#t #f) '#(#t #t)))
(test "97" #t (not (<? default-comparator '#(#t #t) '#(#t #t))))
(test "98" #t (not (<? default-comparator '#(#t #t) '#(#f #t))))
(test "99" #t (not (<? default-comparator '#(#f #t) '#(#f #f))))

(test "100" #t (= (comparator-hash default-comparator #t) (boolean-hash #t)))
(test "101" #t (= (comparator-hash default-comparator #\t) (char-hash #\t)))
(test "102" #t
      (begin
        (let ((a (comparator-hash default-comparator "t"))
              (b (string-hash "t")))
          (= a b))))
(test "103" #t (= (comparator-hash default-comparator 't) (symbol-hash 't)))
(test "104" #t (= (comparator-hash default-comparator 10) (number-hash 10)))
(test "105" #t (= (comparator-hash default-comparator 10.0) (number-hash 10.0)))

(comparator-register-default!
 (make-comparator procedure? (lambda (a b) #t) (lambda (a b) #f) (lambda (obj) 200)))
(test "106" #t (=? default-comparator (lambda () #t) (lambda () #f)))
(test "107" #t (not (<? default-comparator (lambda () #t) (lambda () #f))))
(test "108" 200 (comparator-hash default-comparator (lambda () #t)))



;; SRFI 128 does not actually require a comparator's four procedures
;; to be eq? to the procedures originally passed to make-comparator.
;; For interoperability/interchangeability between the comparators
;; of SRFI 114 and SRFI 128, some of the procedures passed to
;; make-comparator may need to be wrapped inside another lambda
;; expression before they're returned by the corresponding accessor.
;;
;; So this next group of tests is incorrect, hence commented out
;; and replaced by a slightly less naive group of tests.

;; comparators/accessors"
;; (define ttp (lambda (x) #t))
;; (define eqp (lambda (x y) #t))
;; (define orp (lambda (x y) #t))
;; (define hf (lambda (x) 0))
;; (define comp (make-comparator ttp eqp orp hf))
;; (test "" ttp (comparator-type-test-predicate comp))
;; (test "" eqp (comparator-equality-predicate comp))
;; (test "" orp (comparator-ordering-predicate comp))
;; (test "" hf (comparator-hash-function comp))

;; comparators/accessors
(define x1 0)
(define x2 0)
(define x3 0)
(define x4 0)
(define ttp (lambda (x) (set! x1 111) #t))
(define eqp (lambda (x y) (set! x2 222) #t))
(define orp (lambda (x y) (set! x3 333) #t))
(define hf (lambda (x) (set! x4 444) 0))
(define comp (make-comparator ttp eqp orp hf))
(test "109" #t (and ((comparator-type-test-predicate comp) x1)   (= x1 111)))
(test "110" #t (and ((comparator-equality-predicate comp) x1 x2) (= x2 222)))
(test "111" #t (and ((comparator-ordering-predicate comp) x1 x3) (= x3 333)))
(test "112" #t (and (zero? ((comparator-hash-function comp) x1)) (= x4 444)))

;; comparators/invokers
(test "113" #t (comparator-test-type real-comparator 3))
(test "114" #t (comparator-test-type real-comparator 3.0))
(test "115" #t (not (comparator-test-type real-comparator "3.0")))
(test "116" #t (comparator-check-type boolean-comparator #t))
(test "117" *test-failed* (comparator-check-type boolean-comparator 't))

;; comparators/comparison
(test "118" #t (=? real-comparator 2 2.0 2))
(test "119" #t (<? real-comparator 2 3.0 4))
(test "120" #t (>? real-comparator 4.0 3.0 2))
(test "121" #t (<=? real-comparator 2.0 2 3.0))
(test "122" #t (>=? real-comparator 3 3.0 2))
(test "123" #t (not (=? real-comparator 1 2 3)))
(test "124" #t (not (<? real-comparator 3 1 2)))
(test "125" #t (not (>? real-comparator 1 2 3)))
(test "126" #t (not (<=? real-comparator 4 3 3)))
(test "127" #t (not (>=? real-comparator 3 4 4.0)))


;; comparators/syntax
(test "128" 'less (comparator-if<=> real-comparator 1 2 'less 'equal 'greater))
(test "129" 'equal (comparator-if<=> real-comparator 1 1 'less 'equal 'greater))
(test "130" 'greater (comparator-if<=> real-comparator 2 1 'less 'equal 'greater))
(test "131" 'less (comparator-if<=> "1" "2" 'less 'equal 'greater))
(test "132" 'equal (comparator-if<=> "1" "1" 'less 'equal 'greater))
(test "133" 'greater (comparator-if<=> "2" "1" 'less 'equal 'greater))

;; comparators/bound-salt"
(test "hash bound" #t (exact-integer? (hash-bound)))
(test "hash salt" #t (exact-integer? (hash-salt)))
(test "hash salt<hash bound" #t (< (hash-salt) (hash-bound)))


