From d579da879a46d105489113ab33bd49fed077bc9e Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sat, 17 Aug 2019 19:06:36 -0700 Subject: [PATCH] nicer test --- pitfall/pitfall/check-pdf.rkt | 44 +++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/pitfall/pitfall/check-pdf.rkt b/pitfall/pitfall/check-pdf.rkt index 9befdc42..50ed8548 100644 --- a/pitfall/pitfall/check-pdf.rkt +++ b/pitfall/pitfall/check-pdf.rkt @@ -63,7 +63,7 @@ (for/list ([kv (in-slice 2 items)] ;; suppress these keys so we can compare pdfkit & pitfall output #:unless (member (car kv) (list #"/Producer" #"/Creator" #"/CreationDate"))) - (apply cons kv)) + (apply cons kv)) bytesdict pdf) (define pdf-bs (if (bytes? pdf) pdf (file->bytes pdf))) @@ -108,30 +108,34 @@ (sort ; sort by byte offset (cdr ; drop zeroth record: there is no zeroth object (for/list ([i (in-range ref-count)]) - (cons i (read (open-input-bytes (car (regexp-match #px"\\d{10}" xref-ip))))))) + (cons i (read (open-input-bytes (car (regexp-match #px"\\d{10}" xref-ip))))))) < #:key cdr) (list (cons #f xoff)))) (sort ; sort by index (parameterize ([current-input-port (open-input-bytes pdf-bs)]) (for/list ([(idx start) (in-dict obj-locations)] [(_ end) (in-dict (cdr obj-locations))]) - (cons idx (car (parse-pdf-bytes (peek-bytes (- end start) start)))))) + (cons idx (car (parse-pdf-bytes (peek-bytes (- end start) start)))))) < #:key car)) (define (dict-compare arg1 arg2) (define d1 (if (dict? arg1) arg1 (pdf->dict arg1))) (define d2 (if (dict? arg2) arg2 (pdf->dict arg2))) - (and (dict? d1) (dict? d2) - (= (length d1) (length d2)) - (for/and ([(k1 v1) (in-dict d1)] - [(k2 v2) (in-dict d2)]) - (unless (equal? k1 k2) - (error (format "keys unequal in ~e and ~e: ~a ≠ ~a" arg1 arg2 k1 k2))) - (unless (equal? v1 v2) - (error (format "values unequal in ~e and ~e: ~e ≠ ~e" arg1 arg2 v1 v2))) - (when (dict? v1) - (dict-compare v1 v2)) - #true))) + (unless (dict? d1) + (error "d1 is not a dict")) + (unless (dict? d2) + (error "d2 is not a dict")) + (unless (= (length d1) (length d2)) + (error (format "dict lengths different in d1 (~a) and d2 (~a)" (length d1) (length d2)))) + (for/and ([(k1 v1) (in-dict d1)] + [(k2 v2) (in-dict d2)]) + (unless (equal? k1 k2) + (error (format "keys unequal in ~e and ~e: ~a ≠ ~a" arg1 arg2 k1 k2))) + (unless (equal? v1 v2) + (error (format "values unequal in ~e and ~e: ~e ≠ ~e" arg1 arg2 v1 v2))) + (when (dict? v1) + (dict-compare v1 v2)) + #true)) (define-simple-check (check-headers-equal? ps1 ps2) (equal? (peek-bytes 14 0 (open-input-file ps1)) @@ -149,7 +153,7 @@ [(vector? val) (dump (vector->list val))] [(dict? val) (for/list ([(k v) (in-dict (sort (dict->list val) #:key car symboldict f1) 8) 'stream)) @@ -175,5 +179,5 @@ #;(module+ main (for ([p (in-directory)] #:when (path-has-extension? p #"pdf")) - (with-handlers ([exn:fail? (λ (exn) (println (format "~a failed" p)))]) - (pdf->dict p)))) + (with-handlers ([exn:fail? (λ (exn) (println (format "~a failed" p)))]) + (pdf->dict p))))