bv-slice.scm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ;; This file is part of scheme-GNUnet.
  2. ;; Copyright (C) 2021 Maxime Devos
  3. ;;
  4. ;; scheme-GNUnet is free software: you can redistribute it and/or modify it
  5. ;; under the terms of the GNU Affero General Public License as published
  6. ;; by the Free Software Foundation, either version 3 of the License,
  7. ;; or (at your option) any later version.
  8. ;;
  9. ;; scheme-GNUnet is distributed in the hope that it will be useful, but
  10. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Affero General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Affero General Public License
  15. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;
  17. ;; SPDX-License-Identifier: AGPL3.0-or-later
  18. (import (gnu gnunet utils bv-slice)
  19. (srfi srfi-26)
  20. (rnrs conditions)
  21. (rnrs bytevectors))
  22. (test-begin "bv-slice")
  23. ;; slice-copy!
  24. ;; TODO maybe more specific conditions
  25. (test-error "destination of slice-copy! must be writable"
  26. &assertion
  27. (slice-copy! (make-slice/read-write 9)
  28. (slice/read-only (make-slice/read-write 9))))
  29. (test-error "source of slice-copy! must be readable"
  30. &assertion
  31. (slice-copy! (slice/write-only (make-slice/read-write 9))
  32. (make-slice/read-write 9)))
  33. (test-error "lengths must match (1)"
  34. &assertion
  35. (slice-copy! (make-slice/read-write 9)
  36. (make-slice/read-write 0)))
  37. (test-error "lengths must match (2)"
  38. &assertion
  39. (slice-copy! (make-slice/read-write 0)
  40. (make-slice/read-write 9)))
  41. (test-equal "slice-copy! copies"
  42. #vu8(0 1 2 3)
  43. (let ((source (bv-slice/read-write #vu8(0 1 2 3)))
  44. (dest (make-slice/read-write 4)))
  45. (slice-copy! source dest)
  46. (slice-bv dest)))
  47. (test-equal "also if there's an offset in the source"
  48. #vu8(0 1 2 3)
  49. (let ((source (slice-slice (bv-slice/read-write #vu8(0 0 1 2 3)) 1))
  50. (dest (make-slice/read-write 4)))
  51. (slice-copy! source dest)
  52. (slice-bv dest)))
  53. (test-equal "also if the destination bv is long"
  54. #vu8(9 8 0 1 2 3)
  55. (let ((source (bv-slice/read-write #vu8(8 0 1 2)))
  56. (dest (slice-slice
  57. (bv-slice/read-write (bytevector-copy #vu8(9 7 7 7 7 3)))
  58. 1 4)))
  59. (slice-copy! source dest)
  60. (slice-bv dest)))
  61. (test-equal "slice-zero! writes zeros"
  62. #vu8(1 2 0 0 5 6 7 8)
  63. (let ((dest
  64. (slice-slice
  65. (bv-slice/read-write (bytevector-copy #vu8(1 2 3 4 5 6 7 8)))
  66. 2 2)))
  67. (slice-zero! dest)
  68. (slice-bv dest)))
  69. (test-error "slice-zero! requires writability"
  70. &assertion
  71. (slice-zero! (slice/write-only (make-slice/read-write 9))))
  72. (test-error "even if the length is zero"
  73. &assertion
  74. (slice-zero! (slice/write-only (make-slice/read-write 0))))
  75. (test-end "bv-slice")
  76. ;; ^ TODO: test other procedures