
(define-syntax iq
  (syntax-rules ()
    ((iq . tree) (gtree->itree 'tree))))


;; Constructors

(define abc (ilist 'a 'b 'c))

(test "srfi-116-1" 'a (icar abc))
(test "srfi-116-2" 'b (icadr abc))
(test "srfi-116-3" 'c (icaddr abc))
(test "srfi-116-4" (ipair 2 1) (xipair 1 2))

(define abc-dot-d (ipair* 'a 'b 'c 'd))

(test "srfi-116-5" 'd (icdddr abc-dot-d))
(test "srfi-116-6" (iq c c c c) (make-ilist 4 'c))
(test "srfi-116-7" (iq 0 1 2 3) (ilist-tabulate 4 values))
(test "srfi-116-8" (iq 0 1 2 3 4) (iiota 5))

(define abc-copy (ilist-copy abc))

(test "srfi-116-9" abc abc-copy)
(test "srfi-116-10" #t (not (eq? abc abc-copy)))

;; Predicates

(test "srfi-116-11" #t (ipair? (ipair 1 2)))
(test "srfi-116-12" #t (proper-ilist? '()))
(test "srfi-116-13" #t (proper-ilist? (iq 1 2 3)))
(test "srfi-116-14" #t (ilist? '()))
(test "srfi-116-15" #t (ilist? (iq 1 2 3)))
(test "srfi-116-16" #t (dotted-ilist? (ipair 1 2)))
(test "srfi-116-17" #t (dotted-ilist? 2))
(test "srfi-116-18" #t (null-ilist? '()))
(test "srfi-116-19" #t (not (null-ilist? (iq 1 2 3))))
(test/error  "srfi-116-20" (null-ilist? 'a))
(test "srfi-116-21" #t (not-ipair? 'a))
(test "srfi-116-22" #t (not (not-ipair? (ipair 'a 'b))))
(test "srfi-116-23" #t (ilist= = (iq 1 2 3) (iq 1 2 3)))
(test "srfi-116-24" #t (ilist= = (iq 1 2 3) (iq 1 2 3) (iq 1 2 3)))
(test "srfi-116-25" #t (not (ilist= = (iq 1 2 3 4) (iq 1 2 3))))
(test "srfi-116-26" #t (not (ilist= = (iq 1 2 3) (iq 1 2 3 4))))
(test "srfi-116-27" #t (ilist= = (iq 1 2 3) (iq 1 2 3)))
(test "srfi-116-28" #t (not (ilist= = (iq 1 2 3) (iq 1 2 3 4) (iq 1 2 3 4))))
(test "srfi-116-29" #t (not (ilist= = (iq 1 2 3) (iq 1 2 3) (iq 1 2 3 4))))

;; CXRS

(define ab (ipair 'a 'b))
(define cd (ipair 'c 'd))
(define ef (ipair 'e 'f))
(define gh (ipair 'g 'h))
(define abcd (ipair ab cd))
(define efgh (ipair ef gh))
(define abcdefgh (ipair abcd efgh))
(define ij (ipair 'i 'j))
(define kl (ipair 'k 'l))
(define mn (ipair 'm 'n))
(define op (ipair 'o 'p))
(define ijkl (ipair ij kl))
(define mnop (ipair mn op))
(define ijklmnop (ipair ijkl mnop))
(define abcdefghijklmnop (ipair abcdefgh ijklmnop))

(test "srfi-116-30" 'a (icaar abcd))
(test "srfi-116-31" 'b (icdar abcd))
(test "srfi-116-32" 'c (icadr abcd))
(test "srfi-116-33" 'd (icddr abcd))
(test "srfi-116-34" 'a (icaaar abcdefgh))
(test "srfi-116-35" 'b (icdaar abcdefgh))
(test "srfi-116-36" 'c (icadar abcdefgh))
(test "srfi-116-37" 'd (icddar abcdefgh))
(test "srfi-116-38" 'e (icaadr abcdefgh))
(test "srfi-116-39" 'f (icdadr abcdefgh))
(test "srfi-116-40" 'g (icaddr abcdefgh))
(test "srfi-116-41" 'h (icdddr abcdefgh))
(test "srfi-116-42" 'a (icaaaar abcdefghijklmnop))
(test "srfi-116-43" 'b (icdaaar abcdefghijklmnop))
(test "srfi-116-44" 'c (icadaar abcdefghijklmnop))
(test "srfi-116-45" 'd (icddaar abcdefghijklmnop))
(test "srfi-116-46" 'e (icaadar abcdefghijklmnop))
(test "srfi-116-47" 'f (icdadar abcdefghijklmnop))
(test "srfi-116-48" 'g (icaddar abcdefghijklmnop))
(test "srfi-116-49" 'h (icdddar abcdefghijklmnop))
(test "srfi-116-50" 'i (icaaadr abcdefghijklmnop))
(test "srfi-116-51" 'j (icdaadr abcdefghijklmnop))
(test "srfi-116-52" 'k (icadadr abcdefghijklmnop))
(test "srfi-116-53" 'l (icddadr abcdefghijklmnop))
(test "srfi-116-54" 'm (icaaddr abcdefghijklmnop))
(test "srfi-116-55" 'n (icdaddr abcdefghijklmnop))
(test "srfi-116-56" 'o (icadddr abcdefghijklmnop))
(test "srfi-116-57" 'p (icddddr abcdefghijklmnop))


;; Selectors

(test "srfi-116-58" 'c (ilist-ref (iq a b c d) 2))
(define ten (ilist 1 2 3 4 5 6 7 8 9 10))
(test "srfi-116-59" 1 (ifirst ten))
(test "srfi-116-60" 2 (isecond ten))
(test "srfi-116-61" 3 (ithird ten))
(test "srfi-116-62" 4 (ifourth ten))
(test "srfi-116-63" 5 (ififth ten))
(test "srfi-116-64" 6 (isixth ten))
(test "srfi-116-65" 7 (iseventh ten))
(test "srfi-116-66" 8 (ieighth ten))
(test "srfi-116-67" 9 (ininth ten))
(test "srfi-116-68" 10 (itenth ten))
(test/error "srfi-116-69" (ilist-ref '() 2))
(test "srfi-116-70" '(1 2) (call-with-values (lambda () (icar+icdr (ipair 1 2))) list))
(define abcde (iq a b c d e))
(define dotted (ipair 1 (ipair 2 (ipair 3 'd))))
(test "srfi-116-71" (iq a b) (itake abcde 2))
(test "srfi-116-72" (iq c d e) (idrop abcde 2))
(test "srfi-116-73" (iq c d e) (ilist-tail abcde 2))
(test "srfi-116-74" (iq 1 2) (itake dotted 2))
(test "srfi-116-75" (ipair 3 'd) (idrop dotted 2))
(test "srfi-116-76" (ipair 3 'd) (ilist-tail dotted 2))
(test "srfi-116-77" 'd (idrop dotted 3))
(test "srfi-116-78" 'd (ilist-tail dotted 3))
(test "srfi-116-79" abcde (iappend (itake abcde 4) (idrop abcde 4)))
(test "srfi-116-80" (iq d e) (itake-right abcde 2))
(test "srfi-116-81" (iq a b c) (idrop-right abcde 2))
(test "srfi-116-82" (ipair 2 (ipair 3 'd)) (itake-right dotted 2))
(test "srfi-116-83" (iq 1) (idrop-right dotted 2))
(test "srfi-116-84" 'd (itake-right dotted 0))
(test "srfi-116-85" (iq 1 2 3) (idrop-right dotted 0))
(test "srfi-116-86" abcde (call-with-values (lambda () (isplit-at abcde 3)) iappend))
(test "srfi-116-87" 'c (ilast (iq a b c)))
(test "srfi-116-88" (iq c) (last-ipair (iq a b c)))

;; Misc

(test "srfi-116-89" 0 (ilength '()))
(test "srfi-116-90" 3 (ilength (iq 1 2 3)))
(test "srfi-116-91" (iq x y) (iappend (iq x) (iq y)))
(test "srfi-116-92" (iq a b c d) (iappend (iq a b) (iq c d)))
(test "srfi-116-93" (iq a) (iappend '() (iq a)))
(test "srfi-116-94" (iq x y) (iappend (iq x y)))
(test "srfi-116-95" '() (iappend))
(test "srfi-116-96" (iq a b c d) (iconcatenate (iq (a b) (c d))))
(test "srfi-116-97" (iq c b a) (ireverse (iq a b c)))
(test "srfi-116-98" (iq (e (f)) d (b c) a) (ireverse (iq a (b c) d (e (f)))))
(test "srfi-116-99" (ipair 2 (ipair 1 'd)) (iappend-reverse (iq 1 2) 'd))
(test "srfi-116-100" (iq (one 1 odd) (two 2 even) (three 3 odd))
      (izip (iq one two three) (iq 1 2 3) (iq odd even odd)))
(test "srfi-116-101" (iq (1) (2) (3)) (izip (iq 1 2 3)))
(test "srfi-116-102" (iq 1 2 3) (iunzip1 (iq (1) (2) (3))))
(test "srfi-116-103" (iq (1 2 3) (one two three))
      (call-with-values
          (lambda () (iunzip2 (iq (1 one) (2 two) (3 three))))
        ilist))
(test "srfi-116-104" (iq (1 2 3) (one two three) (a b c))
      (call-with-values
          (lambda () (iunzip3 (iq (1 one a) (2 two b) (3 three c))))
        ilist))
(test "srfi-116-105" (iq (1 2 3) (one two three) (a b c) (4 5 6))
      (call-with-values
          (lambda () (iunzip4 (iq (1 one a 4) (2 two b 5) (3 three c 6))))
        ilist))
(test "srfi-116-106" (iq (1 2 3) (one two three) (a b c) (4 5 6) (#t #f #t))
      (call-with-values
          (lambda () (iunzip5 (iq (1 one a 4 #t) (2 two b 5 #f) (3 three c 6 #t))))
        ilist))
(test "srfi-116-107" 3 (icount even? (iq 3 1 4 1 5 9 2 5 6)))
(test "srfi-116-108" 3 (icount < (iq 1 2 4 8) (iq 2 4 6 8 10 12 14 16)))

;; Folds

(define lis (iq 1 2 3))
(test "srfi-116-109" 6 (ifold + 0 lis))
(test "srfi-116-110" (iq 3 2 1) (ifold ipair '() lis))
(test "srfi-116-111" 2 (ifold
                        (lambda (x count) (if (symbol? x) (+ count 1) count))
                        0
                        (iq a 0 b)))
(test "srfi-116-112" 4 (ifold
                        (lambda (s max-len) (max max-len (string-length s)))
                        0
                        (iq "ab" "abcd" "abc")))
(test "srfi-116-113" 32 (ifold
                         (lambda (a b ans) (+ (* a b) ans))
                         0
                         (iq 1 2 3)
                         (iq 4 5 6)))
(define (z x y ans) (ipair (ilist x y) ans))
(test "srfi-116-114" (iq (b d) (a c))
      (ifold z '() (iq a b) (iq c d)))
(test "srfi-116-115" lis (ifold-right ipair '() lis))
(test "srfi-116-116" (iq 0 2 4) (ifold-right
                                 (lambda (x l) (if (even? x) (ipair x l) l))
                                 '()
                                 (iq 0 1 2 3 4)))
(test "srfi-116-117" (iq (a c) (b d))
      (ifold-right z '() (iq a b) (iq c d)))
(test "srfi-116-118" (iq (c) (b c) (a b c))
      (ipair-fold ipair '() (iq a b c)))
(test "srfi-116-119" (iq ((b) (d)) ((a b) (c d)))
      (ipair-fold z '() (iq a b) (iq c d)))
(test "srfi-116-120" (iq (a b c) (b c) (c))
      (ipair-fold-right ipair '() (iq a b c)))
(test "srfi-116-121" (iq ((a b) (c d)) ((b) (d)))
      (ipair-fold-right z '() (iq a b) (iq c d)))
(test "srfi-116-122" 5 (ireduce max 0 (iq 1 3 5 4 2 0)))
(test "srfi-116-123" 1 (ireduce - 0 (iq 1 2)))
(test "srfi-116-124" -1 (ireduce-right - 0 (iq 1 2)))
(define squares (iq 1 4 9 16 25 36 49 64 81 100))
(test "srfi-116-125" squares
      (iunfold (lambda (x) (> x 10))
               (lambda (x) (* x x))
               (lambda (x) (+ x 1))
               1))
(test "srfi-116-126" squares
      (iunfold-right zero?
                     (lambda (x) (* x x))
                     (lambda (x) (- x 1))
                     10))
(test "srfi-116-126" (iq 1 2 3) (iunfold null-ilist? icar icdr (iq 1 2 3)))
(test "srfi-116-127" (iq 3 2 1) (iunfold-right null-ilist? icar icdr (iq 1 2 3)))
(test "srfi-116-128" (iq 1 2 3 4)
      (iunfold null-ilist? icar icdr (iq 1 2) (lambda (x) (iq 3 4))))
(test "srfi-116-129" (iq b e h) (imap icadr (iq (a b) (d e) (g h))))
(test "srfi-116-130" (iq b e h) (imap-in-order icadr (iq (a b) (d e) (g h))))
(test "srfi-116-131" (iq 5 7 9) (imap + (iq 1 2 3) (iq 4 5 6)))
(test "srfi-116-132" (iq 5 7 9) (imap-in-order + (iq 1 2 3) (iq 4 5 6)))
(define z (let ((count 0)) (lambda (ignored) (set! count (+ count 1)) count)))
(test "srfi-116-133" (iq 1 2) (imap-in-order z (iq a b)))
(test "srfi-116-134" '#(0 1 4 9 16)
      (let ((v (make-vector 5)))
        (ifor-each (lambda (i)
                     (vector-set! v i (* i i)))
                   (iq 0 1 2 3 4))
        v))
(test "srfi-116-135" '#(5 7 9 11 13)
      (let ((v (make-vector 5)))
        (ifor-each (lambda (i j)
                     (vector-set! v i (+ i j)))
                   (iq 0 1 2 3 4)
                   (iq 5 6 7 8 9))
        v))
(test "srfi-116-136" (iq 1 -1 3 -3 8 -8)
      (iappend-map (lambda (x) (ilist x (- x))) (iq 1 3 8)))
(test "srfi-116-137" (iq 1 4 2 5 3 6)
      (iappend-map ilist (iq 1 2 3) (iq 4 5 6)))
(test "srfi-116-138" (vector (iq 0 1 2 3 4) (iq 1 2 3 4) (iq 2 3 4) (iq 3 4) (iq 4))
      (let ((v (make-vector 5)))
        (ipair-for-each (lambda (lis) (vector-set! v (icar lis) lis)) (iq 0 1 2 3 4))
        v))
(test "srfi-116-139" (vector (iq 5 6 7 8 9) (iq 6 7 8 9) (iq 7 8 9) (iq 8 9) (iq 9))
      (let ((v (make-vector 5)))
        (ipair-for-each (lambda (i j) (vector-set! v (icar i) j))
                        (iq 0 1 2 3 4)
                        (iq 5 6 7 8 9))
        v))
(test "srfi-116-140" (iq 1 9 49)
      (ifilter-map (lambda (x) (and (number? x) (* x x))) (iq a 1 b 3 c 7)))
(test "srfi-116-141" (iq 5 7 9)
      (ifilter-map
       (lambda (x y) (and (number? x) (number? y) (+ x y)))
       (iq 1 a 2 b 3 4)
       (iq 4 0 5 y 6 z)))

;; Filtering

(test "srfi-116-142" (iq 0 8 8 -4) (ifilter even? (iq 0 7 8 8 43 -4)))
(test "srfi-116-143" (list (iq one four five) (iq 2 3 6))
      (call-with-values
          (lambda () (ipartition symbol? (iq one 2 3 four five 6)))
        list))
(test "srfi-116-144" (iq 7 43) (iremove even? (iq 0 7 8 8 43 -4)))

;; Searching

(test "srfi-116-145" 2 (ifind even? (iq 1 2 3)))
(test "srfi-116-146" #t (iany  even? (iq 1 2 3)))
(test "srfi-116-147" #f (ifind even? (iq 1 7 3)))
(test "srfi-116-148" #f (iany  even? (iq 1 7 3)))
(test/error "srfi-116-149" (ifind even? (ipair 1 (ipair 3 'x))))
(test/error "srfi-116-150" (iany  even? (ipair 1 (ipair 3 'x))))
(test "srfi-116-151" 4 (ifind even? (iq 3 1 4 1 5 9)))
(test "srfi-116-152" (iq -8 -5 0 0) (ifind-tail even? (iq 3 1 37 -8 -5 0 0)))
(test "srfi-116-153" (iq 2 18) (itake-while even? (iq 2 18 3 10 22 9)))
(test "srfi-116-154" (iq 3 10 22 9) (idrop-while even? (iq 2 18 3 10 22 9)))
(test "srfi-116-155" (list (iq 2 18) (iq 3 10 22 9))
      (call-with-values
          (lambda () (ispan even? (iq 2 18 3 10 22 9)))
        list))
(test "srfi-116-156" (list (iq 3 1) (iq 4 1 5 9))
      (call-with-values
          (lambda () (ibreak even? (iq 3 1 4 1 5 9)))
        list))
(test "srfi-116-157" #t (iany integer? (iq a 3 b 2.7)))
(test "srfi-116-158" #f (iany integer? (iq a 3.1 b 2.7)))
(test "srfi-116-159" #t (iany < (iq 3 1 4 1 5) (iq 2 7 1 8 2)))
(test "srfi-116-160" #t (ievery integer? (iq 1 2 3 4 5)))
(test "srfi-116-161" #f (ievery integer? (iq 1 2 3 4.5 5)))
(test "srfi-116-162" #t (ievery (lambda (a b) (< a b)) (iq 1 2 3) (iq 4 5 6)))
(test "srfi-116-163" 2 (ilist-index even? (iq 3 1 4 1 5 9)))
(test "srfi-116-164" 1 (ilist-index < (iq 3 1 4 1 5 9 2 5 6) (iq 2 7 1 8 2)))
(test "srfi-116-165" #f (ilist-index = (iq 3 1 4 1 5 9 2 5 6) (iq 2 7 1 8 2)))
(test "srfi-116-166" (iq a b c) (imemq 'a (iq a b c)))
(test "srfi-116-167" (iq b c) (imemq 'b (iq a b c)))
(test "srfi-116-168" #f (imemq 'a (iq b c d)))
(test "srfi-116-169" #f (imemq (ilist 'a) (iq b (a) c)))
(test "srfi-116-170" (iq (a) c) (imember (ilist 'a) (iq b (a) c)))
(test "srfi-116-171" (iq 101 102) (imemv 101 (iq 100 101 102)))

;; Deletion

(test "srfi-116-172" (iq 1 2 4 5) (idelete 3 (iq 1 2 3 4 5)))
(test "srfi-116-173" (iq 3 4 5) (idelete 5 (iq 3 4 5 6 7) <))
(test "srfi-116-174" (iq a b c z) (idelete-duplicates (iq a b a c a b c z)))

;; Alists

(define e (iq (a 1) (b 2) (c 3)))
(test "srfi-116-175" (iq a 1) (iassq 'a e))
(test "srfi-116-176" (iq b 2) (iassq 'b e))
(test "srfi-116-177" #f (iassq 'd e))
(test "srfi-116-178" #f (iassq (ilist 'a) (iq ((a)) ((b)) ((c)))))
(test "srfi-116-179" (iq (a)) (iassoc (ilist 'a) (iq ((a)) ((b)) ((c)))))
(define e2 (iq (2 3) (5 7) (11 13)))
(test "srfi-116-180" (iq 5 7) (iassv 5 e2))
(test "srfi-116-181" (iq 11 13) (iassoc 5 e2 <))
(test "srfi-116-182" (ipair (iq 1 1) e2) (ialist-cons 1 (ilist 1) e2))
(test "srfi-116-183" (iq (2 3) (11 13)) (ialist-delete 5 e2))
(test "srfi-116-184" (iq (2 3) (5 7)) (ialist-delete 5 e2 <))

;; Replacers

(test "srfi-116-185" (ipair 1 3) (replace-icar (ipair 2 3) 1))
(test "srfi-116-185" (ipair 1 3) (replace-icdr (ipair 1 2) 3))

;; Conversions

(test "srfi-116-186" (ipair 1 2) (pair->ipair '(1 . 2)))
(test "srfi-116-187" '(1 . 2) (ipair->pair (ipair 1 2)))
(test "srfi-116-188" (iq 1 2 3) (list->ilist '(1 2 3)))
(test "srfi-116-189" '(1 2 3) (ilist->list (iq 1 2 3)))
(test "srfi-116-190" (ipair 1 (ipair 2 3)) (list->ilist '(1 2 . 3)))
(test "srfi-116-191" '(1 2 . 3) (ilist->list (ipair 1 (ipair 2 3))))
(test "srfi-116-192" (ipair (ipair 1 2) (ipair 3 4)) (tree->itree '((1 . 2) . (3 . 4))))
(test "srfi-116-193" '((1 . 2) . (3 . 4)) (itree->tree (ipair (ipair 1 2) (ipair 3 4))))
(test "srfi-116-194" (ipair (ipair 1 2) (ipair 3 4)) (gtree->itree (cons (ipair 1 2) (ipair 3 4))))
(test "srfi-116-195" '((1 . 2) . (3 . 4)) (gtree->tree (cons (ipair 1 2) (ipair 3 4))))
(test "srfi-116-196" 6 (iapply + (iq 1 2 3)))
(test "srfi-116-197" 15 (iapply + 1 2 (iq 3 4 5)))
