binary-string.scm 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ;;; Disarchive
  2. ;;; Copyright © 2020, 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. (use-modules (disarchive kinds binary-string)
  19. (disarchive serialization)
  20. (quickcheck)
  21. (quickcheck arbitrary)
  22. (quickcheck generator)
  23. (quickcheck property)
  24. (rnrs bytevectors)
  25. (srfi srfi-64)
  26. (tests kinds))
  27. (configure-quickcheck
  28. ;; Perform 1000 tests per property...
  29. (stop? (lambda (success-count _)
  30. (>= success-count 1000)))
  31. ;; ...over input sizes from 0 to 10.
  32. (size (lambda (test-number)
  33. (if (zero? test-number)
  34. 0
  35. (1+ (quotient test-number 100))))))
  36. (define (round-trip bv)
  37. (encode-binary-string (decode-binary-string bv)))
  38. (test-begin "kinds--binary-string")
  39. (test-equal "Preserves invalid leading byte (10...)"
  40. #vu8(#b10000000)
  41. (round-trip #vu8(#b10000000)))
  42. (test-equal "Preserves invalid leading byte (111110...)"
  43. #vu8(#b11111000)
  44. (round-trip #vu8(#b11111000)))
  45. (test-equal "Preserves missing continuation byte"
  46. #vu8(#b11000000)
  47. (round-trip #vu8(#b11000000)))
  48. (test-equal "Preserves invalid continuation byte"
  49. #vu8(#b11000000 #b00000000)
  50. (round-trip #vu8(#b11000000 #b00000000)))
  51. (test-assert "[prop] Reading is reversible"
  52. (quickcheck
  53. (property ((bv $bytevector))
  54. (equal? bv (encode-binary-string (decode-binary-string bv))))))
  55. (test-assert "[prop] Reading produces a valid result"
  56. (quickcheck
  57. (property ((bv $bytevector))
  58. (valid-binary-string? (decode-binary-string bv)))))
  59. (test-assert "[prop] Writing is reversible"
  60. (quickcheck
  61. (property ((str $binary-string))
  62. (test-when (valid-binary-string? str)
  63. (equal? str (decode-binary-string (encode-binary-string str)))))))
  64. (test-assert "[prop] Serializing is reversible"
  65. (quickcheck
  66. (property ((str $binary-string))
  67. (test-when (valid-binary-string? str)
  68. (equal? str (serdeser -binary-string- str))))))
  69. (test-end "kinds--binary-string")