zlib.scm 5.5 KB

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