(import (srfi 41))        ;; FIXME:  needed because stream-null is broken by previous loaded files
                          ;; must be changed
(define srfi-221-test-name "")

(define test-g-equal
  (case-lambda
    ((g-expect g-actual)
     (test (format #f "srfi-221 ~a" srfi-221-test-name)
       (generator->list g-expect)
       (generator->list g-actual)))
    ((g-expect g-actual take-count)
     (test-g-equal
       (gtake g-expect take-count)
       (gtake g-actual take-count)))))

;; "accumulate-generated-values"

(define expect '(1 2 3 4))

(define actual
  (accumulate-generated-values
   (list-accumulator)
   (generator 1 2 3 4)))

(test "srfi-221 accumulate-generated-values 1" expect actual)

;; "genumerate"

(set! srfi-221-test-name "genumerate 1")

  (test-g-equal (generator '(0 . a) '(1 . b) '(2 . c))
                (genumerate (generator 'a 'b 'c)))

(set! srfi-221-test-name "genumerate 2")

  ;; test empty
  (test-g-equal (generator)
                (genumerate (generator)))

(set! srfi-221-test-name "genumerate 3")
  ;; infinite case with take
  (test-g-equal (generator '(0 . a) '(1 . b) '(2 . c))
                (genumerate (circular-generator 'a 'b 'c))
                3)

;;  "gcompose-left"

(set! srfi-221-test-name "gcompose-left 1")
  (test-g-equal
    (generator 1 2 3 4)
    (gcompose-left
      (lambda () (make-range-generator 1))
      (lambda (g) (gtake g 4))))

(set! srfi-221-test-name "gcompose-left 2")
  (test-g-equal
    (generator 1 2 3 4)
    (gcompose-left
      (lambda () (generator 1 2 3 4))))

;;  "gcompose-right"

(set! srfi-221-test-name "gcompose-right 1")
  (test-g-equal
    (generator 1 2 3 4)
    (gcompose-right
      (lambda (g) (gtake g 4))
      (lambda () (make-range-generator 1))))

(set! srfi-221-test-name "gcompose-right 2")
  (test-g-equal
    (generator 1 2 3 4)
    (gcompose-right
      (lambda () (generator 1 2 3 4))))

;;  "gchoice"

(set! srfi-221-test-name "gchoice 1")
  ;; test normal
  (test-g-equal
    (generator 1 2 1 3)
    (gchoice
      (generator 0 1 0 2)
      (circular-generator 1)
      (circular-generator 2)
      (circular-generator 3)))

(set! srfi-221-test-name "gchoice 2")
  ;; test exhausted source
  (test-g-equal
    (generator 1 2 3)
    (gchoice
      (generator 0 0 0 0 0 1 1 2)
      (generator 1)
      (generator 2)
      (generator 3)))

;;  "generator->stream"

(define (test-stream-equal str1 str2)
  (if (stream-null? str1)
      (test "srfi-221 assert stream null" #t (stream-null? str2))
      (begin
        (test (format #f "srfi-221 ~a" srfi-221-test-name)
              (stream-car str1) (stream-car str2))
        (test-stream-equal (stream-cdr str1) (stream-cdr str2)))))

(set! srfi-221-test-name "gstream 1")
  ;; test normal
  (test-stream-equal
     (stream 1 2 3)
     (generator->stream (generator 1 2 3)))

(set! srfi-221-test-name "gstream 2")
  ;; test infinite with take
  (test-stream-equal
    (stream 1 2 3)
    (stream-take 3 (generator->stream (circular-generator 1 2 3))))

;;  "stream->generator"

(set! srfi-221-test-name "stream->generator 1")
  ;; test normal
  (test-g-equal
    (generator 1 2 3)
    (stream->generator (stream 1 2 3)))

(set! srfi-221-test-name "stream->generator 2")
  ;; test infinite with take
  (test-g-equal
    (circular-generator 1 2 3)
    (stream->generator (stream-constant 1 2 3))
    20)
