You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/xenomorph/xenomorph/test/stream-test.rkt

75 lines
2.6 KiB
Racket

#lang racket/base
(require rackunit
6 years ago
racket/class
6 years ago
racket/stream
5 years ago
"../list.rkt"
"../base.rkt"
6 years ago
"../number.rkt"
5 years ago
"../stream.rkt"
"../base.rkt")
#|
approximates
https://github.com/mbutterick/restructure/blob/master/test/LazyArray.coffee
|#
6 years ago
(test-case
"stream: decode should decode items lazily"
6 years ago
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
5 years ago
(define xla (x:stream uint8 4))
6 years ago
(define arr (decode xla))
(check-equal? (stream-length arr) 4)
(check-equal? (pos (current-input-port)) 4)
(check-equal? (stream-ref arr 0) 1)
(check-equal? (stream-ref arr 1) 2)
(check-equal? (stream-ref arr 2) 3)
(check-equal? (stream-ref arr 3) 4)))
(test-case
"stream: decode should decode items lazily with post-decode"
6 years ago
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
5 years ago
(define xla (x:stream uint8 4 #:post-decode (λ (str) (stream-map (λ (i) (* 2 i)) str))))
6 years ago
(define arr (decode xla))
5 years ago
(check-false (x:list? arr))
6 years ago
(check-equal? (stream-length arr) 4)
(check-equal? (pos (current-input-port)) 4)
(check-equal? (stream-ref arr 0) 2)
(check-equal? (stream-ref arr 1) 4)
(check-equal? (stream-ref arr 2) 6)
(check-equal? (stream-ref arr 3) 8)))
(test-case
"stream: should be able to convert to an array"
6 years ago
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
5 years ago
(define xla (x:stream uint8 4))
6 years ago
(define arr (decode xla))
(check-equal? (stream->list arr) '(1 2 3 4))))
(test-case
"stream: decode should decode length as number before array"
6 years ago
(parameterize ([current-input-port (open-input-bytes (bytes 4 1 2 3 4 5))])
5 years ago
(define xla (x:stream uint8 uint8))
6 years ago
(define arr (decode xla))
(check-equal? (stream->list arr) '(1 2 3 4))))
(test-case
"stream: size should work with xlazy-arrays"
6 years ago
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
5 years ago
(define xla (x:stream uint8 4))
6 years ago
(define arr (decode xla))
(check-equal? (send xla x:size arr) 4)))
6 years ago
(test-case
"stream: encode should work with xlazy-arrays"
6 years ago
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
5 years ago
(define xla (x:stream uint8 4))
6 years ago
(define arr (decode xla))
(check-equal? (encode xla arr #f) (bytes 1 2 3 4))))
(test-case
"stream: encode should work with xlazy-arrays with pre-encode"
6 years ago
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
5 years ago
(define xla (x:stream uint8 4 #:pre-encode (λ (str) (stream-map (λ (val) (* 2 val)) str))))
6 years ago
(define arr (decode xla))
(check-equal? (encode xla arr #f) (bytes 2 4 6 8))))