;;;; (import test) ;(import (only (chicken format) format)) ;(include "test-gloss.incl") ;;; (define R1 #(1 2 3 4 5 6 7 8 9)) (define R2 '(1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9)) ;;; (test-begin "Sequences Utils") (import (prefix sequences seq:) (prefix (sequences utils) seq:) (srfi 1)) (test-group "coercion" (test '(#\a #\b) (seq:->list "ab")) (test #(#\a #\b) (seq:->vector "ab")) (test "ab" (seq:->string '(#\a #\b))) ) (test-group "sort" (define V1 #(5 3 4 2 1 9 7 8 6)) (test "Performs Sort" R1 (seq:sort V1 <)) (test-assert "And Source is Unsorted" (not (seq:sorted? V1 <))) (test "Performs Sort!" R1 (seq:sort! V1 <)) (test-assert "And Source is Sorted" (seq:sorted? V1 <)) ) ;sort was side-effecting a list arg (test-group "sort bug" (define R1L '(1 2 3 4 5 6 7 8 9)) (define V1L '(5 3 4 2 1 9 7 8 6)) (test "Performs Sort" R1L (seq:sort V1L <)) (test-assert "And Source is Unsorted" (not (seq:sorted? V1L <))) (test "Performs Sort!" R1L (seq:sort! V1L <)) (test-assert "And Source is Sorted" (seq:sorted? V1L <)) ) (test-group "merge" (define RV #(1 1.1 2 2.2 3 3.3 4 4.4 5 5.5 6 6.6 7 7.7 8 8.8 9 9.9)) (define RS '(1 2 3 4 5 6 7 8 9)) (define R2x '(1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9)) (test "Merges into same type" RV (seq:merge R1 R2 <)) ;must copy R2 since destructive, but only defined on lists so vector ok (test "Merges into same type" RV (seq:merge! R1 (append R2 '()) <)) ;must copy R2 since destructive, but only defined on lists so vector ok (test "Merges into same list" RS (seq:merge! RS (append R2 '()) <)) ) (test-group "unique" (test "abcdefg" (seq:unique "aaaabcccdeffffg" char=?)) (test #(1 2 3) (seq:unique #(1 2 2 2 3 3))) (test #(1 2 3) (seq:sort (seq:unique/unsorted #(2 1 3 3 2 1)) <)) ) (test-group "random" (define RV #(1 1.1 2 2.2 3 3.3 4 4.4 5 5.5 6 6.6 7 7.7 8 8.8 9 9.9)) (define RS '(1 2 3 4 5 6 7 8 9)) ;just the list-of randoms (let ((rs (seq:randoms 10))) (test-assert (list? rs)) (test 10 (length rs)) (test-assert (every integer? rs)) ) ;vector & string random samples (let ((ss (seq:sample RS 5))) (test-assert (list? ss)) (test 5 (seq:size ss)) (test-assert (seq:all? fixnum? ss)) ) (let ((ss (seq:sample RV 5))) (test-assert (vector? ss)) (test 5 (seq:size ss)) (test-assert (seq:all? number? ss)) ) (let ((ss (seq:sample "akldkewaklawefnfvlwefklr" 7))) (test-assert (string? ss)) (test 7 (seq:size ss)) (test-assert (seq:all? char? ss)) ) ;length could be < requested (5) (let ((ss (seq:sample RV 5 (seq:size RV) 0 #t))) (test-assert (vector? ss)) (test-assert (>= 5 (seq:size ss))) (test-assert (seq:all? number? ss)) ) ) (test-group "foldl->alist" (test '() (seq:foldl->alist '() (lambda (v i) (cons i v)) '())) (test '((b b) (a a a)) (seq:foldl->alist '(a b a) (lambda (i v) (cons v i)) '())) ) (test-group "histogram" (test '() (seq:histogram "")) (test '((#\a . 7)) (seq:histogram "aaaaaaa")) (test '((#\space . 1) (#\p . 2) (#\h . 1) (#\o . 3) (#\s . 4) (#\f . 2) (#\; . 2) (#\q . 1) (#\j . 6) (#\l . 2) (#\e . 3) (#\i . 1) (#\w . 3) (#\k . 5) (#\d . 2) (#\c . 3) (#\b . 1) (#\a . 7)) (seq:histogram "abcccdkwiweklajakjq;jkkladfsjaso;hasejfopasjeopw ")) (test '() (seq:histogram '())) (test '((a . 7)) (seq:histogram '(a a a a a a a))) ) (test-end "Sequences Utils") (test-exit)