kinds.scm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ;;; Disarchive
  2. ;;; Copyright © 2021 Timothy Sample <samplet@ngyro.com>
  3. ;;;
  4. ;;; This file is part of Disarchive.
  5. ;;;
  6. ;;; Disarchive is free software: you can redistribute it and/or modify
  7. ;;; it under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation, either version 3 of the License, or
  9. ;;; (at your option) any later version.
  10. ;;;
  11. ;;; Disarchive is distributed in the hope that it will be useful,
  12. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with Disarchive. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (tests kinds)
  19. #:use-module (disarchive kinds binary-string)
  20. #:use-module (disarchive kinds octal)
  21. #:use-module (disarchive kinds zero-string)
  22. #:use-module (disarchive utils)
  23. #:use-module (quickcheck arbitrary)
  24. #:use-module (quickcheck generator)
  25. #:use-module (ice-9 match)
  26. #:use-module (ice-9 regex)
  27. #:use-module (rnrs bytevectors)
  28. #:use-module (srfi srfi-26)
  29. #:export ($binary-string
  30. $zero-string
  31. $octal
  32. $non-bytevector-pax-records
  33. $pax-records
  34. $gnu-path
  35. $gnu-linkpath
  36. $tar-header))
  37. ;;; Commentary:
  38. ;;;
  39. ;;; This module provides QuickCheck types for generating random
  40. ;;; instances of the various Disarchive "kinds". The generators here
  41. ;;; are not intended to be perfect. Rather, they only need to be good
  42. ;;; enough to work with the help of "test-when" and a validation
  43. ;;; predicate.
  44. ;;;
  45. ;;; Code:
  46. (define (tweak-generator arb proc)
  47. (arbitrary
  48. (gen (proc (arbitrary-gen arb)))
  49. (xform (arbitrary-xform arb))))
  50. (define false? not)
  51. (define $false ($const #f))
  52. (define $binary-string
  53. (let (($raw ($choose
  54. (string? ($string $char))
  55. (bytevector? $bytevector))))
  56. (tweak-generator $raw
  57. (lambda (gen)
  58. (generator-let* ((str gen))
  59. (if (and (bytevector? str) (zero? (bytevector-length str)))
  60. (generator-return "")
  61. (generator-return str)))))))
  62. (define $zero-string
  63. ($record make-zero-string
  64. (zero-string-value $binary-string)
  65. (zero-string-trailer $binary-string)))
  66. (define $padded-octal
  67. ($record make-padded-octal
  68. (padded-octal-value $natural)
  69. (padded-octal-width $natural)
  70. (padded-octal-padding $char)
  71. (padded-octal-trailer $binary-string)))
  72. (define (fix-unstructured-octal-value octal)
  73. (define (first-octal-value str)
  74. (match (string-tokenize str (string->char-set "01234567"))
  75. ((first . rest) (string->number first 8))
  76. (_ #f)))
  77. (match (zero-string-value (unstructured-octal-source octal))
  78. ((? string? str)
  79. (let ((value (or (first-octal-value str)
  80. 0)))
  81. (make-unstructured-octal value (unstructured-octal-source octal))))
  82. ((? bytevector?)
  83. (make-unstructured-octal 0 (unstructured-octal-source octal)))))
  84. (define $unstructured-octal
  85. (let (($raw ($record make-unstructured-octal
  86. (unstructured-octal-value $natural)
  87. (unstructured-octal-source $zero-string))))
  88. (tweak-generator $raw
  89. (cut generator-lift fix-unstructured-octal-value <>))))
  90. (define $octal
  91. ($choose
  92. (padded-octal? $padded-octal)
  93. (unstructured-octal? $unstructured-octal)))
  94. (define $non-bytevector-pax-records
  95. ($list ($pair $binary-string $binary-string)))
  96. (define (binary-string+binary-string? obj)
  97. (match obj
  98. (((? binary-string?) . (? binary-string?)) #t)
  99. (_ #f)))
  100. (define $pax-records
  101. ($list
  102. ($choose
  103. (binary-string+binary-string? ($pair $binary-string $binary-string))
  104. (bytevector? $bytevector))))
  105. (define ($singleton x)
  106. (tweak-generator ($list x) (cut resize-generator 1 <>)))
  107. (define $gnu-path
  108. ($singleton ($pair ($const "path") $zero-string)))
  109. (define $gnu-linkpath
  110. ($singleton ($pair ($const "linkpath") $zero-string)))
  111. ;;; Local Variables:
  112. ;;; eval: (put 'tweak-generator 'scheme-indent-function 1))
  113. ;;; End: