srfi-38.test 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ;;; srfi-38.test --- Tests for SRFI 38. -*- mode: scheme; -*-
  2. ;; Copyright (C) 2010 Free Software Foundation, Inc.
  3. ;; This library is free software; you can redistribute it and/or
  4. ;; modify it under the terms of the GNU Lesser General Public
  5. ;; License as published by the Free Software Foundation; either
  6. ;; version 3 of the License, or (at your option) any later version.
  7. ;; This library is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. ;; Lesser General Public License for more details.
  11. ;; You should have received a copy of the GNU Lesser General Public
  12. ;; License along with this library. If not, see
  13. ;; <http://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (define-module (test-srfi-38)
  16. #:use-module (test-suite lib)
  17. #:use-module (srfi srfi-38)
  18. #:use-module (rnrs bytevectors))
  19. (define (shared-structure->string object)
  20. (call-with-output-string
  21. (lambda (port)
  22. (write-with-shared-structure object port))))
  23. (define (roundtrip object)
  24. (call-with-input-string (shared-structure->string object)
  25. (lambda (port)
  26. (read-with-shared-structure port))))
  27. (with-test-prefix "pairs"
  28. (let ((foo (cons 'value-1 #f)))
  29. (set-cdr! foo foo)
  30. (pass-if "writing"
  31. (string=? "#1=(value-1 . #1#)"
  32. (shared-structure->string foo)))
  33. (pass-if "roundtrip"
  34. (let ((result (roundtrip foo)))
  35. (and (pair? result)
  36. (eq? (car result) 'value-1)
  37. (eq? (cdr result) result))))))
  38. (with-test-prefix "bytevectors"
  39. (let ((vec (vector 0 1 2 3))
  40. (bv (u8-list->bytevector '(42 42))))
  41. (vector-set! vec 0 bv)
  42. (vector-set! vec 2 bv)
  43. (pass-if "roundtrip"
  44. (let ((result (roundtrip vec)))
  45. (and (equal? '#(#vu8(42 42) 1 #vu8(42 42) 3)
  46. result)
  47. (eq? (vector-ref result 0)
  48. (vector-ref result 2)))))))
  49. (with-test-prefix "mixed"
  50. (let* ((pair (cons 'a 'b))
  51. (vec (vector 0 pair 2 pair #f)))
  52. (vector-set! vec 4 vec)
  53. (pass-if "roundtrip"
  54. (let ((result (roundtrip vec)))
  55. (and (eq? (vector-ref result 1)
  56. (vector-ref result 3))
  57. (eq? result (vector-ref result 4)))))))