zlib.scm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ;;; -*- mode: scheme; coding: utf-8; -*-
  2. ;;; Guile-zlib --- Functional package management for GNU
  3. ;;; Copyright © 2016, 2019, 2021 Ludovic Courtès <ludo@gnu.org>
  4. ;;;
  5. ;;; This file is part of Guile-zlib.
  6. ;;;
  7. ;;; Guile-zlib is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; Guile-zlib is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with Guile-zlib. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (test-zlib)
  20. #:use-module (zlib)
  21. #:use-module (srfi srfi-1)
  22. #:use-module (srfi srfi-11)
  23. #:use-module (srfi srfi-64)
  24. #:use-module (srfi srfi-26)
  25. #:use-module (ice-9 binary-ports)
  26. #:use-module (rnrs bytevectors)
  27. #:use-module (rnrs io ports)
  28. #:use-module (ice-9 match))
  29. (test-begin "zlib")
  30. (define (random-seed)
  31. (logxor (getpid) (car (gettimeofday))))
  32. (define %seed
  33. (let ((seed (random-seed)))
  34. (format (current-error-port) "random seed for tests: ~a~%"
  35. seed)
  36. (seed->random-state seed)))
  37. (define (random-bytevector n)
  38. "Return a random bytevector of N bytes."
  39. (let ((bv (make-bytevector n)))
  40. (let loop ((i 0))
  41. (if (< i n)
  42. (begin
  43. (bytevector-u8-set! bv i (random 256 %seed))
  44. (loop (1+ i)))
  45. bv))))
  46. (let ((str "αβγ"))
  47. (test-equal
  48. str
  49. (let ((bv (call-with-output-bytevector
  50. (lambda (o)
  51. ; has to be set because call-with-output-bytevector ignores %default-port-encoding.
  52. (set-port-encoding! o "UTF-8")
  53. (call-with-zlib-output-port
  54. o
  55. (lambda (o)
  56. ; force encoding here to reveal a mismatch on read.
  57. (set-port-encoding! o "UTF-8")
  58. (write str o)))))))
  59. (with-fluids ((%default-port-encoding "UTF-8"))
  60. (call-with-input-bytevector
  61. bv
  62. (lambda (o)
  63. ; has to be set because call-with-input-bytevector ignores %default-port-encoding; see above.
  64. (set-port-encoding! o "UTF-8")
  65. (call-with-zlib-input-port o read)))))))
  66. (test-assert "compression/decompression pipe"
  67. (let ((data (random-bytevector (+ (random 10000)
  68. (* 20 1024)))))
  69. (match (pipe)
  70. ((parent . child)
  71. (match (primitive-fork)
  72. (0 ;compress
  73. (dynamic-wind
  74. (const #t)
  75. (lambda ()
  76. (close-port parent)
  77. (call-with-gzip-output-port child
  78. (lambda (port)
  79. (put-bytevector port data))))
  80. (lambda ()
  81. (primitive-exit 0))))
  82. (pid ;decompress
  83. (begin
  84. (close-port child)
  85. (let ((received (call-with-gzip-input-port parent
  86. (lambda (port)
  87. (get-bytevector-all port))
  88. #:buffer-size (* 64 1024))))
  89. (match (waitpid pid)
  90. ((_ . status)
  91. (and (zero? status)
  92. (port-closed? parent)
  93. (bytevector=? received data))))))))))))
  94. (test-assert "raw compress/decompress"
  95. (let* ((data (random-bytevector (+ (random 10000) (* 20 1024))))
  96. (cdata (compress data))
  97. (ucdata (uncompress cdata)))
  98. (equal? data ucdata)))
  99. (define (test-zlib n fmt level)
  100. (test-assert (format #f "zlib ports [size: ~a, format: ~a, level: ~a]"
  101. n fmt level)
  102. (let* ((size (pk 'size (+ (random n %seed) n)))
  103. (data (random-bytevector size)))
  104. (let*-values (((port get)
  105. (open-bytevector-output-port))
  106. ((compressed)
  107. (make-zlib-output-port port
  108. #:level level
  109. #:format fmt)))
  110. (put-bytevector compressed data)
  111. (close-port compressed)
  112. (let ((data2 (get-bytevector-all
  113. (make-zlib-input-port
  114. (open-bytevector-input-port (get))
  115. #:format fmt))))
  116. (pk 'sizes size 'vs (bytevector-length data2))
  117. (bytevector=? data2 data))))))
  118. (for-each (lambda (n)
  119. (for-each (lambda (format)
  120. (for-each (lambda (level)
  121. (test-zlib n format level))
  122. (iota 9 1)))
  123. '(deflate zlib gzip)))
  124. (list (expt 2 8) (expt 2 10) (expt 2 12)
  125. (expt 2 14) (expt 2 18)))
  126. (test-end)